项目作者: fayfang

项目描述 :
indexedDB Promise Api
高级语言: JavaScript
项目地址: git://github.com/fayfang/indexedDBP.git
创建时间: 2019-05-14T09:07:54Z
项目社区:https://github.com/fayfang/indexedDBP

开源协议:

下载


IndexedDBP

Build Status

IndexedDBP is a simple way to use IndexedDB in browsers, base on Promise, similiar to mongoDB api.

  1. npm install indexeddbp

example

  1. import IndexedDBP from 'indexeddbp';
  2. (async () => {
  3. const mydb = new IndexedDBP({
  4. name: 'testDB',
  5. });
  6. await mydb.init();
  7. // deleteIndex
  8. // const isContainIndex = await mydb.$db.indexObjectStore.containIndex('time');
  9. // if (mydb.containObjectStore('indexObjectStore') && isContainIndex) {
  10. // await mydb.$db.indexObjectStore.deleteIndex('time');
  11. // } else if (mydb.containObjectStore('indexObjectStore')) {
  12. // await mydb.$db.indexObjectStore.createIndex('time', 'time', {unique: false, multiEntry: false});
  13. // }
  14. // create objectStore
  15. await mydb.useObjectStore('testObjectStore');
  16. if (!mydb.containObjectStore('indexObjectStore')) {
  17. await mydb.createObjectStore('indexObjectStore', {keyPath: 'randomId'});
  18. await mydb.$db.indexObjectStore.createIndex('time', 'time', {unique: false, multiEntry: false});
  19. }
  20. // delete ObjectStore
  21. // if (mydb.containObjectStore('testObjectStore2')) {
  22. // mydb.deleteObjectStore('testObjectStore2');
  23. // }
  24. // insert Data
  25. const count = await mydb.$db.testObjectStore.count({$lte: 2});
  26. console.log(count);
  27. await mydb.$db.testObjectStore.insert({key1: 'hello', key2: 123, key3: true, key4: new Date()});
  28. const rand1: number = Math.random();
  29. const rand2: number = Math.random();
  30. await mydb.$db.indexObjectStore.insert({randomId: rand1, time: new Date('2019/04/03')});
  31. await mydb.$db.indexObjectStore.insert({randomId: rand2, time: new Date('2019/04/05')});
  32. // find Data
  33. const res0 = await mydb.$db.testObjectStore.find(1);
  34. const res1 = await mydb.$db.indexObjectStore.find(new Date('2019/04/03'), 'time');
  35. const res2 = await mydb.$db.indexObjectStore.find({$lte: rand2 , count: 5});
  36. console.log(res0, res1);
  37. console.log(res2, rand2);
  38. // remove data
  39. // let resRemove = await mydb.$db.testObjectStore.remove({$lte: 5});
  40. // console.log(resRemove);
  41. // update data
  42. // let resUpdate = await mydb.$db.testObjectStore.update({$lte: 10}, {test: 222}, {multi: true});
  43. const resUpdate = await mydb.$db.testObjectStore.update(1, {test: 222}, {upsert: true});
  44. console.log(resUpdate);
  45. })();

more example to see demo

operation

Database Operation

constructor (options?: object)
  • constructor of the class
  • default options = {name: ‘indexedDBP’, version: 1, onError: fn, onSuccess: fn}
db.containDataBase(databaseName: string): Promise:
  • a database exist or not, you can use it before init()
db.init(): Promise
  • init the database, you must init a new instance before other operation
db.closeDB(): undefined
  • close the database
db.dropDatabase(): Promise
  • delete the databse

ObjectStore Operation

db.containObjectStore(name: string): boolean
  • the database contain the obejectStore or not
db.createObjectStore(name: string, options?: ObjectStoreOptions): Promise
  • create objectStore in this database
  • default ObjectStoreOptions = {autoIncrement: true, keyPath: ‘id’};
db.deleteObjectStore(name: string)
  • Delete objectStore in this database

Document Operation

db.objectStoreName.containIndex(indexName: string): Promise
  • the obejectStore contain the index or not
db.objectStoreName.createIndex(indexName: string, keyPath?: string, objectParameters?: IDBIndexParameters): Promise
db.objectStoreName.deleteIndex(indexName: string): Promise
  • create a index of the objectStore
db.objectStoreName.count(query?: QueryOptions): Promise
  • count the objectStoreName by QueryOptions
  • get total when query is undefined
db.objectStoreName.find(query: key | index | QueryOptions, indexName?: tring): Promise
  • fint data by query
db.objectStoreName.insert(document: any, key?: string): Promise
  • insert data
db.objectStoreName.update(query: key | QueryOptions, data: any, options?: UpdateOptions): Promise
  • update data
  • default UpdateOptions = {upsert: false, multi: fasle, extend: true}
db.objectStoreName.remove(query?: key | QueryOptions): Promise
  • remove data

Interface

  1. interface ObjectStoreOptions {
  2. autoIncrement?: boolean;
  3. keyPath?: string;
  4. }
  5. interface QueryOptions {
  6. value?: any;
  7. count?: number;
  8. $lt?: any;
  9. $lte?: any;
  10. $gte?: any;
  11. $gt?: any;
  12. }
  13. interface UpdateOptions {
  14. extend?: boolean;
  15. multi?: boolean;
  16. upsert?: boolean;
  17. }
  18. interface IndexedDBPOptions {
  19. name: string;
  20. version?: number;
  21. onError?: any;
  22. onSuccess?: any;
  23. }