@wxn0brp/db-storage-web 0.120.0 → 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 CHANGED
@@ -6,8 +6,19 @@ export declare class IndexedDBActions extends ActionsBase {
6
6
  db: IDBDatabase;
7
7
  _version: number;
8
8
  _inited: boolean;
9
+ version: string;
10
+ capabilities: {
11
+ transactions: boolean;
12
+ indexes: boolean;
13
+ persistent: boolean;
14
+ };
9
15
  constructor(name: string);
10
16
  _getDBName(): string;
17
+ _dbContext(): {
18
+ adapter: string;
19
+ db: string;
20
+ };
21
+ _requireDb(): IDBDatabase;
11
22
  init(): Promise<void>;
12
23
  _loadVersion(): Promise<void>;
13
24
  _saveVersion(): Promise<void>;
@@ -20,10 +31,12 @@ export declare class IndexedDBActions extends ActionsBase {
20
31
  getCollections(): Promise<string[]>;
21
32
  removeCollection(collection: string): Promise<boolean>;
22
33
  add(query: VQueryT.Add): Promise<DataInternal>;
34
+ bulkAdd(query: VQueryT.BulkAdd): Promise<DataInternal[]>;
23
35
  find(query: VQueryT.Find): Promise<DataInternal[]>;
24
36
  findOne(query: VQueryT.FindOne): Promise<DataInternal | null>;
25
37
  update(query: VQueryT.Update): Promise<DataInternal[]>;
26
38
  updateOne(query: VQueryT.Update): Promise<DataInternal | null>;
27
39
  remove(query: VQueryT.Remove): Promise<DataInternal[]>;
28
40
  removeOne(query: VQueryT.Remove): Promise<DataInternal | null>;
41
+ count(query: VQueryT.Count): Promise<number>;
29
42
  }
package/dist/indexed.js CHANGED
@@ -1,11 +1,19 @@
1
1
  import { ActionsBase } from "@wxn0brp/db-core/base/actions";
2
- import { addId } from "@wxn0brp/db-core/helpers/addId";
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";
5
+ import { version } from "./version.js";
4
6
  export class IndexedDBActions extends ActionsBase {
5
7
  name;
6
8
  db = null;
7
9
  _version = 1;
8
10
  _inited = false;
11
+ version = version;
12
+ capabilities = {
13
+ transactions: false,
14
+ indexes: false,
15
+ persistent: true,
16
+ };
9
17
  constructor(name) {
10
18
  super();
11
19
  this.name = name;
@@ -13,27 +21,43 @@ export class IndexedDBActions extends ActionsBase {
13
21
  _getDBName() {
14
22
  return "vdb_" + this.name;
15
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
+ }
16
35
  async init() {
17
36
  if (this.db)
18
37
  return;
19
- return new Promise((resolve, reject) => {
20
- const request = indexedDB.open(this._getDBName());
21
- request.onerror = () => reject(request.error);
22
- request.onsuccess = () => {
23
- this.db = request.result;
24
- this._version = this.db.version;
25
- this._loadVersion()
26
- .then(() => resolve())
27
- .catch(reject);
28
- };
29
- request.onupgradeneeded = event => {
30
- const db = request.result;
31
- this._version = db.version;
32
- if (!db.objectStoreNames.contains("_meta")) {
33
- db.createObjectStore("_meta");
34
- }
35
- };
36
- });
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
+ }
37
61
  }
38
62
  async _loadVersion() {
39
63
  if (!this.db)
@@ -61,14 +85,18 @@ export class IndexedDBActions extends ActionsBase {
61
85
  });
62
86
  }
63
87
  async close() {
64
- if (this.db) {
88
+ if (!this.db)
89
+ return;
90
+ try {
65
91
  this.db.close();
66
92
  this.db = null;
67
93
  }
94
+ catch (err) {
95
+ throw new AdapterCloseError(err, this._dbContext());
96
+ }
68
97
  }
69
98
  async _changeSchema(upgradeFn) {
70
- if (!this.db)
71
- throw new Error("Database not initialized");
99
+ this._requireDb();
72
100
  const newVersion = this.db.version + 1;
73
101
  this.db.close();
74
102
  this.db = null;
@@ -92,8 +120,7 @@ export class IndexedDBActions extends ActionsBase {
92
120
  });
93
121
  }
94
122
  async _getStore(collection, mode) {
95
- if (!this.db)
96
- throw new Error("Database not initialized");
123
+ this._requireDb();
97
124
  const tx = this.db.transaction(collection, mode);
98
125
  return tx.objectStore(collection);
99
126
  }
@@ -106,8 +133,7 @@ export class IndexedDBActions extends ActionsBase {
106
133
  });
107
134
  }
