Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: aaronpowell/db.js
...
head fork: aaronpowell/db.js
compare: ab36d9868c
Checking mergeability… Don't worry, you can still create the pull request.
  • 9 commits
  • 9 files changed
  • 0 commit comments
  • 1 contributor
View
41 README.md
@@ -13,38 +13,41 @@ Add a reference to db.js in your application before you want to use IndexedDB:
Once you have the script included you can then open connections to each different database within your application:
var server;
- db.open( 'my-db' , 1 , {
- people: {
- key: {
- keyPath: 'id',
- autoIncrement: true
- }
- }
- }, function ( s ) {
- server = s;
- //connection is open and ready for use
- });
+ db.open({
+ server: 'my-app',
+ version: 1,
+ done: function ( s ) {
+ server = s;
+ }, {
+ people: {
+ key: { keyPath: 'id' , autoIncrement: true }
+ }
+ }
+ });
A connection is intended to be persisted and you can perform multiple operations while it's kept open. Check out the `/tests/public/spec` folder for more examples.
## Adding items
- server.add( 'people' , {
- firstName: 'Aaron',
- lastName: 'Powell'
+ server.people.add( {
+ firstName: 'Aaron',
+ lastName: 'Powell'
} , function ( item ) {
- //item stored
+ //item stored
});
## Removing
-TODO
+ server.people.remove( 1 , function ( item ) {
+ //item stored
+ });
## Querying
-TODO
-
-# License
+ server.people.query( 'firstName' , 'Aaron' )
+ .execute( function ( results ) {
+ //do something with the results
+ });
The MIT License
View
306 project.sublime-workspace
@@ -4,6 +4,26 @@
"selected_items":
[
[
+ "ind",
+ "indexKey"
+ ],
+ [
+ "rea",
+ "readwrite"
+ ],
+ [
+ "tra",
+ "transactionModes"
+ ],
+ [
+ "READ",
+ "READ_WRITE"
+ ],
+ [
+ "REA",
+ "READ_WRITE"
+ ],
+ [
"get",
"getAll"
],
@@ -156,74 +176,66 @@
"buffers":
[
{
- "file": "tests/views/layout.jade",
- "settings":
- {
- "buffer_size": 303,
- "line_ending": "Windows"
- }
- },
- {
- "file": "tests/views/index.jade",
+ "file": "tests/public/specs/server-add.js",
"settings":
{
- "buffer_size": 255,
+ "buffer_size": 4636,
"line_ending": "Windows"
}
},
{
- "file": "tests/app.js",
+ "file": "tests/public/specs/server-remove.js",
"settings":
{
- "buffer_size": 850,
+ "buffer_size": 3858,
"line_ending": "Windows"
}
},
{
- "file": "tests/package.json",
+ "file": "tests/public/specs/indexes.js",
"settings":
{
- "buffer_size": 160,
+ "buffer_size": 7807,
"line_ending": "Windows"
}
},
{
- "file": "README.md",
+ "file": "tests/public/specs/query.js",
"settings":
{
- "buffer_size": 1006,
+ "buffer_size": 8029,
"line_ending": "Windows"
}
},
{
- "file": "/C/Users/Aaron/Downloads/9781430244882/9781430244882_ch05_sourcecode/Chapter 5/MetroGrocer/MetroGrocer/js/default.js",
+ "file": "tests/public/specs/open-db.js",
"settings":
{
- "buffer_size": 0,
+ "buffer_size": 4101,
"line_ending": "Windows"
}
},
{
- "file": "tests/public/specs/server-add.js",
+ "file": "src/db.js",
"settings":
{
- "buffer_size": 3606,
+ "buffer_size": 9060,
"line_ending": "Windows"
}
},
{
- "file": "tests/public/specs/query.js",
+ "file": "tests/views/index.jade",
"settings":
{
- "buffer_size": 2576,
+ "buffer_size": 333,
"line_ending": "Windows"
}
},
{
- "file": "src/db.js",
+ "file": "README.md",
"settings":
{
- "buffer_size": 6259,
+ "buffer_size": 1006,
"line_ending": "Windows"
}
}
@@ -231,18 +243,54 @@
"build_system": "",
"command_palette":
{
- "height": 35.0,
+ "height": 81.0,
"selected_items":
[
[
- "wrap",
- "Word Wrap: Toggle"
- ],
- [
"insta",
"Package Control: Install Package"
],
[
+ "comm",
+ "Git: Commit"
+ ],
+ [
+ "add",
+ "Git: Add..."
+ ],
+ [
+ "git stat",
+ "Git: Status"
+ ],
+ [
+ "quic",
+ "Git: Quick Commit"
+ ],
+ [
+ "stat",
+ "Git: Status"
+ ],
+ [
+ "sta",
+ "Git: Status"
+ ],
+ [
+ "qu",
+ "Git: Quick Commit"
+ ],
+ [
+ "pus",
+ "Git: Push"
+ ],
+ [
+ "git sta",
+ "Git: Status"
+ ],
+ [
+ "wrap",
+ "Word Wrap: Toggle"
+ ],
+ [
"xml",
"Set Syntax: XML"
],
@@ -291,22 +339,10 @@
"Package Control: Discover Packages"
],
[
- "comm",
- "Git: Commit"
- ],
- [
"com",
"Git: Commit"
],
[
- "add",
- "Git: Add..."
- ],
- [
- "git stat",
- "Git: Status"
- ],
- [
"push",
"Git: Push"
],
@@ -315,10 +351,6 @@
"Git: Quick Commit"
],
[
- "git sta",
- "Git: Status"
- ],
- [
"npm inst",
"Nodejs::NPM::Install"
],
@@ -339,10 +371,6 @@
"Word Wrap: Toggle"
],
[
- "stat",
- "Git: Status"
- ],
- [
"add..",
"Git: Add..."
],
@@ -355,7 +383,7 @@
"Package Control: Discover Packages"
]
],
- "width": 416.0
+ "width": 512.0
},
"console":
{
@@ -372,12 +400,21 @@
},
"file_history":
[
- "/D/_Projects/github/db.js/tests/public/specs/server-add.js",
- "/D/_Projects/github/db.js/tests/public/specs/open-db.js",
- "/D/_Projects/github/db.js/tests/app.js",
+ "/D/_Projects/github/db.js/README.md",
+ "/D/_Projects/posts.json",
+ "/D/_Projects/github/jquery/src/core.js",
+ "/C/Users/Aaron/Documents/Visual Studio 11/Projects/App2/App2/pages/itemDetail/itemDetail.html",
+ "/C/Users/Aaron/Documents/Visual Studio 11/Projects/App2/App2/pages/groupedItems/groupedItems.html",
+ "/C/Users/Aaron/Documents/Visual Studio 11/Projects/App2/App2/pages/groupDetail/groupDetail.html",
+ "/C/Users/Aaron/Documents/Visual Studio 11/Projects/App2/App2/default.html",
+ "/C/Users/Aaron/Documents/Visual Studio 11/Projects/App2/App2/js/default.js",
"/D/_Projects/github/db.js/tests/views/layout.jade",
"/D/_Projects/github/db.js/tests/views/index.jade",
+ "/D/_Projects/github/db.js/tests/app.js",
"/D/_Projects/github/db.js/tests/package.json",
+ "/C/Users/Aaron/Downloads/9781430244882/9781430244882_ch05_sourcecode/Chapter 5/MetroGrocer/MetroGrocer/js/default.js",
+ "/D/_Projects/github/db.js/tests/public/specs/server-add.js",
+ "/D/_Projects/github/db.js/tests/public/specs/open-db.js",
"/D/_Projects/github/db.js/src/db.js",
"/D/Dropbox/readify/Fiserv/Accounts/Batch Processing/Sample Files/LK_SLK_VOY_PIN_POINTS_20120418/LK_SLK_VOY_PIN_POINTS_20120418.dat",
"/D/_Projects/bitbucket.org/funnelweb-release/src/FunnelWeb.Web/Views/Shared/_Public.cshtml",
@@ -478,7 +515,6 @@
"/D/_Projects/bitbucket.org/funnelweb-dev/src/FunnelWeb.Web/Views/Shared/PageTemplates/Default.cshtml",
"/D/_Projects/bitbucket.org/funnelweb-dev/src/FunnelWeb.Web/Views/Wiki/Page.cshtml",
"/C/Ruby193/devkit/config.yml",
- "/D/_Projects/github/jquery/src/core.js",
"/D/_Projects/github/Pinboard/Pinboard.suo",
"/D/_Projects/github/Pinboard/Pinboard/Pinboard.jsproj",
"/D/_Projects/github/Pinboard/Pinboard/Pinboard.jsproj.user",
@@ -491,15 +527,7 @@
"/C/Users/Az/Dropbox/Talks/2011 - Teched NZ - JavaScript Pitfalls/js/slideshow.js",
"/C/_Projects/tmp/sniffer.js",
"/D/_Projects/github/cv/public/js/ga.js",
- "/C/Users/Az/Desktop/lync.cer",
- "/C/Users/Az/Desktop/Communicator-uccapi-0.uccapilog",
- "/C/Users/Az/Tracing/Communicator-uccapi-0.uccapilog",
- "/C/_Projects/github/DelivR/DelivR.Samples/DelivR.Samples.csproj",
- "/C/_Projects/github/DelivR/DelivR.Samples.ScreenShare/DelivR.Samples.ScreenShare.csproj",
- "/C/_Projects/github/DelivR/DelivR/DelivR.csproj",
- "/C/_Projects/github/DelivR/.gitignore",
- "/C/Users/Az/Downloads/download (2)",
- "/C/Users/Az/Downloads/Download/ScreenCapture.cs"
+ "/C/Users/Az/Desktop/lync.cer"
],
"find":
{
@@ -507,7 +535,7 @@
},
"find_in_files":
{
- "height": 0.0,
+ "height": 93.0,
"where_history":
[
"",
@@ -520,6 +548,15 @@
"case_sensitive": false,
"find_history":
[
+ "hasOwnProperty",
+ "storeName",
+ "db",
+ "rdashAlpha",
+ "click",
+ "scope",
+ "closing db",
+ "READ_",
+ "READ",
"performInitialSetup",
"header",
"send",
@@ -638,8 +675,7 @@
"instanceOf",
"logLevel",
"\\d+",
- " class=\"MsoNormal\"",
- "windows"
+ " class=\"MsoNormal\""
],
"highlight": true,
"in_selection": false,
@@ -667,51 +703,53 @@
[
{
"buffer": 0,
- "file": "tests/views/layout.jade",
+ "file": "tests/public/specs/server-add.js",
"settings":
{
- "buffer_size": 303,
+ "buffer_size": 4636,
"regions":
{
},
"selection":
[
[
- 0,
- 0
+ 1206,
+ 1690
]
],
"settings":
{
- "syntax": "Packages/Jade/Syntaxes/Jade.tmLanguage",
+ "syntax": "Packages/JavaScript/JavaScript.tmLanguage",
"tab_size": 4,
"translate_tabs_to_spaces": true
},
"translation.x": 0.0,
- "translation.y": 0.0,
+ "translation.y": 342.0,
"zoom_level": 1.0
},
"type": "text"
},
{
"buffer": 1,
- "file": "tests/views/index.jade",
+ "file": "tests/public/specs/server-remove.js",
"settings":
{
- "buffer_size": 255,
+ "buffer_size": 3858,
"regions":
{
},
"selection":
[
[
- 110,
- 110
+ 0,
+ 3858
]
],
"settings":
{
- "syntax": "Packages/Jade/Syntaxes/Jade.tmLanguage"
+ "syntax": "Packages/JavaScript/JavaScript.tmLanguage",
+ "tab_size": 4,
+ "translate_tabs_to_spaces": true
},
"translation.x": 0.0,
"translation.y": 0.0,
@@ -721,99 +759,74 @@
},
{
"buffer": 2,
- "file": "tests/app.js",
+ "file": "tests/public/specs/indexes.js",
"settings":
{
- "buffer_size": 850,
+ "buffer_size": 7807,
"regions":
{
},
"selection":
[
[
- 802,
- 802
+ 7313,
+ 7313
]
],
"settings":
{
"syntax": "Packages/JavaScript/JavaScript.tmLanguage",
- "tab_size": 2,
+ "tab_size": 4,
"translate_tabs_to_spaces": true
},
"translation.x": 0.0,
- "translation.y": 0.0,
+ "translation.y": 1083.0,
"zoom_level": 1.0
},
"type": "text"
},
{
"buffer": 3,
- "file": "tests/package.json",
+ "file": "tests/public/specs/query.js",
"settings":
{
- "buffer_size": 160,
+ "buffer_size": 8029,
"regions":
{
},
"selection":
[
[
- 120,
- 120
+ 7887,
+ 7887
]
],
"settings":
{
- "syntax": "Packages/JavaScript/JSON.tmLanguage"
+ "syntax": "Packages/JavaScript/JavaScript.tmLanguage",
+ "tab_size": 4,
+ "translate_tabs_to_spaces": true
},
"translation.x": 0.0,
- "translation.y": 0.0,
+ "translation.y": 3868.0,
"zoom_level": 1.0
},
"type": "text"
},
{
"buffer": 4,
- "file": "README.md",
- "settings":
- {
- "buffer_size": 1006,
- "regions":
- {
- },
- "selection":
- [
- [
- 0,
- 0
- ]
- ],
- "settings":
- {
- "syntax": "Packages/Markdown/Markdown.tmLanguage",
- "translate_tabs_to_spaces": false
- },
- "translation.x": 0.0,
- "translation.y": 0.0,
- "zoom_level": 1.0
- },
- "type": "text"
- },
- {
- "buffer": 5,
- "file": "/C/Users/Aaron/Downloads/9781430244882/9781430244882_ch05_sourcecode/Chapter 5/MetroGrocer/MetroGrocer/js/default.js",
+ "file": "tests/public/specs/open-db.js",
"settings":
{
- "buffer_size": 0,
+ "buffer_size": 4101,
"regions":
{
},
"selection":
[
[
- 0,
- 0
+ 3381,
+ 3381
]
],
"settings":
@@ -823,25 +836,25 @@
"translate_tabs_to_spaces": true
},
"translation.x": 0.0,
- "translation.y": 0.0,
+ "translation.y": 570.0,
"zoom_level": 1.0
},
"type": "text"
},
{
- "buffer": 6,
- "file": "tests/public/specs/server-add.js",
+ "buffer": 5,
+ "file": "src/db.js",
"settings":
{
- "buffer_size": 3606,
+ "buffer_size": 9060,
"regions":
{
},
"selection":
[
[
- 3464,
- 3606
+ 4693,
+ 4693
]
],
"settings":
@@ -850,64 +863,61 @@
"tab_size": 4,
"translate_tabs_to_spaces": true
},
- "translation.x": 55.0,
- "translation.y": 1296.0,
+ "translation.x": 0.0,
+ "translation.y": 912.0,
"zoom_level": 1.0
},
"type": "text"
},
{
- "buffer": 7,
- "file": "tests/public/specs/query.js",
+ "buffer": 6,
+ "file": "tests/views/index.jade",
"settings":
{
- "buffer_size": 2576,
+ "buffer_size": 333,
"regions":
{
},
"selection":
[
[
- 1601,
- 1601
+ 212,
+ 212
]
],
"settings":
{
- "syntax": "Packages/JavaScript/JavaScript.tmLanguage",
- "tab_size": 4,
- "translate_tabs_to_spaces": true
+ "syntax": "Packages/Jade/Syntaxes/Jade.tmLanguage"
},
"translation.x": 0.0,
- "translation.y": 714.0,
+ "translation.y": 0.0,
"zoom_level": 1.0
},
"type": "text"
},
{
- "buffer": 8,
- "file": "src/db.js",
+ "buffer": 7,
+ "file": "README.md",
"settings":
{
- "buffer_size": 6259,
+ "buffer_size": 1006,
"regions":
{
},
"selection":
[
[
- 2401,
- 2401
+ 0,
+ 0
]
],
"settings":
{
- "syntax": "Packages/JavaScript/JavaScript.tmLanguage",
- "tab_size": 4,
- "translate_tabs_to_spaces": true
+ "syntax": "Packages/Markdown/Markdown.tmLanguage",
+ "translate_tabs_to_spaces": false
},
"translation.x": 0.0,
- "translation.y": 1251.0,
+ "translation.y": 0.0,
"zoom_level": 1.0
},
"type": "text"
@@ -946,6 +956,10 @@
]
},
"menu_visible": true,
+ "output.git":
+ {
+ "height": 124.0
+ },
"replace":
{
"height": 66.0
View
72 src/db.js
@@ -3,17 +3,20 @@
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.oIndexedDB || window.msIndexedDB,
IDBDatabase = window.IDBDatabase || window.webkitIDBDatabase,
IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction,
+ IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange,
transactionModes = {
readonly: IDBTransaction.READ_ONLY || 'readonly',
readwrite: IDBTransaction.READ_WRITE || 'readwrite'
};
+ var hasOwn = Object.prototype.hasOwnProperty;
+
var oldApi = !!IDBDatabase.prototype.setVersion;
if ( !indexedDB ) {
throw 'IndexedDB required';
}
-
+
var Server = function ( db , name ) {
var that = this,
closed = false;
@@ -57,7 +60,7 @@
throw 'Database has been closed';
}
var transaction = db.transaction( table , transactionModes.readwrite );
- var store = transaction( table );
+ var store = transaction.objectStore( table );
store.delete( key );
};
@@ -86,6 +89,50 @@
fn( e.target.result );
};
};
+
+ this.index = function ( table , index ) {
+ return new IndexQuery( table , index , db );
+ };
+
+ for ( var i = 0 , il = db.objectStoreNames.length ; i < il ; i++ ) {
+ (function ( storeName ) {
+ that[ storeName ] = { };
+ for ( var i in that ) {
+ if ( !hasOwn.call( that , i ) || i === 'close' ) {
+ continue;
+ }
+ that[ storeName ][ i ] = (function ( i ) {
+ return function () {
+ var args = [ storeName ].concat( [].slice.call( arguments , 0 ) );
+ return that[ i ].apply( that , args );
+ };
+ })( i );
+ }
+ })( db.objectStoreNames[ i ] );
+ }
+ };
+
+ var IndexQuery = function ( table , indexName , db ) {
+ this.only = function ( val , done ) {
+ var transaction = db.transaction( table ),
+ store = transaction.objectStore( table ),
+ index = store.index( indexName ),
+ singleKeyRange = IDBKeyRange.only( val ),
+ results = [];
+
+ index.openCursor( singleKeyRange ).onsuccess = function ( e ) {
+ var cursor = e.target.result;
+
+ if ( cursor ) {
+ results.push( cursor.value );
+ cursor.continue();
+ }
+ };
+
+ transaction.oncomplete = function () {
+ done( results );
+ };
+ };
};
var Query = function ( table , db ) {
@@ -114,8 +161,9 @@
value = cursor.value;
for ( var i = 0 , il = filters.length ; i < il ; i++ ) {
f = filters[ i ];
-
- if ( value[ f.field ] !== f.value ) {
+ if (typeof f.field === 'function') {
+ inc = f.field(value);
+ } else if (value[f.field] !== f.value) {
inc = false;
}
}
@@ -141,12 +189,18 @@
schema = schema();
}
- for ( var table in schema ) {
- if ( !Object.prototype.hasOwnProperty.call( schema , table ) ) {
+ for ( var tableName in schema ) {
+ var table = schema[ tableName ];
+ if ( !hasOwn.call( schema , tableName ) ) {
continue;
}
- db.createObjectStore( table , schema[ table ].key );
+ var store = db.createObjectStore( tableName , table.key );
+
+ for ( var indexKey in table.indexes ) {
+ var index = table.indexes[ indexKey ];
+ store.createIndex( indexKey , index.key || indexKey , index.options || { unique: false } );
+ }
}
};
@@ -197,7 +251,9 @@
open( e , options.server , options.version , options.done , options.schema );
};
- request.onupgradeneeded = createSchema;
+ request.onupgradeneeded = function ( e ) {
+ createSchema( e , options.schema , e.target.result );
+ };
request.onerror = function () {
console.log( 'failed to open db' , options.server , arguments );
};
View
242 tests/public/specs/indexes.js
@@ -0,0 +1,242 @@
+(function ( db , describe , it , runs , expect , waitsFor , beforeEach , afterEach ) {
+ 'use strict';
+
+ describe( 'db.indexes' , function () {
+ var dbName = 'tests',
+ indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.oIndexedDB || window.msIndexedDB;
+
+ beforeEach( function () {
+ var done = false;
+ var spec = this;
+
+ spec.server = undefined;
+
+ runs( function () {
+ var req = indexedDB.deleteDatabase( dbName );
+
+ req.onsuccess = function () {
+ done = true;
+ };
+
+ req.onerror = function () {
+ console.log( 'failed to delete db in beforeEach' , arguments );
+ };
+
+ req.onblocked = function () {
+ console.log( 'db blocked' , arguments , spec );
+ };
+ });
+
+ waitsFor( function () {
+ return done;
+ }, 'timed out deleting the database', 1000);
+ });
+
+ afterEach( function () {
+ var done;
+
+ runs( function () {
+ if ( this.server ) {
+ this.server.close();
+ }
+
+ var spec = this;
+
+ var req = indexedDB.deleteDatabase( dbName );
+
+ req.onsuccess = function () {
+ done = true;
+ };
+
+ req.onerror = function () {
+ console.log( 'failed to delete db in afterEach' , arguments , spec );
+ };
+
+ req.onblocked = function () {
+ console.log( 'db blocked' , arguments );
+ };
+ });
+
+ waitsFor( function () {
+ return done;
+ }, 'timed out deleting the database', 1000);
+ });
+
+ it( 'should allow creating dbs with indexes' , function () {
+ var spec = this;
+ runs( function () {
+ db.open( {
+ server: dbName ,
+ version: 1 ,
+ done: function ( s ) {
+ spec.server = s;
+ } ,
+ schema: {
+ test: {
+ key: {
+ keyPath: 'id',
+ autoIncrement: true
+ },
+ indexes: {
+ firstName: { }
+ }
+ }
+ }
+ });
+ });
+
+ waitsFor( function () {
+ return !!spec.server;
+ } , 1000 , 'timed out opening the db' );
+
+ var done = false;
+ runs( function () {
+ spec.server.close();
+
+ var req = indexedDB.open( dbName , 1 );
+ req.onsuccess = function ( e ) {
+ var res = e.target.result;
+
+ var transaction = res.transaction( 'test' );
+ var store = transaction.objectStore( 'test' );
+
+ expect( store.indexNames.length ).toEqual( 1 );
+ expect( store.indexNames[ 0 ] ).toEqual( 'firstName' );
+
+ spec.server = res;
+ done = true;
+ };
+ });
+
+ waitsFor( function () {
+ return done;
+ } , 1000 , 'timed out running specs' );
+ });
+ });
+
+ describe( 'index.query' , function () {
+ var dbName = 'tests',
+ indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.oIndexedDB || window.msIndexedDB;
+
+ beforeEach( function () {
+ var done = false;
+ var spec = this;
+
+ spec.server = undefined;
+
+ runs( function () {
+ var req = indexedDB.deleteDatabase( dbName );
+
+ req.onsuccess = function () {
+ done = true;
+ };
+
+ req.onerror = function () {
+ console.log( 'failed to delete db in beforeEach' , arguments );
+ };
+
+ req.onblocked = function () {
+ console.log( 'db blocked' , arguments , spec );
+ };
+ });
+
+ waitsFor( function () {
+ return done;
+ }, 'timed out deleting the database', 1000);
+ });
+
+ afterEach( function () {
+ var done;
+
+ runs( function () {
+ if ( this.server ) {
+ this.server.close();
+ }
+
+ var spec = this;
+
+ var req = indexedDB.deleteDatabase( dbName );
+
+ req.onsuccess = function () {
+ done = true;
+ };
+
+ req.onerror = function () {
+ console.log( 'failed to delete db in afterEach' , arguments , spec );
+ };
+
+ req.onblocked = function () {
+ console.log( 'db blocked' , arguments );
+ };
+ });
+
+ waitsFor( function () {
+ return done;
+ }, 'timed out deleting the database', 1000);
+ });
+
+ it( 'should allow querying on indexes' , function () {
+ var spec = this;
+ runs( function () {
+ db.open( {
+ server: dbName ,
+ version: 1 ,
+ done: function ( s ) {
+ spec.server = s;
+ } ,
+ schema: {
+ test: {
+ key: {
+ keyPath: 'id',
+ autoIncrement: true
+ },
+ indexes: {
+ firstName: { }
+ }
+ }
+ }
+ });
+ });
+
+ waitsFor( function () {
+ return !!spec.server;
+ } , 1000 , 'timed out opening the db' );
+
+ var done = false;
+ runs( function () {
+ var item1 = {
+ firstName: 'Aaron',
+ lastName: 'Powell'
+ };
+ var item2 = {
+ firstName: 'John',
+ lastName: 'Smith'
+ };
+ var item3 = {
+ firstName: 'Aaron',
+ lastName: 'Smith'
+ };
+ spec.server.test.add( [ item1 , item2 , item3 ] , function () {
+ done = true;
+ });
+ });
+
+ waitsFor( function () {
+ return done;
+ } , 1000 , 'timed out adding entries' );
+
+ runs( function () {
+ done = false;
+ spec.server.test.index( 'firstName' ).only( 'Aaron' , function ( results ) {
+ expect( results.length ).toEqual( 2 );
+ done = true;
+ });
+ });
+
+ waitsFor( function () {
+ return done;
+ } , 1000 , 'timed out running specs' );
+ });
+ });
+
+})( window.db , window.describe , window.it , window.runs , window.expect , window.waitsFor , window.beforeEach , window.afterEach );
View
2  tests/public/specs/open-db.js
@@ -1,7 +1,7 @@
(function ( db , describe , it , runs , expect , waitsFor , beforeEach , afterEach ) {
'use strict';
describe( 'db.open' , function () {
- var dbName = 'open-db-tests',
+ var dbName = 'tests',
indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.oIndexedDB || window.msIndexedDB;
beforeEach( function () {
View
50 tests/public/specs/query.js
@@ -2,7 +2,7 @@
'use strict';
describe( 'query' , function () {
- var dbName = 'query-tests',
+ var dbName = 'tests',
indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.oIndexedDB || window.msIndexedDB;
beforeEach( function () {
@@ -196,5 +196,53 @@
return done;
} , 1000 , 'timed out running expects' );
});
+
+ it( 'should query using a function filter' , function () {
+ var item1 = {
+ firstName: 'Aaron',
+ lastName: 'Powell'
+ };
+ var item2 = {
+ firstName: 'John',
+ lastName: 'Smith'
+ };
+ var item3 = {
+ firstName: 'Aaron',
+ lastName: 'Smith'
+ };
+
+ var done = false;
+
+ runs( function () {
+ this.server.test.add( [ item1 , item2 , item3 ] , function () {
+ done = true;
+ });
+ });
+
+ waitsFor( function () {
+ return done;
+ } , 1000 , 'timed out adding record' );
+
+ runs( function () {
+ done = false;
+ this.server
+ .query( 'test' )
+ .filter( function ( x ) {
+ return x.firstName === 'Aaron' && x.lastName === 'Powell'
+ })
+ .execute( function ( results ) {
+ expect( results ).toBeDefined();
+ expect( results.length ).toEqual( 1 );
+ expect( results[0].firstName ).toEqual( item1.firstName );
+ expect( results[0].firstName ).toEqual( item1.firstName );
+
+ done = true;
+ });
+ });
+
+ waitsFor( function () {
+ return done;
+ } , 1000 , 'timed out running expects' );
+ });
});
})( window.db , window.describe , window.it , window.runs , window.expect , window.waitsFor , window.beforeEach , window.afterEach );
View
2  tests/public/specs/server-add.js
@@ -2,7 +2,7 @@
'use strict';
describe( 'server.add' , function () {
- var dbName = 'server-add-tests',
+ var dbName = 'tests',
indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.oIndexedDB || window.msIndexedDB;
beforeEach( function () {
View
119 tests/public/specs/server-remove.js
@@ -0,0 +1,119 @@
+(function ( db , describe , it , runs , expect , waitsFor , beforeEach , afterEach ) {
+ 'use strict';
+
+ describe( 'server.remove' , function () {
+ var dbName = 'tests',
+ indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.oIndexedDB || window.msIndexedDB;
+
+ beforeEach( function () {
+ var done = false;
+ var spec = this;
+
+ spec.server = undefined;
+
+ runs( function () {
+ var req = indexedDB.deleteDatabase( dbName );
+
+ req.onsuccess = function () {
+ done = true;
+ };
+
+ req.onerror = function () {
+ console.log( 'failed to delete db' , arguments );
+ };
+
+ req.onblocked = function () {
+ console.log( 'db blocked' , arguments , spec );
+ };
+ });
+
+ waitsFor( function () {
+ return done;
+ }, 'timed out deleting the database', 1000);
+
+ runs( function () {
+ db.open( {
+ server: dbName ,
+ version: 1 ,
+ done: function ( s ) {
+ spec.server = s;
+ } ,
+ schema: {
+ test: {
+ key: {
+ keyPath: 'id',
+ autoIncrement: true
+ }
+ }
+ }
+ });
+ });
+
+ waitsFor( function () {
+ return !!spec.server;
+ } , 'wait on db' , 500 );
+ });
+
+ afterEach( function () {
+ var done;
+
+ runs( function () {
+ if ( this.server ) {
+ this.server.close();
+ }
+ var req = indexedDB.deleteDatabase( dbName );
+
+ req.onsuccess = function () {
+ done = true;
+ };
+
+ req.onerror = function () {
+ console.log( 'failed to delete db' , arguments );
+ };
+
+ req.onblocked = function () {
+ console.log( 'db blocked' , arguments );
+ };
+ });
+
+ waitsFor( function () {
+ return done;
+ }, 'timed out deleting the database', 1000);
+ });
+
+ it( 'should remove an added item' , function () {
+ var item = {
+ firstName: 'Aaron',
+ lastName: 'Powell'
+ };
+
+ var spec = this;
+
+ runs( function () {
+ spec.server.add( 'test' , item, function ( records ) {
+ item = records[0];
+ });
+ });
+
+ waitsFor( function () {
+ return typeof item.id !== 'undefined';
+ } , 'timeout waiting for item to be added' , 1000 );
+
+ var done = false;
+ runs( function () {
+ spec.server.remove( 'test' , item.id );
+
+ spec.server.get( 'test' , item.id , function ( x ) {
+ expect( x ).toEqual( undefined );
+
+ done = true;
+ });
+ });
+
+ waitsFor( function () {
+ return done;
+ } , 1000 , 'timed out running expects' );
+ });
+ });
+
+})( window.db , window.describe , window.it , window.runs , window.expect , window.waitsFor , window.beforeEach , window.afterEach );
View
2  tests/views/index.jade
@@ -3,7 +3,9 @@ extends layout
append scripts
script(src='/specs/open-db.js')
script(src='/specs/server-add.js')
+ script(src='/specs/server-remove.js')
script(src='/specs/query.js')
+ script(src='/specs/indexes.js')
script
jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
jasmine.getEnv().execute();

No commit comments for this range

Something went wrong with that request. Please try again.