neopg 2.0.9 → 2.1.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/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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neopg",
3
- "version": "2.0.9",
3
+ "version": "2.1.0",
4
4
  "description": "orm for postgres",
5
5
  "keywords": [
6
6
  "neopg",