neopg 2.3.0 → 2.3.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
@@ -44,6 +44,14 @@ class ModelChain {
44
44
  }
45
45
  }
46
46
 
47
+ get self() {
48
+ if (!this.ctx || !this.def || !this.def.modelName) {
49
+ throw new Error('[NeoPG] Cannot use .self on unregistered or raw ModelChain')
50
+ }
51
+
52
+ return this.ctx.model(this.def.modelName, this.schema)
53
+ }
54
+
47
55
  // --- 内部状态管理 ---
48
56
 
49
57
  _ensureActive() {
package/lib/NeoPG.js CHANGED
@@ -84,7 +84,7 @@ class NeoPG {
84
84
  } else if (rawSchema.tableName) {
85
85
  rawSchema.modelName = toPascalCase(rawSchema.tableName)
86
86
  setTimeout(() => {
87
- console.error(`\x1b[33;5m[NeoPG]Warning: modelName is not specified, `
87
+ console.error(`\x1b[33;3m[NeoPG]Warning: modelName is not specified, `
88
88
  + `use ${rawSchema.modelName} as the modelName\x1b[0m`)
89
89
  }, 100)
90
90
  } else {
@@ -98,13 +98,13 @@ class NeoPG {
98
98
  rawSchema.tableName = rawSchema.modelName.toLowerCase()
99
99
 
100
100
  setTimeout(() => {
101
- console.error(`\x1b[33;5m[NeoPG]Warning: tableName is not specified, `
101
+ console.error(`\x1b[33;7m[NeoPG]Warning: tableName is not specified, `
102
102
  + `use ${rawSchema.modelName.toLowerCase()} as the tableName\x1b[0m`)
103
103
  }, 100)
104
104
  }
105
105
 
106
106
  if ((/[a-z]/).test(rawSchema.modelName[0])) {
107
- throw new Error(`\x1b[31;5m[NeoPG] ${rawSchema.modelName}: modelName must start with an uppercase letter.\x1b[0m`)
107
+ throw new Error(`\x1b[31;7m[NeoPG] ${rawSchema.modelName}: modelName must start with an uppercase letter.\x1b[0m`)
108
108
  }
109
109
 
110
110
  const def = new ModelDef(rawSchema)
@@ -22,7 +22,8 @@ class TransactionScope {
22
22
  let m = new item.Class(this, item.def, target)
23
23
  if (!m.def) {
24
24
  m.def = item.def
25
- m.ctx = this.parent
25
+ //fix: m.ctx = this.parent
26
+ m.ctx = this
26
27
  m.sql = this.sql
27
28
  m.schema = target
28
29
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neopg",
3
- "version": "2.3.0",
3
+ "version": "2.3.1",
4
4
  "description": "orm for postgres",
5
5
  "keywords": [
6
6
  "neopg",
package/test/test-db.js CHANGED
@@ -300,15 +300,16 @@ db.add(User)
300
300
 
301
301
  console.log(upd_result.count, upd_result.columns, upd_result)
302
302
 
303
+ let User = tx.model('User')
303
304
  let sex = 3
304
305
  console.log(
305
306
  'test condition or',
306
- await tx.model('User').where(tx.sql`(sex = ${sex} or level > 10)`).select(['id', 'level', 'username', 'sex']).find()
307
+ await User.self.where(tx.sql`(sex = ${sex} or level > 10)`).select(['id', 'level', 'username', 'sex']).find()
307
308
  )
308
309
 
309
310
  console.log(
310
311
  'test select *',
311
- await tx.model('User').orderby('level', 'DESC').orderby('id', 'ASC').select().find()
312
+ await User.self.orderby('level', 'DESC').orderby('id', 'ASC').select().find()
312
313
  )
313
314
 
314
315
  console.log(