108
135
  async ensureCollection(collection) {
109
- if (!this.db)
110
- throw new Error("Database not initialized");
136
+ this._requireDb();
111
137
  if (this.db.objectStoreNames.contains(collection))
112
138
  return false;
113
139
  await this._changeSchema(db => {
@@ -120,13 +146,11 @@ export class IndexedDBActions extends ActionsBase {
120
146
  return true;
121
147
  }
122
148
  async issetCollection(collection) {
123
- if (!this.db)
124
- throw new Error("Database not initialized");
149
+ this._requireDb();
125
150
  return this.db.objectStoreNames.contains(collection);
126
151
  }
127
152
  async getCollections() {
128
- if (!this.db)
129
- throw new Error("Database not initialized");
153
+ this._requireDb();
130
154
  const collections = [];
131
155
  for (let i = 0; i < this.db.objectStoreNames.length; i++) {
132
156
  const name = this.db.objectStoreNames.item(i);
@@ -137,8 +161,7 @@ export class IndexedDBActions extends ActionsBase {
137
161
  return collections;
138
162
  }
139
163
  async removeCollection(collection) {
140
- if (!this.db)
141
- throw new Error("Database not initialized");
164
+ this._requireDb();
142
165
  if (!this.db.objectStoreNames.contains(collection))
143
166
  return false;
144
167
  await this._changeSchema(db => {
@@ -149,8 +172,7 @@ export class IndexedDBActions extends ActionsBase {
149
172
  return true;
150
173
  }
151
174
  async add(query) {
152
- if (!this.db)
153
- throw new Error("Database not initialized");
175
+ this._requireDb();
154
176
  await this.ensureCollection(query.collection);
155
177
  await addId(query, this);
156
178
  const { collection, data } = query;
@@ -161,16 +183,32 @@ export class IndexedDBActions extends ActionsBase {
161
183
  request.onsuccess = () => resolve(data);
162
184
  });
163
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
+ }
164
204
  async find(query) {
165
- if (!this.db)
166
- throw new Error("Database not initialized");
205
+ this._requireDb();
167
206
  await this.ensureCollection(query.collection);
168
207
  const entries = await this._getAllEntries(query.collection);
169
208
  return entries.map(entry => findObj(query, entry)).filter(Boolean);
170
209
  }
171
210
  async findOne(query) {
172
- if (!this.db)
173
- throw new Error("Database not initialized");
211
+ this._requireDb();
174
212
  await this.ensureCollection(query.collection);
175
213
  const entries = await this._getAllEntries(query.collection);
176
214
  for (const entry of entries) {
@@ -181,8 +219,7 @@ export class IndexedDBActions extends ActionsBase {
181
219
  return null;
182
220
  }
183
221
  async update(query) {
184
- if (!this.db)
185
- throw new Error("Database not initialized");
222
+ this._requireDb();
186
223
  await this.ensureCollection(query.collection);
187
224
  const store = await this._getStore(query.collection, "readwrite");
188
225
  const entries = await new Promise((resolve, reject) => {
@@ -205,8 +242,7 @@ export class IndexedDBActions extends ActionsBase {
205
242
  return updated;
206
243
  }
207
244
  async updateOne(query) {
208
- if (!this.db)
209
- throw new Error("Database not initialized");
245
+ this._requireDb();
210
246
  await this.ensureCollection(query.collection);
211
247
  const store = await this._getStore(query.collection, "readwrite");
212
248
  const entries = await new Promise((resolve, reject) => {
@@ -228,8 +264,7 @@ export class IndexedDBActions extends ActionsBase {
228
264
  return null;
229
265
  }
230
266
  async remove(query) {
231
- if (!this.db)
232
- throw new Error("Database not initialized");
267
+ this._requireDb();
233
268
  await this.ensureCollection(query.collection);
234
269
  const store = await this._getStore(query.collection, "readwrite");
235
270
  const entries = await new Promise((resolve, reject) => {
@@ -251,8 +286,7 @@ export class IndexedDBActions extends ActionsBase {
251
286
  return removed;
252
287
  }
253
288
  async removeOne(query) {
254
- if (!this.db)
255
- throw new Error("Database not initialized");
289
+ this._requireDb();
256
290
  await this.ensureCollection(query.collection);
257
291
  const store = await this._getStore(query.collection, "readwrite");
258
292
  const entries = await new Promise((resolve, reject) => {
@@ -272,4 +306,26 @@ export class IndexedDBActions extends ActionsBase {
272
306
  }
273
307
  return null;
274
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
+ }
275
331
  }
package/dist/storage.d.ts CHANGED
@@ -1,7 +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;
7
+ version: string;
8
+ capabilities: {
9
+ transactions: boolean;
10
+ indexes: boolean;
11
+ persistent: boolean;
12
+ };
5
13
  constructor(name: string, _storage?: Storage);
6
14
  _getPath(collection: string): string;
7
15
  _read(collection: string): any;
@@ -10,4 +18,6 @@ export declare class WebStorageActions extends CustomActionsBase {
10
18
  issetCollection(collection: string): Promise<boolean>;
11
19
  getCollections(): Promise<string[]>;
12
20
  removeCollection(collection: string): Promise<boolean>;
21
+ bulkAdd(query: VQueryT.BulkAdd): Promise<DataInternal[]>;
22
+ count(config: VQueryT.Count): Promise<number>;
13
23
  }
package/dist/storage.js CHANGED
@@ -1,8 +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";
5
+ import { version } from "./version.js";
3
6
  export class WebStorageActions extends CustomActionsBase {
4
7
  name;
5
8
  _storage;
9
+ version = version;
10
+ capabilities = {
11
+ transactions: false,
12
+ indexes: false,
13
+ persistent: true,
14
+ };
6
15
  constructor(name, _storage = localStorage) {
7
16
  super();
8
17
  this.name = name;
@@ -42,4 +51,27 @@ export class WebStorageActions extends CustomActionsBase {
42
51
  this._storage.removeItem(this._getPath(collection));
43
52
  return true;
44
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
+ }
45
77
  }
@@ -0,0 +1 @@
1
+ export declare const version = "0.120.2";
@@ -0,0 +1 @@
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.0",
3
+ "version": "0.120.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",
@@ -17,12 +17,11 @@
17
17
  "valtheradb",
18
18
  "vdb-adapter"
19
19
  ],
20
- "peerDependencies": {
21
- "@wxn0brp/db-core": "^0.12.0"
20
+ "dependencies": {
21
+ "@wxn0brp/db-core": "^0.12.10"
22
22
  },
23
23
  "devDependencies": {
24
24
  "@wxn0brp/biome": "*",
25
- "@wxn0brp/db-core": "^0.12.0",
26
25
  "tsc-alias": "^1",
27
26
  "typescript": "^7"
28
27
  },