Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 84 lines (62 sloc) 2.986 kB
cd6c915 Simple prefetching works.
zef authored
1 persistence.js
2 ==============
3 `persistence.js` is a simple asynchronous Javascript object-relational mapper library. It works with the
4 HTML5 SQLite local database as well as Google Gears' database.
5
6 Schema definition
7 -----------------
8
9 Currently there is one global database connection, which is initialized with a `persistence.connect` call.
10 Its first argument is the database name, the second a database description and third the maximum database
11 size (in bytes):
12
13 persistence.connect('testdb', 'My test db', 5 * 1024 * 1024);
14
15 A data model is declared using `persistence.define`. The following two definitions define a `Task` and
16 `Category` entity with a few simple properties. The property types are [SQLite types](http://www.sqlite.org/datatype3.html).
17
18 var task = persistence.define('Task', {
19 name: "TEXT",
20 description: "TEXT",
21 done: "BOOL"
22 });
23
24 var category = persistence.define('Category', {
25 name: "TEXT"
26 });
27
28 The returned values are constructor functions and can be used to create new instances of these entities
29 later:
30
31 var myTask = task();
32 var myCategory = category({name: "My category"});
33
34 Relationships between entities are defined using the constructor function's `hasMany` call:
35
36 category.hasMany('tasks', task, 'category');
37
38 This defines a tasks property on category objects containing a `queryCollection` of `task`s,
39 it also defines an inverse relationship on `task` objects with the name `category`.
40
41 The defined entity definitions are synchronized (activated) with the database using a
42 `persistence.schemaSync` call, which takes a callback function (with the used transaction as an argument),
43 that is called when the schema synchronization has completed, the callback is optional.
44
45 persistence.schemaSync();
46
47 Persisting objects
48 ------------------
49
50 All objects retrieved from the databaase are automatically tracked for changes. New entities can be tracked
51 to be persisted by using the `persistence.add` function:
52
53 var c = category({name: "Main category"});
54 persistence.add(c);
55 for ( var i = 0; i < 5; i++) {
56 var t = task();
57 t.name = 'Task ' + i;
58 t.done = i % 2 == 0;
59 t.category = c;
60 persistence.add(t);
61 }
62
63 All changes made to tracked objects can be flushed to the databse by using `persistence.flush`,
64 which takes a transaction object as argument. A new transaction can be started using
65 `persistence.transaction`:
66
67 persistence.transaction(function(tx) {
68 persistence.flush(tx);
69 });
70
71 Querying
72 --------
73
74 var allTasks = task.all().filter("done", '=', true).prefetch("category").order("name", false);
75
76 persistence.transaction(function(tx) {
77 allTasks.list(tx, function (results) {
78 results.forEach(function (r) {
79 console.log(r.name)
80 window.task = r;
81 });
82 });
83 });
Something went wrong with that request. Please try again.