indexedDB-CRUD
indexedDB-CRUD packs obscure indexedDB CRUD methods to a really friendly succinct interface. And offer multi-objectStore CRUD handler.
If you want to operate one or more indexedDB objectStore, just DB.open(config).then(successCallback).catch()
, and you'll get a indexedDB-crud handler when its DB.open
successed.
Hope you keep in mind that:
config
's format should be correct- if you not input
storeName
explicitly in API, your config's firstsotreName
will be the default storeName - indexedDB object store can only hold JavaScript objects. The objects must have a property with the same name as the key path
Installation
npm install indexeddb-crud --save yarn add indexeddb-crud
API Overview
first step
Only you open it, you can use these synchronous&asynchronous API.
synchronous API:
asynchronous API
indexeddb-crud support for the ES6 Promises API, so you can use then
& catch
carefree.
get:
add:
remove:
update:
usage
import
var DB = ; // or ES6;
API
open(config)
- initialData is Optional, and it's a array object
- about initialData,
key = 0
is just for demo, we only usekey >= 1
, so we usually begain atkey = 1
- your config's structure should like this,
you must have a key(number type)
, in this following code key is id)
config = "name": '' "version": '' "storeConfig": "storeName": '' "key": '' "storeConfig": // must have key property, number type // one or more storeConfig object
correct config just like this:
var DBConfig = "name": "JustToDo" "version": 23 "storeConfig": "storeName": "list" "key": "id" "initialData": "id": 0 "event": "JustDemo" "finished": true // just for demo, not actual use ;
If you need more than 1 ObjectStore:
var DBConfig = "name": "JustToDo" "version": 23 "storeConfig": "storeName": "list" "key": "id" "initialData": "id": 1 "event": "JustDemo" "finished": true "storeName": "aphorism" "key": "id" "initialData": "id": 1 "content": "You're better than that" "id": 2 "content": "Yesterday You Said Tomorrow"
e.g. successCallback:
{ ;} { console;} DB;
getLength(storeName?)
// If you have only 1 objectSotre, suggest use the default storeNamevar randomIndex = Math; // or pass storeName explicitlyvar storeName = 'list';var randomIndex = Math;
getNewKey(storeName?)
// If you have only 1 objectSotre, suggest use the default storeNameDB; // or pass storeName explicitlyvar storeName = 'list';DB;
You will need it in addItem()
.
getItem(key, storeName?)
{ console;} // If you have only 1 objectSotre, suggest use the default storeName DB; // or pass storeName explicitlyvar storeName = 'list';DB;
- the key should be a number, which matched to db's id
getConditionItem(condition, whether, storeName?)
- whether is
Boolean
type condition
should be a boolean-condition, for example:
var DBConfig = "name": "JustToDo" "version": 23 "storeConfig": "storeName": "list" "key": "id" "initialData": "id": 0 "event": "JustDemo" "finished": true // just for demo, not actual use ; // If you have only 1 objectSotre, suggest use the default storeName DB; // or pass storeName explicitlyvar storeName = 'list';DB;
getAll(storeName?)
{ console;} // If you have only 1 objectSotre, suggest use the default storeName DB; // or pass storeName explicitlyvar storeName = 'list';DB;
addItem(data, storeName?)
- data's structure should at least contains number type key.
e.g.
var data = "id": DB "event": 'play soccer' "finished": false ; // If you have only 1 objectSotre, suggest use the default storeNameDB; // or pass storeName explicitlyvar storeName = 'list';DB;
removeItem(key, storeName?)
- the key should be number type, which matched to db's key.
// If you have only 1 objectSotre, suggest use the default storeName DB; // or pass storeName explicitlyvar storeName = 'list';DB;
removeConditionItem(condition, whether, storeName?)
- whether is Boolean
condition
should be a boolean-condition, for example:
var DBConfig = "name": "JustToDo" "version": 23 "storeConfig": "storeName": "list" "key": "id" "initialData": "id": 0 "event": "JustDemo" "finished": true // just for demo, not actual use ; // If you have only 1 objectSotre, suggest use the default storeName DB; // or pass storeName explicitlyvar storeName = 'list';DB;
clear(storeName?)
// If you have only 1 objectSotre, suggest use the default storeNameDBclear; // or pass storeName explicitlyvar storeName = 'list';DBclearstoreName;
updateItem(newData, storeName?)
var DBConfig = "name": "JustToDo" "version": 23 "storeConfig": "storeName": "list" "key": "id" "initialData": "id": 0 "event": "JustDemo" "finished": true // just for demo, not actual use ; // If you have only 1 objectSotre, suggest use the default storeNameDB; // or pass storeName explicitlyvar storeName = 'list';DB;
example
a simple todolist web-app, storage data in indexedDB (use indexeddb-crud to handler 2 different objectStores): https://github.com/RayJune/JustToDo/blob/gh-pages/src/scripts/main.js
author
RayJune: a CS university student from Fuzhou, Fujian, China
License
MIT