Skip to content
This repository was archived by the owner on Jul 3, 2024. It is now read-only.

Commit 1b04169

Browse files
just getting started
0 parents  commit 1b04169

File tree

1 file changed

+324
-0
lines changed

1 file changed

+324
-0
lines changed

src/LocalStorageDB.js

Lines changed: 324 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,324 @@
1+
/*------------------------------------------------------------------------------
2+
Function: LocalStorageDB()
3+
Author: Aaron Gustafson (aaron at easy-designs dot net)
4+
Creation Date: 2011-10-03
5+
Version: 0.1
6+
Homepage: http://github.com/easy-designs/LocalStorageDB.js
7+
License: MIT License (see homepage)
8+
------------------------------------------------------------------------------*/
9+
;if ( 'localStorage' in window )
10+
{
11+
/**
12+
* LocalStorageDB()
13+
* a simple cross-browser DB using localStorage
14+
*
15+
* @param str name - the name for your DB
16+
* @return obj - a LocalStorageDB instance
17+
*
18+
*/
19+
function LocalStorageDB( name )
20+
{
21+
this.version = '0.1';
22+
23+
var
24+
UNDEFINED,
25+
WINDOW = window,
26+
__cache = WINDOW.localStorage,
27+
HYPHEN = '-',
28+
PREFIX = 'LocalStorageDB-',
29+
TABLES = '::tables::',
30+
31+
encode = JSON.stringify,
32+
decode = JSON.parse,
33+
34+
db = {};
35+
36+
37+
// Private Methods
38+
function load()
39+
{
40+
var
41+
tables = readFromCache( TABLES ),
42+
t = tables.length;
43+
if ( t )
44+
{
45+
// store for easier access
46+
db[TABLES] = tables;
47+
// loop and load
48+
while ( t-- )
49+
{
50+
db[ tables[t] ] = readFromCache( tables[t] );
51+
}
52+
}
53+
else
54+
{
55+
db[TABLES] = [];
56+
cache( TABLES );
57+
}
58+
}
59+
function tableExists( table )
60+
{
61+
if ( TABLES in db )
62+
{
63+
var
64+
tables = db[TABLES],
65+
t = tables.length;
66+
while ( t-- )
67+
{
68+
if ( table == tables[t] )
69+
{
70+
throw new Error( table + ' already exists in DB ' + name );
71+
return true;
72+
}
73+
}
74+
}
75+
return false;
76+
}
77+
function insert( table, data )
78+
{
79+
var
80+
t = db[table],
81+
p = t.proto,
82+
i = t.index++,
83+
d = {},
84+
k;
85+
for ( k in p )
86+
{
87+
if ( p.hasOwnProperty( k ) )
88+
{
89+
// ids get auto-assigned, otherwise use the supplied data or the prototype fallback
90+
d[k] = ( k == 'id' ) ? i : ( data[k] || p[k] );
91+
}
92+
}
93+
db[table].data.push( d );
94+
}
95+
function findMatches( d, c )
96+
{
97+
var
98+
i = d.length,
99+
a = [],
100+
r, p;
101+
rows: while ( i-- )
102+
{
103+
r = d[i];
104+
for ( p in c )
105+
{
106+
if ( c.hasOwnProperty( p ) &&
107+
r[p] != c[p] )
108+
{
109+
continue rows;
110+
}
111+
}
112+
a.push( r );
113+
}
114+
return a.reverse();
115+
}
116+
function withMatches( d, c, f )
117+
{
118+
var
119+
i = d.length,
120+
r, p;
121+
rows: while ( i-- )
122+
{
123+
r = d[i];
124+
for ( p in c )
125+
{
126+
if ( c.hasOwnProperty( p ) &&
127+
r[p] != c[p] )
128+
{
129+
continue rows;
130+
}
131+
}
132+
// trigger the callback, supplying the index
133+
f( i );
134+
}
135+
return true;
136+
}
137+
function cache( table )
138+
{
139+
writeToCache( table, db[table] );
140+
}
141+
function argTest( required, provided, message )
142+
{
143+
if ( provided < required )
144+
{
145+
throw new Error( message );
146+
}
147+
}
148+
// storage
149+
function readFromCache( table )
150+
{
151+
table = PREFIX + name + HYPHEN + table;
152+
table = __cache.getItem( table );
153+
return !! table ? decode( table ) : [];
154+
}
155+
function writeToCache( table, data )
156+
{
157+
table = PREFIX + name + HYPHEN + table;
158+
__cache.setItem( table, encode( data ) );
159+
return true;
160+
}
161+
function removeFromCache( table )
162+
{
163+
var
164+
table = table ? PREFIX + name + HYPHEN + table : PREFIX + name;
165+
__cache.removeItem( table );
166+
return true;
167+
}
168+
169+
/**
170+
* LocalStorageDB.truncate() -> boolean
171+
* Empties the DB
172+
*
173+
* Option 1: Empties the entire DB
174+
* LocalStorageDB.truncate()
175+
*
176+
* Option 2: Empties a table
177+
* LocalStorageDB.truncate( table )
178+
* @param str table - the table you'd like to empty
179+
*/
180+
this.truncate = function()
181+
{
182+
var
183+
table = false,
184+
args = arguments;
185+
if ( args.length == 1 )
186+
{
187+
table = SUB + args[0];
188+
}
189+
return removeFromCache( table );
190+
};
191+
192+
/**
193+
* LocalStorageDB.selectRows( table, criteria ) -> array
194+
* Selects rows from a given table based on the supplied criteria
195+
*
196+
* Option 1: Select the entire DB table
197+
* LocalStorageDB.selectRows( table )
198+
* @param str table - the table to read
199+
*
200+
* Option 2: Select based on criteria
201+
* LocalStorageDB.selectRows( table, criteria )
202+
* @param str table - the table to read
203+
* @param obj criteria - the criteria to match
204+
*/
205+
this.selectRows = function( table, criteria )
206+
{
207+
return findMatches( db[table].data, criteria );
208+
};
209+
210+
/**
211+
* LocalStorageDB.insertRows( table, data ) -> boolean
212+
* Adds data to the table
213+
*
214+
* Option 1: Add a row
215+
* LocalStorageDB.insertRows( table, data )
216+
* @param str table - the table to use
217+
* @param obj data - the data object to insert
218+
*/
219+
this.insertRows = function( table, data )
220+
{
221+
if ( data instanceof Array )
222+
{
223+
for ( var i=0, len = data.length; i < len; i++ )
224+
{
225+
if ( data[i] instanceof Object )
226+
{
227+
insert( table, data[i] );
228+
}
229+
}
230+
}
231+
else if ( data instanceof Object )
232+
{
233+
insert( table, data );
234+
}
235+
else
236+
{
237+
throw new Error( 'LocalStorageDB.insert() expects an Object or an array of Objects to be inserted as data' );
238+
}
239+
cache( table );
240+
return true;
241+
};
242+
243+
244+
/**
245+
* LocalStorageDB.createTable( table, proto, data ) -> boolean
246+
* Creates a new table and (optionally) in serts data into it
247+
*
248+
* Option 1: Create a table
249+
* LocalStorageDB.createTable( table, proto )
250+
* @param str table - the table name
251+
* @param obj proto - the data object to use as a prototype for all rows
252+
*
253+
* Option 2: Create a table and prefill it with data
254+
* LocalStorageDB.createTable( table, proto, data )
255+
* @param str table - the table name
256+
* @param obj proto - the data object to use as a prototype for all rows
257+
* @param array data - the data you want to prefill the table with
258+
*/
259+
this.createTable = function( table, proto, data )
260+
{
261+
if ( ! proto ||
262+
tableExists( table ) )
263+
{
264+
return false;
265+
}
266+
267+
// set up the table
268+
db[table] = {};
269+
db[table].data = [];
270+
db[table].proto = proto;
271+
db[table].index = 0;
272+
273+
// inser the data (if asked)
274+
if ( data &&
275+
data instanceof Array )
276+
{
277+
console.log('inserting data');
278+
this.insert( table, data );
279+
}
280+
281+
// cache the table
282+
cache( table );
283+
// cache the table index
284+
db[TABLES].push( table );
285+
cache( TABLES );
286+
return true;
287+
};
288+
289+
/**
290+
* LocalStorageDB.deleteRows() -> boolean
291+
* Removes rows matching criteria from table
292+
*
293+
* Option 1: Remove all rows
294+
* LocalStorageDB.deleteRows( table )
295+
* @param str table - the table to delete all rows from
296+
*
297+
* Option 2: Remove select rows
298+
* LocalStorageDB.deleteRows( table, criteria )
299+
* @param str table - the table to delete rows from
300+
* @param obj criteria - the criteria to match
301+
*/
302+
this.deleteRows = function( table, criteria )
303+
{
304+
if ( criteria == UNDEFINED )
305+
{
306+
return this.truncate( table );
307+
}
308+
else
309+
{
310+
withMatches( db[table].data, criteria, function( i ){
311+
db[table].data.splice(i,1);
312+
});
313+
cache( table );
314+
return true;
315+
}
316+
};
317+
318+
319+
320+
load();
321+
}
322+
}
323+
324+

0 commit comments

Comments
 (0)