rads-db 0.1.102 → 0.1.104

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.
@@ -4,500 +4,216 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  module.exports = void 0;
7
- var _cosmos = require("@azure/cosmos");
7
+ var _dexie = _interopRequireDefault(require("dexie"));
8
8
  var _lodash = _interopRequireDefault(require("lodash"));
9
+ var _memory = require("./memory.cjs");
9
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
- const cosmosClientCache = {};
11
- const databaseClientCache = {};
12
- const containerClientCache = {};
13
- const simpleSubclauseRegex = /^[a-z0-9_@]+.[a-z0-9_@]+ = [a-z0-9_@.]+$/i;
14
- var _default = options => (schema, entity) => {
15
- const client = getCosmosContainerClient(options);
16
- async function getItemByIds(ids, ctx) {
17
- const {
18
- query,
19
- parameters
20
- } = getCosmosQuery(schema, entity, {
21
- where: {
22
- id_in: ids
23
- }
24
- });
25
- const response = client.items.query({
26
- query,
27
- parameters: Object.keys(parameters).map(k => ({
28
- name: `@${k}`,
29
- value: parameters[k]
30
- }))
31
- });
32
- const r = await response.fetchAll();
33
- ctx?.log?.({
34
- charge: r.requestCharge,
35
- request: query
36
- });
37
- return r.resources;
38
- }
39
- async function getMany(args, ctx) {
40
- args = args || {};
41
- const where = args.where || {};
42
- const whereKeys = _lodash.default.keys(where);
43
- if (whereKeys.length === 1) {
44
- if (whereKeys[0] === "id" && where.id != null) {
45
- const items = await getItemByIds([where.id], ctx);
46
- return {
47
- nodes: [items[0]].filter(x => x),
48
- cursor: null
49
- };
50
- }
51
- if (whereKeys[0] === "id_in" && where.id_in != null) {
52
- const items = await getItemByIds(where.id_in, ctx);
53
- return {
54
- nodes: items.filter(x => x),
55
- cursor: null
56
- };
57
- }
58
- }
59
- const {
60
- query,
61
- parameters
62
- } = getCosmosQuery(schema, entity, args);
63
- const response = client.items.query({
64
- query,
65
- parameters: Object.keys(parameters).map(k => ({
66
- name: `@${k}`,
67
- value: parameters[k]
68
- }))
69
- }, {
70
- continuationToken: args.cursor || void 0,
71
- continuationTokenLimitInKB: 4,
72
- maxItemCount: args.maxItemCount
73
- });
74
- const page = await response.fetchNext();
75
- ctx?.log?.({
76
- charge: page.requestCharge,
77
- request: query
78
- });
11
+ const dbs = {};
12
+ const tableOperatorFns = {
13
+ eq: (where, value) => where.equals(value),
14
+ ieq: (where, value) => where.equalsIgnoreCase(value),
15
+ in: (where, value) => where.anyOf(value),
16
+ startsWith: (where, value) => where.startsWith(value),
17
+ istartsWith: (where, value) => where.startsWithIgnoreCase(value),
18
+ gt: (where, value) => where.above(value),
19
+ lt: (where, value) => where.below(value)
20
+ };
21
+ const tableOperatorNotFns = {
22
+ eq: (where, value) => where.notEqual(value),
23
+ in: (where, value) => where.noneOf(value),
24
+ gt: (where, value) => where.belowOrEqual(value),
25
+ lt: (where, value) => where.aboveOrEqual(value)
26
+ };
27
+ var _default = options => {
28
+ const dbName = options?.dbName || "db";
29
+ const dbWrapper = getDb(options);
30
+ return (schema, entity) => {
31
+ const handle = schema[entity]?.handle;
32
+ if (!handle) throw new Error(`Entity ${entity} not found`);
33
+ dbWrapper.entities[handle] = true;
79
34
  return {
80
- nodes: page.resources,
81
- cursor: page.continuationToken || null
82
- };
83
- }
84
- const instance = {
85
- driverName: "azureCosmos",
86
- getMany,
87
- async clear(ctx) {
88
- let currentCursor = null;
89
- do {
90
- const {
91
- nodes,
92
- cursor
93
- } = await getMany({
94
- cursor: currentCursor
95
- });
96
- for (const r of nodes) {
97
- const resp = await client.item(r.id, r._partition).delete();
98
- ctx?.log?.({
99
- charge: resp.requestCharge,
100
- request: `delete#${r._partition}|${r.id}`
101
- });
35
+ driverName: "indexedDb",
36
+ async getMany(args, ctx) {
37
+ await initDbIfNeeded(dbName);
38
+ const normalizedArgs = normalizeArgs(args);
39
+ const dexieResult = applyArgsToDexieTable(dbWrapper.db.table(handle), normalizedArgs);
40
+ let resultCursor;
41
+ const newCursor = normalizedArgs.cursor + dexieResult.maxItemCount;
42
+ let result = await dexieResult.collection.toArray();
43
+ if (dexieResult.orderByProp) {
44
+ result = _lodash.default.orderBy(result, [dexieResult.orderByProp], [dexieResult.orderByDirection.toLowerCase()]);
45
+ if (dexieResult.maxItemCount) {
46
+ if (result.length > newCursor) {
47
+ resultCursor = newCursor;
48
+ }
49
+ result = result.slice(normalizedArgs.cursor, newCursor);
50
+ }
51
+ return {
52
+ nodes: result,
53
+ cursor: resultCursor ? _lodash.default.toString(resultCursor) : null
54
+ };
102
55
  }
103
- currentCursor = cursor;
104
- } while (currentCursor);
105
- },
106
- async putMany(items, ctx) {
107
- for (const item of items) {
108
- const id = item?.id;
109
- if (!id) throw new Error(`You must provide an id`);
110
- const itemToPut = {
111
- _partition: entity,
112
- id,
113
- ...item
56
+ if (dexieResult.maxItemCount && result.length >= dexieResult.maxItemCount) {
57
+ resultCursor = newCursor;
58
+ }
59
+ return {
60
+ nodes: result,
61
+ cursor: resultCursor ? _lodash.default.toString(resultCursor) : null
114
62
  };
115
- const response = await client.items.upsert(itemToPut);
116
- ctx?.log?.({
117
- charge: response.requestCharge,
118
- request: `put#${itemToPut._partition}|${itemToPut.id}`
119
- });
63
+ },
64
+ async putMany(items, ctx) {
65
+ await initDbIfNeeded(dbName);
66
+ const table = dbWrapper.db.table(handle);
67
+ await table.bulkPut(items);
120
68
  }
121
- }
69
+ };
122
70
  };
123
- return instance;
124
71
  };
125
72
  module.exports = _default;
126
- function getCosmosQuery(schema, entity, args) {
127
- args = args || {};
128
- const where = args.where || {};
129
- const parameters = {};
130
- const whereClauses = [`r._partition = '${entity}'`, getCosmosQueryWhere({
131
- schema,
132
- entity
133
- }, parameters, where)].filter(x => x);
134
- let orderByClause = "";
135
- if (args.orderBy) {
136
- const orderByParts = args.orderBy.split("_");
137
- const orderPropFromOrderBy = orderByParts.slice(0, -1).join(".");
138
- const orderDirection = orderByParts.at(-1);
139
- const orderProp = orderPropFromOrderBy === "value" ? `r["value"]` : `r.${orderPropFromOrderBy}`;
140
- orderByClause = `order by ${orderProp} ${orderDirection}`;
73
+ function applyArgsToDexieTable(table, args) {
74
+ const {
75
+ where,
76
+ orderByProperty,
77
+ orderByDirection,
78
+ maxItemCount
79
+ } = args;
80
+ let orderByProp = orderByProperty;
81
+ const indexedProps = ["id", ...table.schema.indexes.map(idx => idx.name)];
82
+ let collection;
83
+ for (const ip of indexedProps) {
84
+ collection = applyTableFiltering(table, where, ip);
85
+ if (collection) {
86
+ break;
87
+ }
88
+ }
89
+ if (!collection && orderByProp) {
90
+ if (indexedProps.includes(orderByProp)) {
91
+ collection = table.orderBy(orderByProp);
92
+ if (orderByDirection === "desc") collection = collection.reverse();
93
+ orderByProp = "";
94
+ }
95
+ }
96
+ if (!collection) {
97
+ collection = table.toCollection();
98
+ }
99
+ const f = (0, _memory.getFilter)(where);
100
+ if (f) {
101
+ collection = collection.filter(f);
102
+ }
103
+ if (maxItemCount && !orderByProp) {
104
+ if (args.cursor) collection = collection.offset(_lodash.default.toNumber(args.cursor));
105
+ collection = collection.limit(maxItemCount);
141
106
  }
142
- const query = `select * from r where ${whereClauses.join(" AND ")} ${orderByClause}`;
143
107
  return {
144
- query,
145
- parameters
108
+ collection,
109
+ orderByProp,
110
+ orderByDirection,
111
+ maxItemCount
146
112
  };
147
113
  }
148
- const operatorHandlers = {
149
- some: (ctx, parameters, whereArgs) => {
150
- const {
151
- name,
152
- namePrefix,
153
- paramNamePrefix,
154
- paramName,
155
- whereVal
156
- } = whereArgs;
157
- const newCtx = {
158
- ...ctx,
159
- entity: ctx.schema[ctx.entity].fields[ctx.field].type
160
- };
161
- const subClause = getCosmosQueryWhere(newCtx, parameters, whereVal, `${name}.`, `${paramNamePrefix}${paramName}_`);
162
- if (subClause) {
163
- if (simpleSubclauseRegex.test(subClause)) {
164
- const parts = subClause.split(" ");
165
- const field = parts[0];
166
- const variable = parts[2];
167
- return `array_contains(${namePrefix}${name}, { ${field.split(".")[1]}: ${variable} }, true)`;
168
- }
169
- return `exists (select ${name} from ${name} in ${namePrefix}${name} where ${subClause})`;
170
- }
171
- return `array_length(${namePrefix}${name}) > 0`;
172
- },
173
- none: (ctx, parameters, whereArgs) => {
174
- const {
175
- name,
176
- namePrefix,
177
- paramNamePrefix,
178
- paramName,
179
- whereVal
180
- } = whereArgs;
181
- const newCtx = {
182
- ...ctx,
183
- entity: ctx.schema[ctx.entity].fields[ctx.field].type
184
- };
185
- const subClause = getCosmosQueryWhere(newCtx, parameters, whereVal, `${name}.`, `${paramNamePrefix}${paramName}_`);
186
- if (subClause) return `not exists (select ${name} from ${name} in ${namePrefix}${name} where ${subClause})`;
187
- return `array_length(${namePrefix}${name}) = 0`;
188
- },
189
- and: (ctx, parameters, whereArgs) => {
190
- const {
191
- namePrefix,
192
- paramNamePrefix,
193
- whereVal
194
- } = whereArgs;
195
- if (!_lodash.default.isArray(whereVal)) throw new Error(`Value for where._and must be an array`);
196
- const clauses = [];
197
- for (let i = 0; i < whereVal.length; i++) {
198
- const andQuery = getCosmosQueryWhere(ctx, parameters, whereVal[i], namePrefix, `${paramNamePrefix}and${i}_`);
199
- clauses.push(andQuery);
200
- }
201
- if (!clauses.length) return null;
202
- return `((${clauses.join(") and (")}))`;
203
- },
204
- not: (ctx, parameters, whereArgs) => {
205
- const {
206
- name,
207
- namePrefix,
208
- paramNamePrefix,
209
- whereVal
210
- } = whereArgs;
211
- const subClause = getCosmosQueryWhere(ctx, parameters, whereVal, `${namePrefix}${name}`, `${paramNamePrefix}not_`);
212
- if (!subClause) return null;
213
- return `not(${subClause})`;
214
- },
215
- or: (ctx, parameters, whereArgs) => {
216
- const {
217
- namePrefix,
218
- paramNamePrefix,
219
- whereVal
220
- } = whereArgs;
221
- if (!_lodash.default.isArray(whereVal)) throw new Error(`Value for where._or must be an array`);
222
- const clauses = [];
223
- for (let i = 0; i < whereVal.length; i++) {
224
- const orQuery = getCosmosQueryWhere(ctx, parameters, whereVal[i], namePrefix, `${paramNamePrefix}or${i}_`);
225
- clauses.push(orQuery);
226
- }
227
- if (!clauses.length) return null;
228
- return `((${clauses.join(") or (")}))`;
229
- },
230
- isNull: (ctx, parameters, whereArgs) => {
231
- const {
232
- name,
233
- namePrefix,
234
- whereVal
235
- } = whereArgs;
236
- const n = `${namePrefix}${name}`;
237
- if (whereVal) return `(not (is_defined(${n})) or ${n} = null)`;
238
- return `(is_defined(${n}) and ${n} != null)`;
239
- },
240
- eq: (ctx, parameters, whereArgs) => {
241
- const {
242
- name,
243
- namePrefix,
244
- paramName,
245
- paramNamePrefix,
246
- whereVal
247
- } = whereArgs;
248
- const pn = `${paramNamePrefix}${paramName}`;
249
- parameters[pn] = whereVal;
250
- return `${namePrefix}${name} = @${pn}`;
251
- },
252
- in: (ctx, parameters, whereArgs) => {
253
- const {
254
- name,
255
- namePrefix,
256
- paramName,
257
- paramNamePrefix,
258
- whereVal
259
- } = whereArgs;
260
- const pn = `${paramNamePrefix}${paramName}`;
261
- parameters[pn] = whereVal;
262
- return `array_contains(@${pn}, ${namePrefix}${name})`;
263
- },
264
- startsWith: (ctx, parameters, whereArgs) => {
265
- const {
266
- name,
267
- namePrefix,
268
- paramName,
269
- paramNamePrefix,
270
- whereVal
271
- } = whereArgs;
272
- const pn = `${paramNamePrefix}${paramName}`;
273
- parameters[pn] = whereVal;
274
- return `startswith(${namePrefix}${name}, @${pn})`;
275
- },
276
- istartsWith: (ctx, parameters, whereArgs) => {
277
- const {
278
- name,
279
- namePrefix,
280
- paramName,
281
- paramNamePrefix,
282
- whereVal
283
- } = whereArgs;
284
- const pn = `${paramNamePrefix}${paramName}`;
285
- parameters[pn] = whereVal;
286
- return `startswith(${namePrefix}${name}, @${pn}, true)`;
287
- },
288
- endsWith: (ctx, parameters, whereArgs) => {
289
- const {
290
- name,
291
- namePrefix,
292
- paramName,
293
- paramNamePrefix,
294
- whereVal
295
- } = whereArgs;
296
- const pn = `${paramNamePrefix}${paramName}`;
297
- parameters[pn] = whereVal;
298
- return `endswith(${namePrefix}${name}, @${pn})`;
299
- },
300
- iendsWith: (ctx, parameters, whereArgs) => {
301
- const {
302
- name,
303
- namePrefix,
304
- paramName,
305
- paramNamePrefix,
306
- whereVal
307
- } = whereArgs;
308
- const pn = `${paramNamePrefix}${paramName}`;
309
- parameters[pn] = whereVal;
310
- return `endswith(${namePrefix}${name}, @${pn}, true)`;
311
- },
312
- contains: (ctx, parameters, whereArgs) => {
313
- const {
314
- name,
315
- namePrefix,
316
- paramName,
317
- paramNamePrefix,
318
- whereVal
319
- } = whereArgs;
320
- const fieldType = ctx.schema[ctx.entity].fields[ctx.field];
321
- const pn = `${paramNamePrefix}${paramName}`;
322
- parameters[pn] = whereVal;
323
- if (fieldType.isArray) {
324
- return `array_contains(${namePrefix}${name}, @${pn})`;
325
- }
326
- return `contains(${namePrefix}${name}, @${pn})`;
327
- },
328
- icontains: (ctx, parameters, whereArgs) => {
329
- const {
330
- name,
331
- namePrefix,
332
- paramName,
333
- paramNamePrefix,
334
- whereVal
335
- } = whereArgs;
336
- const pn = `${paramNamePrefix}${paramName}`;
337
- parameters[pn] = whereVal;
338
- return `contains(${namePrefix}${name}, @${pn}, true)`;
339
- },
340
- ieq: (ctx, parameters, whereArgs) => {
341
- const {
342
- name,
343
- namePrefix,
344
- paramName,
345
- paramNamePrefix,
346
- whereVal
347
- } = whereArgs;
348
- const pn = `${paramNamePrefix}${paramName}`;
349
- parameters[pn] = whereVal;
350
- return `stringequals(${namePrefix}${name}, @${pn}, true)`;
351
- },
352
- gt: (ctx, parameters, whereArgs) => {
353
- const {
354
- name,
355
- namePrefix,
356
- paramName,
357
- paramNamePrefix,
358
- whereVal
359
- } = whereArgs;
360
- const pn = `${paramNamePrefix}${paramName}`;
361
- parameters[pn] = whereVal;
362
- return `${namePrefix}${name} > @${pn}`;
363
- },
364
- gte: (ctx, parameters, whereArgs) => {
365
- const {
366
- name,
367
- namePrefix,
368
- paramName,
369
- paramNamePrefix,
370
- whereVal
371
- } = whereArgs;
372
- const pn = `${paramNamePrefix}${paramName}`;
373
- parameters[pn] = whereVal;
374
- return `${namePrefix}${name} >= @${pn}`;
375
- },
376
- lt: (ctx, parameters, whereArgs) => {
377
- const {
378
- name,
379
- namePrefix,
380
- paramName,
381
- paramNamePrefix,
382
- whereVal
383
- } = whereArgs;
384
- const pn = `${paramNamePrefix}${paramName}`;
385
- parameters[pn] = whereVal;
386
- return `${namePrefix}${name} < @${pn}`;
387
- },
388
- lte: (ctx, parameters, whereArgs) => {
389
- const {
390
- name,
391
- namePrefix,
392
- paramName,
393
- paramNamePrefix,
394
- whereVal
395
- } = whereArgs;
396
- const pn = `${paramNamePrefix}${paramName}`;
397
- parameters[pn] = whereVal;
398
- return `${namePrefix}${name} <= @${pn}`;
114
+ function applyTableFiltering(table, where, fieldName) {
115
+ if (["isArchived", "_type"].includes(fieldName)) {
116
+ return void 0;
399
117
  }
400
- };
401
- function getCosmosQueryWhere(ctx, parameters, where, namePrefix = "r.", paramNamePrefix = "") {
402
- const whereClauses = [];
403
- for (const key in where) {
404
- const [nameFromWhere, operator] = getWhereNameOperatorPair(key);
405
- let name = nameFromWhere;
406
- if (name === "value") {
407
- name = `["${name}"]`;
408
- namePrefix = namePrefix.slice(0, -1);
118
+ for (const operatorKey in tableOperatorFns) {
119
+ const operator = operatorKey === "eq" ? null : operatorKey;
120
+ const whereKey = [fieldName, operator].filter(x => x).join("_");
121
+ const whereVal = _lodash.default.get(where, whereKey);
122
+ if (whereVal != null) {
123
+ const whereClause = table.where(fieldName);
124
+ _lodash.default.set(where, whereKey, void 0);
125
+ return tableOperatorFns[operatorKey](whereClause, whereVal);
409
126
  }
410
- const paramName = key;
411
- const whereVal = where[key];
412
- if (whereVal == null) continue;
413
- const f = getCosmosQueryWhereInner({
414
- ...ctx,
415
- field: nameFromWhere
416
- }, parameters, {
417
- operator,
418
- whereVal,
419
- name,
420
- namePrefix,
421
- paramName,
422
- paramNamePrefix
423
- });
424
- if (f) whereClauses.push(f);
425
127
  }
426
- return whereClauses.join(" AND ");
427
- }
428
- function getCosmosQueryWhereInner(ctx, parameters, whereArgs) {
429
- const {
430
- operator,
431
- whereVal,
432
- name,
433
- namePrefix,
434
- paramName,
435
- paramNamePrefix
436
- } = whereArgs;
437
- if (!operator && _lodash.default.isObject(whereVal)) {
438
- return getCosmosQueryWhere(ctx, parameters, whereVal, `${namePrefix}${name}.`, `${paramNamePrefix}${paramName}_`);
128
+ for (const operatorKey in tableOperatorNotFns) {
129
+ const operator = operatorKey === "eq" ? null : operatorKey;
130
+ const whereKey = [fieldName, operator].filter(x => x).join("_");
131
+ const whereKeyNot = `_not.${whereKey}`;
132
+ const whereValNot = _lodash.default.get(where, whereKeyNot);
133
+ if (whereValNot != null) {
134
+ const whereClause = table.where(fieldName);
135
+ _lodash.default.set(where, whereKeyNot, void 0);
136
+ return tableOperatorNotFns[operatorKey](whereClause, whereValNot);
137
+ }
439
138
  }
440
- const fn = operatorHandlers[operator || "eq"];
441
- if (!fn) console.warn(`Unsupported operator: "${operator}"`);
442
- return fn(ctx, parameters, whereArgs);
443
139
  }
444
- function getCosmosContainerClient(options) {
445
- options = {
446
- databaseName: "main",
447
- containerName: "main",
448
- ...options
140
+ function normalizeArgs(args) {
141
+ if (!args) args = {};
142
+ const where = _lodash.default.cloneDeep(args.where) || {};
143
+ let orderBy = args.orderBy || "";
144
+ if (_lodash.default.isArray(orderBy)) orderBy = orderBy[0] || "";
145
+ const orderByParts = orderBy.split("_");
146
+ const orderByProperty = orderByParts.slice(0, -1).join(".");
147
+ const orderByDirection = orderByParts.slice(-1)[0];
148
+ const maxItemCount = args.maxItemCount || 100;
149
+ return {
150
+ where,
151
+ orderByProperty,
152
+ orderByDirection,
153
+ maxItemCount,
154
+ cursor: args.cursor ? _lodash.default.toNumber(args.cursor) : 0
449
155
  };
450
- const {
451
- databaseName,
452
- containerName,
453
- endpoint
454
- } = options;
455
- const key = `${endpoint}|${databaseName}|${containerName}`;
456
- if (!containerClientCache[key]) {
457
- containerClientCache[key] = getCosmosDatabaseClient(options).container(containerName);
458
- }
459
- return containerClientCache[key];
460
156
  }
461
- function getCosmosDatabaseClient(options) {
462
- options = {
463
- databaseName: "main",
464
- containerName: "main",
465
- ...options
466
- };
467
- const {
468
- databaseName,
469
- endpoint
470
- } = options;
471
- const key = `${endpoint}|${databaseName}`;
472
- if (!databaseClientCache[key]) {
473
- databaseClientCache[key] = getCosmosAccountClient(options).database(databaseName);
157
+ async function initDbIfNeeded(dbName) {
158
+ const dbWrapper = dbs[dbName];
159
+ if (!dbWrapper.initPromise) {
160
+ dbWrapper.initPromise = initDbInner(dbName);
474
161
  }
475
- return databaseClientCache[key];
162
+ return dbWrapper.initPromise;
476
163
  }
477
- function getCosmosAccountClient(options) {
478
- options = {
479
- databaseName: "main",
480
- containerName: "main",
481
- ...options
164
+ async function initDbInner(dbName) {
165
+ const dbWrapper = dbs[dbName];
166
+ const stores = {
167
+ _system: "id"
482
168
  };
483
- const {
484
- endpoint,
485
- accountKey
486
- } = options;
487
- const key = endpoint;
488
- if (!cosmosClientCache[key]) {
489
- cosmosClientCache[key] = new _cosmos.CosmosClient({
490
- endpoint,
491
- key: accountKey,
492
- ...options.options
169
+ for (const entity in dbWrapper.entities) {
170
+ stores[entity] = "id";
171
+ }
172
+ let db = new _dexie.default(dbWrapper.driverOptions.dbName, dbWrapper.driverOptions.dexieOptions);
173
+ db.version(1).stores(stores);
174
+ let systemTable = db._system;
175
+ const invalidateCounter = dbWrapper.driverOptions.invalidateCounter;
176
+ const currentVersionObject = await systemTable.get("_versionObject");
177
+ if (!currentVersionObject) {
178
+ await systemTable.put({
179
+ id: "_versionObject",
180
+ stores,
181
+ version: 1,
182
+ invalidateCounter
183
+ });
184
+ } else if (!_lodash.default.isEqual(currentVersionObject.invalidateCounter, invalidateCounter)) {
185
+ await db.delete();
186
+ db = new _dexie.default(dbWrapper.driverOptions.dbName, dbWrapper.driverOptions.dexieOptions);
187
+ db.version(1).stores(stores);
188
+ systemTable = db._system;
189
+ await systemTable.put({
190
+ id: "_versionObject",
191
+ stores,
192
+ version: 1,
193
+ invalidateCounter
194
+ });
195
+ } else if (!_lodash.default.isEqual(currentVersionObject.stores, stores)) {
196
+ const newVersion = currentVersionObject.version + 1;
197
+ db.version(newVersion).stores(stores);
198
+ await systemTable.put({
199
+ id: "_versionObject",
200
+ stores,
201
+ version: newVersion
493
202
  });
494
203
  }
495
- return cosmosClientCache[key];
204
+ dbWrapper.db = db;
496
205
  }
497
- function getWhereNameOperatorPair(whereKey) {
498
- if (whereKey.startsWith("_type")) {
499
- const operator = whereKey.split("_")[2];
500
- return ["_type", operator];
501
- }
502
- return whereKey.split("_");
206
+ function getDb(options) {
207
+ const dbName = options?.dbName || "db";
208
+ if (dbs[dbName]?.db) dbs[dbName].db?.close();
209
+ dbs[dbName] = {
210
+ entities: {},
211
+ driverOptions: {
212
+ dbName,
213
+ dexieOptions: {},
214
+ invalidateCounter: 0,
215
+ ...options
216
+ }
217
+ };
218
+ return dbs[dbName];
503
219
  }