@dbcube/query-builder 5.2.1 → 5.2.3
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/index.cjs +148 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +70 -0
- package/dist/index.d.ts +70 -0
- package/dist/index.js +148 -17
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -80,10 +80,13 @@ var Trigger = class {
|
|
|
80
80
|
delete require2.cache[require2.resolve(pathFile)];
|
|
81
81
|
} catch {
|
|
82
82
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
83
|
+
try {
|
|
84
|
+
const triggerModule = require2(pathFile);
|
|
85
|
+
const dataProcess = triggerModule.default || triggerModule;
|
|
86
|
+
await dataProcess({ db: this.instance, oldData: row, newData: row });
|
|
87
|
+
} finally {
|
|
88
|
+
interceptor.restore();
|
|
89
|
+
}
|
|
87
90
|
return interceptor;
|
|
88
91
|
}
|
|
89
92
|
return null;
|
|
@@ -155,6 +158,39 @@ var Database = class _Database {
|
|
|
155
158
|
throw error;
|
|
156
159
|
}
|
|
157
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* Atomic batch: every write queued in the callback runs inside ONE
|
|
163
|
+
* transaction with a SINGLE engine round-trip (begin/commit included).
|
|
164
|
+
* The callback is synchronous — writes are collected, not awaited.
|
|
165
|
+
* Any failure rolls back everything.
|
|
166
|
+
*
|
|
167
|
+
* This is the fastest way to run a sequence of writes atomically.
|
|
168
|
+
* For transactions that need to READ intermediate results, use
|
|
169
|
+
* `transaction()` (interactive) instead. Triggers/computed fields do
|
|
170
|
+
* not run in batch mode.
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* await db.batch(b => {
|
|
174
|
+
* b.table('accounts').where('id', '=', 1).decrement('balance', 200);
|
|
175
|
+
* b.table('accounts').where('id', '=', 2).increment('balance', 200);
|
|
176
|
+
* });
|
|
177
|
+
*/
|
|
178
|
+
async batch(builder) {
|
|
179
|
+
const ops = [];
|
|
180
|
+
const collector = new _Database(this.name);
|
|
181
|
+
collector.engine = this.engine;
|
|
182
|
+
collector.computedFields = this.computedFields;
|
|
183
|
+
collector.triggers = this.triggers;
|
|
184
|
+
collector._collectInto = ops;
|
|
185
|
+
builder(collector);
|
|
186
|
+
if (ops.length === 0) return [];
|
|
187
|
+
const response = await this.engine.executeBatch(ops);
|
|
188
|
+
if (response.status !== 200) {
|
|
189
|
+
returnFormattedError(response.status, response.message);
|
|
190
|
+
throw new Error(String(response.message));
|
|
191
|
+
}
|
|
192
|
+
return response.data;
|
|
193
|
+
}
|
|
158
194
|
async useComputes() {
|
|
159
195
|
const newDatabase = new _Database(this.name);
|
|
160
196
|
const arrayComputedFields = await import_core2.ComputedFieldProcessor.getComputedFields(this.name);
|
|
@@ -220,7 +256,10 @@ var Database = class _Database {
|
|
|
220
256
|
* const columns = await db.table('users').columns().get();
|
|
221
257
|
*/
|
|
222
258
|
table(tableName) {
|
|
223
|
-
|
|
259
|
+
const t = new Table(this, this.name, tableName, this.engine, this.computedFields, this.triggers, this.txId);
|
|
260
|
+
const sink = this._collectInto;
|
|
261
|
+
if (sink) t._batchSink = sink;
|
|
262
|
+
return t;
|
|
224
263
|
}
|
|
225
264
|
setComputedFields(computedFields) {
|
|
226
265
|
this.computedFields = computedFields;
|
|
@@ -240,13 +279,16 @@ var Table = class _Table {
|
|
|
240
279
|
relations = [];
|
|
241
280
|
/** Builders de grupo mutan en sitio (ver clone()) */
|
|
242
281
|
_mutable = false;
|
|
282
|
+
/** En modo batch (db.batch) las escrituras se encolan aquí en vez de
|
|
283
|
+
* ejecutarse: toda la transacción viaja al engine en UN solo cruce. */
|
|
284
|
+
_batchSink = null;
|
|
243
285
|
instance;
|
|
244
286
|
constructor(instance, databaseName, tableName, engine, computedFields = [], triggers = [], txId = null) {
|
|
245
287
|
this.engine = engine;
|
|
246
288
|
this.instance = instance;
|
|
247
289
|
this.computedFields = computedFields;
|
|
248
290
|
this.triggers = triggers;
|
|
249
|
-
this.trigger = new Trigger(instance, databaseName, triggers);
|
|
291
|
+
this.trigger = triggers.length > 0 ? new Trigger(instance, databaseName, triggers) : null;
|
|
250
292
|
this.nextType = "AND";
|
|
251
293
|
this.txId = txId;
|
|
252
294
|
this.dml = {
|
|
@@ -583,6 +625,7 @@ var Table = class _Table {
|
|
|
583
625
|
column,
|
|
584
626
|
alias: "count"
|
|
585
627
|
};
|
|
628
|
+
clone.dml.orderBy = [];
|
|
586
629
|
clone.dml.columns = [`COUNT(${column}) AS count`];
|
|
587
630
|
clone.dml.data = null;
|
|
588
631
|
clone.dml.limit = null;
|
|
@@ -616,6 +659,7 @@ var Table = class _Table {
|
|
|
616
659
|
column,
|
|
617
660
|
alias: "sum"
|
|
618
661
|
};
|
|
662
|
+
clone.dml.orderBy = [];
|
|
619
663
|
clone.dml.columns = [`SUM(${column}) AS sum`];
|
|
620
664
|
clone.dml.data = null;
|
|
621
665
|
clone.dml.limit = 1;
|
|
@@ -648,6 +692,7 @@ var Table = class _Table {
|
|
|
648
692
|
column,
|
|
649
693
|
alias: "avg"
|
|
650
694
|
};
|
|
695
|
+
clone.dml.orderBy = [];
|
|
651
696
|
clone.dml.columns = [`AVG(${column}) AS avg`];
|
|
652
697
|
clone.dml.data = null;
|
|
653
698
|
clone.dml.limit = 1;
|
|
@@ -680,6 +725,7 @@ var Table = class _Table {
|
|
|
680
725
|
column,
|
|
681
726
|
alias: "max"
|
|
682
727
|
};
|
|
728
|
+
clone.dml.orderBy = [];
|
|
683
729
|
clone.dml.columns = [`MAX(${column}) AS max`];
|
|
684
730
|
clone.dml.data = null;
|
|
685
731
|
clone.dml.limit = 1;
|
|
@@ -712,6 +758,7 @@ var Table = class _Table {
|
|
|
712
758
|
column,
|
|
713
759
|
alias: "min"
|
|
714
760
|
};
|
|
761
|
+
clone.dml.orderBy = [];
|
|
715
762
|
clone.dml.columns = [`MIN(${column}) AS min`];
|
|
716
763
|
clone.dml.data = null;
|
|
717
764
|
clone.dml.limit = 1;
|
|
@@ -868,6 +915,30 @@ var Table = class _Table {
|
|
|
868
915
|
* console.log(user); // { id: 1, name: 'John' }
|
|
869
916
|
*/
|
|
870
917
|
async find(value, column = "id") {
|
|
918
|
+
if (this.computedFields.length === 0 && this.relations.length === 0 && !this._mutable) {
|
|
919
|
+
const dml = {
|
|
920
|
+
type: "select",
|
|
921
|
+
database: this.dml.database,
|
|
922
|
+
table: this.dml.table,
|
|
923
|
+
columns: ["*"],
|
|
924
|
+
distinct: false,
|
|
925
|
+
joins: [],
|
|
926
|
+
where: [{ column, operator: "=", value, type: "AND", isGroup: false }],
|
|
927
|
+
orderBy: [],
|
|
928
|
+
groupBy: [],
|
|
929
|
+
limit: 1,
|
|
930
|
+
offset: null,
|
|
931
|
+
data: null,
|
|
932
|
+
aggregation: null,
|
|
933
|
+
having: []
|
|
934
|
+
};
|
|
935
|
+
const response = await this.engine.executeDml(dml, this.txId ?? void 0);
|
|
936
|
+
if (response.status != 200) {
|
|
937
|
+
returnFormattedError(response.status, response.message);
|
|
938
|
+
throw new Error(String(response.message));
|
|
939
|
+
}
|
|
940
|
+
return response.data?.[0] || null;
|
|
941
|
+
}
|
|
871
942
|
const clone = this.clone().where(column, "=", value);
|
|
872
943
|
clone.dml.type = "select";
|
|
873
944
|
clone.dml.data = null;
|
|
@@ -894,13 +965,26 @@ var Table = class _Table {
|
|
|
894
965
|
* console.log(newUsers); // [{ id: 3, name: 'Alice', age: 28 }, { id: 4, name: 'Bob', age: 32 }]
|
|
895
966
|
*/
|
|
896
967
|
async insert(data) {
|
|
897
|
-
const clone = this.clone();
|
|
898
968
|
if (!Array.isArray(data)) {
|
|
899
969
|
throw new Error("The insert method requires an array of objects with key-value pairs.");
|
|
900
970
|
}
|
|
901
971
|
if (!data.every((item) => typeof item === "object" && item !== null)) {
|
|
902
972
|
throw new Error("The array must contain only valid objects.");
|
|
903
973
|
}
|
|
974
|
+
if (this._batchSink) {
|
|
975
|
+
this._batchSink.push({ ...this.dml, type: "insert", data });
|
|
976
|
+
return void 0;
|
|
977
|
+
}
|
|
978
|
+
if (this.triggers.length === 0 && this.computedFields.length === 0) {
|
|
979
|
+
const dml = { ...this.dml, type: "insert", data };
|
|
980
|
+
const response = await this.engine.executeDml(dml, this.txId ?? void 0);
|
|
981
|
+
if (response.status != 200) {
|
|
982
|
+
returnFormattedError(response.status, response.message);
|
|
983
|
+
throw new Error(String(response.message));
|
|
984
|
+
}
|
|
985
|
+
return response.data ?? data;
|
|
986
|
+
}
|
|
987
|
+
const clone = this.clone();
|
|
904
988
|
clone.dml.type = "insert";
|
|
905
989
|
clone.dml.data = data;
|
|
906
990
|
const result = await clone.getResponse(clone.dml, "Add");
|
|
@@ -919,13 +1003,26 @@ var Table = class _Table {
|
|
|
919
1003
|
* console.log(result); // { affectedRows: 1 }
|
|
920
1004
|
*/
|
|
921
1005
|
async update(data) {
|
|
922
|
-
const clone = this.clone();
|
|
923
1006
|
if (typeof data !== "object" || Array.isArray(data)) {
|
|
924
1007
|
throw new Error("The update method requires an object with key-value pairs.");
|
|
925
1008
|
}
|
|
926
|
-
if (
|
|
1009
|
+
if (this.dml.where.length === 0) {
|
|
927
1010
|
throw new Error("You must specify at least one WHERE condition to perform an update.");
|
|
928
1011
|
}
|
|
1012
|
+
if (this._batchSink) {
|
|
1013
|
+
this._batchSink.push({ ...this.dml, type: "update", data });
|
|
1014
|
+
return void 0;
|
|
1015
|
+
}
|
|
1016
|
+
if (this.triggers.length === 0 && this.computedFields.length === 0) {
|
|
1017
|
+
const dml = { ...this.dml, type: "update", data };
|
|
1018
|
+
const response = await this.engine.executeDml(dml, this.txId ?? void 0);
|
|
1019
|
+
if (response.status != 200) {
|
|
1020
|
+
returnFormattedError(response.status, response.message);
|
|
1021
|
+
throw new Error(String(response.message));
|
|
1022
|
+
}
|
|
1023
|
+
return response.data;
|
|
1024
|
+
}
|
|
1025
|
+
const clone = this.clone();
|
|
929
1026
|
clone.dml.type = "update";
|
|
930
1027
|
clone.dml.data = data;
|
|
931
1028
|
return clone.getResponse(clone.dml, "Update");
|
|
@@ -940,10 +1037,23 @@ var Table = class _Table {
|
|
|
940
1037
|
* console.log(result); // { affectedRows: 1 }
|
|
941
1038
|
*/
|
|
942
1039
|
async delete() {
|
|
943
|
-
|
|
944
|
-
if (clone.dml.where.length === 0) {
|
|
1040
|
+
if (this.dml.where.length === 0) {
|
|
945
1041
|
throw new Error("You must specify at least one WHERE condition to perform a delete.");
|
|
946
1042
|
}
|
|
1043
|
+
if (this._batchSink) {
|
|
1044
|
+
this._batchSink.push({ ...this.dml, type: "delete", data: null });
|
|
1045
|
+
return void 0;
|
|
1046
|
+
}
|
|
1047
|
+
if (this.triggers.length === 0 && this.computedFields.length === 0) {
|
|
1048
|
+
const dml = { ...this.dml, type: "delete", data: null };
|
|
1049
|
+
const response = await this.engine.executeDml(dml, this.txId ?? void 0);
|
|
1050
|
+
if (response.status != 200) {
|
|
1051
|
+
returnFormattedError(response.status, response.message);
|
|
1052
|
+
throw new Error(String(response.message));
|
|
1053
|
+
}
|
|
1054
|
+
return response.data;
|
|
1055
|
+
}
|
|
1056
|
+
const clone = this.clone();
|
|
947
1057
|
clone.dml.type = "delete";
|
|
948
1058
|
const deleteData = await clone.getResponse(clone.dml, "Delete");
|
|
949
1059
|
return deleteData;
|
|
@@ -1096,12 +1206,26 @@ var Table = class _Table {
|
|
|
1096
1206
|
* await db.table('posts').where('id', '=', 1).increment('views', 1, { last_viewed_at: new Date().toISOString() });
|
|
1097
1207
|
*/
|
|
1098
1208
|
async increment(column, amount = 1, extra = {}) {
|
|
1099
|
-
|
|
1100
|
-
if (clone.dml.where.length === 0) {
|
|
1209
|
+
if (this.dml.where.length === 0) {
|
|
1101
1210
|
throw new Error("You must specify at least one WHERE condition to perform an increment.");
|
|
1102
1211
|
}
|
|
1212
|
+
const data = { ...extra, [column]: { $inc: Number(amount) } };
|
|
1213
|
+
if (this._batchSink) {
|
|
1214
|
+
this._batchSink.push({ ...this.dml, type: "update", data });
|
|
1215
|
+
return void 0;
|
|
1216
|
+
}
|
|
1217
|
+
if (this.triggers.length === 0 && this.computedFields.length === 0) {
|
|
1218
|
+
const dml = { ...this.dml, type: "update", data };
|
|
1219
|
+
const response = await this.engine.executeDml(dml, this.txId ?? void 0);
|
|
1220
|
+
if (response.status != 200) {
|
|
1221
|
+
returnFormattedError(response.status, response.message);
|
|
1222
|
+
throw new Error(String(response.message));
|
|
1223
|
+
}
|
|
1224
|
+
return response.data;
|
|
1225
|
+
}
|
|
1226
|
+
const clone = this.clone();
|
|
1103
1227
|
clone.dml.type = "update";
|
|
1104
|
-
clone.dml.data =
|
|
1228
|
+
clone.dml.data = data;
|
|
1105
1229
|
return clone.getResponse();
|
|
1106
1230
|
}
|
|
1107
1231
|
/**
|
|
@@ -1284,6 +1408,7 @@ var Table = class _Table {
|
|
|
1284
1408
|
const localDML = dml ? dml : this.dml;
|
|
1285
1409
|
const computedFieldsNeeded = [];
|
|
1286
1410
|
let dependeciesArrray = [];
|
|
1411
|
+
const requestedColumns = new Set(localDML.columns ?? []);
|
|
1287
1412
|
if (this.computedFields.length > 0) {
|
|
1288
1413
|
let columns = localDML.columns;
|
|
1289
1414
|
for (const field of localDML.columns) {
|
|
@@ -1300,8 +1425,8 @@ var Table = class _Table {
|
|
|
1300
1425
|
}
|
|
1301
1426
|
let arrayResult = [];
|
|
1302
1427
|
if (type) {
|
|
1303
|
-
const beffore = this.trigger.get("before" + type);
|
|
1304
|
-
const after = this.trigger.get("after" + type);
|
|
1428
|
+
const beffore = this.trigger ? this.trigger.get("before" + type) : void 0;
|
|
1429
|
+
const after = this.trigger ? this.trigger.get("after" + type) : void 0;
|
|
1305
1430
|
if (this.triggers.length > 0 && (beffore || after)) {
|
|
1306
1431
|
const dataset = localDML.data;
|
|
1307
1432
|
for (let index = 0; index < dataset.length; index++) {
|
|
@@ -1313,6 +1438,7 @@ var Table = class _Table {
|
|
|
1313
1438
|
if (response.status != 200) {
|
|
1314
1439
|
interceptor?.discard();
|
|
1315
1440
|
returnFormattedError(response.status, response.message);
|
|
1441
|
+
throw new Error(String(response.message));
|
|
1316
1442
|
}
|
|
1317
1443
|
await interceptor?.commit();
|
|
1318
1444
|
arrayResult = response.data;
|
|
@@ -1321,6 +1447,7 @@ var Table = class _Table {
|
|
|
1321
1447
|
const response = await this.engine.executeDml(newDML, this.txId ?? void 0);
|
|
1322
1448
|
if (response.status != 200) {
|
|
1323
1449
|
returnFormattedError(response.status, response.message);
|
|
1450
|
+
throw new Error(String(response.message));
|
|
1324
1451
|
}
|
|
1325
1452
|
const interceptor = await this.trigger.execute("after" + type, data);
|
|
1326
1453
|
await interceptor?.commit();
|
|
@@ -1330,6 +1457,7 @@ var Table = class _Table {
|
|
|
1330
1457
|
const response = await this.engine.executeDml(localDML, this.txId ?? void 0);
|
|
1331
1458
|
if (response.status != 200) {
|
|
1332
1459
|
returnFormattedError(response.status, response.message);
|
|
1460
|
+
throw new Error(String(response.message));
|
|
1333
1461
|
}
|
|
1334
1462
|
arrayResult = response.data;
|
|
1335
1463
|
}
|
|
@@ -1337,14 +1465,16 @@ var Table = class _Table {
|
|
|
1337
1465
|
const response = await this.engine.executeDml(localDML, this.txId ?? void 0);
|
|
1338
1466
|
if (response.status != 200) {
|
|
1339
1467
|
returnFormattedError(response.status, response.message);
|
|
1468
|
+
throw new Error(String(response.message));
|
|
1340
1469
|
}
|
|
1341
1470
|
arrayResult = response.data;
|
|
1342
1471
|
}
|
|
1343
1472
|
if (computedFieldsNeeded.length > 0) {
|
|
1344
1473
|
let newDataset = import_core2.ComputedFieldProcessor.computedFields(arrayResult, computedFieldsNeeded);
|
|
1474
|
+
const toRemove = dependeciesArrray.filter((key) => !requestedColumns.has(key));
|
|
1345
1475
|
const result = newDataset.map((obj) => {
|
|
1346
1476
|
const newObj = { ...obj };
|
|
1347
|
-
|
|
1477
|
+
toRemove.forEach((key) => delete newObj[key]);
|
|
1348
1478
|
return newObj;
|
|
1349
1479
|
});
|
|
1350
1480
|
return result;
|
|
@@ -1356,6 +1486,7 @@ var Table = class _Table {
|
|
|
1356
1486
|
const cloned = Object.create(Object.getPrototypeOf(this));
|
|
1357
1487
|
cloned.engine = this.engine;
|
|
1358
1488
|
cloned.instance = this.instance;
|
|
1489
|
+
cloned._batchSink = this._batchSink;
|
|
1359
1490
|
cloned.nextType = this.nextType;
|
|
1360
1491
|
cloned.computedFields = this.computedFields;
|
|
1361
1492
|
cloned.trigger = this.trigger;
|