neopg 2.2.5 → 2.2.7

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/lib/ModelChain.js CHANGED
@@ -198,8 +198,16 @@ class ModelChain {
198
198
  const v = arg1[k]
199
199
  if (v === undefined) continue
200
200
  if (v === null) this._conditions.push(this.sql`${this.sql(k)} IS NULL`)
201
- else if (Array.isArray(v)) this._conditions.push(this.sql`${this.sql(k)} IN ${this.sql(v)}`)
202
- else this._conditions.push(this.sql`${this.sql(k)} = ${v}`)
201
+ else if (Array.isArray(v)) {
202
+ //this._conditions.push(this.sql`${this.sql(k)} IN ${this.sql(v)}`)
203
+ if (v.length === 0) {
204
+ // 性能优化:空数组直接在应用层生成 FALSE,不触发数据库复杂的数组运算
205
+ this._conditions.push(this.sql`FALSE`)
206
+ } else {
207
+ // 在 postgres.js 中,${v} 会被转换为 Postgres 数组变量 $1
208
+ this._conditions.push(this.sql`${this.sql(k)} = ANY(${v})`)
209
+ }
210
+ } else this._conditions.push(this.sql`${this.sql(k)} = ${v}`)
203
211
  }
204
212
  return this
205
213
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neopg",
3
- "version": "2.2.5",
3
+ "version": "2.2.7",
4
4
  "description": "orm for postgres",
5
5
  "keywords": [
6
6
  "neopg",
@@ -350,7 +350,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
350
350
  }
351
351
 
352
352
  function reconnect() {
353
- setTimeout(connect, closedDate ? closedDate + delay - performance.now() : 0)
353
+ setTimeout(connect, closedDate ? Math.max(0, closedDate + delay - performance.now()) : 0)
354
354
  }
355
355
 
356
356
  function connected() {
package/test/test-db.js CHANGED
@@ -130,6 +130,11 @@ const User = {
130
130
  type: dataTypes.POINT,
131
131
  default: '(0,0)',
132
132
  indexType: 'GiST'
133
+ },
134
+
135
+ role: {
136
+ type: dataTypes.STRING(16),
137
+ default: 'user'
133
138
  }
134
139
  },
135
140
 
@@ -213,6 +218,7 @@ db.add(User)
213
218
  console.log('test has', db.has('User'))
214
219
 
215
220
  await db.model('ShopOrder').where('1=1').delete()
221
+
216
222
  await db.model('ShopOrder').insert([
217
223
  {
218
224
  name: 'topbit',
@@ -222,6 +228,10 @@ db.add(User)
222
228
  {
223
229
  name: 'neopg',
224
230
  order_no: Math.random().toString(16)
231
+ },
232
+ {
233
+ name: 'hydra-thread',
234
+ order_no: Math.random().toString(16)
225
235
  }
226
236
  ])
227
237
 
@@ -240,20 +250,23 @@ db.add(User)
240
250
  username: 'Neo',
241
251
  email: '123@w.com',
242
252
  sex: 1,
243
- level: Math.floor((Math.random() * 105))
253
+ level: Math.floor((Math.random() * 105)),
254
+ role: 'user'
244
255
  },
245
256
  {
246
257
  username: 'PG',
247
258
  email: '1234@w.com',
248
259
  sex: 2,
249
- level: Math.floor((Math.random() * 100))
260
+ level: Math.floor((Math.random() * 100)),
261
+ role: 'db'
250
262
  },
251
263
 
252
264
  {
253
265
  username: 'NPG',
254
266
  email: '1235@w.com',
255
267
  sex: 3,
256
- level: 3
268
+ level: 3,
269
+ role: 'test'
257
270
  }
258
271
  ])
259
272
  )
@@ -294,6 +307,16 @@ db.add(User)
294
307
  await tx.model('User').group('level').select(tx.sql`level, MAX(username) as username`).findAndCount()
295
308
  )
296
309
 
310
+ console.log(
311
+ 'test for any (role in ...)',
312
+ await tx.model('User').where({role: ['test', 'db']}).find()
313
+ )
314
+
315
+ console.log(
316
+ 'test for any (empty)',
317
+ await tx.model('User').where({role: []}).find()
318
+ )
319
+
297
320
  console.log(
298
321
  'test avg',
299
322
  await tx.model('User').avg('level')