neopg 2.0.7 → 2.0.9

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
@@ -196,6 +196,8 @@ db.define({
196
196
  }
197
197
  })
198
198
 
199
+ console.log(db.has('User'))
200
+
199
201
  ```
200
202
 
201
203
  ### 同步数据库
@@ -228,6 +230,10 @@ const db = new NeoPG(config)
228
230
  // 注意:这是一个异步方法,因为它兼容 ESM (.mjs) 的动态导入
229
231
  await db.loadModels('./models')
230
232
 
233
+
234
+ // 加载文件列表
235
+ await db.loadFiles(['./models2/WxUser.js', './models2/Role.js'])
236
+
231
237
  // 加载完成后即可同步或使用
232
238
  await db.sync()
233
239
  ```
package/README.md CHANGED
@@ -195,6 +195,8 @@ db.define({
195
195
  }
196
196
  })
197
197
 
198
+ console.log(db.has('User'))
199
+
198
200
  ```
199
201
 
200
202
  ### Syncing Database
@@ -227,6 +229,9 @@ const db = new NeoPG(config)
227
229
  // This is asynchronous because it supports .mjs dynamic imports
228
230
  await db.loadModels('./models')
229
231
 
232
+ //load files
233
+ await db.loadFiles(['./models2/WxUser.js', './models2/Role.js'])
234
+
230
235
  // Now you can sync and use them
231
236
  await db.sync()
232
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
@@ -268,6 +268,43 @@ class NeoPG {
268
268
  return this
269
269
  }
270
270
 
271
+ /**
272
+ *
273
+ * @param {string} mname
274
+ */
275
+ has(mname) {
276
+ return this.registry.has(mname)
277
+ }
278
+
279
+ /**
280
+ *
281
+ * @param {array} mlist
282
+ */
283
+ async loadFiles(mlist) {
284
+ if (typeof mlist === 'string') {
285
+ mlist = [mlist]
286
+ }
287
+
288
+ if (!Array.isArray(mlist)) {
289
+ throw new Error(`[NeoPG] mlist is not [string]`)
290
+ }
291
+
292
+ let modelExport
293
+
294
+ for (let m of mlist) {
295
+ if (m.endsWith('.mjs')) {
296
+ const imported = await import(m)
297
+ modelExport = imported.default
298
+ } else if (m.endsWith('.js')) {
299
+ modelExport = require(m)
300
+ } else {
301
+ continue
302
+ }
303
+
304
+ this.add(modelExport)
305
+ }
306
+ }
307
+
271
308
  }
272
309
 
273
310
  NeoPG.dataTypes = dataTypes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neopg",
3
- "version": "2.0.7",
3
+ "version": "2.0.9",
4
4
  "description": "orm for postgres",
5
5
  "keywords": [
6
6
  "neopg",
package/test/test-db.js CHANGED
@@ -197,6 +197,8 @@ db.add(User)
197
197
 
198
198
  db.sync({force: true, debug: true, model: 'ShopOrder'})
199
199
 
200
+ console.log('test has', db.has('User'))
201
+
200
202
  await db.model('ShopOrder').where('1=1').delete()
201
203
  await db.model('ShopOrder').insert([
202
204
  {
@@ -256,7 +258,7 @@ db.add(User)
256
258
  console.log('update', data)
257
259
 
258
260
  let result = await tx.model('User').where(tx.sql`level > 10`).returning('*').update(data)
259
- console.log(result)
261
+ console.log('test update returning *', result)
260
262
 
261
263
  let sex = 3
262
264
  console.log(
@@ -264,6 +266,16 @@ db.add(User)
264
266
  await tx.model('User').where(tx.sql`(sex = ${sex} or level > 10)`).select(['id', 'level', 'username', 'sex']).find()
265
267
  )
266
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
+
267
279
  console.log(
268
280
  'test avg',
269
281
  await tx.model('User').avg('level')
@@ -302,4 +314,4 @@ db.add(User)
302
314
  })
303
315
 
304
316
  db.close()
305
- })();
317
+ })();