jsql-neo 3.4.2 → 3.4.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/lib/table.js +65 -2
- package/package.json +1 -1
package/lib/table.js
CHANGED
|
@@ -303,7 +303,39 @@ class Table {
|
|
|
303
303
|
|
|
304
304
|
updateById(id, updates) {
|
|
305
305
|
if (!this._primaryKey) throw createError('ER_PARSE_ERROR', 'Table has no primary key');
|
|
306
|
-
|
|
306
|
+
for (const hook of this._hooks.beforeUpdate) {
|
|
307
|
+
updates = hook({ [this._primaryKey]: id }, { ...updates }) || updates;
|
|
308
|
+
}
|
|
309
|
+
updates = this._validateDataTypes(updates, true);
|
|
310
|
+
let rowIndex;
|
|
311
|
+
if (this._pkIndex) {
|
|
312
|
+
rowIndex = this._pkIndex.get(id);
|
|
313
|
+
} else {
|
|
314
|
+
this._ensureBTrees();
|
|
315
|
+
if (this._btrees[this._primaryKey]) {
|
|
316
|
+
const indices = this._btrees[this._primaryKey].search(id);
|
|
317
|
+
rowIndex = indices.length > 0 ? indices[0] : undefined;
|
|
318
|
+
} else {
|
|
319
|
+
rowIndex = this._rows.findIndex(r => r[this._primaryKey] === id);
|
|
320
|
+
if (rowIndex === -1) rowIndex = undefined;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if (rowIndex === undefined) return 0;
|
|
324
|
+
const row = this._rows[rowIndex];
|
|
325
|
+
if (row && row._deleted) return 0;
|
|
326
|
+
const oldData = { ...row };
|
|
327
|
+
const merged = { ...row, ...updates };
|
|
328
|
+
this._validateConstraints(merged, rowIndex);
|
|
329
|
+
this._removeFromIndexes(rowIndex, oldData);
|
|
330
|
+
Object.assign(row, updates);
|
|
331
|
+
this._addToIndexes(rowIndex, row);
|
|
332
|
+
this._dirty = true;
|
|
333
|
+
this._db._markDirty();
|
|
334
|
+
for (const hook of this._hooks.afterUpdate) {
|
|
335
|
+
hook({ [this._primaryKey]: id }, updates, 1);
|
|
336
|
+
}
|
|
337
|
+
this._db._emitChange('update', this._name, { query: { [this._primaryKey]: id }, updates, count: 1 });
|
|
338
|
+
return 1;
|
|
307
339
|
}
|
|
308
340
|
|
|
309
341
|
// ============================================================
|
|
@@ -353,7 +385,38 @@ class Table {
|
|
|
353
385
|
|
|
354
386
|
removeById(id) {
|
|
355
387
|
if (!this._primaryKey) throw createError('ER_PARSE_ERROR', 'Table has no primary key');
|
|
356
|
-
|
|
388
|
+
let rowIndex;
|
|
389
|
+
if (this._pkIndex) {
|
|
390
|
+
rowIndex = this._pkIndex.get(id);
|
|
391
|
+
} else {
|
|
392
|
+
this._ensureBTrees();
|
|
393
|
+
if (this._btrees[this._primaryKey]) {
|
|
394
|
+
const indices = this._btrees[this._primaryKey].search(id);
|
|
395
|
+
rowIndex = indices.length > 0 ? indices[0] : undefined;
|
|
396
|
+
} else {
|
|
397
|
+
rowIndex = this._rows.findIndex(r => r[this._primaryKey] === id);
|
|
398
|
+
if (rowIndex === -1) rowIndex = undefined;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
if (rowIndex === undefined) return 0;
|
|
402
|
+
const row = this._rows[rowIndex];
|
|
403
|
+
if (!row || row._deleted) return 0;
|
|
404
|
+
this._cascadeForeignKeys(row);
|
|
405
|
+
if (this._softDelete) {
|
|
406
|
+
row._deleted = true;
|
|
407
|
+
row._deletedAt = Date.now();
|
|
408
|
+
} else {
|
|
409
|
+
this._removeFromIndexes(rowIndex, row);
|
|
410
|
+
this._rows.splice(rowIndex, 1);
|
|
411
|
+
this._adjustBTreeAfterRemove(rowIndex);
|
|
412
|
+
}
|
|
413
|
+
this._dirty = true;
|
|
414
|
+
this._db._markDirty();
|
|
415
|
+
for (const hook of this._hooks.afterRemove) {
|
|
416
|
+
hook({ [this._primaryKey]: id }, 1);
|
|
417
|
+
}
|
|
418
|
+
this._db._emitChange('remove', this._name, { query: { [this._primaryKey]: id }, count: 1 });
|
|
419
|
+
return 1;
|
|
357
420
|
}
|
|
358
421
|
|
|
359
422
|
removeSoft(query = {}) {
|