w-orm-postgresql 1.0.0 → 1.0.2

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.
@@ -1,5 +1,5 @@
1
1
  import assert from 'assert'
2
- import wo from '../src/WOrmPostgresql.mjs'
2
+ import WOrm from '../src/WOrmPostgresql.mjs'
3
3
 
4
4
 
5
5
  function isWindows() {
@@ -13,7 +13,6 @@ if (isWindows()) {
13
13
  let vans = {}
14
14
  let vget = {}
15
15
 
16
-
17
16
  before(async function () {
18
17
 
19
18
  let opt = {
@@ -90,7 +89,7 @@ if (isWindows()) {
90
89
  },
91
90
  ]
92
91
 
93
- let rsm = [
92
+ let rsm = [
94
93
  { //相同
95
94
  time: '2025-01-01T00:09:00Z',
96
95
  name: 'peter',
@@ -114,18 +113,25 @@ if (isWindows()) {
114
113
  { //無time須新增且新增數據無value
115
114
  time: '2025-01-01T00:13:00Z',
116
115
  name: 'sandler',
117
- },
118
- ]
119
-
120
- //w
121
- let w = wo(opt)
116
+ },
117
+ ]
118
+
119
+ let rsa = [
120
+ {
121
+ time: '2025-01-01T00:13:00Z',
122
+ name: 'sandler',
123
+ },
124
+ ]
125
+
126
+ //wo
127
+ let wo = WOrm(opt)
122
128
 
123
129
  //on
124
- w.on('change', function(mode, data, res) {
130
+ wo.on('change', function(mode, data, res) {
125
131
  // console.log('change', mode)
126
132
  })
127
133
 
128
- await w.createTable(opt.cl, 'time', {
134
+ await wo.createTable(opt.cl, 'time', {
129
135
  time: '2000-01-01T00:00:00Z', //time
130
136
  name: 'abc', //string
131
137
  value: 0.1, //float
@@ -140,7 +146,7 @@ if (isWindows()) {
140
146
  //delAll
141
147
  rt = null
142
148
  vans[1] = { ok: 1 }
143
- await w.delAll()
149
+ await wo.delAll()
144
150
  .then(function(msg) {
145
151
  // console.log('delAll then', msg)
146
152
  //考慮有不同初始狀態, 僅比對ok欄位
@@ -157,7 +163,7 @@ if (isWindows()) {
157
163
  //insert
158
164
  rt = null
159
165
  vans[2] = { n: 13, nInserted: 13, ok: 1 }
160
- await w.insert(rs)
166
+ await wo.insert(rs)
161
167
  .then(function(msg) {
162
168
  // console.log('insert then', msg)
163
169
  rt = msg
@@ -170,27 +176,27 @@ if (isWindows()) {
170
176
 
171
177
  //save
172
178
  rt = null
173
- vans[3] = [
174
- { n: 1, nModified: 0, ok: 1 },
175
- { n: 1, nModified: 1, ok: 1 },
176
- { n: 1, nModified: 1, ok: 1 },
177
- { n: 1, nModified: 1, ok: 1 },
178
- { n: 1, nInserted: 1, ok: 1 }
179
- ]
180
- await w.save(rsm, { autoInsert: true })
181
- .then(function(msg) {
182
- // console.log('save then', msg)
183
- rt = msg
184
- })
179
+ vans[3] = [
180
+ { n: 1, nModified: 0, ok: 1 },
181
+ { n: 1, nModified: 1, ok: 1 },
182
+ { n: 1, nModified: 1, ok: 1 },
183
+ { n: 1, nModified: 1, ok: 1 },
184
+ { n: 0, nModified: 0, ok: 1 }
185
+ ]
186
+ await wo.save(rsm, { autoInsert: false })
187
+ .then(function(msg) {
188
+ // console.log('save then', msg)
189
+ rt = msg
190
+ })
185
191
  .catch(function(msg) {
186
192
  // console.log('save catch', msg)
187
193
  rt = msg.toString()
188
- })
189
- vget[3] = rt
190
-
191
- //select all
192
- rt = null
193
- vans[4] = [
194
+ })
195
+ vget[3] = rt
196
+
197
+ //select all
198
+ rt = null
199
+ vans[4] = [
194
200
  { time: new Date('2025-01-01T00:00:00.000Z'), name: 'peter', value: 123 },
195
201
  { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
196
202
  { time: new Date('2025-01-01T00:02:00.000Z'), name: 'kettle', value: 456 },
@@ -200,31 +206,44 @@ if (isWindows()) {
200
206
  { time: new Date('2025-01-01T00:06:00.000Z'), name: 'peter', value: 125 },
201
207
  { time: new Date('2025-01-01T00:07:00.000Z'), name: 'rosemary', value: 124.76 },
202
208
  { time: new Date('2025-01-01T00:08:00.000Z'), name: 'kettle', value: 524 },
203
- { time: new Date('2025-01-01T00:09:00.000Z'), name: 'peter', value: 127 },
204
- { time: new Date('2025-01-01T00:10:00.000Z'), name: 'rosemary(modify)', value: 113.98 },
205
- { time: new Date('2025-01-01T00:11:00.000Z'), name: 'kettle(modify)', value: 447 },
206
- { time: new Date('2025-01-01T00:12:00.000Z'), name: 'peter', value: 99 },
207
- { time: new Date('2025-01-01T00:13:00.000Z'), name: 'sandler', value: null }
208
- ]
209
- await w.select()
210
- .then(function(msg) {
211
- // console.log('select all then', msg)
212
- rt = msg
213
- })
209
+ { time: new Date('2025-01-01T00:09:00.000Z'), name: 'peter', value: 127 },
210
+ { time: new Date('2025-01-01T00:10:00.000Z'), name: 'rosemary(modify)', value: 113.98 },
211
+ { time: new Date('2025-01-01T00:11:00.000Z'), name: 'kettle(modify)', value: 447 },
212
+ { time: new Date('2025-01-01T00:12:00.000Z'), name: 'peter', value: 99 }
213
+ ]
214
+ await wo.select()
215
+ .then(function(msg) {
216
+ // console.log('select all then', msg)
217
+ rt = msg
218
+ })
214
219
  .catch(function(msg) {
215
220
  // console.log('select all catch', msg)
216
221
  rt = msg.toString()
217
- })
218
- vget[4] = rt
219
-
220
- //select by name
221
- rt = null
222
- vans[5] = [
222
+ })
223
+ vget[4] = rt
224
+
225
+ //select all by cache
226
+ rt = null
227
+ vans[5] = vans[4]
228
+ await wo.select()
229
+ .then(function(msg) {
230
+ // console.log('select all by cache then', msg)
231
+ rt = msg
232
+ })
233
+ .catch(function(msg) {
234
+ // console.log('select all by cache catch', msg)
235
+ rt = msg.toString()
236
+ })
237
+ vget[5] = rt
238
+
239
+ //select by name
240
+ rt = null
241
+ vans[6] = [
223
242
  { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
224
243
  { time: new Date('2025-01-01T00:04:00.000Z'), name: 'rosemary', value: 123.1236 },
225
244
  { time: new Date('2025-01-01T00:07:00.000Z'), name: 'rosemary', value: 124.76 }
226
245
  ]
227
- await w.select({ name: 'rosemary' })
246
+ await wo.select({ name: 'rosemary' })
228
247
  .then(function(msg) {
229
248
  // console.log('select all then', msg)
230
249
  rt = msg
@@ -233,18 +252,18 @@ if (isWindows()) {
233
252
  // console.log('select all catch', msg)
234
253
  rt = msg.toString()
235
254
  })
236
- vget[5] = rt
255
+ vget[6] = rt
237
256
 
238
257
  //select by $and, $gt, $lt
239
258
  rt = null
240
- vans[6] = [
259
+ vans[7] = [
241
260
  { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
242
261
  { time: new Date('2025-01-01T00:04:00.000Z'), name: 'rosemary', value: 123.1236 },
243
262
  { time: new Date('2025-01-01T00:06:00.000Z'), name: 'peter', value: 125 },
244
263
  { time: new Date('2025-01-01T00:07:00.000Z'), name: 'rosemary', value: 124.76 },
245
264
  { time: new Date('2025-01-01T00:09:00.000Z'), name: 'peter', value: 127 }
246
265
  ]
247
- await w.select({ '$and': [{ value: { '$gt': 123 } }, { value: { '$lt': 200 } }] })
266
+ await wo.select({ '$and': [{ value: { '$gt': 123 } }, { value: { '$lt': 200 } }] })
248
267
  .then(function(msg) {
249
268
  // console.log('select all then', msg)
250
269
  rt = msg
@@ -253,18 +272,18 @@ if (isWindows()) {
253
272
  // console.log('select all catch', msg)
254
273
  rt = msg.toString()
255
274
  })
256
- vget[6] = rt
275
+ vget[7] = rt
257
276
 
258
277
  //select by $or, $gte, $lte
259
278
  rt = null
260
- vans[7] = [
279
+ vans[8] = [
261
280
  { time: new Date('2025-01-01T00:02:00.000Z'), name: 'kettle', value: 456 },
262
281
  { time: new Date('2025-01-01T00:03:00.000Z'), name: 'peter', value: 200 },
263
282
  { time: new Date('2025-01-01T00:05:00.000Z'), name: 'kettle', value: 488 },
264
283
  { time: new Date('2025-01-01T00:08:00.000Z'), name: 'kettle', value: 524 },
265
284
  { time: new Date('2025-01-01T00:11:00.000Z'), name: 'kettle(modify)', value: 447 }
266
285
  ]
267
- await w.select({ '$or': [{ value: { '$lte': -1 } }, { value: { '$gte': 200 } }] })
286
+ await wo.select({ '$or': [{ value: { '$lte': -1 } }, { value: { '$gte': 200 } }] })
268
287
  .then(function(msg) {
269
288
  // console.log('select all then', msg)
270
289
  rt = msg
@@ -273,18 +292,18 @@ if (isWindows()) {
273
292
  // console.log('select all catch', msg)
274
293
  rt = msg.toString()
275
294
  })
276
- vget[7] = rt
295
+ vget[8] = rt
277
296
 
278
297
  //select by $or, $and, $ne, $in, $nin
279
298
  rt = null
280
- vans[8] = [
299
+ vans[9] = [
281
300
  { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
282
301
  { time: new Date('2025-01-01T00:02:00.000Z'), name: 'kettle', value: 456 },
283
302
  { time: new Date('2025-01-01T00:05:00.000Z'), name: 'kettle', value: 488 },
284
303
  { time: new Date('2025-01-01T00:08:00.000Z'), name: 'kettle', value: 524 },
285
304
  { time: new Date('2025-01-01T00:11:00.000Z'), name: 'kettle(modify)', value: 447 }
286
305
  ]
287
- await w.select({ '$or': [{ '$and': [{ value: { '$ne': 123 } }, { value: { '$in': [123, 321, 123.456, 456] } }, { value: { '$nin': [456, 654] } }] }, { '$or': [{ value: { '$lte': -1 } }, { value: { '$gte': 400 } }] }] })
306
+ await wo.select({ '$or': [{ '$and': [{ value: { '$ne': 123 } }, { value: { '$in': [123, 321, 123.456, 456] } }, { value: { '$nin': [456, 654] } }] }, { '$or': [{ value: { '$lte': -1 } }, { value: { '$gte': 400 } }] }] })
288
307
  .then(function(msg) {
289
308
  // console.log('select all then', msg)
290
309
  rt = msg
@@ -293,12 +312,12 @@ if (isWindows()) {
293
312
  // console.log('select all catch', msg)
294
313
  rt = msg.toString()
295
314
  })
296
- vget[8] = rt
315
+ vget[9] = rt
297
316
 
298
317
  // //select by regex
299
318
  // rt = null
300
- // vans[9] = []
301
- // let sr = await w.select({ name: { $regex: 'PeT', $options: '$i' } })
319
+ // vans[14] = []
320
+ // let sr = await wo.select({ name: { $regex: 'PeT', $options: '$i' } })
302
321
  // .then(function(msg) {
303
322
  // // console.log('select all then', msg)
304
323
  // rt = msg
@@ -306,16 +325,30 @@ if (isWindows()) {
306
325
  // .catch(function(msg) {
307
326
  // // console.log('select all catch', msg)
308
327
  // rt = msg.toString()
309
- // })
310
- // vget[9] = rt
311
-
312
- //del
313
- rt = null
314
- vans[10] = [{ n: 1, nDeleted: 0, ok: 1 }]
328
+ // })
329
+ // vget[14] = rt
330
+
331
+ //save
332
+ rt = null
333
+ vans[10] = [{ n: 1, nInserted: 1, ok: 1 }]
334
+ await wo.save(rsa, { autoInsert: true })
335
+ .then(function(msg) {
336
+ // console.log('save then', msg)
337
+ rt = msg
338
+ })
339
+ .catch(function(msg) {
340
+ // console.log('save catch', msg)
341
+ rt = msg.toString()
342
+ })
343
+ vget[10] = rt
344
+
345
+ //del
346
+ rt = null
347
+ vans[11] = [{ n: 1, nDeleted: 0, ok: 1 }]
315
348
  let d = {
316
349
  time: '2024-01-01T00:00:00Z',
317
350
  }
318
- await w.del(d)
351
+ await wo.del(d)
319
352
  .then(function(msg) {
320
353
  // console.log('del then', msg)
321
354
  rt = msg
@@ -324,11 +357,11 @@ if (isWindows()) {
324
357
  // console.log('del catch', msg)
325
358
  rt = msg.toString()
326
359
  })
327
- vget[10] = rt
328
-
329
- //del
330
- rt = null
331
- vans[11] = [
360
+ vget[11] = rt
361
+
362
+ //del
363
+ rt = null
364
+ vans[12] = [
332
365
  { n: 1, nDeleted: 1, ok: 1 },
333
366
  { n: 1, nDeleted: 1, ok: 1 },
334
367
  { n: 1, nDeleted: 1, ok: 1 },
@@ -340,11 +373,11 @@ if (isWindows()) {
340
373
  { n: 1, nDeleted: 1, ok: 1 },
341
374
  { n: 1, nDeleted: 1, ok: 1 }
342
375
  ]
343
- let ss = await w.select()
376
+ let ss = await wo.select()
344
377
  let ds = ss.filter(function(v) {
345
378
  return v.name.indexOf('peter') >= 0 || v.name.indexOf('kettle') >= 0 || v.name.indexOf('sandler') >= 0
346
379
  })
347
- await w.del(ds)
380
+ await wo.del(ds)
348
381
  .then(function(msg) {
349
382
  // console.log('del then', msg)
350
383
  rt = msg
@@ -353,17 +386,17 @@ if (isWindows()) {
353
386
  // console.log('del catch', msg)
354
387
  rt = msg.toString()
355
388
  })
356
- vget[11] = rt
357
-
358
- //select all final
359
- rt = null
360
- vans[12] = [
389
+ vget[12] = rt
390
+
391
+ //select all final
392
+ rt = null
393
+ vans[13] = [
361
394
  { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
362
395
  { time: new Date('2025-01-01T00:04:00.000Z'), name: 'rosemary', value: 123.1236 },
363
396
  { time: new Date('2025-01-01T00:07:00.000Z'), name: 'rosemary', value: 124.76 },
364
397
  { time: new Date('2025-01-01T00:10:00.000Z'), name: 'rosemary(modify)', value: 113.98 }
365
398
  ]
366
- await w.select()
399
+ await wo.select()
367
400
  .then(function(msg) {
368
401
  // console.log('select all then', msg)
369
402
  rt = msg
@@ -372,7 +405,7 @@ if (isWindows()) {
372
405
  // console.log('select all catch', msg)
373
406
  rt = msg.toString()
374
407
  })
375
- vget[12] = rt
408
+ vget[13] = rt
376
409
 
377
410
  })
378
411
 
@@ -384,45 +417,53 @@ if (isWindows()) {
384
417
  assert.strict.deepStrictEqual(vget[2], vans[2])
385
418
  })
386
419
 
387
- it(`should get ${JSON.stringify(vans[3])} for save`, async function() {
388
- assert.strict.deepStrictEqual(vget[3], vans[3])
389
- })
390
-
391
- it(`should get ${JSON.stringify(vans[4])} for select all`, async function() {
392
- assert.strict.deepStrictEqual(vget[4], vans[4])
393
- })
394
-
395
- it(`should get ${JSON.stringify(vans[5])} for select by name`, async function() {
396
- assert.strict.deepStrictEqual(vget[5], vans[5])
397
- })
398
-
399
- it(`should get ${JSON.stringify(vans[6])} for select by $and, $gt, $lt`, async function() {
400
- assert.strict.deepStrictEqual(vget[6], vans[6])
401
- })
402
-
403
- it(`should get ${JSON.stringify(vans[7])} for select by $or, $gte, $lte`, async function() {
404
- assert.strict.deepStrictEqual(vget[7], vans[7])
405
- })
406
-
407
- it(`should get ${JSON.stringify(vans[8])} for select by $or, $and, $ne, $in, $nin`, async function() {
408
- assert.strict.deepStrictEqual(vget[8], vans[8])
409
- })
410
-
411
- // it(`should get ${JSON.stringify(vans[9])} for select by regex`, async function() {
412
- // assert.strict.deepStrictEqual(vget[9], vans[9])
413
- // })
414
-
415
- it(`should get ${JSON.stringify(vans[10])} for del`, async function() {
416
- assert.strict.deepStrictEqual(vget[10], vans[10])
417
- })
418
-
419
- it(`should get ${JSON.stringify(vans[11])} for del`, async function() {
420
- assert.strict.deepStrictEqual(vget[11], vans[11])
421
- })
422
-
423
- it(`should get ${JSON.stringify(vans[12])} for select all final`, async function() {
424
- assert.strict.deepStrictEqual(vget[12], vans[12])
425
- })
420
+ it(`should get ${JSON.stringify(vans[3])} for save(autoInsert=false)`, async function() {
421
+ assert.strict.deepStrictEqual(vget[3], vans[3])
422
+ })
423
+
424
+ it(`should get ${JSON.stringify(vans[4])} for select all`, async function() {
425
+ assert.strict.deepStrictEqual(vget[4], vans[4])
426
+ })
427
+
428
+ it(`should get ${JSON.stringify(vans[5])} for select all by cache`, async function() {
429
+ assert.strict.deepStrictEqual(vget[5], vans[5])
430
+ })
431
+
432
+ it(`should get ${JSON.stringify(vans[6])} for select by name`, async function() {
433
+ assert.strict.deepStrictEqual(vget[6], vans[6])
434
+ })
435
+
436
+ it(`should get ${JSON.stringify(vans[7])} for select by $and, $gt, $lt`, async function() {
437
+ assert.strict.deepStrictEqual(vget[7], vans[7])
438
+ })
439
+
440
+ it(`should get ${JSON.stringify(vans[8])} for select by $or, $gte, $lte`, async function() {
441
+ assert.strict.deepStrictEqual(vget[8], vans[8])
442
+ })
443
+
444
+ it(`should get ${JSON.stringify(vans[9])} for select by $or, $and, $ne, $in, $nin`, async function() {
445
+ assert.strict.deepStrictEqual(vget[9], vans[9])
446
+ })
447
+
448
+ // it(`should get ${JSON.stringify(vans[14])} for select by regex`, async function() {
449
+ // assert.strict.deepStrictEqual(vget[14], vans[14])
450
+ // })
451
+
452
+ it(`should get ${JSON.stringify(vans[10])} for save(autoInsert=true)`, async function() {
453
+ assert.strict.deepStrictEqual(vget[10], vans[10])
454
+ })
455
+
456
+ it(`should get ${JSON.stringify(vans[11])} for del`, async function() {
457
+ assert.strict.deepStrictEqual(vget[11], vans[11])
458
+ })
459
+
460
+ it(`should get ${JSON.stringify(vans[12])} for del`, async function() {
461
+ assert.strict.deepStrictEqual(vget[12], vans[12])
462
+ })
463
+
464
+ it(`should get ${JSON.stringify(vans[13])} for select all final`, async function() {
465
+ assert.strict.deepStrictEqual(vget[13], vans[13])
466
+ })
426
467
 
427
468
  })
428
469
  }