A simple U1DB example for Ubuntu SDK apps

On Reddit, Aaron Hastings said:

One of the features I’d really like to implement is for the app [a timetable viewer for trains] to save state upon exit. In other words, if a user selected the “Abbey Street” stop, then exited, the app should remember to load Abbey Street on next launch. I’ll have to look into how that’s implemented in Ubuntu.

I’d use U1DB to do that. A simple example:

import QtQuick 2.0
import Ubuntu.Components 0.1
import U1db 1.0 as U1db

MainView {
    width: units.gu(40)
    height: units.gu(71)

    /* ----------------------------------------------------
       Set up the U1DB database
       Declare a named document
       ---------------------------------------------------- */
    U1db.Database { id: db; path: "simpleu1dbdemo.u1db" }
    U1db.Document {
        id: lastPlace
        database: db
        docId: "lastPlace"
        create: true
        defaults: { placename: "" }
    }


    Page {
        title: "Simple U1DB demo"

        Column {
            width: parent.width
            spacing: units.gu(1)
            Label {
                width: parent.width
                text: "Enter a place"
                horizontalAlignment: Text.AlignHCenter
            }
            Rectangle {
                width: parent.width - units.gu(2)
                color: "white"
                height: inp.height * 2
                anchors.horizontalCenter: parent.horizontalCenter
                radius: 5
                TextInput {
                    id: inp
                    width: parent.width - units.gu(2)
                    anchors.centerIn: parent

                    /* ----------------------------------------------------
                       Important part number one
                       Retrieve the value from the declared U1DB document
                       ---------------------------------------------------- */
                    text: lastPlace.contents.placename || ""

                    /* ----------------------------------------------------
                       Important part number two
                       Save a changed value back to the U1DB document
                       ---------------------------------------------------- */
                    onTextChanged: lastPlace.contents = {placename: text}
                }
            }
        }
    }
}

This mechanism fulfils about 80% of my data storage needs for Ubuntu SDK apps. You declare a database and a named document; you use the values in that document (documentQMLid.contents.fieldname) declaratively, and to save values, just set documentQMLid.contents.

Of course, you could do this with QML LocalStorage, but do you really want to be constructing SQL statements all day? I don’t. And if you use U1DB now, it keeps the door open for more complicated things later, such as syncing this data between devices, or storing more dynamic data and then querying it, which I should probably write another blog post about if there’s interest.