@objectql/driver-memory 4.1.0 → 4.2.1
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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +36 -0
- package/dist/index.d.ts +11 -12
- package/dist/index.js +37 -32
- package/dist/index.js.map +1 -1
- package/package.json +12 -6
- package/src/index.ts +41 -36
- package/test/index.test.ts +1 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/jest.config.js +0 -29
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Data, System as
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
type
|
|
1
|
+
import { Data, System as _SystemSpec } from '@objectstack/spec';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { QueryAST } from '@objectql/types';
|
|
4
|
+
type _DriverInterface = z.infer<typeof Data.DriverInterface>;
|
|
5
5
|
/**
|
|
6
6
|
* ObjectQL
|
|
7
7
|
* Copyright (c) 2026-present ObjectStack Inc.
|
|
@@ -181,7 +181,7 @@ export class MemoryDriver implements Driver {
|
|
|
181
181
|
* Find multiple records matching the query criteria.
|
|
182
182
|
* Supports filtering, sorting, pagination, and field projection using Mingo.
|
|
183
183
|
*/
|
|
184
|
-
async find(objectName: string, query: any = {},
|
|
184
|
+
async find(objectName: string, query: any = {}, _options?: any): Promise<any[]> {
|
|
185
185
|
// Get all records for this object type
|
|
186
186
|
const pattern = `${objectName}:`;
|
|
187
187
|
let records: any[] = [];
|
|
@@ -245,7 +245,7 @@ export class MemoryDriver implements Driver {
|
|
|
245
245
|
/**
|
|
246
246
|
* Create a new record.
|
|
247
247
|
*/
|
|
248
|
-
async create(objectName: string, data: any,
|
|
248
|
+
async create(objectName: string, data: any, _options?: any): Promise<any> {
|
|
249
249
|
// Generate ID if not provided
|
|
250
250
|
const id = data.id || this.generateId(objectName);
|
|
251
251
|
const key = `${objectName}:${id}`;
|
|
@@ -278,7 +278,7 @@ export class MemoryDriver implements Driver {
|
|
|
278
278
|
/**
|
|
279
279
|
* Update an existing record.
|
|
280
280
|
*/
|
|
281
|
-
async update(objectName: string, id: string | number, data: any,
|
|
281
|
+
async update(objectName: string, id: string | number, data: any, _options?: any): Promise<any> {
|
|
282
282
|
const key = `${objectName}:${id}`;
|
|
283
283
|
const existing = this.store.get(key);
|
|
284
284
|
|
|
@@ -312,7 +312,7 @@ export class MemoryDriver implements Driver {
|
|
|
312
312
|
/**
|
|
313
313
|
* Delete a record.
|
|
314
314
|
*/
|
|
315
|
-
async delete(objectName: string, id: string | number,
|
|
315
|
+
async delete(objectName: string, id: string | number, _options?: any): Promise<any> {
|
|
316
316
|
const key = `${objectName}:${id}`;
|
|
317
317
|
const deleted = this.store.delete(key);
|
|
318
318
|
|
|
@@ -335,7 +335,7 @@ export class MemoryDriver implements Driver {
|
|
|
335
335
|
/**
|
|
336
336
|
* Count records matching filters using Mingo.
|
|
337
337
|
*/
|
|
338
|
-
async count(objectName: string, filters: any,
|
|
338
|
+
async count(objectName: string, filters: any, _options?: any): Promise<number> {
|
|
339
339
|
const pattern = `${objectName}:`;
|
|
340
340
|
|
|
341
341
|
// Extract where condition from query object if needed
|
|
@@ -345,7 +345,7 @@ export class MemoryDriver implements Driver {
|
|
|
345
345
|
}
|
|
346
346
|
|
|
347
347
|
// Get all records for this object type
|
|
348
|
-
|
|
348
|
+
const records: any[] = [];
|
|
349
349
|
for (const [key, value] of this.store.entries()) {
|
|
350
350
|
if (key.startsWith(pattern)) {
|
|
351
351
|
records.push(value);
|
|
@@ -371,7 +371,7 @@ export class MemoryDriver implements Driver {
|
|
|
371
371
|
/**
|
|
372
372
|
* Get distinct values for a field using Mingo.
|
|
373
373
|
*/
|
|
374
|
-
async distinct(objectName: string, field: string, filters?: any,
|
|
374
|
+
async distinct(objectName: string, field: string, filters?: any, _options?: any): Promise<any[]> {
|
|
375
375
|
const pattern = `${objectName}:`;
|
|
376
376
|
|
|
377
377
|
// Get all records for this object type
|
|
@@ -418,11 +418,11 @@ export class MemoryDriver implements Driver {
|
|
|
418
418
|
/**
|
|
419
419
|
* Update multiple records matching filters using Mingo.
|
|
420
420
|
*/
|
|
421
|
-
async updateMany(objectName: string, filters: any, data: any,
|
|
421
|
+
async updateMany(objectName: string, filters: any, data: any, _options?: any): Promise<any> {
|
|
422
422
|
const pattern = `${objectName}:`;
|
|
423
423
|
|
|
424
424
|
// Get all records for this object type
|
|
425
|
-
|
|
425
|
+
const records: any[] = [];
|
|
426
426
|
const recordKeys = new Map<string, string>();
|
|
427
427
|
|
|
428
428
|
for (const [key, record] of this.store.entries()) {
|
|
@@ -464,11 +464,11 @@ export class MemoryDriver implements Driver {
|
|
|
464
464
|
/**
|
|
465
465
|
* Delete multiple records matching filters using Mingo.
|
|
466
466
|
*/
|
|
467
|
-
async deleteMany(objectName: string, filters: any,
|
|
467
|
+
async deleteMany(objectName: string, filters: any, _options?: any): Promise<any> {
|
|
468
468
|
const pattern = `${objectName}:`;
|
|
469
469
|
|
|
470
470
|
// Get all records for this object type
|
|
471
|
-
|
|
471
|
+
const records: any[] = [];
|
|
472
472
|
const recordKeys = new Map<string, string>();
|
|
473
473
|
|
|
474
474
|
for (const [key, record] of this.store.entries()) {
|
|
@@ -550,11 +550,11 @@ export class MemoryDriver implements Driver {
|
|
|
550
550
|
* { $group: { _id: null, avgPrice: { $avg: '$price' } } }
|
|
551
551
|
* ]);
|
|
552
552
|
*/
|
|
553
|
-
async aggregate(objectName: string, pipeline: any[],
|
|
553
|
+
async aggregate(objectName: string, pipeline: any[], _options?: any): Promise<any[]> {
|
|
554
554
|
const pattern = `${objectName}:`;
|
|
555
555
|
|
|
556
556
|
// Get all records for this object type
|
|
557
|
-
|
|
557
|
+
const records: any[] = [];
|
|
558
558
|
for (const [key, value] of this.store.entries()) {
|
|
559
559
|
if (key.startsWith(pattern)) {
|
|
560
560
|
records.push({ ...value });
|
|
@@ -675,8 +675,8 @@ export class MemoryDriver implements Driver {
|
|
|
675
675
|
}
|
|
676
676
|
}
|
|
677
677
|
}
|
|
678
|
-
} catch (
|
|
679
|
-
|
|
678
|
+
} catch (_error) {
|
|
679
|
+
// Error silently ignored
|
|
680
680
|
}
|
|
681
681
|
|
|
682
682
|
// Set up auto-save timer
|
|
@@ -703,8 +703,8 @@ export class MemoryDriver implements Driver {
|
|
|
703
703
|
};
|
|
704
704
|
|
|
705
705
|
fs.writeFileSync(this.config.persistence.filePath, JSON.stringify(data, null, 2), 'utf8');
|
|
706
|
-
} catch (
|
|
707
|
-
|
|
706
|
+
} catch (_error) {
|
|
707
|
+
// Error silently ignored
|
|
708
708
|
}
|
|
709
709
|
}
|
|
710
710
|
|
|
@@ -839,7 +839,7 @@ export class MemoryDriver implements Driver {
|
|
|
839
839
|
}
|
|
840
840
|
|
|
841
841
|
// Process the filter array to build MongoDB query
|
|
842
|
-
const
|
|
842
|
+
const _conditions: Record<string, any>[] = [];
|
|
843
843
|
let currentLogic: 'and' | 'or' = 'and';
|
|
844
844
|
const logicGroups: { logic: 'and' | 'or', conditions: Record<string, any>[] }[] = [
|
|
845
845
|
{ logic: 'and', conditions: [] }
|
|
@@ -1083,9 +1083,9 @@ export class MemoryDriver implements Driver {
|
|
|
1083
1083
|
const cmdOptions = { ...options, ...command.options };
|
|
1084
1084
|
|
|
1085
1085
|
switch (command.type) {
|
|
1086
|
-
case 'create':
|
|
1086
|
+
case 'create': {
|
|
1087
1087
|
if (!command.data) {
|
|
1088
|
-
throw new
|
|
1088
|
+
throw new ObjectQLError({ code: 'DRIVER_QUERY_FAILED', message: 'Create command requires data' });
|
|
1089
1089
|
}
|
|
1090
1090
|
const created = await this.create(command.object, command.data, cmdOptions);
|
|
1091
1091
|
return {
|
|
@@ -1093,10 +1093,11 @@ export class MemoryDriver implements Driver {
|
|
|
1093
1093
|
data: created,
|
|
1094
1094
|
affected: 1
|
|
1095
1095
|
};
|
|
1096
|
+
}
|
|
1096
1097
|
|
|
1097
|
-
case 'update':
|
|
1098
|
+
case 'update': {
|
|
1098
1099
|
if (!command.id || !command.data) {
|
|
1099
|
-
throw new
|
|
1100
|
+
throw new ObjectQLError({ code: 'DRIVER_QUERY_FAILED', message: 'Update command requires id and data' });
|
|
1100
1101
|
}
|
|
1101
1102
|
const updated = await this.update(command.object, command.id, command.data, cmdOptions);
|
|
1102
1103
|
return {
|
|
@@ -1104,10 +1105,11 @@ export class MemoryDriver implements Driver {
|
|
|
1104
1105
|
data: updated,
|
|
1105
1106
|
affected: 1
|
|
1106
1107
|
};
|
|
1108
|
+
}
|
|
1107
1109
|
|
|
1108
1110
|
case 'delete':
|
|
1109
1111
|
if (!command.id) {
|
|
1110
|
-
throw new
|
|
1112
|
+
throw new ObjectQLError({ code: 'DRIVER_QUERY_FAILED', message: 'Delete command requires id' });
|
|
1111
1113
|
}
|
|
1112
1114
|
await this.delete(command.object, command.id, cmdOptions);
|
|
1113
1115
|
return {
|
|
@@ -1115,9 +1117,9 @@ export class MemoryDriver implements Driver {
|
|
|
1115
1117
|
affected: 1
|
|
1116
1118
|
};
|
|
1117
1119
|
|
|
1118
|
-
case 'bulkCreate':
|
|
1120
|
+
case 'bulkCreate': {
|
|
1119
1121
|
if (!command.records || !Array.isArray(command.records)) {
|
|
1120
|
-
throw new
|
|
1122
|
+
throw new ObjectQLError({ code: 'DRIVER_QUERY_FAILED', message: 'BulkCreate command requires records array' });
|
|
1121
1123
|
}
|
|
1122
1124
|
const bulkCreated = [];
|
|
1123
1125
|
for (const record of command.records) {
|
|
@@ -1129,10 +1131,11 @@ export class MemoryDriver implements Driver {
|
|
|
1129
1131
|
data: bulkCreated,
|
|
1130
1132
|
affected: command.records.length
|
|
1131
1133
|
};
|
|
1134
|
+
}
|
|
1132
1135
|
|
|
1133
|
-
case 'bulkUpdate':
|
|
1136
|
+
case 'bulkUpdate': {
|
|
1134
1137
|
if (!command.updates || !Array.isArray(command.updates)) {
|
|
1135
|
-
throw new
|
|
1138
|
+
throw new ObjectQLError({ code: 'DRIVER_QUERY_FAILED', message: 'BulkUpdate command requires updates array' });
|
|
1136
1139
|
}
|
|
1137
1140
|
const updateResults = [];
|
|
1138
1141
|
for (const update of command.updates) {
|
|
@@ -1144,10 +1147,11 @@ export class MemoryDriver implements Driver {
|
|
|
1144
1147
|
data: updateResults,
|
|
1145
1148
|
affected: command.updates.length
|
|
1146
1149
|
};
|
|
1150
|
+
}
|
|
1147
1151
|
|
|
1148
|
-
case 'bulkDelete':
|
|
1152
|
+
case 'bulkDelete': {
|
|
1149
1153
|
if (!command.ids || !Array.isArray(command.ids)) {
|
|
1150
|
-
throw new
|
|
1154
|
+
throw new ObjectQLError({ code: 'DRIVER_QUERY_FAILED', message: 'BulkDelete command requires ids array' });
|
|
1151
1155
|
}
|
|
1152
1156
|
let deleted = 0;
|
|
1153
1157
|
for (const id of command.ids) {
|
|
@@ -1158,9 +1162,10 @@ export class MemoryDriver implements Driver {
|
|
|
1158
1162
|
success: true,
|
|
1159
1163
|
affected: deleted
|
|
1160
1164
|
};
|
|
1165
|
+
}
|
|
1161
1166
|
|
|
1162
1167
|
default:
|
|
1163
|
-
throw new
|
|
1168
|
+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: `Unknown command type: ${(command as any).type}` });
|
|
1164
1169
|
}
|
|
1165
1170
|
} catch (error: any) {
|
|
1166
1171
|
return {
|
|
@@ -1178,9 +1183,9 @@ export class MemoryDriver implements Driver {
|
|
|
1178
1183
|
* @param parameters - Command parameters
|
|
1179
1184
|
* @param options - Execution options
|
|
1180
1185
|
*/
|
|
1181
|
-
async execute(
|
|
1186
|
+
async execute(_command: any, _parameters?: any[], _options?: any): Promise<any> {
|
|
1182
1187
|
// For memory driver, this is primarily for compatibility
|
|
1183
1188
|
// We don't support raw SQL/commands
|
|
1184
|
-
throw new
|
|
1189
|
+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: 'Memory driver does not support raw command execution. Use executeCommand() instead.' });
|
|
1185
1190
|
}
|
|
1186
1191
|
}
|