@wxn0brp/db-storage-web 0.120.1 → 0.120.2
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/dist/indexed.d.ts +12 -0
- package/dist/indexed.js +100 -46
- package/dist/storage.d.ts +9 -0
- package/dist/storage.js +30 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -2
package/dist/indexed.d.ts
CHANGED
|
@@ -7,8 +7,18 @@ export declare class IndexedDBActions extends ActionsBase {
|
|
|
7
7
|
_version: number;
|
|
8
8
|
_inited: boolean;
|
|
9
9
|
version: string;
|
|
10
|
+
capabilities: {
|
|
11
|
+
transactions: boolean;
|
|
12
|
+
indexes: boolean;
|
|
13
|
+
persistent: boolean;
|
|
14
|
+
};
|
|
10
15
|
constructor(name: string);
|
|
11
16
|
_getDBName(): string;
|
|
17
|
+
_dbContext(): {
|
|
18
|
+
adapter: string;
|
|
19
|
+
db: string;
|
|
20
|
+
};
|
|
21
|
+
_requireDb(): IDBDatabase;
|
|
12
22
|
init(): Promise<void>;
|
|
13
23
|
_loadVersion(): Promise<void>;
|
|
14
24
|
_saveVersion(): Promise<void>;
|
|
@@ -21,10 +31,12 @@ export declare class IndexedDBActions extends ActionsBase {
|
|
|
21
31
|
getCollections(): Promise<string[]>;
|
|
22
32
|
removeCollection(collection: string): Promise<boolean>;
|
|
23
33
|
add(query: VQueryT.Add): Promise<DataInternal>;
|
|
34
|
+
bulkAdd(query: VQueryT.BulkAdd): Promise<DataInternal[]>;
|
|
24
35
|
find(query: VQueryT.Find): Promise<DataInternal[]>;
|
|
25
36
|
findOne(query: VQueryT.FindOne): Promise<DataInternal | null>;
|
|
26
37
|
update(query: VQueryT.Update): Promise<DataInternal[]>;
|
|
27
38
|
updateOne(query: VQueryT.Update): Promise<DataInternal | null>;
|
|
28
39
|
remove(query: VQueryT.Remove): Promise<DataInternal[]>;
|
|
29
40
|
removeOne(query: VQueryT.Remove): Promise<DataInternal | null>;
|
|
41
|
+
count(query: VQueryT.Count): Promise<number>;
|
|
30
42
|
}
|
package/dist/indexed.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ActionsBase } from "@wxn0brp/db-core/base/actions";
|
|
2
|
-
import {
|
|
2
|
+
import { AdapterCloseError, AdapterError, AdapterInitError, } from "@wxn0brp/db-core/errors";
|
|
3
|
+
import { addId, addIdBulk } from "@wxn0brp/db-core/helpers/addId";
|
|
3
4
|
import { findObj, matchObj, updateObj } from "@wxn0brp/db-core/utils/process";
|
|
4
5
|
import { version } from "./version.js";
|
|
5
6
|
export class IndexedDBActions extends ActionsBase {
|
|
@@ -8,6 +9,11 @@ export class IndexedDBActions extends ActionsBase {
|
|
|
8
9
|
_version = 1;
|
|
9
10
|
_inited = false;
|
|
10
11
|
version = version;
|
|
12
|
+
capabilities = {
|
|
13
|
+
transactions: false,
|
|
14
|
+
indexes: false,
|
|
15
|
+
persistent: true,
|
|
16
|
+
};
|
|
11
17
|
constructor(name) {
|
|
12
18
|
super();
|
|
13
19
|
this.name = name;
|
|
@@ -15,27 +21,43 @@ export class IndexedDBActions extends ActionsBase {
|
|
|
15
21
|
_getDBName() {
|
|
16
22
|
return "vdb_" + this.name;
|
|
17
23
|
}
|
|
24
|
+
_dbContext() {
|
|
25
|
+
return {
|
|
26
|
+
adapter: "IndexedDB",
|
|
27
|
+
db: this._getDBName(),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
_requireDb() {
|
|
31
|
+
if (!this.db)
|
|
32
|
+
throw new AdapterError("Database not initialized", this._dbContext());
|
|
33
|
+
return this.db;
|
|
34
|
+
}
|
|
18
35
|
async init() {
|
|
19
36
|
if (this.db)
|
|
20
37
|
return;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
db.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
38
|
+
try {
|
|
39
|
+
await new Promise((resolve, reject) => {
|
|
40
|
+
const request = indexedDB.open(this._getDBName());
|
|
41
|
+
request.onerror = () => reject(request.error);
|
|
42
|
+
request.onsuccess = () => {
|
|
43
|
+
this.db = request.result;
|
|
44
|
+
this._version = this.db.version;
|
|
45
|
+
this._loadVersion()
|
|
46
|
+
.then(() => resolve())
|
|
47
|
+
.catch(reject);
|
|
48
|
+
};
|
|
49
|
+
request.onupgradeneeded = event => {
|
|
50
|
+
const db = request.result;
|
|
51
|
+
this._version = db.version;
|
|
52
|
+
if (!db.objectStoreNames.contains("_meta")) {
|
|
53
|
+
db.createObjectStore("_meta");
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
throw new AdapterInitError(err, this._dbContext());
|
|
60
|
+
}
|
|
39
61
|
}
|
|
40
62
|
async _loadVersion() {
|
|
41
63
|
if (!this.db)
|
|
@@ -63,14 +85,18 @@ export class IndexedDBActions extends ActionsBase {
|
|
|
63
85
|
});
|
|
64
86
|
}
|
|
65
87
|
async close() {
|
|
66
|
-
if (this.db)
|
|
88
|
+
if (!this.db)
|
|
89
|
+
return;
|
|
90
|
+
try {
|
|
67
91
|
this.db.close();
|
|
68
92
|
this.db = null;
|
|
69
93
|
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
throw new AdapterCloseError(err, this._dbContext());
|
|
96
|
+
}
|
|
70
97
|
}
|
|
71
98
|
async _changeSchema(upgradeFn) {
|
|
72
|
-
|
|
73
|
-
throw new Error("Database not initialized");
|
|
99
|
+
this._requireDb();
|
|
74
100
|
const newVersion = this.db.version + 1;
|
|
75
101
|
this.db.close();
|
|
76
102
|
this.db = null;
|
|
@@ -94,8 +120,7 @@ export class IndexedDBActions extends ActionsBase {
|
|
|
94
120
|
});
|
|
95
121
|
}
|
|
96
122
|
async _getStore(collection, mode) {
|
|
97
|
-
|
|
98
|
-
throw new Error("Database not initialized");
|
|
123
|
+
this._requireDb();
|
|
99
124
|
const tx = this.db.transaction(collection, mode);
|
|
100
125
|
return tx.objectStore(collection);
|
|
101
126
|
}
|
|
@@ -108,8 +133,7 @@ export class IndexedDBActions extends ActionsBase {
|
|
|
108
133
|
});
|
|
109
134
|
}
|
|
110
135
|
async ensureCollection(collection) {
|
|
111
|
-
|
|
112
|
-
throw new Error("Database not initialized");
|
|
136
|
+
this._requireDb();
|
|
113
137
|
if (this.db.objectStoreNames.contains(collection))
|
|
114
138
|
return false;
|
|
115
139
|
await this._changeSchema(db => {
|
|
@@ -122,13 +146,11 @@ export class IndexedDBActions extends ActionsBase {
|
|
|
122
146
|
return true;
|
|
123
147
|
}
|
|
124
148
|
async issetCollection(collection) {
|
|
125
|
-
|
|
126
|
-
throw new Error("Database not initialized");
|
|
149
|
+
this._requireDb();
|
|
127
150
|
return this.db.objectStoreNames.contains(collection);
|
|
128
151
|
}
|
|
129
152
|
async getCollections() {
|
|
130
|
-
|
|
131
|
-
throw new Error("Database not initialized");
|
|
153
|
+
this._requireDb();
|
|
132
154
|
const collections = [];
|
|
133
155
|
for (let i = 0; i < this.db.objectStoreNames.length; i++) {
|
|
134
156
|
const name = this.db.objectStoreNames.item(i);
|
|
@@ -139,8 +161,7 @@ export class IndexedDBActions extends ActionsBase {
|
|
|
139
161
|
return collections;
|
|
140
162
|
}
|
|
141
163
|
async removeCollection(collection) {
|
|
142
|
-
|
|
143
|
-
throw new Error("Database not initialized");
|
|
164
|
+
this._requireDb();
|
|
144
165
|
if (!this.db.objectStoreNames.contains(collection))
|
|
145
166
|
return false;
|
|
146
167
|
await this._changeSchema(db => {
|
|
@@ -151,8 +172,7 @@ export class IndexedDBActions extends ActionsBase {
|
|
|
151
172
|
return true;
|
|
152
173
|
}
|
|
153
174
|
async add(query) {
|
|
154
|
-
|
|
155
|
-
throw new Error("Database not initialized");
|
|
175
|
+
this._requireDb();
|
|
156
176
|
await this.ensureCollection(query.collection);
|
|
157
177
|
await addId(query, this);
|
|
158
178
|
const { collection, data } = query;
|
|
@@ -163,16 +183,32 @@ export class IndexedDBActions extends ActionsBase {
|
|
|
163
183
|
request.onsuccess = () => resolve(data);
|
|
164
184
|
});
|
|
165
185
|
}
|
|
186
|
+
async bulkAdd(query) {
|
|
187
|
+
this._requireDb();
|
|
188
|
+
await this.ensureCollection(query.collection);
|
|
189
|
+
await addIdBulk(query, this);
|
|
190
|
+
const store = await this._getStore(query.collection, "readwrite");
|
|
191
|
+
const tx = store.transaction;
|
|
192
|
+
return new Promise((resolve, reject) => {
|
|
193
|
+
const results = [];
|
|
194
|
+
tx.onerror = () => reject(new AdapterError(tx.error, this._dbContext()));
|
|
195
|
+
tx.onabort = () => reject(new AdapterError(tx.error ?? new Error("Transaction aborted"), this._dbContext()));
|
|
196
|
+
tx.oncomplete = () => resolve(results);
|
|
197
|
+
for (const data of query.datas) {
|
|
198
|
+
const request = store.put(data);
|
|
199
|
+
request.onerror = () => reject(new AdapterError(request.error, this._dbContext()));
|
|
200
|
+
request.onsuccess = () => results.push(data);
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
}
|
|
166
204
|
async find(query) {
|
|
167
|
-
|
|
168
|
-
throw new Error("Database not initialized");
|
|
205
|
+
this._requireDb();
|
|
169
206
|
await this.ensureCollection(query.collection);
|
|
170
207
|
const entries = await this._getAllEntries(query.collection);
|
|
171
208
|
return entries.map(entry => findObj(query, entry)).filter(Boolean);
|
|
172
209
|
}
|
|
173
210
|
async findOne(query) {
|
|
174
|
-
|
|
175
|
-
throw new Error("Database not initialized");
|
|
211
|
+
this._requireDb();
|
|
176
212
|
await this.ensureCollection(query.collection);
|
|
177
213
|
const entries = await this._getAllEntries(query.collection);
|
|
178
214
|
for (const entry of entries) {
|
|
@@ -183,8 +219,7 @@ export class IndexedDBActions extends ActionsBase {
|
|
|
183
219
|
return null;
|
|
184
220
|
}
|
|
185
221
|
async update(query) {
|
|
186
|
-
|
|
187
|
-
throw new Error("Database not initialized");
|
|
222
|
+
this._requireDb();
|
|
188
223
|
await this.ensureCollection(query.collection);
|
|
189
224
|
const store = await this._getStore(query.collection, "readwrite");
|
|
190
225
|
const entries = await new Promise((resolve, reject) => {
|
|
@@ -207,8 +242,7 @@ export class IndexedDBActions extends ActionsBase {
|
|
|
207
242
|
return updated;
|
|
208
243
|
}
|
|
209
244
|
async updateOne(query) {
|
|
210
|
-
|
|
211
|
-
throw new Error("Database not initialized");
|
|
245
|
+
this._requireDb();
|
|
212
246
|
await this.ensureCollection(query.collection);
|
|
213
247
|
const store = await this._getStore(query.collection, "readwrite");
|
|
214
248
|
const entries = await new Promise((resolve, reject) => {
|
|
@@ -230,8 +264,7 @@ export class IndexedDBActions extends ActionsBase {
|
|
|
230
264
|
return null;
|
|
231
265
|
}
|
|
232
266
|
async remove(query) {
|
|
233
|
-
|
|
234
|
-
throw new Error("Database not initialized");
|
|
267
|
+
this._requireDb();
|
|
235
268
|
await this.ensureCollection(query.collection);
|
|
236
269
|
const store = await this._getStore(query.collection, "readwrite");
|
|
237
270
|
const entries = await new Promise((resolve, reject) => {
|
|
@@ -253,8 +286,7 @@ export class IndexedDBActions extends ActionsBase {
|
|
|
253
286
|
return removed;
|
|
254
287
|
}
|
|
255
288
|
async removeOne(query) {
|
|
256
|
-
|
|
257
|
-
throw new Error("Database not initialized");
|
|
289
|
+
this._requireDb();
|
|
258
290
|
await this.ensureCollection(query.collection);
|
|
259
291
|
const store = await this._getStore(query.collection, "readwrite");
|
|
260
292
|
const entries = await new Promise((resolve, reject) => {
|
|
@@ -274,4 +306,26 @@ export class IndexedDBActions extends ActionsBase {
|
|
|
274
306
|
}
|
|
275
307
|
return null;
|
|
276
308
|
}
|
|
309
|
+
async count(query) {
|
|
310
|
+
this._requireDb();
|
|
311
|
+
await this.ensureCollection(query.collection);
|
|
312
|
+
const entries = await this._getAllEntries(query.collection);
|
|
313
|
+
if (entries.length === 0)
|
|
314
|
+
return 0;
|
|
315
|
+
const { search } = query;
|
|
316
|
+
if (!search ||
|
|
317
|
+
(typeof search === "object" && Object.keys(search).length === 0)) {
|
|
318
|
+
return entries.length;
|
|
319
|
+
}
|
|
320
|
+
let count = 0;
|
|
321
|
+
for (const entry of entries) {
|
|
322
|
+
if (matchObj({
|
|
323
|
+
search,
|
|
324
|
+
collection: query.collection,
|
|
325
|
+
}, entry)) {
|
|
326
|
+
count++;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return count;
|
|
330
|
+
}
|
|
277
331
|
}
|
package/dist/storage.d.ts
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import { CustomActionsBase } from "@wxn0brp/db-core/base/custom";
|
|
2
|
+
import { DataInternal } from "@wxn0brp/db-core/types/data";
|
|
3
|
+
import { VQueryT } from "@wxn0brp/db-core/types/query";
|
|
2
4
|
export declare class WebStorageActions extends CustomActionsBase {
|
|
3
5
|
name: string;
|
|
4
6
|
_storage: Storage;
|
|
5
7
|
version: string;
|
|
8
|
+
capabilities: {
|
|
9
|
+
transactions: boolean;
|
|
10
|
+
indexes: boolean;
|
|
11
|
+
persistent: boolean;
|
|
12
|
+
};
|
|
6
13
|
constructor(name: string, _storage?: Storage);
|
|
7
14
|
_getPath(collection: string): string;
|
|
8
15
|
_read(collection: string): any;
|
|
@@ -11,4 +18,6 @@ export declare class WebStorageActions extends CustomActionsBase {
|
|
|
11
18
|
issetCollection(collection: string): Promise<boolean>;
|
|
12
19
|
getCollections(): Promise<string[]>;
|
|
13
20
|
removeCollection(collection: string): Promise<boolean>;
|
|
21
|
+
bulkAdd(query: VQueryT.BulkAdd): Promise<DataInternal[]>;
|
|
22
|
+
count(config: VQueryT.Count): Promise<number>;
|
|
14
23
|
}
|
package/dist/storage.js
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import { CustomFileCpu } from "@wxn0brp/db-core";
|
|
2
2
|
import { CustomActionsBase } from "@wxn0brp/db-core/base/custom";
|
|
3
|
+
import { addIdBulk } from "@wxn0brp/db-core/helpers/addId";
|
|
4
|
+
import { hasFieldsAdvanced } from "@wxn0brp/db-core/utils/hasFieldsAdvanced";
|
|
3
5
|
import { version } from "./version.js";
|
|
4
6
|
export class WebStorageActions extends CustomActionsBase {
|
|
5
7
|
name;
|
|
6
8
|
_storage;
|
|
7
9
|
version = version;
|
|
10
|
+
capabilities = {
|
|
11
|
+
transactions: false,
|
|
12
|
+
indexes: false,
|
|
13
|
+
persistent: true,
|
|
14
|
+
};
|
|
8
15
|
constructor(name, _storage = localStorage) {
|
|
9
16
|
super();
|
|
10
17
|
this.name = name;
|
|
@@ -44,4 +51,27 @@ export class WebStorageActions extends CustomActionsBase {
|
|
|
44
51
|
this._storage.removeItem(this._getPath(collection));
|
|
45
52
|
return true;
|
|
46
53
|
}
|
|
54
|
+
async bulkAdd(query) {
|
|
55
|
+
await this.ensureCollection(query.collection);
|
|
56
|
+
await addIdBulk(query, this);
|
|
57
|
+
const data = this._read(query.collection);
|
|
58
|
+
data.push(...query.datas);
|
|
59
|
+
this._write(query.collection, data);
|
|
60
|
+
return query.datas;
|
|
61
|
+
}
|
|
62
|
+
async count(config) {
|
|
63
|
+
const data = this._read(config.collection);
|
|
64
|
+
if (data.length === 0)
|
|
65
|
+
return 0;
|
|
66
|
+
const { search } = config;
|
|
67
|
+
if (!search ||
|
|
68
|
+
(typeof search === "object" && Object.keys(search).length === 0)) {
|
|
69
|
+
return data.length;
|
|
70
|
+
}
|
|
71
|
+
let count = 0;
|
|
72
|
+
for (const item of data)
|
|
73
|
+
if (hasFieldsAdvanced(item, search))
|
|
74
|
+
count++;
|
|
75
|
+
return count;
|
|
76
|
+
}
|
|
47
77
|
}
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const version = "0.120.
|
|
1
|
+
export declare const version = "0.120.2";
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "0.120.
|
|
1
|
+
export const version = "0.120.2";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxn0brp/db-storage-web",
|
|
3
|
-
"version": "0.120.
|
|
3
|
+
"version": "0.120.2",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"author": "wxn0brP",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"vdb-adapter"
|
|
19
19
|
],
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@wxn0brp/db-core": "^0.12.
|
|
21
|
+
"@wxn0brp/db-core": "^0.12.10"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@wxn0brp/biome": "*",
|