promise-idb 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -0
- package/dist/AsyncDB.d.ts +13 -0
- package/dist/AsyncDB.js +58 -0
- package/dist/DBError.d.ts +6 -0
- package/dist/DBError.js +9 -0
- package/dist/DBStore.d.ts +19 -0
- package/dist/DBStore.js +138 -0
- package/dist/DBStoreIndex.d.ts +6 -0
- package/dist/DBStoreIndex.js +10 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/types.d.ts +45 -0
- package/dist/types.js +1 -0
- package/package.json +77 -0
package/README.md
ADDED
|
Binary file
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import DBStore from "./DBStore";
|
|
2
|
+
import type { AsyncDBInstance } from "./types";
|
|
3
|
+
export default class AsyncDB {
|
|
4
|
+
private openReq;
|
|
5
|
+
private db;
|
|
6
|
+
private dbName;
|
|
7
|
+
private version?;
|
|
8
|
+
private stores;
|
|
9
|
+
private constructor();
|
|
10
|
+
static init(dbName: string, version?: number): AsyncDB;
|
|
11
|
+
createStore<T>(name: string, options?: IDBObjectStoreParameters): DBStore<T>;
|
|
12
|
+
build(): Promise<AsyncDBInstance>;
|
|
13
|
+
}
|
package/dist/AsyncDB.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import IDbError from "./DBError";
|
|
2
|
+
import DBStore from "./DBStore";
|
|
3
|
+
export default class AsyncDB {
|
|
4
|
+
openReq;
|
|
5
|
+
db;
|
|
6
|
+
dbName;
|
|
7
|
+
version;
|
|
8
|
+
stores;
|
|
9
|
+
constructor(dbName, version) {
|
|
10
|
+
this.dbName = dbName;
|
|
11
|
+
this.version = version;
|
|
12
|
+
this.stores = new Map();
|
|
13
|
+
}
|
|
14
|
+
static init(dbName, version) {
|
|
15
|
+
return new AsyncDB(dbName, version);
|
|
16
|
+
}
|
|
17
|
+
createStore(name, options) {
|
|
18
|
+
const store = new DBStore(name, options);
|
|
19
|
+
this.stores.set(name, store);
|
|
20
|
+
return store;
|
|
21
|
+
}
|
|
22
|
+
async build() {
|
|
23
|
+
return new Promise((res, rej) => {
|
|
24
|
+
this.openReq = window.indexedDB.open(this.dbName, this.version);
|
|
25
|
+
this.openReq.onerror = (e) => {
|
|
26
|
+
rej(new IDbError(e.target.error?.message, e));
|
|
27
|
+
};
|
|
28
|
+
this.openReq.onsuccess = (e) => {
|
|
29
|
+
this.db = e.target.result;
|
|
30
|
+
const entries = this.stores.entries();
|
|
31
|
+
for (const [_, store] of entries)
|
|
32
|
+
store.setDB(this.db);
|
|
33
|
+
res({
|
|
34
|
+
db: this.db,
|
|
35
|
+
dbName: this.dbName,
|
|
36
|
+
openReq: this.openReq,
|
|
37
|
+
version: this.version,
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
this.openReq.onupgradeneeded = (e) => {
|
|
41
|
+
this.db = e.target.result;
|
|
42
|
+
for (const store of this.stores.values()) {
|
|
43
|
+
const s = this.db.createObjectStore(store.name, store.getOptions());
|
|
44
|
+
const indexes = store.getIndexes();
|
|
45
|
+
for (const index of indexes) {
|
|
46
|
+
const name = index.name;
|
|
47
|
+
const keyPath = index.keyPath;
|
|
48
|
+
const options = index.options;
|
|
49
|
+
s.createIndex(name, keyPath, options);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
this.openReq.onblocked = (e) => {
|
|
54
|
+
rej(new IDbError(e.target.error?.message, e));
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
package/dist/DBError.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import DBSToreIndex from "./DBStoreIndex";
|
|
2
|
+
export default class DBStore<T> {
|
|
3
|
+
name: string;
|
|
4
|
+
private options?;
|
|
5
|
+
private indexes;
|
|
6
|
+
db: IDBDatabase;
|
|
7
|
+
constructor(name: string, options?: IDBObjectStoreParameters);
|
|
8
|
+
setDB(db: IDBDatabase): void;
|
|
9
|
+
add(data: T): Promise<unknown>;
|
|
10
|
+
get(query: IDBValidKey | IDBKeyRange): Promise<T>;
|
|
11
|
+
getAll(query?: IDBValidKey | IDBKeyRange | null | undefined, count?: number): Promise<T[]>;
|
|
12
|
+
getKey(query: IDBValidKey | IDBKeyRange): Promise<T>;
|
|
13
|
+
getAllKeys(query?: IDBValidKey | IDBKeyRange | null | undefined, count?: number): Promise<T[]>;
|
|
14
|
+
update<K = T>(value: K, key?: IDBValidKey): Promise<T>;
|
|
15
|
+
delete(query: IDBValidKey | IDBKeyRange): Promise<unknown>;
|
|
16
|
+
addIndex(name: string, keyPath: string | string[], options?: IDBIndexParameters): void;
|
|
17
|
+
getIndexes(): DBSToreIndex[];
|
|
18
|
+
getOptions(): IDBObjectStoreParameters | undefined;
|
|
19
|
+
}
|
package/dist/DBStore.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import DBSToreIndex from "./DBStoreIndex";
|
|
2
|
+
import IDbError from "./DBError";
|
|
3
|
+
export default class DBStore {
|
|
4
|
+
name;
|
|
5
|
+
options;
|
|
6
|
+
indexes;
|
|
7
|
+
db;
|
|
8
|
+
constructor(name, options) {
|
|
9
|
+
this.name = name;
|
|
10
|
+
this.options = options;
|
|
11
|
+
this.indexes = [];
|
|
12
|
+
}
|
|
13
|
+
setDB(db) {
|
|
14
|
+
this.db = db;
|
|
15
|
+
}
|
|
16
|
+
add(data) {
|
|
17
|
+
return new Promise((res, rej) => {
|
|
18
|
+
const transaction = this.db.transaction(this.name, "readwrite");
|
|
19
|
+
const s = transaction.objectStore(this.name);
|
|
20
|
+
const result = s.add(data);
|
|
21
|
+
result.onerror = (e) => {
|
|
22
|
+
const message = e.target.error?.message;
|
|
23
|
+
transaction.commit();
|
|
24
|
+
rej(new IDbError(message, e));
|
|
25
|
+
};
|
|
26
|
+
result.onsuccess = () => {
|
|
27
|
+
transaction.commit();
|
|
28
|
+
res(true);
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
get(query) {
|
|
33
|
+
return new Promise((res, rej) => {
|
|
34
|
+
const transaction = this.db.transaction(this.name, "readwrite");
|
|
35
|
+
const s = transaction.objectStore(this.name);
|
|
36
|
+
const result = s.get(query);
|
|
37
|
+
result.onerror = (e) => {
|
|
38
|
+
const message = e.target.error?.message;
|
|
39
|
+
transaction.commit();
|
|
40
|
+
rej(new IDbError(message, e));
|
|
41
|
+
};
|
|
42
|
+
result.onsuccess = (e) => {
|
|
43
|
+
transaction.commit();
|
|
44
|
+
res(e.target.result);
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
getAll(query, count) {
|
|
49
|
+
return new Promise((res, rej) => {
|
|
50
|
+
const transaction = this.db.transaction(this.name, "readwrite");
|
|
51
|
+
const s = transaction.objectStore(this.name);
|
|
52
|
+
const result = s.getAll(query, count);
|
|
53
|
+
result.onerror = (e) => {
|
|
54
|
+
const message = e.target.error?.message;
|
|
55
|
+
transaction.commit();
|
|
56
|
+
rej(new IDbError(message, e));
|
|
57
|
+
};
|
|
58
|
+
result.onsuccess = (e) => {
|
|
59
|
+
transaction.commit();
|
|
60
|
+
res(e.target.result);
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
getKey(query) {
|
|
65
|
+
return new Promise((res, rej) => {
|
|
66
|
+
const transaction = this.db.transaction(this.name, "readwrite");
|
|
67
|
+
const s = transaction.objectStore(this.name);
|
|
68
|
+
const result = s.getKey(query);
|
|
69
|
+
result.onerror = (e) => {
|
|
70
|
+
const message = e.target.error?.message;
|
|
71
|
+
transaction.commit();
|
|
72
|
+
rej(new IDbError(message, e));
|
|
73
|
+
};
|
|
74
|
+
result.onsuccess = (e) => {
|
|
75
|
+
transaction.commit();
|
|
76
|
+
res(e.target.result);
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
getAllKeys(query, count) {
|
|
81
|
+
return new Promise((res, rej) => {
|
|
82
|
+
const transaction = this.db.transaction(this.name, "readwrite");
|
|
83
|
+
const s = transaction.objectStore(this.name);
|
|
84
|
+
const result = s.add(query, count);
|
|
85
|
+
result.onerror = (e) => {
|
|
86
|
+
const message = e.target.error?.message;
|
|
87
|
+
transaction.commit();
|
|
88
|
+
rej(new IDbError(message, e));
|
|
89
|
+
};
|
|
90
|
+
result.onsuccess = (e) => {
|
|
91
|
+
transaction.commit();
|
|
92
|
+
res(e.target.result);
|
|
93
|
+
};
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
update(value, key) {
|
|
97
|
+
return new Promise((res, rej) => {
|
|
98
|
+
const transaction = this.db.transaction(this.name, "readwrite");
|
|
99
|
+
const s = transaction.objectStore(this.name);
|
|
100
|
+
const result = s.put(value, key);
|
|
101
|
+
result.onerror = (e) => {
|
|
102
|
+
const message = e.target.error?.message;
|
|
103
|
+
transaction.commit();
|
|
104
|
+
rej(new IDbError(message, e));
|
|
105
|
+
};
|
|
106
|
+
result.onsuccess = (e) => {
|
|
107
|
+
transaction.commit();
|
|
108
|
+
res(e.target.result);
|
|
109
|
+
};
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
delete(query) {
|
|
113
|
+
return new Promise((res, rej) => {
|
|
114
|
+
const transaction = this.db.transaction(this.name, "readwrite");
|
|
115
|
+
const s = transaction.objectStore(this.name);
|
|
116
|
+
const result = s.delete(query);
|
|
117
|
+
result.onerror = (e) => {
|
|
118
|
+
const message = e.target.error?.message;
|
|
119
|
+
transaction.commit();
|
|
120
|
+
rej(new IDbError(message, e));
|
|
121
|
+
};
|
|
122
|
+
result.onsuccess = (e) => {
|
|
123
|
+
transaction.commit();
|
|
124
|
+
res(e.target.result);
|
|
125
|
+
};
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
addIndex(name, keyPath, options) {
|
|
129
|
+
const index = new DBSToreIndex(name, keyPath, options);
|
|
130
|
+
this.indexes.push(index);
|
|
131
|
+
}
|
|
132
|
+
getIndexes() {
|
|
133
|
+
return this.indexes;
|
|
134
|
+
}
|
|
135
|
+
getOptions() {
|
|
136
|
+
return this.options;
|
|
137
|
+
}
|
|
138
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import DBStore from "./DBStore";
|
|
2
|
+
export type IEvent = Event & {
|
|
3
|
+
target: IDBOpenDBRequest;
|
|
4
|
+
};
|
|
5
|
+
export type StoreArgs = {
|
|
6
|
+
name: string;
|
|
7
|
+
options?: IDBObjectStoreParameters | undefined;
|
|
8
|
+
};
|
|
9
|
+
export type IndexArgs = {
|
|
10
|
+
name: string;
|
|
11
|
+
keyPath: string | string[];
|
|
12
|
+
options?: IDBIndexParameters;
|
|
13
|
+
};
|
|
14
|
+
export type StoreArguments = {
|
|
15
|
+
store: StoreArgs;
|
|
16
|
+
indexes: IndexArgs[];
|
|
17
|
+
};
|
|
18
|
+
export type StringsToObject<T extends readonly string[]> = {
|
|
19
|
+
[K in T[number]]: null;
|
|
20
|
+
};
|
|
21
|
+
export type AsyncDBCallback = ((ev: IEvent) => any) | null;
|
|
22
|
+
export interface IDbAsync {
|
|
23
|
+
addEventListener(type: unknown, listener: unknown, options?: unknown): void;
|
|
24
|
+
dispatchEvent(event: IEvent): boolean;
|
|
25
|
+
error: DOMException | null;
|
|
26
|
+
onblocked: AsyncDBCallback;
|
|
27
|
+
onerror: AsyncDBCallback;
|
|
28
|
+
onsuccess: AsyncDBCallback;
|
|
29
|
+
onupgradeneeded: AsyncDBCallback;
|
|
30
|
+
readyState: IDBRequestReadyState;
|
|
31
|
+
removeEventListener(type: unknown, listener: unknown, options?: unknown): void;
|
|
32
|
+
result: AsyncDBDatabase;
|
|
33
|
+
source: IDBObjectStore | IDBIndex | IDBCursor;
|
|
34
|
+
transaction: IDBTransaction | null;
|
|
35
|
+
}
|
|
36
|
+
export type AsyncDBDatabase = IDBDatabase & {
|
|
37
|
+
getStore<T>(name: string): DBStore<T>;
|
|
38
|
+
};
|
|
39
|
+
export type StoreMap = Record<string, DBStore<any>>;
|
|
40
|
+
export type AsyncDBInstance = {
|
|
41
|
+
openReq: IDbAsync;
|
|
42
|
+
db: AsyncDBDatabase;
|
|
43
|
+
dbName: string;
|
|
44
|
+
version?: number;
|
|
45
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "promise-idb",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A lightweight TypeScript library that simplifies IndexedDB with async/await syntax and Promise-based API for modern browser storage",
|
|
5
|
+
"homepage": "https://github.com/Maliklar/async-indexeddb#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/Maliklar/async-indexeddb/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/Maliklar/async-indexeddb.git"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"indexeddb",
|
|
15
|
+
"indexed-db",
|
|
16
|
+
"idb",
|
|
17
|
+
"database",
|
|
18
|
+
"db",
|
|
19
|
+
"browser-database",
|
|
20
|
+
"client-database",
|
|
21
|
+
"web-database",
|
|
22
|
+
"storage",
|
|
23
|
+
"browser-storage",
|
|
24
|
+
"persistent-storage",
|
|
25
|
+
"offline-storage",
|
|
26
|
+
"async",
|
|
27
|
+
"promise",
|
|
28
|
+
"async-await",
|
|
29
|
+
"promises",
|
|
30
|
+
"typescript",
|
|
31
|
+
"typed",
|
|
32
|
+
"types",
|
|
33
|
+
"typescript-library",
|
|
34
|
+
"wrapper",
|
|
35
|
+
"indexeddb-wrapper",
|
|
36
|
+
"indexeddb-helper",
|
|
37
|
+
"database-wrapper",
|
|
38
|
+
"utility",
|
|
39
|
+
"helper",
|
|
40
|
+
"offline",
|
|
41
|
+
"offline-first",
|
|
42
|
+
"pwa",
|
|
43
|
+
"progressive-web-app",
|
|
44
|
+
"client-side",
|
|
45
|
+
"frontend",
|
|
46
|
+
"browser",
|
|
47
|
+
"data",
|
|
48
|
+
"persistence",
|
|
49
|
+
"key-value",
|
|
50
|
+
"nosql",
|
|
51
|
+
"store",
|
|
52
|
+
"crud"
|
|
53
|
+
],
|
|
54
|
+
"exports": {
|
|
55
|
+
".": {
|
|
56
|
+
"import": "./dist/index.js",
|
|
57
|
+
"require": "./dist/index.js",
|
|
58
|
+
"types": "./dist/index.d.ts"
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"license": "MIT",
|
|
62
|
+
"author": "Malik Elbadri <elbadrimalik@gmail.com>",
|
|
63
|
+
"type": "module",
|
|
64
|
+
"types": "dist/types.d.ts",
|
|
65
|
+
"files": [
|
|
66
|
+
"dist",
|
|
67
|
+
"README.md"
|
|
68
|
+
],
|
|
69
|
+
"main": "dist/index.js",
|
|
70
|
+
"scripts": {
|
|
71
|
+
"build": "tsc",
|
|
72
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
73
|
+
},
|
|
74
|
+
"devDependencies": {
|
|
75
|
+
"typescript": "^5.9.3"
|
|
76
|
+
}
|
|
77
|
+
}
|