neopg 2.0.3 → 2.0.5

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
@@ -2,6 +2,7 @@
2
2
 
3
3
  const makeId = require('./makeId.js')
4
4
  const makeTimestamp = require('./makeTimestamp.js')
5
+ const serialId = makeId.serialId
5
6
 
6
7
  // 提取常量定义
7
8
  const INT_TYPES = new Set([
@@ -16,12 +17,10 @@ const FLOAT_TYPES = new Set([
16
17
  ])
17
18
 
18
19
  class ModelChain {
19
- constructor(ctx, def, schema = 'public') {
20
+ constructor(ctx, def, schema='public') {
20
21
  this.ctx = ctx
21
22
  this.def = def
22
- this.sql = ctx.sql
23
-
24
- this.tableName = def.tableName
23
+ this.sql = ctx ? ctx.sql : null
25
24
  this.schema = schema
26
25
 
27
26
  // --- 查询状态 ---
@@ -35,7 +34,7 @@ class ModelChain {
35
34
  this._group = []
36
35
  this._lock = null
37
36
 
38
- this._isRaw = !!def.isRaw
37
+ this._isRaw = false
39
38
  this._executed = false
40
39
  }
41
40
 
@@ -50,7 +49,7 @@ class ModelChain {
50
49
  _ensureActive() {
51
50
  if (this._executed) {
52
51
  throw new Error(
53
- `[NeoPG] ModelChain for '${this.tableName}' has already been executed. ` +
52
+ `[NeoPG] ModelChain for '${this.def.tableName}' has already been executed. ` +
54
53
  `Do NOT reuse the chain variable. Use .clone() if you need to fork queries.`
55
54
  )
56
55
  }
@@ -266,7 +265,7 @@ class ModelChain {
266
265
  }
267
266
 
268
267
  _buildSelectQuery() {
269
- const t = this.sql(this.tableName)
268
+ const t = this.sql(this.def.tableName)
270
269
  const c = this._columns ? this.sql(this._columns) : this.sql`*`
271
270
 
272
271
  const w = this._buildWhere()
@@ -310,7 +309,7 @@ class ModelChain {
310
309
  const dataQuery = this._buildSelectQuery()
311
310
 
312
311
  // 2. 总数查询
313
- const t = this.sql(this.tableName)
312
+ const t = this.sql(this.def.tableName)
314
313
  const w = this._buildWhere()
315
314
  const j = this._buildJoins()
316
315
  const g = this._buildGroup()
@@ -338,7 +337,7 @@ class ModelChain {
338
337
  async count() {
339
338
  this._ensureActive()
340
339
  try {
341
- const t = this.sql(this.tableName)
340
+ const t = this.sql(this.def.tableName)
342
341
  const w = this._buildWhere()
343
342
  const j = this._buildJoins()
344
343
  const g = this._buildGroup()
@@ -366,11 +365,11 @@ class ModelChain {
366
365
  const inputs = isArray ? data : [data]
367
366
  if (inputs.length === 0) throw new Error('[NeoPG] Insert data cannot be empty')
368
367
 
369
- if (this.def) {
368
+ if (!this._isRaw) {
370
369
  this._prepareDataForInsert(inputs)
371
370
  }
372
371
 
373
- const fullTable = this.sql`${this.sql(this.schema)}.${this.sql(this.tableName)}`
372
+ const fullTable = this.sql`${this.sql(this.schema)}.${this.sql(this.def.tableName)}`
374
373
  const retFragment = this._buildReturning()
375
374
 
376
375
  const result = await this.sql`INSERT INTO ${fullTable} ${this.sql(inputs)} ${retFragment}`
@@ -389,10 +388,14 @@ class ModelChain {
389
388
  this._ensureActive()
390
389
  try {
391
390
  if (!data || Object.keys(data).length === 0) throw new Error('[NeoPG] Update data cannot be empty')
392
- if (this.def) { this._prepareDataForUpdate(data) }
391
+
392
+ if (!this._isRaw) {
393
+ this._prepareDataForUpdate(data)
394
+ }
395
+
393
396
  if (this._conditions.length === 0) throw new Error('[NeoPG] UPDATE requires a WHERE condition')
394
397
 
395
- const fullTable = this.sql`${this.sql(this.schema)}.${this.sql(this.tableName)}`
398
+ const fullTable = this.sql`${this.sql(this.schema)}.${this.sql(this.def.tableName)}`
396
399
  const whereFragment = this._buildWhere()
397
400
  const retFragment = this._buildReturning()
398
401
 
@@ -412,7 +415,7 @@ class ModelChain {
412
415
  this._ensureActive()
413
416
  try {
414
417
  if (this._conditions.length === 0) throw new Error('[NeoPG] DELETE requires a WHERE condition')
415
- const fullTable = this.sql`${this.sql(this.schema)}.${this.sql(this.tableName)}`
418
+ const fullTable = this.sql`${this.sql(this.schema)}.${this.sql(this.def.tableName)}`
416
419
  const whereFragment = this._buildWhere()
417
420
  const retFragment = this._buildReturning()
418
421
 
@@ -432,7 +435,7 @@ class ModelChain {
432
435
  try {
433
436
  if (!field) throw new Error(`[NeoPG] ${func} requires a field name.`)
434
437
 
435
- const t = this.sql(this.tableName)
438
+ const t = this.sql(this.def.tableName)
436
439
  const w = this._buildWhere()
437
440
  const j = this._buildJoins()
438
441
  const ft = this.sql`${this.sql(this.schema)}.${t}`
@@ -524,9 +527,11 @@ class ModelChain {
524
527
  if (autoId && row[pk] === undefined) {
525
528
  row[pk] = this.def.makeId(pkLen)
526
529
  }
530
+
527
531
  if (make_timestamp) {
528
532
  for (const t of ts.insert) makeTimestamp(row, t)
529
533
  }
534
+
530
535
  for (const key in row) {
531
536
  this.def.validateField(key, row[key])
532
537
  }
@@ -538,10 +543,15 @@ class ModelChain {
538
543
  if (ts.update && ts.update.length > 0) {
539
544
  for (const t of ts.update) makeTimestamp(row, t)
540
545
  }
546
+
541
547
  for (const key in row) {
542
548
  this.def.validateField(key, row[key])
543
549
  }
544
550
  }
551
+
552
+ makeId(len=16) {
553
+ return this.def ? this.def.makeId(this.def.pkLen) : serialId(len)
554
+ }
545
555
  }
546
556
 
547
557
  module.exports = ModelChain
package/lib/NeoPG.js CHANGED
@@ -35,7 +35,9 @@ class NeoPG {
35
35
 
36
36
  table(tableName, schema = null) {
37
37
  const target = schema || this.defaultSchema
38
- return new this.ModelChain(this, {tableName, isRaw: true}, target)
38
+ let m = new this.ModelChain(this, {tableName, isRaw: true}, target)
39
+ m._isRaw = true
40
+ return m
39
41
  }
40
42
 
41
43
  model(name, schema = null) {
@@ -43,7 +45,16 @@ class NeoPG {
43
45
  if (!item) throw new Error(`[NeoPG] Model '${name}' not found.`)
44
46
 
45
47
  const target = schema || this.defaultSchema
46
- return new item.Class(this, item.def, target)
48
+ let m = new item.Class(this, item.def, target)
49
+
50
+ if (!m.def) {
51
+ m.ctx = this
52
+ m.sql = this.sql
53
+ m.def = item.def
54
+ m.schema = target
55
+ }
56
+
57
+ return m
47
58
  }
48
59
 
49
60
  // --- 注册 ---
@@ -80,7 +91,6 @@ class NeoPG {
80
91
  // 此时说明modelName无法确定,但是tableName也没有指定
81
92
  throw new Error(`\x1b[31m[NeoPG] Missing modelName and tableName\x1b[0m`)
82
93
  }
83
- // 3. 如果依然无法确定,稍后 ModelDef 可能会报错,或者你可以选择在这里抛出
84
94
  }
85
95
 
86
96
  //经过以上处理,modelName已经确定了,此时若没有指定tableName则把modelName转换为小写作为tableName
@@ -144,7 +154,7 @@ class NeoPG {
144
154
  let model = this.registry.get(options.model)
145
155
 
146
156
  if (!model) {
147
- throw new Error(`[NeoPG] sync: ${modelname} not found.`)
157
+ throw new Error(`[NeoPG] sync: ${options.model} not found.`)
148
158
  }
149
159
 
150
160
  return await SchemaSync.execute(this.driver, model.def, this, options)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neopg",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "description": "orm for postgres",
5
5
  "keywords": [
6
6
  "neopg",
package/test/test-db.js CHANGED
@@ -162,6 +162,10 @@ db.add(User)
162
162
  }
163
163
  }
164
164
  }
165
+
166
+ constructor() {
167
+ super()
168
+ }
165
169
  }
166
170
 
167
171
  db.define(ShopOrder)
@@ -190,6 +194,23 @@ db.add(User)
190
194
 
191
195
  db.sync({force: true, debug: true, model: 'ShopOrder'})
192
196
 
197
+ await db.model('ShopOrder').where('1=1').delete()
198
+ await db.model('ShopOrder').insert([
199
+ {
200
+ name: 'topbit',
201
+ order_no: Math.random().toString(16)
202
+ },
203
+
204
+ {
205
+ name: 'neopg',
206
+ order_no: Math.random().toString(16)
207
+ }
208
+ ])
209
+
210
+ console.log('get shoporder...\n',
211
+ await db.model('ShopOrder').where('1=1').find()
212
+ )
213
+
193
214
  await db.model('User').where('1=1').delete()
194
215
 
195
216
  try {