neopg 2.2.9 → 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 +12 -1
- package/lib/NeoPG.js +3 -3
- package/lib/TransactionScope.js +2 -1
- package/package.json +1 -1
- package/test/test-db.js +3 -2
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() {
|
|
@@ -443,7 +451,10 @@ class ModelChain {
|
|
|
443
451
|
const result = await this.sql`UPDATE ${fullTable} SET ${this.sql(data)} ${whereFragment} ${retFragment}`
|
|
444
452
|
|
|
445
453
|
if (this._returning && this._returning.length > 0) {
|
|
446
|
-
if (result.length
|
|
454
|
+
if (one_result && result.length < 2) {
|
|
455
|
+
return result[0] ?? null
|
|
456
|
+
}
|
|
457
|
+
|
|
447
458
|
return result
|
|
448
459
|
}
|
|
449
460
|
return result
|
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;
|
|
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;
|
|
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;
|
|
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)
|
package/lib/TransactionScope.js
CHANGED
package/package.json
CHANGED
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
|
|
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
|
|
312
|
+
await User.self.orderby('level', 'DESC').orderby('id', 'ASC').select().find()
|
|
312
313
|
)
|
|
313
314
|
|
|
314
315
|
console.log(
|