@@ -12,7 +12,7 @@ The API is still developing, but using LocalStorageDB is pretty simple:
12
12
13
13
1 . Create a new instance of the LocalStorageDB object, supplying the name you wish to give the DB
14
14
15
- > ` var DB = new LocalStorageDB('my_first_database'); `
15
+ ` var DB = new LocalStorageDB('my_first_database'); `
16
16
17
17
2 . If the DB exists already (from a previous session), you're good to go. Otherwise, see the API to do the usual CRUD operations.
18
18
@@ -22,14 +22,14 @@ __`CREATE()` - creates a new table__
22
22
23
23
You can define your tables by supplying a name for the table and a object-based definition (type checking is coming…).
24
24
25
- > ` DB.CREATE( 'my_first_table', { id: 0, foo: 'bar', test: 'ing' } ); `
25
+ ` DB.CREATE( 'my_first_table', { id: 0, foo: 'bar', test: 'ing' } ); `
26
26
27
27
If you want to load data in immediately, you can do that by supplying a single object (for one row) or an array of objects (for multiple rows) as the optional third argument (see ` INSERT_INTO() ` ).
28
28
29
29
30
30
__ ` INSERT_INTO() ` - adds data to a table__
31
31
32
- > ` DB.INSERT_INTO( 'my_first_table', [{test:'ed'},{foo:'bat'}] ); `
32
+ ` DB.INSERT_INTO( 'my_first_table', [{test:'ed'},{foo:'bat'}] ); `
33
33
34
34
_ Note: Any table defined to have an ` id ` property will automatically assign a unique auto-incremented id to that property upon insertion. Also, any defaults set in the definition object will be used if the property is not defined on the inserted object._
35
35
@@ -38,48 +38,57 @@ __`SELECT()` - find data in a table__
38
38
39
39
You can select all data in a table by only supplying the table name to the method
40
40
41
- > ` DB.SELECT( 'my_first_table' ); [{id:0,foo:'bar',test:'ed'},{id:1,foo:'bat',test:'ing'}] `
41
+ ` DB.SELECT( 'my_first_table' ); [{id:0,foo:'bar',test:'ed'},{id:1,foo:'bat',test:'ing'}] `
42
42
43
- Or you can collect a subset of rows, using an object to define your search criteria (JOIN and complex criteria are coming) .
43
+ Or you can collect a subset of rows, using an object to define your search criteria.
44
44
45
- > ` DB.SELECT( 'my_first_table', {foo:'bar'} ); // [{id:0,foo:'bar',test:'ed'}] `
45
+ ` DB.SELECT( 'my_first_table', {foo:'bar'} ); // [{id:0,foo:'bar',test:'ed'}] `
46
+
47
+ Or you can collect a subset of rows, using a function to collect rows (by returning ` true ` ).
48
+
49
+ ` DB.SELECT( 'my_first_table', function( row ){ return ( row.foo == 'bar' ); } ); // [{id:0,foo:'bar',test:'ed'}] `
50
+
51
+ ` SELECT() ` returns a ` RESULT_SET ` object (which inherits from Array). ` RESULT_SET ` objects can be mutated by the methods ` ORDER_BY() ` and ` LIMIT() ` .
52
+
53
+ ` DB.SELECT( 'my_first_table' ).LIMIT(1)[0]; // {id:0,foo:'bar',test:'ed'} `
54
+ ` DB.SELECT( 'my_first_table' ).ORDER_BY( 'foo DESC' ).LIMIT(1)[0]; // {id:1,foo:'bat',test:'ing'} `
46
55
47
56
48
57
__ ` UPDATE() ` - updates rows in a table__
49
58
50
- > ` DB.SELECT( 'my_first_table', {foo:'bar'} ); // [{id:0,foo:'bar',test:'ed'}] `
51
- > ` DB.UPDATE( 'my_first_table', {test:'nada'}, {foo:'bar'} ); `
52
- > ` DB.SELECT( 'my_first_table', {foo:'bar'} ); // [{id:0,foo:'bar',test:'nada'}] `
59
+ ` DB.SELECT( 'my_first_table', {foo:'bar'} ); // [{id:0,foo:'bar',test:'ed'}] `
60
+ ` DB.UPDATE( 'my_first_table', {test:'nada'}, {foo:'bar'} ); `
61
+ ` DB.SELECT( 'my_first_table', {foo:'bar'} ); // [{id:0,foo:'bar',test:'nada'}] `
53
62
54
63
55
64
__ ` DELETE() ` - remove data from a table__
56
65
57
66
You can delete select rows from a table using (you guessed it) a criteria object. Currently, all criteria must be met for the row to be removed.
58
67
59
- > ` DB.DELETE( 'my_first_table', {foo:'bar'} ); `
60
- > ` DB.SELECT( 'my_first_table', {foo:'bar'} ); // [] `
68
+ ` DB.DELETE( 'my_first_table', {foo:'bar'} ); `
69
+ ` DB.SELECT( 'my_first_table', {foo:'bar'} ); // [] `
61
70
62
71
Supplying only the table name is the same as calling ` TRUNCATE() `
63
72
64
73
65
74
__ ` AFFECTED_ROWS() ` - returns the number of rows affected by the last operation__
66
75
67
- > ` DB.INSERT_INTO( 'my_first_table', {test:'ed'} ); `
68
- > `DB.AFFECTED_ROWS(); // 1
76
+ ` DB.INSERT_INTO( 'my_first_table', {test:'ed'} ); `
77
+ ` DB.AFFECTED_ROWS(); // 1 `
69
78
70
79
__ ` TRUNCATE() ` - empty a table & reset its index, but retain its definition__
71
80
72
- > ` DB.TRUNCATE( 'my_first_table' ); `
73
- > ` DB.SELECT( 'my_first_table' ); // [] `
81
+ ` DB.TRUNCATE( 'my_first_table' ); `
82
+ ` DB.SELECT( 'my_first_table' ); // [] `
74
83
75
84
__ ` DESCRIBE() ` - returns the definition for a table__
76
85
77
- > ` DB.DESCRIBE( 'my_first_table' ); // { id: 0, foo: 'bar', test: 'ing' } `
86
+ ` DB.DESCRIBE( 'my_first_table' ); // { id: 0, foo: 'bar', test: 'ing' } `
78
87
79
88
__ ` DROP() ` - drop a table from the DB__
80
89
81
- > ` DB.DROP( 'my_first_table' ); `
82
- > `DB.SELECT( 'my_first_table' ); // ERROR
90
+ ` DB.DROP( 'my_first_table' ); `
91
+ ` DB.SELECT( 'my_first_table' ); // ERROR `
83
92
84
93
85
94
## License
0 commit comments