Last active
August 27, 2016 16:52
-
-
Save 1999/ca2f4af78eafa050521f to your computer and use it in GitHub Desktop.
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/docs/README_skladConnection_clear.md | |
const dbName = `dbName_${Date.now()}`; | |
const conn = await sklad.open(dbName, { | |
version: 2, | |
migration: { | |
'1': (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 | |
const objStore = database.createObjectStore('users', {autoIncrement: true}); | |
objStore.createIndex('email_search', 'email', {unique: true}); | |
objStore.createIndex('name_search', 'firstname'); | |
}, | |
'2': (database) => { | |
// This migration part starts when your database migrates from "1" to "2" version | |
const objStore = database.createObjectStore('foo_obj_store', {keyPath: 'foo'}); | |
} | |
} | |
}); | |
const words = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'.split(' '); | |
const data = { | |
users: [ | |
{email: '[email protected]', firstname: 'John'}, | |
{email: '[email protected]', firstname: 'Jack'}, | |
{email: '[email protected]', firstname: 'Peter'}, | |
], | |
foo_obj_store: words.map(word => ({foo: word})) | |
}; | |
await conn.insert(data); | |
await conn.clear(['users', 'foo_obj_store']); | |
// objects stores are empty now |
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/docs/README_skladConnection_close.md | |
const dbName = `dbName_${Date.now()}`; | |
const conn = await sklad.open(dbName, { | |
version: 2, | |
migration: { | |
'1': (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 | |
const objStore = database.createObjectStore('users', {autoIncrement: true}); | |
objStore.createIndex('email_search', 'email', {unique: true}); | |
objStore.createIndex('name_search', 'firstname'); | |
}, | |
'2': (database) => { | |
// This migration part starts when your database migrates from "1" to "2" version | |
const objStore = database.createObjectStore('foo_obj_store', {keyPath: 'foo'}); | |
} | |
} | |
}); | |
conn.close(); |
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/docs/README_skladConnection_count.md | |
const dbName = `dbName_${Date.now()}`; | |
const conn = await sklad.open(dbName, { | |
version: 2, | |
migration: { | |
'1': (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 | |
const objStore = database.createObjectStore('users', {autoIncrement: true}); | |
objStore.createIndex('email_search', 'email', {unique: true}); | |
objStore.createIndex('name_search', 'firstname'); | |
}, | |
'2': (database) => { | |
// This migration part starts when your database migrates from "1" to "2" version | |
const objStore = database.createObjectStore('foo_obj_store', {keyPath: 'foo'}); | |
} | |
} | |
}); | |
const words = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'.split(' '); | |
const data = { | |
users: [ | |
{email: '[email protected]', firstname: 'John'}, | |
{email: '[email protected]', firstname: 'Jack'}, | |
{email: '[email protected]', firstname: 'Peter'}, | |
], | |
foo_obj_store: words.map(word => ({foo: word})) | |
}; | |
const insertedKeys = await conn.insert(data); | |
const total = await conn.count('foo_obj_store', {range: IDBKeyRange.bound('A', 'l')}); |
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/docs/README_skladConnection_delete.md | |
const dbName = `dbName_${Date.now()}`; | |
const conn = sklad.open(dbName, { | |
version: 2, | |
migration: { | |
'1': (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 | |
const objStore = database.createObjectStore('users', {autoIncrement: true}); | |
objStore.createIndex('email_search', 'email', {unique: true}); | |
objStore.createIndex('name_search', 'firstname'); | |
}, | |
'2': (database) => { | |
// This migration part starts when your database migrates from "1" to "2" version | |
const objStore = database.createObjectStore('foo_obj_store', {keyPath: 'foo'}); | |
} | |
} | |
}); | |
const words = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'.split(' '); | |
const data = { | |
users: [ | |
{email: '[email protected]', firstname: 'John'}, | |
{email: '[email protected]', firstname: 'Jack'}, | |
{email: '[email protected]', firstname: 'Peter'}, | |
], | |
foo_obj_store: words.map(word => ({foo: word})) | |
}; | |
const insertedKeys = await conn.insert(data); | |
const data = { | |
foo_obj_store: words.filter(() => (Math.random() > 0.5)) | |
}; | |
await conn.delete(data); |
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/docs/README_skladConnection_get.md | |
const dbName = `dbName_${Date.now()}`; | |
const conn = await sklad.open(dbName, { | |
version: 2, | |
migration: { | |
'1': (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 | |
const objStore = database.createObjectStore('users', {autoIncrement: true}); | |
objStore.createIndex('email_search', 'email', {unique: true}); | |
objStore.createIndex('name_search', 'firstname'); | |
}, | |
'2': (database) => { | |
// This migration part starts when your database migrates from "1" to "2" version | |
const objStore = database.createObjectStore('foo_obj_store', {keyPath: 'foo'}); | |
} | |
} | |
}); | |
const words = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'.split(' '); | |
const data = { | |
users: [ | |
{email: '[email protected]', firstname: 'John'}, | |
{email: '[email protected]', firstname: 'Jack'}, | |
{email: '[email protected]', firstname: 'Peter'}, | |
], | |
foo_obj_store: words.map(word => ({foo: word})) | |
}; | |
const insertedKeys = await conn.insert(data); | |
const data = await conn.get({ | |
users: {direction: sklad.DESC, index: 'name_search'}, | |
foo_obj_store: {offset: 10, limit: 5} | |
}); |
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/docs/README_skladConnection_insert.md | |
const dbName = `dbName_${Date.now()}`; | |
const conn = await sklad.open(dbName, { | |
version: 2, | |
migration: { | |
'1': (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 | |
const objStore = database.createObjectStore('users', {autoIncrement: true}); | |
objStore.createIndex('email_search', 'email', {unique: true}); | |
objStore.createIndex('name_search', 'firstname'); | |
}, | |
'2': (database) => { | |
// This migration part starts when your database migrates from "1" to "2" version | |
const objStore = database.createObjectStore('foo_obj_store', {keyPath: 'foo'}); | |
} | |
} | |
}); | |
const words = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'.split(' '); | |
const data = { | |
users: [ | |
{email: '[email protected]', firstname: 'John'}, | |
{email: '[email protected]', firstname: 'Jack'}, | |
{email: '[email protected]', firstname: 'Peter'}, | |
], | |
foo_obj_store: words.map(word => ({foo: word})) | |
}; | |
await conn.insert(data); |
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/docs/README_skladConnection_upsert.md | |
const dbName = `dbName_${Date.now()}`; | |
const conn = await sklad.open(dbName, { | |
version: 2, | |
migration: { | |
'1': (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 | |
const objStore = database.createObjectStore('users', {autoIncrement: true}); | |
objStore.createIndex('email_search', 'email', {unique: true}); | |
objStore.createIndex('name_search', 'firstname'); | |
}, | |
'2': (database) => { | |
// This migration part starts when your database migrates from "1" to "2" version | |
const objStore = database.createObjectStore('foo_obj_store', {keyPath: 'foo'}); | |
} | |
} | |
}); | |
var words = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'.split(' '); | |
var data = { | |
users: [ | |
{email: '[email protected]', firstname: 'John'}, | |
{email: '[email protected]', firstname: 'Jack'}, | |
{email: '[email protected]', firstname: 'Peter'}, | |
], | |
foo_obj_store: words.map(function (word) { return {foo: word}; }) | |
}; | |
const insertedKeys = await conn.insert(data); | |
const data = { | |
foo_obj_store: words.map(word => ({ | |
foo: word, | |
some_new_field: Math.random() | |
})) | |
}; | |
const upsertedKeys = await conn.upsert(data); |
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/docs/README_sklad_open.md | |
const dbName = `dbName_${Date.now()}`; | |
const conn = await sklad.open(dbName, { | |
version: 2, | |
migration: { | |
'1': (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 | |
const objStore = database.createObjectStore('users', {autoIncrement: true}); | |
objStore.createIndex('email_search', 'email', {unique: true}); | |
objStore.createIndex('name_search', 'firstname'); | |
}, | |
'2': (database) => { | |
// This migration part starts when your database migrates from "1" to "2" version | |
const objStore = database.createObjectStore('foo_obj_store', {keyPath: 'foo'}); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment