A hands-on intro to using U1Db to store permanent app data and in the future sync it across Ubuntu devices. Watch the video at http://youtu.be/-Tuczd1IVR8
3. (c) 2013 Canonical
U1Db in a nutshell
> U1DB is a database API for synchronised databases of JSON documents
> Enables apps to store documents and sync them across devices
> Implementations are platform-specific, the API is platform-agnostic
> Simple to use
> Schemaless - more flexibility and faster development
> Intended for user-specific data
U1Db basics
4. (c) 2013 Canonical
U1Db is document-oriented
> In U1Db databases store JSON documents
> Documents don’t need a predefined list of fields
{ "key": "value" }
{ "firstname": "Jan", "surname": "Molby", "position": "midfield" }
{ "department": "Department of redundancy department",
"managers": [
{ "name": "Mary", "phone_number": "12345" },
{ "name": "Katherine" },
{ "name": "Rob", "phone_number": "54321" } ]
}
U1Db basics
5. (c) 2013 Canonical
U1Db elements
> Document storage and retrieval
> Covers the basic creation, retrieval and edition of documents
> Database, Document
> Querying
> Retrieval of a subset of documents based on certain criteria
> Index, Query
> Sync
> Synchronization of data across devices via a U1Db server
> Synchronizer
U1Db basics
7. (c) 2013 Canonical
Document storage and retrieval: the Database
> Provides the permanent storage for the app’s data
> Can be used directly as a model by other QML elements (e.g. ListView)
import U1db 1.0 as U1db
U1db.Database {
id: aDatabase
path: "aDatabase.u1db"
}
U1Db for app developers
8. (c) 2013 Canonical
Document storage and retrieval: the Document
> A database will generally contain multiple JSON-oriented documents
> Documents can be created dynamically at runtime
import U1db 1.0 as U1db
U1db.Document {
id: aDocument
database: aDatabase
docId: "lastPlace"
create: true
defaults: { "place": "Barcelona" }
}
U1Db for app developers
9. (c) 2013 Canonical
Querying: the Index
> An Index defines what fields can be filtered using a Query
> Used to retrieve documents other than by doc ID
> An index is created from ‘’index expressions’‘, which name one
or more fields in the document
import U1db 1.0 as U1db
U1db.Index {
id: by_place
database: aDatabase
expression: ["place"] // Use dot notation for nested fields
}
U1Db for app developers
10. (c) 2013 Canonical
Querying: the Query
> Once an index has been defined, the query is used to retrieve the data
> Can be used directly as a model by other QML elements (e.g. ListView)
> “Starts with” queries with wildcards are also supported
import U1db 1.0 as U1db
U1db.Query {
id: places
index: by_place
query: [ { "*" } ] // Could also be "Barc*" for further filtering
}
U1Db for app developers