neopg 2.2.1 → 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 +5 -1
- package/README.md +5 -1
- package/lib/ModelChain.js +32 -13
- package/package.json +1 -1
- package/test/test-db.js +6 -1
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')
|
|
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')
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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(
|
|
167
|
-
|
|
168
|
-
})
|
|
174
|
+
const parts = arg.split(',').map(s => s.trim()).filter(Boolean)
|
|
175
|
+
this._group.push(...parts)
|
|
169
176
|
} else {
|
|
170
|
-
this._group.push(
|
|
177
|
+
this._group.push(arg)
|
|
171
178
|
}
|
|
172
179
|
}
|
|
173
180
|
return this
|
|
@@ -247,7 +254,9 @@ class ModelChain {
|
|
|
247
254
|
|
|
248
255
|
_buildOrder() {
|
|
249
256
|
if (this._order.length === 0) return this.sql``
|
|
250
|
-
return this.sql`ORDER BY ${this._order}`
|
|
257
|
+
if (this._order.length === 1) return this.sql`ORDER BY ${this._order}`
|
|
258
|
+
let _orderby = this._order.reduce((acc, curr) => this.sql`${acc}, ${curr}`)
|
|
259
|
+
return this.sql`ORDER BY ${_orderby}`
|
|
251
260
|
}
|
|
252
261
|
|
|
253
262
|
_buildJoins() {
|
|
@@ -266,14 +275,24 @@ class ModelChain {
|
|
|
266
275
|
|
|
267
276
|
_buildGroup() {
|
|
268
277
|
if (this._group.length === 0) return this.sql``
|
|
269
|
-
return this.sql`GROUP BY ${this._group}`
|
|
278
|
+
return this.sql`GROUP BY ${this.sql(this._group)}`
|
|
270
279
|
}
|
|
271
280
|
|
|
272
281
|
_buildSelectQuery() {
|
|
273
282
|
const t = this.sql(this.def.tableName)
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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
|
+
}
|
|
277
296
|
|
|
278
297
|
const w = this._buildWhere()
|
|
279
298
|
const o = this._buildOrder()
|
package/package.json
CHANGED
package/test/test-db.js
CHANGED
|
@@ -271,13 +271,18 @@ db.add(User)
|
|
|
271
271
|
|
|
272
272
|
console.log(
|
|
273
273
|
'test select *',
|
|
274
|
-
await tx.model('User').select().find()
|
|
274
|
+
await tx.model('User').orderby('level', 'DESC').orderby('id', 'ASC').select().find()
|
|
275
275
|
)
|
|
276
276
|
|
|
277
277
|
console.log(
|
|
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',
|