neopg 2.0.2 → 2.0.3
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/NeoPG.js +57 -0
- package/package.json +1 -1
- package/test/test-db.js +47 -1
package/lib/NeoPG.js
CHANGED
|
@@ -11,6 +11,17 @@ const path = require('node:path')
|
|
|
11
11
|
const fs = require('node:fs')
|
|
12
12
|
const { pathToFileURL } = require('node:url')
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* 将下划线命名转换为大驼峰命名 (e.g. shop_order -> ShopOrder)
|
|
16
|
+
*/
|
|
17
|
+
function toPascalCase(str) {
|
|
18
|
+
if (!str) return ''
|
|
19
|
+
|
|
20
|
+
return str.split('_')
|
|
21
|
+
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
|
|
22
|
+
.join('')
|
|
23
|
+
}
|
|
24
|
+
|
|
14
25
|
class NeoPG {
|
|
15
26
|
constructor(config) {
|
|
16
27
|
this.driver = postgres(config)
|
|
@@ -50,6 +61,42 @@ class NeoPG {
|
|
|
50
61
|
|
|
51
62
|
if (!rawSchema) throw new Error(`[NeoPG] Missing static schema for ${ModelClass.name}`)
|
|
52
63
|
|
|
64
|
+
// 如果没有显式指定 modelName,则尝试自动推断
|
|
65
|
+
if (!rawSchema.modelName) {
|
|
66
|
+
const className = ModelClass.name
|
|
67
|
+
|
|
68
|
+
const genericNames = ['AnonymousModel', 'ModelChain', 'Function', '']
|
|
69
|
+
|
|
70
|
+
// 1. 优先尝试使用类名 (如果类名不是通用的)
|
|
71
|
+
if (className && !genericNames.includes(className)) {
|
|
72
|
+
rawSchema.modelName = className
|
|
73
|
+
} else if (rawSchema.tableName) {
|
|
74
|
+
rawSchema.modelName = toPascalCase(rawSchema.tableName)
|
|
75
|
+
setTimeout(() => {
|
|
76
|
+
console.error(`\x1b[33;5m[NeoPG]Warning: modelName is not specified, `
|
|
77
|
+
+ `use ${rawSchema.modelName} as the modelName\x1b[0m`)
|
|
78
|
+
}, 100)
|
|
79
|
+
} else {
|
|
80
|
+
// 此时说明modelName无法确定,但是tableName也没有指定
|
|
81
|
+
throw new Error(`\x1b[31m[NeoPG] Missing modelName and tableName\x1b[0m`)
|
|
82
|
+
}
|
|
83
|
+
// 3. 如果依然无法确定,稍后 ModelDef 可能会报错,或者你可以选择在这里抛出
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
//经过以上处理,modelName已经确定了,此时若没有指定tableName则把modelName转换为小写作为tableName
|
|
87
|
+
if (!rawSchema.tableName) {
|
|
88
|
+
rawSchema.tableName = rawSchema.modelName.toLowerCase()
|
|
89
|
+
|
|
90
|
+
setTimeout(() => {
|
|
91
|
+
console.error(`\x1b[33;5m[NeoPG]Warning: tableName is not specified, `
|
|
92
|
+
+ `use ${rawSchema.modelName.toLowerCase()} as the tableName\x1b[0m`)
|
|
93
|
+
}, 100)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if ((/[a-z]/).test(rawSchema.modelName[0])) {
|
|
97
|
+
throw new Error(`\x1b[31;5m[NeoPG] ${rawSchema.modelName}: modelName must start with an uppercase letter.\x1b[0m`)
|
|
98
|
+
}
|
|
99
|
+
|
|
53
100
|
const def = new ModelDef(rawSchema)
|
|
54
101
|
|
|
55
102
|
//已经存在又不是更新,则报错
|
|
@@ -93,6 +140,16 @@ class NeoPG {
|
|
|
93
140
|
|
|
94
141
|
if (!options.schema) options.schema = this.defaultSchema
|
|
95
142
|
|
|
143
|
+
if (options.model) {
|
|
144
|
+
let model = this.registry.get(options.model)
|
|
145
|
+
|
|
146
|
+
if (!model) {
|
|
147
|
+
throw new Error(`[NeoPG] sync: ${modelname} not found.`)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return await SchemaSync.execute(this.driver, model.def, this, options)
|
|
151
|
+
}
|
|
152
|
+
|
|
96
153
|
for (const item of this.registry.values()) {
|
|
97
154
|
await SchemaSync.execute(this.driver, item.def, this, options)
|
|
98
155
|
}
|
package/package.json
CHANGED
package/test/test-db.js
CHANGED
|
@@ -138,12 +138,58 @@ const User = {
|
|
|
138
138
|
}
|
|
139
139
|
|
|
140
140
|
// 3. 注册
|
|
141
|
-
db.add(User)
|
|
141
|
+
db.add(User)
|
|
142
142
|
|
|
143
143
|
;(async () => {
|
|
144
144
|
await db.sync({force: true, debug: true})
|
|
145
145
|
// 插入
|
|
146
146
|
|
|
147
|
+
//测试modelName和tableName
|
|
148
|
+
|
|
149
|
+
class ShopOrder extends ModelChain {
|
|
150
|
+
static schema = {
|
|
151
|
+
column: {
|
|
152
|
+
id: {
|
|
153
|
+
type: dataTypes.ID
|
|
154
|
+
},
|
|
155
|
+
|
|
156
|
+
name: {
|
|
157
|
+
type: dataTypes.STRING(30)
|
|
158
|
+
},
|
|
159
|
+
|
|
160
|
+
order_no: {
|
|
161
|
+
type: dataTypes.STRING(40)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
db.define(ShopOrder)
|
|
168
|
+
|
|
169
|
+
//未指定modelName
|
|
170
|
+
let Cart = {
|
|
171
|
+
tableName: 'cart',
|
|
172
|
+
column: {
|
|
173
|
+
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
db.define(Cart)
|
|
178
|
+
|
|
179
|
+
let Category = {
|
|
180
|
+
column: {}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
db.define(Category)
|
|
185
|
+
} catch (err) {
|
|
186
|
+
setTimeout(() => {
|
|
187
|
+
console.error(err.message)
|
|
188
|
+
}, 100)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
db.sync({force: true, debug: true, model: 'ShopOrder'})
|
|
192
|
+
|
|
147
193
|
await db.model('User').where('1=1').delete()
|
|
148
194
|
|
|
149
195
|
try {
|