Skip to content

Commit 3bc753a

Browse files
committed
save
1 parent 69ef375 commit 3bc753a

File tree

12 files changed

+246
-33
lines changed

12 files changed

+246
-33
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# indexDBP
2-
indexDBP is a simple way to use indexDB in browsers, base on Promise, similiar to mongoDB api.
1+
# IndexedDBP
2+
IndexedDBP is a simple way to use IndexedDB in browsers, base on Promise, similiar to mongoDB api.
33

4-
```npm install indexdb-p```
4+
```npm install indexeddb-p```
55

66
#### Database Operation
77

demo/index.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
import IndexDBP from '../dist/indexDBP.js';
1+
import IndexedDBP from '../src/main';
22

33
(async () => {
4-
const mydb = new IndexDBP({
4+
const mydb = new IndexedDBP({
55
name: 'testDB',
66
});
77
await mydb.init();
8+
console.log(mydb.db);
9+
mydb.dropDatabase();
10+
console.log(mydb.db);
11+
return false;
812
// deleteIndex
9-
// if (mydb.containObjectStore('indexObjectStore') && mydb.$db.indexObjectStore.containIndex('time')) {
10-
// await mydb.$db.indexObjectStore.deleteIndex('time')
13+
// const isContainIndex = await mydb.$db.indexObjectStore.containIndex('time');
14+
// if (mydb.containObjectStore('indexObjectStore') && isContainIndex) {
15+
// await mydb.$db.indexObjectStore.deleteIndex('time');
16+
// } else if (mydb.containObjectStore('indexObjectStore')) {
17+
// await mydb.$db.indexObjectStore.createIndex('time', 'time', {unique: false, multiEntry: false});
1118
// }
1219

1320
// create objectStore
@@ -25,6 +32,8 @@ import IndexDBP from '../dist/indexDBP.js';
2532
// }
2633

2734
// insert Data
35+
const count = await mydb.$db.testObjectStore.count({$lte: 2});
36+
console.log(count);
2837
await mydb.$db.testObjectStore.insert({key1: 'hello', key2: 123, key3: true, key4: new Date()});
2938
const rand1: number = Math.random();
3039
const rand2: number = Math.random();

dist/IndexedDBP.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/indexDBP.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
{
2-
"name": "indexdb-p",
2+
"name": "indexeddb-p",
33
"version": "0.0.1",
4-
"description": "indexDB Promise Api, similiar to mongoDB api",
5-
"main": "dist/indexDBP.js",
4+
"description": "IndexedDB Promise Api, similiar to mongoDB api",
5+
"main": "dist/IndexedDBP.js",
66
"license": "MIT",
77
"private": false,
88
"files": [
99
"src",
1010
"dist"
1111
],
1212
"keywords": [
13-
"indexDB",
13+
"IndexedDB",
1414
"promise"
1515
],
1616
"scripts": {
1717
"prepublish": "node script/build.js",
1818
"demo": "node script/demo.js",
1919
"build": "node script/build.js",
20-
"test": "mochify"
20+
"test": "mochify --timeout 150000"
2121
},
2222
"author": "FayFang",
2323
"devDependencies": {

script/build.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ let compiler = webpack({
66
entry: path.join(__dirname, '../src/main.ts'),
77
output: {
88
path: path.resolve(__dirname, '../dist'),
9-
filename: 'indexDBP.js',
10-
library: 'indexDBP',
9+
filename: 'IndexedDBP.js',
10+
library: 'IndexedDBP',
1111
libraryTarget: 'umd',
1212
umdNamedDefine: true
1313
},

src/interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface UpdateOptions {
1818
upsert?: boolean;
1919
}
2020

21-
export interface IndexDBPOptions {
21+
export interface IndexedDBPOptions {
2222
name: string;
2323
version?: number;
2424
onError?: any;

src/main.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* 说明:
3-
* 封装indexDB成类mongo风格的API,提供方便的结构化调用
3+
* 封装indexedDB成类mongo风格的API,提供方便的结构化调用
44
*/
5-
import {IndexDBPOptions, ObjectStoreOptions, UpdateOptions, transactionType, QueryOptions} from './interface';
5+
import {IndexedDBPOptions, ObjectStoreOptions, UpdateOptions, transactionType, QueryOptions} from './interface';
66
import {hasVersionError, parseQueryToIDBKeyRange, getIndex} from './tools';
77

8-
const DefaultIndexDBPOptions: IndexDBPOptions = {
9-
name: 'indexDbP',
8+
const DefaultIndexedDBPOptions: IndexedDBPOptions = {
9+
name: 'indexedDBP',
1010
version: 1,
1111
onError(error: any) {
1212
throw error;
@@ -25,9 +25,9 @@ const DefaultUpdateConfig = {
2525
extend: true,
2626
};
2727

28-
const IndexDB = window.indexedDB;
28+
const IndexedDB = window.indexedDB;
2929

30-
class IndexDBP {
30+
class IndexedDBP {
3131
get db() {
3232
return this.pdb;
3333
}
@@ -46,11 +46,11 @@ class IndexDBP {
4646
private versionTransaction!: IDBTransaction;
4747
private onError!: any;
4848
private onSuccess!: any;
49-
constructor(options: IndexDBPOptions = DefaultIndexDBPOptions) {
49+
constructor(options: IndexedDBPOptions = DefaultIndexedDBPOptions) {
5050
this.name = options.name;
5151
this.version = options.version;
52-
this.onError = options.onError || DefaultIndexDBPOptions.onError;
53-
this.onSuccess = options.onSuccess || DefaultIndexDBPOptions.onSuccess;
52+
this.onError = options.onError || DefaultIndexedDBPOptions.onError;
53+
this.onSuccess = options.onSuccess || DefaultIndexedDBPOptions.onSuccess;
5454
}
5555
public init() {
5656
return this.use(this.name, this.version);
@@ -62,18 +62,43 @@ class IndexDBP {
6262
const transaction = this.getTransaction(name, type);
6363
return transaction.objectStore(name);
6464
}
65+
public containDataBase(name: string) {
66+
const containPromise = new Promise((resolve, reject) => {
67+
let dbExists = true;
68+
const req = IndexedDB.open(name);
69+
req.onblocked = (event: any) => {
70+
event.target.result.close();
71+
resolve(dbExists);
72+
};
73+
req.onsuccess = (event: any) => {
74+
event.target.result.close();
75+
resolve(dbExists);
76+
};
77+
req.onerror = (error) => {
78+
reject(this.patchError('open db error'));
79+
};
80+
req.onupgradeneeded = (event: any) => {
81+
dbExists = false;
82+
event.target.result.close();
83+
IndexedDB.deleteDatabase(name);
84+
resolve(dbExists);
85+
};
86+
});
87+
88+
return containPromise;
89+
}
6590
/**
6691
* delete Database
6792
* @param {none} .
6893
* @returns {IDBVersionChangeEvent}
6994
*/
7095
public dropDatabase() {
7196
const dropPromise = new Promise((resolve, reject) => {
72-
if (IndexDB.deleteDatabase && this.db) {
97+
if (IndexedDB.deleteDatabase && this.db) {
7398
const name = this.db.name;
7499
this.closeDB();
75100

76-
const deleteRequest = IndexDB.deleteDatabase(name);
101+
const deleteRequest = IndexedDB.deleteDatabase(name);
77102
deleteRequest.onsuccess = (event) => {
78103
resolve(event);
79104
};
@@ -362,7 +387,7 @@ class IndexDBP {
362387
*/
363388
private use(dbName: string, dbVersion?: number) {
364389
const dbPromise = new Promise((resolve, reject) => {
365-
const openRequest = IndexDB.open(dbName, dbVersion);
390+
const openRequest = IndexedDB.open(dbName, dbVersion);
366391
this.openRequest = openRequest;
367392

368393
openRequest.onerror = (errorEvent: any) => {
@@ -469,4 +494,4 @@ class IndexDBP {
469494
}
470495
}
471496

472-
export default IndexDBP;
497+
export default IndexedDBP;

0 commit comments

Comments
 (0)