neopg 2.0.8 → 2.0.9
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 +19 -17
- package/package.json +1 -1
- package/test/test-db.js +12 -2
package/lib/ModelChain.js
CHANGED
|
@@ -6,13 +6,13 @@ const serialId = makeId.serialId
|
|
|
6
6
|
|
|
7
7
|
// 提取常量定义
|
|
8
8
|
const INT_TYPES = new Set([
|
|
9
|
-
'int', 'integer', 'smallint', 'bigint',
|
|
9
|
+
'int', 'integer', 'smallint', 'bigint',
|
|
10
10
|
'serial', 'bigserial', 'smallserial',
|
|
11
11
|
'int2', 'int4', 'int8'
|
|
12
12
|
])
|
|
13
13
|
|
|
14
14
|
const FLOAT_TYPES = new Set([
|
|
15
|
-
'float', 'double', 'numeric', 'decimal', 'real',
|
|
15
|
+
'float', 'double', 'numeric', 'decimal', 'real',
|
|
16
16
|
'money', 'double precision', 'float4', 'float8'
|
|
17
17
|
])
|
|
18
18
|
|
|
@@ -65,13 +65,13 @@ class ModelChain {
|
|
|
65
65
|
clone() {
|
|
66
66
|
this._ensureActive()
|
|
67
67
|
const copy = new ModelChain(this.ctx, this.def, this.schema)
|
|
68
|
-
|
|
68
|
+
|
|
69
69
|
// 拷贝状态
|
|
70
70
|
copy._conditions = [...this._conditions]
|
|
71
71
|
copy._joins = [...this._joins]
|
|
72
72
|
copy._group = [...this._group]
|
|
73
73
|
copy._order = [...this._order]
|
|
74
|
-
|
|
74
|
+
|
|
75
75
|
copy._limit = this._limit
|
|
76
76
|
copy._offset = this._offset
|
|
77
77
|
copy._lock = this._lock
|
|
@@ -180,7 +180,7 @@ class ModelChain {
|
|
|
180
180
|
this._conditions.push(arg1)
|
|
181
181
|
return this
|
|
182
182
|
}
|
|
183
|
-
|
|
183
|
+
|
|
184
184
|
if (typeof arg1 === 'object' && !Array.isArray(arg1)) {
|
|
185
185
|
for (const k of Object.keys(arg1)) {
|
|
186
186
|
const v = arg1[k]
|
|
@@ -266,8 +266,10 @@ class ModelChain {
|
|
|
266
266
|
|
|
267
267
|
_buildSelectQuery() {
|
|
268
268
|
const t = this.sql(this.def.tableName)
|
|
269
|
-
const c = this._columns
|
|
270
|
-
|
|
269
|
+
const c = this._columns
|
|
270
|
+
? ((this._columns.length===1 && this._columns[0] === '*') ? this.sql`*` : this.sql(this._columns))
|
|
271
|
+
: this.sql`*`
|
|
272
|
+
|
|
271
273
|
const w = this._buildWhere()
|
|
272
274
|
const o = this._buildOrder()
|
|
273
275
|
const j = this._buildJoins()
|
|
@@ -277,7 +279,7 @@ class ModelChain {
|
|
|
277
279
|
const off = this._offset ? this.sql`OFFSET ${this._offset}` : this.sql``
|
|
278
280
|
const lck = this._lock || this.sql``
|
|
279
281
|
const ft = this.sql`${this.sql(this.schema)}.${t}`
|
|
280
|
-
|
|
282
|
+
|
|
281
283
|
return this.sql`SELECT ${c} FROM ${ft} ${j} ${w} ${g} ${o} ${l} ${off} ${lck}`
|
|
282
284
|
}
|
|
283
285
|
|
|
@@ -314,7 +316,7 @@ class ModelChain {
|
|
|
314
316
|
const j = this._buildJoins()
|
|
315
317
|
const g = this._buildGroup()
|
|
316
318
|
const ft = this.sql`${this.sql(this.schema)}.${t}`
|
|
317
|
-
|
|
319
|
+
|
|
318
320
|
let countPromise
|
|
319
321
|
|
|
320
322
|
if (this._group.length > 0) {
|
|
@@ -342,7 +344,7 @@ class ModelChain {
|
|
|
342
344
|
const j = this._buildJoins()
|
|
343
345
|
const g = this._buildGroup()
|
|
344
346
|
const ft = this.sql`${this.sql(this.schema)}.${t}`
|
|
345
|
-
|
|
347
|
+
|
|
346
348
|
let query;
|
|
347
349
|
if (this._group.length > 0) {
|
|
348
350
|
query = this.sql`SELECT count(*) as total FROM (SELECT 1 FROM ${ft} ${j} ${w} ${g}) as temp`
|
|
@@ -388,17 +390,17 @@ class ModelChain {
|
|
|
388
390
|
this._ensureActive()
|
|
389
391
|
try {
|
|
390
392
|
if (!data || Object.keys(data).length === 0) throw new Error('[NeoPG] Update data cannot be empty')
|
|
391
|
-
|
|
393
|
+
|
|
392
394
|
if (!this._isRaw) {
|
|
393
395
|
this._prepareDataForUpdate(data)
|
|
394
396
|
}
|
|
395
397
|
|
|
396
398
|
if (this._conditions.length === 0) throw new Error('[NeoPG] UPDATE requires a WHERE condition')
|
|
397
|
-
|
|
399
|
+
|
|
398
400
|
const fullTable = this.sql`${this.sql(this.schema)}.${this.sql(this.def.tableName)}`
|
|
399
401
|
const whereFragment = this._buildWhere()
|
|
400
402
|
const retFragment = this._buildReturning()
|
|
401
|
-
|
|
403
|
+
|
|
402
404
|
const result = await this.sql`UPDATE ${fullTable} SET ${this.sql(data)} ${whereFragment} ${retFragment}`
|
|
403
405
|
|
|
404
406
|
if (this._returning && this._returning.length > 0) {
|
|
@@ -418,7 +420,7 @@ class ModelChain {
|
|
|
418
420
|
const fullTable = this.sql`${this.sql(this.schema)}.${this.sql(this.def.tableName)}`
|
|
419
421
|
const whereFragment = this._buildWhere()
|
|
420
422
|
const retFragment = this._buildReturning()
|
|
421
|
-
|
|
423
|
+
|
|
422
424
|
return await this.sql`DELETE FROM ${fullTable} ${whereFragment} ${retFragment}`
|
|
423
425
|
} finally {
|
|
424
426
|
this._destroy()
|
|
@@ -450,9 +452,9 @@ class ModelChain {
|
|
|
450
452
|
} else {
|
|
451
453
|
colFragment = this.sql(field)
|
|
452
454
|
}
|
|
453
|
-
|
|
455
|
+
|
|
454
456
|
const query = this.sql`
|
|
455
|
-
SELECT ${this.sql.unsafe(func)}(${colFragment}) as val
|
|
457
|
+
SELECT ${this.sql.unsafe(func)}(${colFragment}) as val
|
|
456
458
|
FROM ${ft} ${j} ${w}
|
|
457
459
|
`
|
|
458
460
|
|
|
@@ -558,4 +560,4 @@ class ModelChain {
|
|
|
558
560
|
}
|
|
559
561
|
}
|
|
560
562
|
|
|
561
|
-
module.exports = ModelChain
|
|
563
|
+
module.exports = ModelChain
|
package/package.json
CHANGED
package/test/test-db.js
CHANGED
|
@@ -258,7 +258,7 @@ db.add(User)
|
|
|
258
258
|
console.log('update', data)
|
|
259
259
|
|
|
260
260
|
let result = await tx.model('User').where(tx.sql`level > 10`).returning('*').update(data)
|
|
261
|
-
console.log(result)
|
|
261
|
+
console.log('test update returning *', result)
|
|
262
262
|
|
|
263
263
|
let sex = 3
|
|
264
264
|
console.log(
|
|
@@ -266,6 +266,16 @@ db.add(User)
|
|
|
266
266
|
await tx.model('User').where(tx.sql`(sex = ${sex} or level > 10)`).select(['id', 'level', 'username', 'sex']).find()
|
|
267
267
|
)
|
|
268
268
|
|
|
269
|
+
console.log(
|
|
270
|
+
'test select *',
|
|
271
|
+
await tx.model('User').select().find()
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
console.log(
|
|
275
|
+
'test select * count',
|
|
276
|
+
await tx.model('User').select('*').findAndCount()
|
|
277
|
+
)
|
|
278
|
+
|
|
269
279
|
console.log(
|
|
270
280
|
'test avg',
|
|
271
281
|
await tx.model('User').avg('level')
|
|
@@ -304,4 +314,4 @@ db.add(User)
|
|
|
304
314
|
})
|
|
305
315
|
|
|
306
316
|
db.close()
|
|
307
|
-
})();
|
|
317
|
+
})();
|