jsql-neo 1.2.0 → 1.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/lib/table.js +29 -2
- package/package.json +1 -1
package/lib/table.js
CHANGED
|
@@ -118,6 +118,8 @@ class Table {
|
|
|
118
118
|
hook(data);
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
+
this._db._emitChange('insert', this._name, data);
|
|
122
|
+
|
|
121
123
|
return data;
|
|
122
124
|
}
|
|
123
125
|
|
|
@@ -224,6 +226,7 @@ class Table {
|
|
|
224
226
|
for (const hook of this._hooks.afterUpdate) {
|
|
225
227
|
hook(query, updates, count);
|
|
226
228
|
}
|
|
229
|
+
this._db._emitChange('update', this._name, { query, updates, count });
|
|
227
230
|
}
|
|
228
231
|
return count;
|
|
229
232
|
}
|
|
@@ -280,6 +283,7 @@ class Table {
|
|
|
280
283
|
for (const hook of this._hooks.afterRemove) {
|
|
281
284
|
hook(query, count);
|
|
282
285
|
}
|
|
286
|
+
this._db._emitChange('remove', this._name, { query, count });
|
|
283
287
|
}
|
|
284
288
|
return count;
|
|
285
289
|
}
|
|
@@ -338,7 +342,15 @@ class Table {
|
|
|
338
342
|
addColumn(field, definition) {
|
|
339
343
|
if (this._schema[field]) throw new Error(`Column '${field}' already exists`);
|
|
340
344
|
this._schema[field] = definition;
|
|
341
|
-
if (definition.default !== undefined)
|
|
345
|
+
if (definition.default !== undefined) {
|
|
346
|
+
this._defaults[field] = definition.default;
|
|
347
|
+
// 填充现有行的默认值
|
|
348
|
+
for (const row of this._rows) {
|
|
349
|
+
if (row[field] === undefined) {
|
|
350
|
+
row[field] = typeof definition.default === 'function' ? definition.default() : definition.default;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
342
354
|
if (definition.check) this._checkConstraints.push({ field, fn: definition.check });
|
|
343
355
|
if (definition.foreignKey) {
|
|
344
356
|
this._foreignKeys.push({
|
|
@@ -471,6 +483,20 @@ class Table {
|
|
|
471
483
|
|
|
472
484
|
_matchRow(row, query) {
|
|
473
485
|
for (const [field, condition] of Object.entries(query)) {
|
|
486
|
+
// 顶层 $or / $and / $not
|
|
487
|
+
if (field === '$or') {
|
|
488
|
+
if (!Array.isArray(condition) || !condition.some(sub => this._matchRow(row, sub))) return false;
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
491
|
+
if (field === '$and') {
|
|
492
|
+
if (!Array.isArray(condition) || !condition.every(sub => this._matchRow(row, sub))) return false;
|
|
493
|
+
continue;
|
|
494
|
+
}
|
|
495
|
+
if (field === '$not') {
|
|
496
|
+
if (this._matchRow(row, condition)) return false;
|
|
497
|
+
continue;
|
|
498
|
+
}
|
|
499
|
+
|
|
474
500
|
// JSON 路径: 'profile.city' 或 'data.profile.city'
|
|
475
501
|
const value = this._resolvePath(row, field);
|
|
476
502
|
|
|
@@ -511,7 +537,8 @@ class Table {
|
|
|
511
537
|
case '$nin': return Array.isArray(target) && !target.includes(value);
|
|
512
538
|
case '$like':
|
|
513
539
|
if (typeof value !== 'string' || typeof target !== 'string') return false;
|
|
514
|
-
|
|
540
|
+
const likeRegex = target.replace(/%/g, '.*').replace(/_/g, '.');
|
|
541
|
+
return new RegExp('^' + likeRegex + '$', 'i').test(value);
|
|
515
542
|
case '$regex':
|
|
516
543
|
return new RegExp(target).test(String(value));
|
|
517
544
|
case '$exists':
|