项目作者: lrlna

项目描述 :
fun wrapper around IndexedDB
高级语言: JavaScript
项目地址: git://github.com/lrlna/nanoidb.git
创建时间: 2017-05-14T10:58:44Z
项目社区:https://github.com/lrlna/nanoidb

开源协议:MIT License

下载


nanoidb

npm version build status
downloads js-standard-style

IndexedDB is a web-api for client-side storage, and although widely used, the API
itself is at times confusing. Nanoidb is small wrapper to help standardize most
useful methods in a callback based fashion.

IndexedDB is an async transactional database that lets you store objects as
<key, value> pairs. The pairs get stored in object stores that are
essentially a set of database tables, but in an indexedDB context.

Usage

  1. var db = Nanoidb('catStore', 1)
  2. db.on('upgrade', function (diffData) {
  3. diffData.db.createObjectStore('catStore')
  4. })
  5. db.on('open', function (stores) {
  6. putOp(stores.catStore)
  7. function putOp (store) {
  8. stores.catStore.put('key-12345', 'ChashuCat', function (err) {
  9. if (err) throw err
  10. console.log('put done')
  11. batchOp(stores.catStore)
  12. getOp(stores.catStore)
  13. getAllOp(stores.catStore)
  14. })
  15. }
  16. function getAllOp (store) {
  17. store.getAll('dang', 10, function (err, values) {
  18. if (err) throw err
  19. values.forEach(function (value) {
  20. console.log('new value', value)
  21. })
  22. })
  23. }
  24. function getOp (store) {
  25. store.get('key-12345', function (err, val) {
  26. if (err) throw err
  27. console.log('get value', val)
  28. deleteOp(store)
  29. })
  30. }
  31. function deleteOp (store) {
  32. store.del('catStore', function (err) {
  33. if (err) throw err
  34. console.log('deleted')
  35. batchOp(store)
  36. })
  37. }
  38. function batchOp (store) {
  39. store.batch()
  40. .put('coolThang', 'hell yea')
  41. .put('dang', 'no wayyy')
  42. .put('ding', 'whoaaa yea')
  43. .del('ding')
  44. .flush(function (err) {
  45. if (err) throw err
  46. console.log('it flushed successfully')
  47. })
  48. }
  49. })

API

db = Nanoidb(name, version)

This creates an instance of IndexedDB. It takes in a database name and
version. IndexedDB’s versioning starts with 1, rather than 0.

db.upgrade(version)

Upgrades the current version of Nanoidb with the specified version. This is
useful for when you need to create an extra object store under the same
Nanoidb instance.

db.on('upgrade', callback(diffData))

Returns an object composed of a previously created indexedDB and a
IDBVersionChangeEvent. This is where you should create your object store by
calling diffData.db.createObjectStore('<name>').

diffData.event provides you with an oldVersion property to help with
schema updates.

db.on('open', callback(stores))

Returns an instance of an object store that you can later use.

db.on('error', callback(error))

Returns an IndexedDB error object. This event will be sent when IndexedDB runs
into an error creating a database.

store = stores.<name>

Instance of an Object Store.

store.put(key, val, callback(err))

Given the Object Store you previously created, you can add a value to the
database via the put method. Takes an object key, val, and an error
callback.

store.get(key, callback(err, val))

You can also get a value from the database with a get. Takes a key and an
error callback.

store.getAll([query, count], callback(err, val))

Get all records in a given store. Can optionally take a query
key
or
range, as well as a count for values to be returned if there are duplicates.
Will return all records if neither query nor count are provided.

store.del(key, callback(err))

Delete method takes a key and an error callback.

batch = store.batch()

You can also batch chain del and get methods. When you’re done, you have to
call a .flush() to handle your callback.

batch.put(key, val)

Add an object within a batch operation. Takes a key and a val.

batch.del(key)

Delete an object within a batch operation. Takes a key.

batch.flush(callback(err))

When working with a batch() method, you have to call a flush to handle your
errors. This just takes an error callback.

Install

  1. npm install nanoidb

Related content

License

MIT