rads-db 3.1.9 → 3.1.12
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 +19 -0
- package/dist/config.d.ts +1 -1
- package/dist/index.cjs +56 -22
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +56 -22
- package/dist/{types-9c3659f0.d.ts → types-9cb9d6e7.d.ts} +9 -0
- package/drivers/azureCosmos.cjs +57 -14
- package/drivers/azureCosmos.d.ts +1 -0
- package/drivers/azureCosmos.mjs +68 -40
- package/drivers/azureStorageBlob.cjs +2 -2
- package/drivers/azureStorageBlob.d.ts +1 -0
- package/drivers/azureStorageBlob.mjs +4 -8
- package/drivers/indexedDb.cjs +2 -2
- package/drivers/indexedDb.d.ts +1 -0
- package/drivers/indexedDb.mjs +6 -12
- package/drivers/memory.cjs +38 -21
- package/drivers/memory.d.ts +1 -0
- package/drivers/memory.mjs +61 -78
- package/drivers/restApi.cjs +1 -1
- package/drivers/restApi.d.ts +2 -1
- package/drivers/restApi.mjs +2 -4
- package/features/cache.cjs +2 -2
- package/features/cache.mjs +15 -30
- package/features/eventSourcing.cjs +2 -2
- package/features/eventSourcing.d.ts +1 -0
- package/features/eventSourcing.mjs +8 -16
- package/features/softDelete.cjs +1 -1
- package/features/softDelete.mjs +2 -4
- package/fileUploadDrivers/azureStorageBlob.cjs +1 -1
- package/fileUploadDrivers/cloudinary.cjs +1 -1
- package/fileUploadDrivers/memory.cjs +1 -1
- package/fileUploadDrivers/restApi.cjs +1 -1
- package/fileUploadDrivers/restApi.d.ts +1 -0
- package/fileUploadDrivers/restApi.mjs +1 -2
- package/fileUploadDrivers/supabaseStorage.cjs +1 -1
- package/fileUploadDrivers/supabaseStorage.mjs +3 -6
- package/integrations/node.cjs +2 -2
- package/integrations/node.mjs +12 -22
- package/integrations/nuxtModule.cjs +2 -3
- package/integrations/restEndpoints.cjs +1 -1
- package/integrations/restEndpoints.mjs +8 -16
- package/integrations/restEndpointsDev/restEndpointsDevLint.cjs +2 -3
- package/integrations/restEndpointsDev/restEndpointsDevLint.d.ts +1 -1
- package/integrations/restEndpointsDev/restEndpointsDevLint.mjs +5 -10
- package/integrations/restEndpointsDev.cjs +3 -4
- package/integrations/restEndpointsDev.d.ts +1 -1
- package/integrations/restEndpointsDev.mjs +9 -18
- package/integrations/restEndpointsDev2.cjs +3 -4
- package/integrations/restEndpointsDev2.d.ts +0 -1
- package/integrations/restEndpointsDev2.mjs +3 -6
- package/integrations/vite.mjs +3 -6
- package/package.json +2 -2
package/drivers/azureCosmos.mjs
CHANGED
|
@@ -17,8 +17,8 @@ export default (options) => (schema, entity) => {
|
|
|
17
17
|
if (options.createDatabaseIfNotExists) {
|
|
18
18
|
createDatabaseIfNotExists(normalizedOptions, client.database.client);
|
|
19
19
|
}
|
|
20
|
-
async function getItemByIds(ids, ctx) {
|
|
21
|
-
const { query, parameters } = getCosmosQuery(schema, entity, { where: { id_in: ids } });
|
|
20
|
+
async function getItemByIds(ids, ctx, include) {
|
|
21
|
+
const { query, parameters } = getCosmosQuery(schema, entity, { where: { id_in: ids }, include });
|
|
22
22
|
const response = client.items.query({
|
|
23
23
|
query,
|
|
24
24
|
parameters: Object.keys(parameters).map((k) => ({ name: `@${k}`, value: parameters[k] }))
|
|
@@ -33,11 +33,11 @@ export default (options) => (schema, entity) => {
|
|
|
33
33
|
const whereKeys = _.keys(where);
|
|
34
34
|
if (whereKeys.length === 1) {
|
|
35
35
|
if (whereKeys[0] === "id" && where.id != null) {
|
|
36
|
-
const items = await getItemByIds([where.id], ctx);
|
|
36
|
+
const items = await getItemByIds([where.id], ctx, args.include);
|
|
37
37
|
return { nodes: [items[0]].filter((x) => x), cursor: null };
|
|
38
38
|
}
|
|
39
39
|
if (whereKeys[0] === "id_in" && where.id_in != null) {
|
|
40
|
-
const items = await getItemByIds(where.id_in, ctx);
|
|
40
|
+
const items = await getItemByIds(where.id_in, ctx, args.include);
|
|
41
41
|
return { nodes: items.filter((x) => x), cursor: null };
|
|
42
42
|
}
|
|
43
43
|
}
|
|
@@ -84,8 +84,7 @@ export default (options) => (schema, entity) => {
|
|
|
84
84
|
const itemsToPut = [];
|
|
85
85
|
for (const item of items) {
|
|
86
86
|
const id = item?.id;
|
|
87
|
-
if (!id)
|
|
88
|
-
throw new Error(`You must provide an id`);
|
|
87
|
+
if (!id) throw new Error(`You must provide an id`);
|
|
89
88
|
itemsToPut.push({ _partition: entity, id, ...item });
|
|
90
89
|
}
|
|
91
90
|
for (const chunk of _.chunk(itemsToPut, 100)) {
|
|
@@ -111,15 +110,12 @@ function getCosmosQuery(schema, entity, args) {
|
|
|
111
110
|
`r._partition = '${entity}'`,
|
|
112
111
|
getCosmosQueryWhere({ schema, entity }, parameters, where)
|
|
113
112
|
].filter((x) => x);
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
const orderDirection = orderByParts.at(-1);
|
|
119
|
-
const orderProp = orderPropFromOrderBy === "value" ? `r["value"]` : `r.${orderPropFromOrderBy}`;
|
|
120
|
-
orderByClause = `order by ${orderProp} ${orderDirection}`;
|
|
113
|
+
const orderByClause = getCosmosOrderBy(args);
|
|
114
|
+
let colums = "*";
|
|
115
|
+
if (args.include?._pick) {
|
|
116
|
+
colums = getCosmosSelectValue(args.include);
|
|
121
117
|
}
|
|
122
|
-
const query = `select
|
|
118
|
+
const query = `select ${colums} from r where ${whereClauses.join(" AND ")} ${orderByClause}`;
|
|
123
119
|
return { query, parameters };
|
|
124
120
|
}
|
|
125
121
|
const operatorHandlers = {
|
|
@@ -140,48 +136,41 @@ const operatorHandlers = {
|
|
|
140
136
|
none: (ctx, parameters, whereArgs) => {
|
|
141
137
|
const { name, namePrefix, paramNamePrefix, paramName, whereVal } = whereArgs;
|
|
142
138
|
const subClause = getCosmosQueryWhere(ctx, parameters, whereVal, `${name}.`, `${paramNamePrefix}${paramName}_`);
|
|
143
|
-
if (subClause)
|
|
144
|
-
return `not exists (select ${name} from ${name} in ${namePrefix}${name} where ${subClause})`;
|
|
139
|
+
if (subClause) return `not exists (select ${name} from ${name} in ${namePrefix}${name} where ${subClause})`;
|
|
145
140
|
return `array_length(${namePrefix}${name}) = 0`;
|
|
146
141
|
},
|
|
147
142
|
and: (ctx, parameters, whereArgs) => {
|
|
148
143
|
const { namePrefix, paramNamePrefix, whereVal } = whereArgs;
|
|
149
|
-
if (!_.isArray(whereVal))
|
|
150
|
-
throw new Error(`Value for where._and must be an array`);
|
|
144
|
+
if (!_.isArray(whereVal)) throw new Error(`Value for where._and must be an array`);
|
|
151
145
|
const clauses = [];
|
|
152
146
|
for (let i = 0; i < whereVal.length; i++) {
|
|
153
147
|
const andQuery = getCosmosQueryWhere(ctx, parameters, whereVal[i], namePrefix, `${paramNamePrefix}and${i}_`);
|
|
154
148
|
clauses.push(andQuery);
|
|
155
149
|
}
|
|
156
|
-
if (!clauses.length)
|
|
157
|
-
return null;
|
|
150
|
+
if (!clauses.length) return null;
|
|
158
151
|
return `((${clauses.join(") and (")}))`;
|
|
159
152
|
},
|
|
160
153
|
not: (ctx, parameters, whereArgs) => {
|
|
161
154
|
const { name, namePrefix, paramNamePrefix, whereVal } = whereArgs;
|
|
162
155
|
const subClause = getCosmosQueryWhere(ctx, parameters, whereVal, `${namePrefix}${name}`, `${paramNamePrefix}not_`);
|
|
163
|
-
if (!subClause)
|
|
164
|
-
return null;
|
|
156
|
+
if (!subClause) return null;
|
|
165
157
|
return `not(${subClause})`;
|
|
166
158
|
},
|
|
167
159
|
or: (ctx, parameters, whereArgs) => {
|
|
168
160
|
const { namePrefix, paramNamePrefix, whereVal } = whereArgs;
|
|
169
|
-
if (!_.isArray(whereVal))
|
|
170
|
-
throw new Error(`Value for where._or must be an array`);
|
|
161
|
+
if (!_.isArray(whereVal)) throw new Error(`Value for where._or must be an array`);
|
|
171
162
|
const clauses = [];
|
|
172
163
|
for (let i = 0; i < whereVal.length; i++) {
|
|
173
164
|
const orQuery = getCosmosQueryWhere(ctx, parameters, whereVal[i], namePrefix, `${paramNamePrefix}or${i}_`);
|
|
174
165
|
clauses.push(orQuery);
|
|
175
166
|
}
|
|
176
|
-
if (!clauses.length)
|
|
177
|
-
return null;
|
|
167
|
+
if (!clauses.length) return null;
|
|
178
168
|
return `((${clauses.join(") or (")}))`;
|
|
179
169
|
},
|
|
180
170
|
isNull: (ctx, parameters, whereArgs) => {
|
|
181
171
|
const { name, namePrefix, whereVal } = whereArgs;
|
|
182
172
|
const n = `${namePrefix}${name}`;
|
|
183
|
-
if (whereVal)
|
|
184
|
-
return `(not (is_defined(${n})) or ${n} = null)`;
|
|
173
|
+
if (whereVal) return `(not (is_defined(${n})) or ${n} = null)`;
|
|
185
174
|
return `(is_defined(${n}) and ${n} != null)`;
|
|
186
175
|
},
|
|
187
176
|
eq: (ctx, parameters, whereArgs) => {
|
|
@@ -241,17 +230,14 @@ const operatorHandlers = {
|
|
|
241
230
|
isEmpty: (ctx, parameters, whereArgs) => {
|
|
242
231
|
const { name, namePrefix, whereVal } = whereArgs;
|
|
243
232
|
const n = `${namePrefix}${name}`;
|
|
244
|
-
if (whereVal)
|
|
245
|
-
return `(not (is_defined(${n})) or ${n} = null or array_length(${n}) = 0)`;
|
|
233
|
+
if (whereVal) return `(not (is_defined(${n})) or ${n} = null or array_length(${n}) = 0)`;
|
|
246
234
|
return `(is_defined(${n}) and ${n} != null and array_length(${n}) > 0)`;
|
|
247
235
|
},
|
|
248
236
|
jsonContains: (ctx, parameters, whereArgs) => {
|
|
249
237
|
const { name, namePrefix, paramName, paramNamePrefix, whereVal } = whereArgs;
|
|
250
238
|
const { path, isNull, value } = whereVal;
|
|
251
|
-
if (!path)
|
|
252
|
-
|
|
253
|
-
if (!sanitizePathRegex.test(path))
|
|
254
|
-
throw new Error(`Invalid path ${path}`);
|
|
239
|
+
if (!path) return "";
|
|
240
|
+
if (!sanitizePathRegex.test(path)) throw new Error(`Invalid path ${path}`);
|
|
255
241
|
const pn = `${paramNamePrefix}${paramName}`;
|
|
256
242
|
parameters[pn] = whereVal;
|
|
257
243
|
const fullPath = `${namePrefix}${name}.${path}`;
|
|
@@ -299,6 +285,28 @@ const operatorHandlers = {
|
|
|
299
285
|
return `${namePrefix}${name} <= @${pn}`;
|
|
300
286
|
}
|
|
301
287
|
};
|
|
288
|
+
function getCosmosOrderBy(args) {
|
|
289
|
+
let orderByClause = "";
|
|
290
|
+
if (args.orderByArray) {
|
|
291
|
+
orderByClause = `order by `;
|
|
292
|
+
const orderByColumns = [];
|
|
293
|
+
for (const order of args.orderByArray) {
|
|
294
|
+
orderByColumns.push(parseOrderBy(order));
|
|
295
|
+
}
|
|
296
|
+
orderByClause = orderByClause + orderByColumns.join(",");
|
|
297
|
+
} else if (args.orderBy) {
|
|
298
|
+
orderByClause = `order by ${parseOrderBy(args.orderBy)}`;
|
|
299
|
+
}
|
|
300
|
+
return orderByClause;
|
|
301
|
+
}
|
|
302
|
+
function parseOrderBy(order) {
|
|
303
|
+
const orderByParts = order.split("_");
|
|
304
|
+
const orderPropFromOrderBy = orderByParts.slice(0, -1).join(".");
|
|
305
|
+
const orderDirection = orderByParts.at(-1);
|
|
306
|
+
const orderProp = orderPropFromOrderBy === "value" ? `r["value"]` : `r.${orderPropFromOrderBy}`;
|
|
307
|
+
const ordr = `${orderProp} ${orderDirection}`;
|
|
308
|
+
return ordr;
|
|
309
|
+
}
|
|
302
310
|
function getCosmosQueryWhere(ctx, parameters, where, namePrefix = "r.", paramNamePrefix = "") {
|
|
303
311
|
const whereClauses = [];
|
|
304
312
|
for (const key in where) {
|
|
@@ -310,8 +318,7 @@ function getCosmosQueryWhere(ctx, parameters, where, namePrefix = "r.", paramNam
|
|
|
310
318
|
}
|
|
311
319
|
const paramName = key;
|
|
312
320
|
const whereVal = where[key];
|
|
313
|
-
if (whereVal == null)
|
|
314
|
-
continue;
|
|
321
|
+
if (whereVal == null) continue;
|
|
315
322
|
const f = getCosmosQueryWhereInner({ ...ctx, field: nameFromWhere }, parameters, {
|
|
316
323
|
operator,
|
|
317
324
|
whereVal,
|
|
@@ -320,8 +327,7 @@ function getCosmosQueryWhere(ctx, parameters, where, namePrefix = "r.", paramNam
|
|
|
320
327
|
paramName,
|
|
321
328
|
paramNamePrefix
|
|
322
329
|
});
|
|
323
|
-
if (f)
|
|
324
|
-
whereClauses.push(f);
|
|
330
|
+
if (f) whereClauses.push(f);
|
|
325
331
|
}
|
|
326
332
|
return whereClauses.join(" AND ");
|
|
327
333
|
}
|
|
@@ -389,8 +395,7 @@ async function bulkSendWithRetry(client, chunkToSend) {
|
|
|
389
395
|
}
|
|
390
396
|
}
|
|
391
397
|
chunkToSend = newChunkToSend;
|
|
392
|
-
if (chunkToSend.length === 0)
|
|
393
|
-
break;
|
|
398
|
+
if (chunkToSend.length === 0) break;
|
|
394
399
|
const delay = 50 + i * 100;
|
|
395
400
|
console.warn(`Db overloaded. Retrying after ${delay}ms...`);
|
|
396
401
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
@@ -402,3 +407,26 @@ async function bulkSendWithRetry(client, chunkToSend) {
|
|
|
402
407
|
}
|
|
403
408
|
return responses;
|
|
404
409
|
}
|
|
410
|
+
function buildSelectValue(includeArgs, path = "r") {
|
|
411
|
+
const parts = [];
|
|
412
|
+
if (Array.isArray(includeArgs._pick)) {
|
|
413
|
+
for (const key of includeArgs._pick) {
|
|
414
|
+
parts.push(`${key}: ${path}.${key}`);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
for (const key of Object.keys(includeArgs)) {
|
|
418
|
+
if (key === "_pick") continue;
|
|
419
|
+
const nested = includeArgs[key];
|
|
420
|
+
if (typeof nested === "object" && nested !== null) {
|
|
421
|
+
const nestedSelect = buildSelectValue(nested, `${path}.${key}`);
|
|
422
|
+
parts.push(`${key}: { ${nestedSelect} }`);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
return parts.join(", ");
|
|
426
|
+
}
|
|
427
|
+
function getCosmosSelectValue(includeArgs) {
|
|
428
|
+
const projection = buildSelectValue(includeArgs);
|
|
429
|
+
return `VALUE {
|
|
430
|
+
${projection}
|
|
431
|
+
} `;
|
|
432
|
+
}
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
var _storageBlob = require("@azure/storage-blob");
|
|
8
8
|
var _lodash = _interopRequireDefault(require("lodash"));
|
|
9
|
-
function _interopRequireDefault(
|
|
9
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
10
|
const blobServiceClientCache = {};
|
|
11
11
|
const containerClientCache = {};
|
|
12
12
|
var _default = options => (schema, entity) => {
|
|
@@ -15,8 +15,7 @@ export default (options) => (schema, entity) => {
|
|
|
15
15
|
ctx?.log?.({ requestId: downloadResponse.requestId, charge: str.length, request: `download ${name}` });
|
|
16
16
|
return JSON.parse(str);
|
|
17
17
|
} catch (e) {
|
|
18
|
-
if (e?.statusCode === 404)
|
|
19
|
-
return void 0;
|
|
18
|
+
if (e?.statusCode === 404) return void 0;
|
|
20
19
|
throw e;
|
|
21
20
|
}
|
|
22
21
|
}
|
|
@@ -41,8 +40,7 @@ export default (options) => (schema, entity) => {
|
|
|
41
40
|
return { nodes: items.filter((x) => x), cursor: null };
|
|
42
41
|
}
|
|
43
42
|
}
|
|
44
|
-
if (whereKeys.length > 1)
|
|
45
|
-
throw new Error("Complex queries are not supported by azureStorageBlob");
|
|
43
|
+
if (whereKeys.length > 1) throw new Error("Complex queries are not supported by azureStorageBlob");
|
|
46
44
|
const iterator = containerClient.listBlobsFlat({}).byPage({ maxPageSize: args.maxItemCount || 30, continuationToken: args.cursor || void 0 });
|
|
47
45
|
for await (const page of iterator) {
|
|
48
46
|
ctx?.log?.({
|
|
@@ -81,8 +79,7 @@ export default (options) => (schema, entity) => {
|
|
|
81
79
|
async putMany(items, ctx) {
|
|
82
80
|
for (const item of items) {
|
|
83
81
|
const id = item?.id;
|
|
84
|
-
if (!id)
|
|
85
|
-
throw new Error(`You must provide an id`);
|
|
82
|
+
if (!id) throw new Error(`You must provide an id`);
|
|
86
83
|
const blobName = getBlobName(entity, item);
|
|
87
84
|
const itemToPut = item;
|
|
88
85
|
const blobClient = containerClient.getBlockBlobClient(blobName);
|
|
@@ -95,8 +92,7 @@ export default (options) => (schema, entity) => {
|
|
|
95
92
|
return instance;
|
|
96
93
|
};
|
|
97
94
|
function getBlobName(entity, item) {
|
|
98
|
-
if (!item.id)
|
|
99
|
-
throw new Error("Item must have an id");
|
|
95
|
+
if (!item.id) throw new Error("Item must have an id");
|
|
100
96
|
return `${entity}/${item.id}.json`;
|
|
101
97
|
}
|
|
102
98
|
async function blobOrStreamToString(response) {
|
package/drivers/indexedDb.cjs
CHANGED
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
var _dexie = _interopRequireDefault(require("dexie"));
|
|
8
8
|
var _lodash = _interopRequireDefault(require("lodash"));
|
|
9
9
|
var _memory = require("./memory.cjs");
|
|
10
|
-
function _interopRequireDefault(
|
|
10
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
11
|
const dbs = {};
|
|
12
12
|
const tableOperatorFns = {
|
|
13
13
|
eq: (where, value) => where.equals(value),
|
package/drivers/indexedDb.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { DexieOptions } from 'dexie';
|
|
2
|
+
import type { MinimalDriver, Schema } from 'rads-db';
|
|
2
3
|
declare const _default: (options?: IndexedDbDriverOptions) => (schema: Schema, entity: string) => MinimalDriver;
|
|
3
4
|
export default _default;
|
|
4
5
|
export interface IndexedDbDriverOptions {
|
package/drivers/indexedDb.mjs
CHANGED
|
@@ -22,8 +22,7 @@ export default (options) => {
|
|
|
22
22
|
const dbWrapper = getDb(options);
|
|
23
23
|
return (schema, entity) => {
|
|
24
24
|
const handle = schema[entity]?.handle;
|
|
25
|
-
if (!handle)
|
|
26
|
-
throw new Error(`Entity ${entity} not found`);
|
|
25
|
+
if (!handle) throw new Error(`Entity ${entity} not found`);
|
|
27
26
|
dbWrapper.entities[handle] = true;
|
|
28
27
|
async function getMany(args, ctx) {
|
|
29
28
|
await initDbIfNeeded(dbName);
|
|
@@ -79,8 +78,7 @@ function applyArgsToDexieTable(table, args) {
|
|
|
79
78
|
if (!collection && orderByProp) {
|
|
80
79
|
if (indexedProps.includes(orderByProp)) {
|
|
81
80
|
collection = table.orderBy(orderByProp);
|
|
82
|
-
if (orderByDirection === "desc")
|
|
83
|
-
collection = collection.reverse();
|
|
81
|
+
if (orderByDirection === "desc") collection = collection.reverse();
|
|
84
82
|
orderByProp = "";
|
|
85
83
|
}
|
|
86
84
|
}
|
|
@@ -92,8 +90,7 @@ function applyArgsToDexieTable(table, args) {
|
|
|
92
90
|
collection = collection.filter((x) => f(x) || false);
|
|
93
91
|
}
|
|
94
92
|
if (maxItemCount && !orderByProp) {
|
|
95
|
-
if (args.cursor)
|
|
96
|
-
collection = collection.offset(_.toNumber(args.cursor));
|
|
93
|
+
if (args.cursor) collection = collection.offset(_.toNumber(args.cursor));
|
|
97
94
|
collection = collection.limit(maxItemCount);
|
|
98
95
|
}
|
|
99
96
|
return { collection, orderByProp, orderByDirection, maxItemCount };
|
|
@@ -125,12 +122,10 @@ function applyTableFiltering(table, where, fieldName) {
|
|
|
125
122
|
}
|
|
126
123
|
}
|
|
127
124
|
function normalizeArgs(args) {
|
|
128
|
-
if (!args)
|
|
129
|
-
args = {};
|
|
125
|
+
if (!args) args = {};
|
|
130
126
|
const where = _.cloneDeep(args.where) || {};
|
|
131
127
|
let orderBy = args.orderBy || "";
|
|
132
|
-
if (_.isArray(orderBy))
|
|
133
|
-
orderBy = orderBy[0] || "";
|
|
128
|
+
if (_.isArray(orderBy)) orderBy = orderBy[0] || "";
|
|
134
129
|
const orderByParts = orderBy.split("_");
|
|
135
130
|
const orderByProperty = orderByParts.slice(0, -1).join(".");
|
|
136
131
|
const orderByDirection = orderByParts.slice(-1)[0];
|
|
@@ -180,8 +175,7 @@ async function initDbInner(dbName) {
|
|
|
180
175
|
}
|
|
181
176
|
function getDb(options) {
|
|
182
177
|
const dbName = options?.dbName || "db";
|
|
183
|
-
if (dbs[dbName]?.db)
|
|
184
|
-
dbs[dbName].db?.close();
|
|
178
|
+
if (dbs[dbName]?.db) dbs[dbName].db?.close();
|
|
185
179
|
dbs[dbName] = { entities: {}, driverOptions: { dbName, dexieOptions: void 0, invalidateCounter: 0, ...options } };
|
|
186
180
|
return dbs[dbName];
|
|
187
181
|
}
|
package/drivers/memory.cjs
CHANGED
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
exports.getAggFromArray = getAggFromArray;
|
|
8
8
|
exports.getFilter = getFilter;
|
|
9
9
|
exports.queryArray = queryArray;
|
|
10
10
|
var _lodash = _interopRequireDefault(require("lodash"));
|
|
11
|
-
function _interopRequireDefault(
|
|
11
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
12
12
|
const operatorFns = {
|
|
13
13
|
eq: (x, w) => x === w,
|
|
14
14
|
ieq: (x, w) => x?.toLowerCase() === w?.toLowerCase(),
|
|
@@ -76,14 +76,20 @@ var _default = options => (schema, entity) => {
|
|
|
76
76
|
const where = args.where || {};
|
|
77
77
|
const whereKeys = _lodash.default.keys(where);
|
|
78
78
|
if (whereKeys.length === 1) {
|
|
79
|
-
if (whereKeys[0] === "id")
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
79
|
+
if (whereKeys[0] === "id") {
|
|
80
|
+
const node = _lodash.default.cloneDeep([getItemById(where.id)].filter(x => x));
|
|
81
|
+
return {
|
|
82
|
+
nodes: node,
|
|
83
|
+
cursor: null
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
if (whereKeys[0] === "id_in") {
|
|
87
|
+
const nodes = _lodash.default.cloneDeep(getItemByIds(where.id_in).filter(x => x));
|
|
88
|
+
return {
|
|
89
|
+
nodes,
|
|
90
|
+
cursor: null
|
|
91
|
+
};
|
|
92
|
+
}
|
|
87
93
|
}
|
|
88
94
|
return queryArray(Object.values(itemsById), args);
|
|
89
95
|
}
|
|
@@ -135,8 +141,8 @@ function queryArray(array, args) {
|
|
|
135
141
|
let result = array;
|
|
136
142
|
const {
|
|
137
143
|
where,
|
|
138
|
-
|
|
139
|
-
|
|
144
|
+
orderByProperties,
|
|
145
|
+
orderByDirections,
|
|
140
146
|
maxItemCount,
|
|
141
147
|
cursor
|
|
142
148
|
} = prepareArgs(args);
|
|
@@ -144,7 +150,7 @@ function queryArray(array, args) {
|
|
|
144
150
|
const endIndex = startIndex + maxItemCount;
|
|
145
151
|
const f = getFilter(where);
|
|
146
152
|
if (f) result = result.filter(f);
|
|
147
|
-
if (
|
|
153
|
+
if (orderByProperties) result = _lodash.default.orderBy(result, orderByProperties, orderByDirections);
|
|
148
154
|
if (maxItemCount) result = result.slice(startIndex, endIndex);
|
|
149
155
|
const newCursor = endIndex >= array.length ? null : endIndex;
|
|
150
156
|
return {
|
|
@@ -225,18 +231,29 @@ function prepareArgs(args) {
|
|
|
225
231
|
const where = {
|
|
226
232
|
...args.where
|
|
227
233
|
};
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
234
|
+
const orderByProperties = [];
|
|
235
|
+
const orderByDirections = [];
|
|
236
|
+
if (args.orderBy) {
|
|
237
|
+
let orderBy = args.orderBy || "";
|
|
238
|
+
if (Array.isArray(orderBy)) orderBy = orderBy[0] || "";
|
|
239
|
+
const orderByParts = orderBy.split("_");
|
|
240
|
+
orderByProperties.push(orderByParts.slice(0, -1).join("."));
|
|
241
|
+
orderByDirections.push(orderByParts.slice(-1)[0]);
|
|
242
|
+
} else if (args.orderByArray) {
|
|
243
|
+
for (const order of args.orderByArray) {
|
|
244
|
+
const [property, direction] = order.split("_");
|
|
245
|
+
orderByProperties.push(property);
|
|
246
|
+
orderByDirections.push(direction);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
233
249
|
let maxItemCount = args.maxItemCount;
|
|
234
250
|
maxItemCount = maxItemCount || 100;
|
|
235
251
|
return {
|
|
236
252
|
where,
|
|
237
|
-
|
|
238
|
-
|
|
253
|
+
orderByProperties,
|
|
254
|
+
orderByDirections,
|
|
239
255
|
maxItemCount,
|
|
240
|
-
cursor: args.cursor
|
|
256
|
+
cursor: args.cursor,
|
|
257
|
+
pick: args.include
|
|
241
258
|
};
|
|
242
259
|
}
|
package/drivers/memory.d.ts
CHANGED