neopg 2.2.2 → 2.2.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/README.cn.md CHANGED
@@ -263,7 +263,10 @@ const users = await db.model('User')
263
263
  const user = await db.model('User').where({ id: '123' }).get();
264
264
 
265
265
  // 分页查询
266
- const page2 = await db.model('User').page(2, 20).find(); // 第 2 页,每页 20 条
266
+ const page2 = await db.model('User')
267
+ .select(['id', 'username', 'level'])
268
+ .page(2, 20)
269
+ .find(); // 第 2 页,每页 20 条
267
270
  ```
268
271
 
269
272
  ### 链式 Where 条件
@@ -276,6 +279,7 @@ await db.model('User')
276
279
  })
277
280
  .where('create_time', '>', 1600000000)
278
281
  .where('id IS NOT NULL')
282
+ .select(db.sql`id, username, role`)
279
283
  .find()
280
284
  ```
281
285
 
package/README.md CHANGED
@@ -261,7 +261,10 @@ const users = await db.model('User')
261
261
  const user = await db.model('User').where({ id: '123' }).get();
262
262
 
263
263
  // Pagination
264
- const page2 = await db.model('User').page(2, 20).find(); // Page 2, Size 20
264
+ const page2 = await db.model('User')
265
+ .select(['id', 'username', 'role'])
266
+ .page(2, 20)
267
+ .find(); // Page 2, Size 20
265
268
  ```
266
269
 
267
270
  ### Chained Where
@@ -274,6 +277,7 @@ await db.model('User')
274
277
  })
275
278
  .where('create_time', '>', 1600000000)
276
279
  .where('id IS NOT NULL')
280
+ .select(db.sql`id, username, role`)
277
281
  .find()
278
282
  ```
279
283
 
package/lib/ModelChain.js CHANGED
@@ -90,11 +90,19 @@ class ModelChain {
90
90
 
91
91
  select(columns) {
92
92
  if (!columns) return this
93
- if (typeof columns === 'string') {
94
- this._columns = columns.split(',').map(s => s.trim())
95
- } else if (Array.isArray(columns)) {
96
- this._columns = columns
93
+
94
+ // 1. 判断是否为 postgres.js 的原生 SQL 片段 (Tag Function 返回的对象)
95
+ // 如果不是数组且具备 Query 构造器特征,认为是开发者手写的 sql`...` 片段
96
+ if (!Array.isArray(columns) && typeof columns === 'object' && columns.constructor?.name === 'Query') {
97
+ this._columns = columns
98
+ }
99
+ else if (typeof columns === 'string') {
100
+ this._columns = columns.split(',').map(s => s.trim()).filter(Boolean)
97
101
  }
102
+ else if (Array.isArray(columns)) {
103
+ this._columns = columns
104
+ }
105
+
98
106
  return this
99
107
  }
100
108
 
@@ -163,11 +171,10 @@ class ModelChain {
163
171
  }
164
172
  if (typeof arg === 'string') {
165
173
  if (arg.includes(',')) {
166
- arg.split(',').map(s => s.trim()).filter(s=>s).forEach(s => {
167
- this._group.push(this.sql(s))
168
- })
174
+ const parts = arg.split(',').map(s => s.trim()).filter(Boolean)
175
+ this._group.push(...parts)
169
176
  } else {
170
- this._group.push(this.sql(arg))
177
+ this._group.push(arg)
171
178
  }
172
179
  }
173
180
  return this
@@ -268,14 +275,24 @@ class ModelChain {
268
275
 
269
276
  _buildGroup() {
270
277
  if (this._group.length === 0) return this.sql``
271
- return this.sql`GROUP BY ${this._group}`
278
+ return this.sql`GROUP BY ${this.sql(this._group)}`
272
279
  }
273
280
 
274
281
  _buildSelectQuery() {
275
282
  const t = this.sql(this.def.tableName)
276
- const c = this._columns
277
- ? ((this._columns.length===1 && this._columns[0] === '*') ? this.sql`*` : this.sql(this._columns))
278
- : this.sql`*`
283
+ let c
284
+ if (Array.isArray(this._columns)) {
285
+ if ((this._columns.length === 1 && this._columns[0] === '*') || this._columns.length === 0) {
286
+ c = this.sql`*`
287
+ } else {
288
+ c = this.sql(this._columns)
289
+ }
290
+ } else if (this._columns) {
291
+ // 如果是 sql`...` 片段,直接使用,不加任何额外处理
292
+ c = this._columns
293
+ } else {
294
+ c = this.sql`*`
295
+ }
279
296
 
280
297
  const w = this._buildWhere()
281
298
  const o = this._buildOrder()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neopg",
3
- "version": "2.2.2",
3
+ "version": "2.2.3",
4
4
  "description": "orm for postgres",
5
5
  "keywords": [
6
6
  "neopg",
package/test/test-db.js CHANGED
@@ -278,6 +278,11 @@ db.add(User)
278
278
  'test select * count',
279
279
  await tx.model('User').select('*').findAndCount()
280
280
  )
281
+
282
+ console.log(
283
+ 'test select group',
284
+ await tx.model('User').group('level').select(tx.sql`level, MAX(username) as username`).findAndCount()
285
+ )
281
286
 
282
287
  console.log(
283
288
  'test avg',