neopg 2.0.8 → 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/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 ? this.sql(this._columns) : this.sql`*`
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/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.8",
3
+ "version": "2.1.0",
4
4
  "description": "orm for postgres",
5
5
  "keywords": [
6
6
  "neopg",
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
+ })();