Last active
December 16, 2015 18:19
-
-
Save 1999/5476270 to your computer and use it in GitHub Desktop.
Sklad.js examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Clear one object store | |
// @see https://github.com/1999/sklad/blob/master/examples/README_skladConnection_clear.md | |
sklad.open('dbName', function (err, database) { | |
if (err) | |
throw new Error(err); | |
database.clear('objStoreName', function (err) { | |
if (err) | |
throw new Error(err); | |
// object store is clear | |
}); | |
}); | |
// Clear multiple object stores (during one transaction) | |
// @see https://github.com/1999/sklad/blob/master/examples/README_skladConnection_clear.md | |
sklad.open('dbName', function (err, database) { | |
if (err) | |
throw new Error(err); | |
database.clear(['objStoreName_1', 'objStoreName_2', 'objStoreName_3'], function (err) { | |
if (err) | |
throw new Error(err); | |
// object stores are clear | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Count objects in one object store | |
// @see https://github.com/1999/sklad/blob/master/examples/README_skladConnection_count.md | |
sklad.open('dbName', function (err, database) { | |
if (err) | |
throw new Error(err); | |
database.count('objStoreName', { | |
range: IDBKeyRange.bound('lower', 'upper', true, true), | |
index: 'index_name' | |
}, function (err, totalNum) { | |
if (err) | |
throw new Error(err); | |
// total number of records in "objStoreName" (with this range and index) is totalNum | |
}); | |
}); | |
// Count objects in multiple object stores (during one transaction) | |
// @see https://github.com/1999/sklad/blob/master/examples/README_skladConnection_count.md | |
sklad.open('dbName', function (err, database) { | |
if (err) | |
throw new Error(err); | |
database.count({ | |
'objStoreName_1': null, | |
'objStoreName_2': {range: IDBKeyRange.upperBound('upper')}, | |
'objStoreName_3': {index: 'index_name', range: IDBKeyRange.only('key')} | |
}, function (err, total) { | |
if (err) | |
throw new Error(err); | |
// total is smth like this: | |
// { | |
// objStoreName_1: 0, | |
// objStoreName_2: 10 | |
// objStoreName_3: 4 | |
// } | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Delete one record from the object store | |
// @see https://github.com/1999/sklad/blob/master/examples/README_skladConnection_delete.md | |
sklad.open('dbName', function (err, database) { | |
if (err) | |
throw new Error(err); | |
database.delete('objStoreName', 'key', function (err) { | |
if (err) | |
throw new Error(err); | |
// record is deleted | |
}); | |
}); | |
// Delete multiple records from the object stores (during one transaction) | |
// @see https://github.com/1999/sklad/blob/master/examples/README_skladConnection_delete.md | |
sklad.open('dbName', function (err, database) { | |
if (err) | |
throw new Error(err); | |
database.delete({ | |
'objStoreName_1': ['key_1', 'key_2', 'key_3'], | |
'objStoreName_2': ['key1'] | |
}, function (err) { | |
if (err) | |
throw new Error(err); | |
// all records are deleted | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get objects from one object store | |
// @see https://github.com/1999/sklad/blob/master/examples/README_skladConnection_get.md | |
sklad.open('dbName', function (err, database) { | |
if (err) | |
throw new Error(err); | |
database.get('objStoreName', { | |
range: IDBKeyRange.bound('lower', 'upper', true, true), | |
index: 'index_name', | |
offset: 20, | |
limit: 10, | |
direction: sklad.DESC | |
}, function (err, records) { | |
if (err) | |
throw new Error(err); | |
// records in an object with structure: | |
// { | |
// key1: object1, | |
// key2: object2, | |
// ... | |
// } | |
}); | |
}); | |
// Get objects from multiple object stores (during one transaction) | |
// @see https://github.com/1999/sklad/blob/master/examples/README_skladConnection_get.md | |
sklad.open('dbName', function (err, database) { | |
if (err) | |
throw new Error(err); | |
database.get({ | |
'objStoreName_1': { | |
range: IDBKeyRange.bound('lower', 'upper', true, true), | |
index: 'index_name', | |
offset: 20, | |
limit: 10, | |
direction: sklad.DESC | |
}, | |
'objStoreName_2': { | |
limit: 3, | |
direction: sklad.ASC_UNIQUE | |
} | |
}, function (err, records) { | |
if (err) | |
throw new Error(err); | |
// records in an object with structure: | |
// { | |
// objStoreName_1: { | |
// key1: object1, | |
// key2: object2, | |
// ... | |
// }, | |
// objStoreName_2: { | |
// key3: object3, | |
// ... | |
// } | |
// } | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Insert one record into the object store | |
// @see https://github.com/1999/sklad/blob/master/examples/README_skladConnection_insert.md | |
sklad.open('dbName', function (err, database) { | |
if (err) | |
throw new Error(err); | |
database.insert('objStoreName', {foo: 'bar'}, function (err, insertedKey) { | |
if (err) | |
throw new Error(err); | |
// work with inserted key | |
}); | |
}); | |
// Insert multiple records into the object stores (during one transaction) | |
// @see https://github.com/1999/sklad/blob/master/examples/README_skladConnection_insert.md | |
sklad.open('dbName', function (err, database) { | |
if (err) | |
throw new Error(err); | |
database.insert({ | |
'objStoreName_1': ['Lorem', 'ipsum', 'dolor', 'sit', 'amet'], | |
'objStoreName_2': [{foo: 'bar'}, {foo: 'bar'}] | |
}, function (err, insertedKeys) { | |
if (err) | |
throw new Error(err); | |
// insertedKeys is smth like this: | |
// { | |
// objStoreName_1: [key1, key2, key3, key4, key5] | |
// objStoreName_2: [key6, key7] | |
// } | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Insert or update one record in the object store | |
// @see https://github.com/1999/sklad/blob/master/examples/README_skladConnection_upsert.md | |
sklad.open('dbName', function (err, database) { | |
if (err) | |
throw new Error(err); | |
database.upsert('objStoreName', {foo: 'bar'}, function (err, upsertedKey) { | |
if (err) | |
throw new Error(err); | |
// work with upserted key | |
}); | |
}); | |
// Insert or update multiple records in the object stores (during one transaction) | |
// @see https://github.com/1999/sklad/blob/master/examples/README_skladConnection_upsert.md | |
sklad.open('dbName', function (err, database) { | |
if (err) | |
throw new Error(err); | |
database.upsert({ | |
'objStoreName_1': ['Lorem', 'ipsum', 'dolor', 'sit', 'amet'], | |
'objStoreName_2': [{foo: 'bar'}, {foo: 'bar'}] | |
}, function (err, upsertedKeys) { | |
if (err) | |
throw new Error(err); | |
// upsertedKeys is smth like this: | |
// { | |
// objStoreName_1: [key1, key2, key3, key4, key5] | |
// objStoreName_2: [key6, key7] | |
// } | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @see https://github.com/1999/sklad/blob/master/examples/README_sklad_open.md | |
sklad.open('dbName', { | |
version: 2, | |
migration: { | |
'1': function (database) { | |
// This migration part starts when your code runs first time in the browser. | |
// This is a migration from "didn't exist" to "1" database version | |
var objStore = database.createObjectStore('users', {autoIncrement: true}); | |
objStore.createIndex('fb_search', 'facebook_id', {unique: true}); | |
}, | |
'2': function (database) { | |
// This migration part starts when your database migrates from "1" to "2" version | |
var objStore = database.createObjectStore('users_likes', {keyPath: 'date'}); | |
} | |
} | |
}, function (err, database) { | |
// work with database | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment