neopg 2.1.0 → 2.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/ModelChain.js CHANGED
@@ -49,8 +49,13 @@ class ModelChain {
49
49
  _ensureActive() {
50
50
  if (this._executed) {
51
51
  throw new Error(
52
- `[NeoPG] ModelChain for '${this.def.tableName}' has already been executed. ` +
53
- `Do NOT reuse the chain variable. Use .clone() if you need to fork queries.`
52
+ `[NeoPG] Security Error: This query chain for \x1b[36m'${this.def.tableName}'\x1b[0m has already been executed to prevent state pollution.\n` +
53
+ `❌ Do NOT reuse the same variable for multiple executions.\n` +
54
+ `✅ If you need to execute multiple queries based on the same conditions, use .clone() before execution.\n\x1b[35m` +
55
+ ` Example: \n` +
56
+ ` const q = model.where(...);\n` +
57
+ ` const data = await q.clone().find();\n` +
58
+ ` await q.clone().update(...);\x1b[0m`
54
59
  )
55
60
  }
56
61
  }
package/lib/SchemaSync.js CHANGED
@@ -299,7 +299,7 @@ class SchemaSync {
299
299
  continue;
300
300
  }
301
301
 
302
- const idxCols = indname.split(',').map(s=>s.trim()).filter(s=>s);
302
+ let idxCols = Array.isArray(indname) ? indname : indname.split(',').map(s=>s.trim()).filter(s=>s);
303
303
  const idxNamePart = idxCols.join('_');
304
304
  const targetIdxName = `${def.tableName}_${idxNamePart}_idx`;
305
305
 
@@ -329,7 +329,7 @@ class SchemaSync {
329
329
  for (const indname of uniques) {
330
330
  if (!this._checkColumnsExist(indname, def)) continue;
331
331
 
332
- const idxCols = indname.split(',').map(s=>s.trim()).filter(s=>s);
332
+ let idxCols = Array.isArray(indname) ? indname : indname.split(',').map(s=>s.trim()).filter(s=>s);
333
333
 
334
334
  // 监测是否等于主键
335
335
  if (pkSet.size > 0 && idxCols.length === pkSet.size) {
@@ -372,7 +372,7 @@ class SchemaSync {
372
372
  const uniques = def.unique || [];
373
373
 
374
374
  const keepSet = new Set();
375
- const makeName = (n) => `${tableName}_${n.split(',').map(x=>x.trim()).filter(x=>x).join('_')}_idx`;
375
+ const makeName = (n) => `${tableName}_${Array.isArray(n) ? n.map(x=>x.trim()).join('_') : n.split(',').map(x=>x.trim()).filter(x=>x).join('_')}_idx`;
376
376
 
377
377
  indices.forEach(n => keepSet.add(makeName(n)));
378
378
  uniques.forEach(n => keepSet.add(makeName(n)));
@@ -483,7 +483,13 @@ class SchemaSync {
483
483
  }
484
484
 
485
485
  static _checkColumnsExist(colsStr, def) {
486
- const parts = colsStr.split(',').map(x=>x.trim()).filter(x=>x);
486
+ let parts;
487
+ if (Array.isArray(colsStr)) {
488
+ parts = colsStr;
489
+ } else {
490
+ parts = colsStr.split(',').map(x=>x.trim()).filter(x=>x);
491
+ }
492
+
487
493
  for (const p of parts) {
488
494
  if (!def.columns[p]) return false;
489
495
  }
@@ -501,4 +507,4 @@ class SchemaSync {
501
507
  }
502
508
  }
503
509
 
504
- module.exports = SchemaSync
510
+ module.exports = SchemaSync
@@ -9,14 +9,24 @@ class TransactionScope {
9
9
 
10
10
  table(tableName, schema = null) {
11
11
  const target = schema || this.parent.defaultSchema
12
- return new this.parent.ModelChain(this, {tableName, isRaw: true}, target)
12
+ let m = new this.parent.ModelChain(this, {tableName, isRaw: true}, target)
13
+ m._isRaw = true
14
+ return m
13
15
  }
14
16
 
15
17
  model(name, schema = null) {
16
18
  const item = this.parent.registry.get(name)
17
19
  if (!item) throw new Error(`[NeoPG] Model '${name}' not found.`)
18
20
  const target = schema || this.parent.defaultSchema
19
- return new item.Class(this, item.def, target)
21
+ let m = new item.Class(this, item.def, target)
22
+ if (!m.def) {
23
+ m.def = item.def
24
+ m.ctx = this.parent
25
+ m.sql = this.sql
26
+ m.schema = target
27
+ }
28
+
29
+ return m
20
30
  }
21
31
 
22
32
  async transaction(callback) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neopg",
3
- "version": "2.1.0",
3
+ "version": "2.2.1",
4
4
  "description": "orm for postgres",
5
5
  "keywords": [
6
6
  "neopg",
package/test/test-db.js CHANGED
@@ -129,13 +129,16 @@ const User = {
129
129
  index: [
130
130
  'create_time',
131
131
  'level',
132
- 'is_root'
132
+ 'is_root',
133
+ ['level', 'is_root']
133
134
  ],
134
135
 
135
136
  //唯一索引
136
137
  unique: [
137
138
  'username',
138
- 'email', 'id'
139
+ 'email', 'id',
140
+ ['mobile', 'email']
141
+
139
142
  ]
140
143
  }
141
144