neopg 2.0.3 → 2.0.4
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 +20 -15
- package/lib/NeoPG.js +14 -4
- package/package.json +1 -1
- package/test/test-db.js +21 -0
package/lib/ModelChain.js
CHANGED
|
@@ -16,12 +16,10 @@ const FLOAT_TYPES = new Set([
|
|
|
16
16
|
])
|
|
17
17
|
|
|
18
18
|
class ModelChain {
|
|
19
|
-
constructor(ctx, def, schema
|
|
19
|
+
constructor(ctx, def, schema='public') {
|
|
20
20
|
this.ctx = ctx
|
|
21
21
|
this.def = def
|
|
22
|
-
this.sql = ctx.sql
|
|
23
|
-
|
|
24
|
-
this.tableName = def.tableName
|
|
22
|
+
this.sql = ctx ? ctx.sql : null
|
|
25
23
|
this.schema = schema
|
|
26
24
|
|
|
27
25
|
// --- 查询状态 ---
|
|
@@ -35,7 +33,7 @@ class ModelChain {
|
|
|
35
33
|
this._group = []
|
|
36
34
|
this._lock = null
|
|
37
35
|
|
|
38
|
-
this._isRaw =
|
|
36
|
+
this._isRaw = false
|
|
39
37
|
this._executed = false
|
|
40
38
|
}
|
|
41
39
|
|
|
@@ -50,7 +48,7 @@ class ModelChain {
|
|
|
50
48
|
_ensureActive() {
|
|
51
49
|
if (this._executed) {
|
|
52
50
|
throw new Error(
|
|
53
|
-
`[NeoPG] ModelChain for '${this.tableName}' has already been executed. ` +
|
|
51
|
+
`[NeoPG] ModelChain for '${this.def.tableName}' has already been executed. ` +
|
|
54
52
|
`Do NOT reuse the chain variable. Use .clone() if you need to fork queries.`
|
|
55
53
|
)
|
|
56
54
|
}
|
|
@@ -266,7 +264,7 @@ class ModelChain {
|
|
|
266
264
|
}
|
|
267
265
|
|
|
268
266
|
_buildSelectQuery() {
|
|
269
|
-
const t = this.sql(this.tableName)
|
|
267
|
+
const t = this.sql(this.def.tableName)
|
|
270
268
|
const c = this._columns ? this.sql(this._columns) : this.sql`*`
|
|
271
269
|
|
|
272
270
|
const w = this._buildWhere()
|
|
@@ -310,7 +308,7 @@ class ModelChain {
|
|
|
310
308
|
const dataQuery = this._buildSelectQuery()
|
|
311
309
|
|
|
312
310
|
// 2. 总数查询
|
|
313
|
-
const t = this.sql(this.tableName)
|
|
311
|
+
const t = this.sql(this.def.tableName)
|
|
314
312
|
const w = this._buildWhere()
|
|
315
313
|
const j = this._buildJoins()
|
|
316
314
|
const g = this._buildGroup()
|
|
@@ -338,7 +336,7 @@ class ModelChain {
|
|
|
338
336
|
async count() {
|
|
339
337
|
this._ensureActive()
|
|
340
338
|
try {
|
|
341
|
-
const t = this.sql(this.tableName)
|
|
339
|
+
const t = this.sql(this.def.tableName)
|
|
342
340
|
const w = this._buildWhere()
|
|
343
341
|
const j = this._buildJoins()
|
|
344
342
|
const g = this._buildGroup()
|
|
@@ -366,11 +364,11 @@ class ModelChain {
|
|
|
366
364
|
const inputs = isArray ? data : [data]
|
|
367
365
|
if (inputs.length === 0) throw new Error('[NeoPG] Insert data cannot be empty')
|
|
368
366
|
|
|
369
|
-
if (this.
|
|
367
|
+
if (!this._isRaw) {
|
|
370
368
|
this._prepareDataForInsert(inputs)
|
|
371
369
|
}
|
|
372
370
|
|
|
373
|
-
const fullTable = this.sql`${this.sql(this.schema)}.${this.sql(this.tableName)}`
|
|
371
|
+
const fullTable = this.sql`${this.sql(this.schema)}.${this.sql(this.def.tableName)}`
|
|
374
372
|
const retFragment = this._buildReturning()
|
|
375
373
|
|
|
376
374
|
const result = await this.sql`INSERT INTO ${fullTable} ${this.sql(inputs)} ${retFragment}`
|
|
@@ -389,10 +387,14 @@ class ModelChain {
|
|
|
389
387
|
this._ensureActive()
|
|
390
388
|
try {
|
|
391
389
|
if (!data || Object.keys(data).length === 0) throw new Error('[NeoPG] Update data cannot be empty')
|
|
392
|
-
|
|
390
|
+
|
|
391
|
+
if (!this._isRaw) {
|
|
392
|
+
this._prepareDataForUpdate(data)
|
|
393
|
+
}
|
|
394
|
+
|
|
393
395
|
if (this._conditions.length === 0) throw new Error('[NeoPG] UPDATE requires a WHERE condition')
|
|
394
396
|
|
|
395
|
-
const fullTable = this.sql`${this.sql(this.schema)}.${this.sql(this.tableName)}`
|
|
397
|
+
const fullTable = this.sql`${this.sql(this.schema)}.${this.sql(this.def.tableName)}`
|
|
396
398
|
const whereFragment = this._buildWhere()
|
|
397
399
|
const retFragment = this._buildReturning()
|
|
398
400
|
|
|
@@ -412,7 +414,7 @@ class ModelChain {
|
|
|
412
414
|
this._ensureActive()
|
|
413
415
|
try {
|
|
414
416
|
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)}`
|
|
417
|
+
const fullTable = this.sql`${this.sql(this.schema)}.${this.sql(this.def.tableName)}`
|
|
416
418
|
const whereFragment = this._buildWhere()
|
|
417
419
|
const retFragment = this._buildReturning()
|
|
418
420
|
|
|
@@ -432,7 +434,7 @@ class ModelChain {
|
|
|
432
434
|
try {
|
|
433
435
|
if (!field) throw new Error(`[NeoPG] ${func} requires a field name.`)
|
|
434
436
|
|
|
435
|
-
const t = this.sql(this.tableName)
|
|
437
|
+
const t = this.sql(this.def.tableName)
|
|
436
438
|
const w = this._buildWhere()
|
|
437
439
|
const j = this._buildJoins()
|
|
438
440
|
const ft = this.sql`${this.sql(this.schema)}.${t}`
|
|
@@ -524,9 +526,11 @@ class ModelChain {
|
|
|
524
526
|
if (autoId && row[pk] === undefined) {
|
|
525
527
|
row[pk] = this.def.makeId(pkLen)
|
|
526
528
|
}
|
|
529
|
+
|
|
527
530
|
if (make_timestamp) {
|
|
528
531
|
for (const t of ts.insert) makeTimestamp(row, t)
|
|
529
532
|
}
|
|
533
|
+
|
|
530
534
|
for (const key in row) {
|
|
531
535
|
this.def.validateField(key, row[key])
|
|
532
536
|
}
|
|
@@ -538,6 +542,7 @@ class ModelChain {
|
|
|
538
542
|
if (ts.update && ts.update.length > 0) {
|
|
539
543
|
for (const t of ts.update) makeTimestamp(row, t)
|
|
540
544
|
}
|
|
545
|
+
|
|
541
546
|
for (const key in row) {
|
|
542
547
|
this.def.validateField(key, row[key])
|
|
543
548
|
}
|
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
|
-
|
|
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
|
-
|
|
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: ${
|
|
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
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 {
|