jsql-neo 4.1.2 → 4.2.0-beta.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/database.js +45 -2
- package/lib/table.js +4 -3
- package/package.json +1 -1
package/lib/database.js
CHANGED
|
@@ -92,8 +92,8 @@ class Database {
|
|
|
92
92
|
this._walPath = this._filePath ? this._filePath + '.wal' : null;
|
|
93
93
|
this._walOps = []; // WAL 操作日志
|
|
94
94
|
|
|
95
|
-
//
|
|
96
|
-
this._isolationLevel = options.isolationLevel || '
|
|
95
|
+
// 事务隔离:默认 REPEATABLE_READ(begin 时保存行快照,rollback 可回滚)
|
|
96
|
+
this._isolationLevel = options.isolationLevel || 'REPEATABLE_READ';
|
|
97
97
|
|
|
98
98
|
// JSql 格式 (binary block-based)
|
|
99
99
|
this._jsqlMode = this._filePath && this._filePath.endsWith('.jsql');
|
|
@@ -1286,6 +1286,49 @@ class Database {
|
|
|
1286
1286
|
return { ok: true, count: removed };
|
|
1287
1287
|
}
|
|
1288
1288
|
|
|
1289
|
+
insertMany(tableName, items) {
|
|
1290
|
+
const table = this._ensureTable(tableName);
|
|
1291
|
+
if (!table) throw createError('ER_NO_SUCH_TABLE', tableName);
|
|
1292
|
+
const arr = Array.isArray(items) ? items : [items];
|
|
1293
|
+
if (!this._runHooks('beforeInsert', [tableName, arr])) return [];
|
|
1294
|
+
const inserted = table.insertMany(arr);
|
|
1295
|
+
this._emit('insert', { table: tableName, count: arr.length });
|
|
1296
|
+
this._runHooks('afterInsert', [tableName, arr, inserted]);
|
|
1297
|
+
this._markDirty(tableName);
|
|
1298
|
+
return inserted;
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
update(tableName, query, updates) {
|
|
1302
|
+
const table = this._ensureTable(tableName);
|
|
1303
|
+
if (!table) throw createError('ER_NO_SUCH_TABLE', tableName);
|
|
1304
|
+
if (!this._runHooks('beforeUpdate', [tableName, query, updates])) return 0;
|
|
1305
|
+
const count = table.update(query, updates);
|
|
1306
|
+
this._emit('update', { table: tableName, query, count });
|
|
1307
|
+
this._runHooks('afterUpdate', [tableName, query, updates, count]);
|
|
1308
|
+
this._markDirty(tableName);
|
|
1309
|
+
return count;
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
delete(tableName, query = {}) {
|
|
1313
|
+
const table = this._ensureTable(tableName);
|
|
1314
|
+
if (!table) throw createError('ER_NO_SUCH_TABLE', tableName);
|
|
1315
|
+
if (!this._runHooks('beforeDelete', [tableName, query])) return 0;
|
|
1316
|
+
const count = table.remove(query);
|
|
1317
|
+
this._emit('delete', { table: tableName, query, count });
|
|
1318
|
+
this._runHooks('afterDelete', [tableName, query, count]);
|
|
1319
|
+
this._markDirty(tableName);
|
|
1320
|
+
return count;
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
truncate(tableName) {
|
|
1324
|
+
const table = this._ensureTable(tableName);
|
|
1325
|
+
if (!table) throw createError('ER_NO_SUCH_TABLE', tableName);
|
|
1326
|
+
const count = table.truncate();
|
|
1327
|
+
this._emit('delete', { table: tableName, count });
|
|
1328
|
+
this._markDirty(tableName);
|
|
1329
|
+
return count;
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1289
1332
|
async stop() {
|
|
1290
1333
|
this._runHooks('onStop', []);
|
|
1291
1334
|
this._emit('stop', {});
|
package/lib/table.js
CHANGED
|
@@ -133,10 +133,11 @@ class Table {
|
|
|
133
133
|
this._dateFields = {}; // { fieldName: 'date'|'datetime'|'timestamp' }
|
|
134
134
|
|
|
135
135
|
for (const [field, def] of Object.entries(schema)) {
|
|
136
|
-
|
|
136
|
+
const isPk = !!(def.primaryKey || def.primary === true);
|
|
137
|
+
if (isPk) this._primaryKey = field;
|
|
137
138
|
if (def.autoIncrement) {
|
|
138
139
|
this._autoIncrementField = field;
|
|
139
|
-
if (
|
|
140
|
+
if (isPk) this._primaryKey = field;
|
|
140
141
|
}
|
|
141
142
|
if (def.default !== undefined) {
|
|
142
143
|
this._defaults[field] = def.default;
|
|
@@ -177,7 +178,7 @@ class Table {
|
|
|
177
178
|
_autoCreateBTrees() {
|
|
178
179
|
for (const [field, def] of Object.entries(this._schema)) {
|
|
179
180
|
if (field === '_softDelete') continue;
|
|
180
|
-
if (def.primaryKey || def.unique) {
|
|
181
|
+
if (def.primaryKey || def.primary === true || def.unique) {
|
|
181
182
|
this._btrees[field] = new BTree(64, true);
|
|
182
183
|
}
|
|
183
184
|
}
|