neopg 2.0.9 → 2.2.0

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/README.cn.md CHANGED
@@ -231,6 +231,9 @@ const db = new NeoPG(config)
231
231
  await db.loadModels('./models')
232
232
 
233
233
 
234
+ //加载ESM模块
235
+ await db.loadModels('./esmodels', 'esm')
236
+
234
237
  // 加载文件列表
235
238
  await db.loadFiles(['./models2/WxUser.js', './models2/Role.js'])
236
239
 
package/README.md CHANGED
@@ -229,6 +229,9 @@ const db = new NeoPG(config)
229
229
  // This is asynchronous because it supports .mjs dynamic imports
230
230
  await db.loadModels('./models')
231
231
 
232
+ //load esm modules
233
+ await db.loadModels('./esmodels', 'esm')
234
+
232
235
  //load files
233
236
  await db.loadFiles(['./models2/WxUser.js', './models2/Role.js'])
234
237
 
package/lib/ModelChain.js CHANGED
@@ -49,8 +49,13 @@ class ModelChain {
49
49
  _ensureActive() {
50
50
  if (this._executed) {
51
51
  throw new Error(
52
- `[NeoPG] ModelChain for '${this.def.tableName}' has already been executed. ` +
53
- `Do NOT reuse the chain variable. Use .clone() if you need to fork queries.`
52
+ `[NeoPG] Security Error: This query chain for \x1b[36m'${this.def.tableName}'\x1b[0m has already been executed to prevent state pollution.\n` +
53
+ `❌ Do NOT reuse the same variable for multiple executions.\n` +
54
+ `✅ If you need to execute multiple queries based on the same conditions, use .clone() before execution.\n\x1b[35m` +
55
+ ` Example: \n` +
56
+ ` const q = model.where(...);\n` +
57
+ ` const data = await q.clone().find();\n` +
58
+ ` await q.clone().update(...);\x1b[0m`
54
59
  )
55
60
  }
56
61
  }
package/lib/NeoPG.js CHANGED
@@ -10,6 +10,7 @@ const dataTypes = require('./dataTypes.js')
10
10
  const path = require('node:path')
11
11
  const fs = require('node:fs')
12
12
  const { pathToFileURL } = require('node:url')
13
+ const process = require('node:process')
13
14
 
14
15
  /**
15
16
  * 将下划线命名转换为大驼峰命名 (e.g. shop_order -> ShopOrder)
@@ -219,8 +220,9 @@ class NeoPG {
219
220
  * 3. 跳过以 '!' 开头的文件 (视为保留或禁用文件)
220
221
  *
221
222
  * @param {string} dirPath - 目录路径 (相对 process.cwd() 或绝对路径)
223
+ * @param {stirng} modtype - 模块类型 cjs或esm
222
224
  */
223
- async loadModels(dirPath) {
225
+ async loadModels(dirPath, modtype='cjs') {
224
226
  // 解析绝对路径
225
227
  const absPath = path.isAbsolute(dirPath)
226
228
  ? dirPath
@@ -244,7 +246,7 @@ class NeoPG {
244
246
  let modelExport
245
247
 
246
248
  try {
247
- if (ext === '.mjs') {
249
+ if (ext === '.mjs' || modtype === 'esm') {
248
250
  // 处理 ESM 动态导入
249
251
  // Windows 下 import() 需要 file:// 协议路径
250
252
  const fileUrl = pathToFileURL(fullFilePath).href
@@ -278,9 +280,10 @@ class NeoPG {
278
280
 
279
281
  /**
280
282
  *
281
- * @param {array} mlist
283
+ * @param {array} mlist - 文件列表
284
+ * @param {stirng} modtype - 模块类型 cjs或esm
282
285
  */
283
- async loadFiles(mlist) {
286
+ async loadFiles(mlist, modtype='cjs') {
284
287
  if (typeof mlist === 'string') {
285
288
  mlist = [mlist]
286
289
  }
@@ -289,14 +292,17 @@ class NeoPG {
289
292
  throw new Error(`[NeoPG] mlist is not [string]`)
290
293
  }
291
294
 
295
+ let absPath
292
296
  let modelExport
293
297
 
294
298
  for (let m of mlist) {
295
- if (m.endsWith('.mjs')) {
296
- const imported = await import(m)
297
- modelExport = imported.default
299
+ absPath = path.isAbsolute(m) ? m : path.resolve(process.cwd(), m)
300
+
301
+ if (m.endsWith('.mjs') || modtype === 'esm') {
302
+ const imported = await import(pathToFileURL(absPath).href)
303
+ modelExport = imported.default
298
304
  } else if (m.endsWith('.js')) {
299
- modelExport = require(m)
305
+ modelExport = require(absPath)
300
306
  } else {
301
307
  continue
302
308
  }
@@ -9,14 +9,24 @@ class TransactionScope {
9
9
 
10
10
  table(tableName, schema = null) {
11
11
  const target = schema || this.parent.defaultSchema
12
- return new this.parent.ModelChain(this, {tableName, isRaw: true}, target)
12
+ let m = new this.parent.ModelChain(this, {tableName, isRaw: true}, target)
13
+ m._isRaw = true
14
+ return m
13
15
  }
14
16
 
15
17
  model(name, schema = null) {
16
18
  const item = this.parent.registry.get(name)
17
19
  if (!item) throw new Error(`[NeoPG] Model '${name}' not found.`)
18
20
  const target = schema || this.parent.defaultSchema
19
- return new item.Class(this, item.def, target)
21
+ let m = new item.Class(this, item.def, target)
22
+ if (!m.def) {
23
+ m.def = item.def
24
+ m.ctx = this.parent
25
+ m.sql = this.sql
26
+ m.schema = target
27
+ }
28
+
29
+ return m
20
30
  }
21
31
 
22
32
  async transaction(callback) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neopg",
3
- "version": "2.0.9",
3
+ "version": "2.2.0",
4
4
  "description": "orm for postgres",
5
5
  "keywords": [
6
6
  "neopg",