@platformatic/sql-mapper 0.6.1 → 0.8.0

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.
@@ -0,0 +1,318 @@
1
+ 'use strict'
2
+
3
+ const { test } = require('tap')
4
+ const { connect } = require('..')
5
+ const { clear, connInfo, isSQLite, isMysql } = require('./helper')
6
+ const { setTimeout } = require('timers/promises')
7
+ const fakeLogger = {
8
+ trace: () => {},
9
+ error: () => {}
10
+ }
11
+
12
+ test('updateMany successful', async ({ pass, teardown, same }) => {
13
+ const mapper = await connect({
14
+ ...connInfo,
15
+ log: fakeLogger,
16
+ async onDatabaseLoad (db, sql) {
17
+ teardown(() => db.dispose())
18
+ pass('onDatabaseLoad called')
19
+
20
+ await clear(db, sql)
21
+
22
+ if (isSQLite) {
23
+ await db.query(sql`CREATE TABLE posts (
24
+ id INTEGER PRIMARY KEY,
25
+ title VARCHAR(42),
26
+ long_text TEXT,
27
+ counter INTEGER
28
+ );`)
29
+ } else {
30
+ await db.query(sql`CREATE TABLE posts (
31
+ id SERIAL PRIMARY KEY,
32
+ title VARCHAR(42),
33
+ long_text TEXT,
34
+ counter INTEGER
35
+ );`)
36
+ }
37
+ }
38
+ })
39
+
40
+ const entity = mapper.entities.post
41
+
42
+ const posts = [{
43
+ title: 'Dog',
44
+ longText: 'Foo',
45
+ counter: 10
46
+ }, {
47
+ title: 'Cat',
48
+ longText: 'Bar',
49
+ counter: 20
50
+ }, {
51
+ title: 'Mouse',
52
+ longText: 'Baz',
53
+ counter: 30
54
+ }, {
55
+ title: 'Duck',
56
+ longText: 'A duck tale',
57
+ counter: 40
58
+ }]
59
+
60
+ await entity.insert({
61
+ inputs: posts
62
+ })
63
+
64
+ await entity.updateMany({
65
+ where: {
66
+ counter: {
67
+ gte: 30
68
+ }
69
+ },
70
+ input: {
71
+ title: 'Updated title'
72
+ }
73
+ })
74
+
75
+ const updatedPosts = await entity.find({})
76
+
77
+ same(updatedPosts, [{
78
+ id: '1',
79
+ title: 'Dog',
80
+ longText: 'Foo',
81
+ counter: 10
82
+ }, {
83
+ id: '2',
84
+ title: 'Cat',
85
+ longText: 'Bar',
86
+ counter: 20
87
+ }, {
88
+ id: '3',
89
+ title: 'Updated title',
90
+ longText: 'Baz',
91
+ counter: 30
92
+ }, {
93
+ id: '4',
94
+ title: 'Updated title',
95
+ longText: 'A duck tale',
96
+ counter: 40
97
+ }])
98
+ })
99
+
100
+ test('updateMany will return the updated values', async ({ pass, teardown, same }) => {
101
+ const mapper = await connect({
102
+ ...connInfo,
103
+ log: fakeLogger,
104
+ async onDatabaseLoad (db, sql) {
105
+ teardown(() => db.dispose())
106
+ pass('onDatabaseLoad called')
107
+
108
+ await clear(db, sql)
109
+
110
+ if (isSQLite) {
111
+ await db.query(sql`CREATE TABLE posts (
112
+ id INTEGER PRIMARY KEY,
113
+ title VARCHAR(42),
114
+ long_text TEXT,
115
+ counter INTEGER
116
+ );`)
117
+ } else {
118
+ await db.query(sql`CREATE TABLE posts (
119
+ id SERIAL PRIMARY KEY,
120
+ title VARCHAR(42),
121
+ long_text TEXT,
122
+ counter INTEGER
123
+ );`)
124
+ }
125
+ }
126
+ })
127
+
128
+ const entity = mapper.entities.post
129
+
130
+ const posts = [{
131
+ title: 'Dog',
132
+ longText: 'Foo',
133
+ counter: 10
134
+ }, {
135
+ title: 'Cat',
136
+ longText: 'Bar',
137
+ counter: 20
138
+ }, {
139
+ title: 'Mouse',
140
+ longText: 'Baz',
141
+ counter: 30
142
+ }, {
143
+ title: 'Duck',
144
+ longText: 'A duck tale',
145
+ counter: 40
146
+ }]
147
+
148
+ await entity.insert({
149
+ inputs: posts
150
+ })
151
+
152
+ const updatedPosts = await entity.updateMany({
153
+ where: {
154
+ counter: {
155
+ gte: 30
156
+ }
157
+ },
158
+ input: {
159
+ title: 'Updated title'
160
+ },
161
+ fields: ['id', 'counter']
162
+ })
163
+
164
+ same(updatedPosts, [{
165
+ id: '3',
166
+ counter: 30
167
+ }, {
168
+ id: '4',
169
+ counter: 40
170
+ }])
171
+ })
172
+
173
+ test('updateMany missing input', async ({ pass, teardown, rejects }) => {
174
+ const mapper = await connect({
175
+ ...connInfo,
176
+ log: fakeLogger,
177
+ async onDatabaseLoad (db, sql) {
178
+ teardown(() => db.dispose())
179
+ pass('onDatabaseLoad called')
180
+
181
+ await clear(db, sql)
182
+
183
+ if (isSQLite) {
184
+ await db.query(sql`CREATE TABLE posts (
185
+ id INTEGER PRIMARY KEY,
186
+ title VARCHAR(42),
187
+ long_text TEXT,
188
+ counter INTEGER
189
+ );`)
190
+ } else {
191
+ await db.query(sql`CREATE TABLE posts (
192
+ id SERIAL PRIMARY KEY,
193
+ title VARCHAR(42),
194
+ long_text TEXT,
195
+ counter INTEGER
196
+ );`)
197
+ }
198
+ }
199
+ })
200
+
201
+ const entity = mapper.entities.post
202
+
203
+ const posts = [{
204
+ title: 'Dog',
205
+ longText: 'Foo',
206
+ counter: 10
207
+ }, {
208
+ title: 'Cat',
209
+ longText: 'Bar',
210
+ counter: 20
211
+ }, {
212
+ title: 'Mouse',
213
+ longText: 'Baz',
214
+ counter: 30
215
+ }, {
216
+ title: 'Duck',
217
+ longText: 'A duck tale',
218
+ counter: 40
219
+ }]
220
+
221
+ await entity.insert({
222
+ inputs: posts
223
+ })
224
+
225
+ rejects(entity.updateMany({
226
+ where: {
227
+ counter: {
228
+ gte: 30
229
+ }
230
+ }
231
+ }), new Error('Input not provided.'))
232
+ })
233
+
234
+ test('updateMany successful and update updated_at', async ({ pass, teardown, same, notSame }) => {
235
+ const mapper = await connect({
236
+ ...connInfo,
237
+ autoTimestamp: true,
238
+ log: fakeLogger,
239
+ async onDatabaseLoad (db, sql) {
240
+ teardown(() => db.dispose())
241
+ pass('onDatabaseLoad called')
242
+
243
+ await clear(db, sql)
244
+
245
+ if (isSQLite) {
246
+ await db.query(sql`CREATE TABLE posts (
247
+ id INTEGER PRIMARY KEY,
248
+ title VARCHAR(42),
249
+ long_text TEXT,
250
+ counter INTEGER,
251
+ inserted_at TIMESTAMP,
252
+ updated_at TIMESTAMP
253
+ );`)
254
+ } else if (isMysql) {
255
+ await db.query(sql`CREATE TABLE posts (
256
+ id SERIAL PRIMARY KEY,
257
+ title VARCHAR(42),
258
+ long_text TEXT,
259
+ counter INTEGER,
260
+ inserted_at TIMESTAMP NULL DEFAULT NULL,
261
+ updated_at TIMESTAMP NULL DEFAULT NULL
262
+ );`)
263
+ } else {
264
+ await db.query(sql`CREATE TABLE posts (
265
+ id SERIAL PRIMARY KEY,
266
+ title VARCHAR(42),
267
+ long_text TEXT,
268
+ counter INTEGER,
269
+ inserted_at TIMESTAMP,
270
+ updated_at TIMESTAMP
271
+ );`)
272
+ }
273
+ }
274
+ })
275
+
276
+ const entity = mapper.entities.post
277
+
278
+ const posts = [{
279
+ title: 'Dog',
280
+ longText: 'Foo',
281
+ counter: 10
282
+ }, {
283
+ title: 'Cat',
284
+ longText: 'Bar',
285
+ counter: 20
286
+ }, {
287
+ title: 'Mouse',
288
+ longText: 'Baz',
289
+ counter: 30
290
+ }, {
291
+ title: 'Duck',
292
+ longText: 'A duck tale',
293
+ counter: 40
294
+ }]
295
+
296
+ await entity.insert({
297
+ inputs: posts
298
+ })
299
+ const createdPost3 = (await entity.find({ where: { id: { eq: '3' } } }))[0]
300
+
301
+ await setTimeout(1000) // await 1s
302
+
303
+ await entity.updateMany({
304
+ where: {
305
+ counter: {
306
+ gte: 30
307
+ }
308
+ },
309
+ input: {
310
+ title: 'Updated title'
311
+ }
312
+ })
313
+
314
+ const updatedPost3 = (await entity.find({ where: { id: { eq: '3' } } }))[0]
315
+ same(updatedPost3.title, 'Updated title')
316
+ same(createdPost3.insertedAt, updatedPost3.insertedAt)
317
+ notSame(createdPost3.updatedAt, updatedPost3.updatedAt)
318
+ })
@@ -434,3 +434,143 @@ test('is NULL', async ({ pass, teardown, same, equal }) => {
434
434
  title: 'Dog'
435
435
  }])
436
436
  })
437
+
438
+ test('LIKE', async ({ pass, teardown, same, equal }) => {
439
+ const mapper = await connect({
440
+ ...connInfo,
441
+ log: fakeLogger,
442
+ async onDatabaseLoad (db, sql) {
443
+ teardown(() => db.dispose())
444
+ pass('onDatabaseLoad called')
445
+
446
+ await clear(db, sql)
447
+
448
+ if (isSQLite) {
449
+ await db.query(sql`CREATE TABLE posts (
450
+ id INTEGER PRIMARY KEY,
451
+ title VARCHAR(42),
452
+ long_text TEXT,
453
+ counter INTEGER
454
+ );`)
455
+ } else {
456
+ await db.query(sql`CREATE TABLE posts (
457
+ id SERIAL PRIMARY KEY,
458
+ title VARCHAR(42),
459
+ long_text TEXT,
460
+ counter INTEGER
461
+ );`)
462
+ }
463
+ }
464
+ })
465
+
466
+ const entity = mapper.entities.post
467
+
468
+ const posts = [
469
+ {
470
+ title: 'Dog',
471
+ longText: 'The Dog barks',
472
+ counter: 1
473
+ },
474
+ {
475
+ title: 'Cat',
476
+ longText: 'The Cat meows',
477
+ counter: 2
478
+ },
479
+ {
480
+ title: 'Potato',
481
+ longText: 'The Potato is vegetable',
482
+ counter: 3
483
+ },
484
+ {
485
+ title: 'atmosphere',
486
+ longText: 'The atmosphere is not a sphere',
487
+ counter: 4
488
+ },
489
+ {
490
+ title: 'planet',
491
+ longText: 'The planet have atmosphere',
492
+ counter: 14
493
+ }
494
+ ]
495
+
496
+ await entity.insert({
497
+ inputs: posts
498
+ })
499
+
500
+ same(await entity.find({ where: { title: { like: '%at' } } }), [{
501
+ id: '2',
502
+ title: 'Cat',
503
+ longText: 'The Cat meows',
504
+ counter: 2
505
+ }], 'where: { title: { like: \'%at\' } }')
506
+
507
+ same(await entity.find({ where: { title: { like: '%at%' } } }), [{
508
+ id: '2',
509
+ title: 'Cat',
510
+ longText: 'The Cat meows',
511
+ counter: 2
512
+ },
513
+ {
514
+ id: '3',
515
+ title: 'Potato',
516
+ longText: 'The Potato is vegetable',
517
+ counter: 3
518
+ },
519
+ {
520
+ id: '4',
521
+ title: 'atmosphere',
522
+ longText: 'The atmosphere is not a sphere',
523
+ counter: 4
524
+ }], 'where: { title: { like: \'%at%\' } }')
525
+
526
+ same(await entity.find({ where: { title: { like: 'at%' } } }), [{
527
+ id: '4',
528
+ title: 'atmosphere',
529
+ longText: 'The atmosphere is not a sphere',
530
+ counter: 4
531
+ }], 'where: { title: { like: \'at%\' } }')
532
+
533
+ same(await entity.find({ where: { longText: { like: '%is%' } } }), [{
534
+ id: '3',
535
+ title: 'Potato',
536
+ longText: 'The Potato is vegetable',
537
+ counter: 3
538
+ },
539
+ {
540
+ id: '4',
541
+ title: 'atmosphere',
542
+ longText: 'The atmosphere is not a sphere',
543
+ counter: 4
544
+ }], 'where: { longText: { like: \'%is%\' } }')
545
+
546
+ same(await entity.find({ where: { longText: { like: null } } }), [], 'where: { longText: { like: null } }')
547
+
548
+ same(await entity.find({ where: { counter: { like: 4 } } }), [{
549
+ id: '4',
550
+ title: 'atmosphere',
551
+ longText: 'The atmosphere is not a sphere',
552
+ counter: 4
553
+ }], 'where: { counter: { like: 4 } }')
554
+
555
+ same(await entity.find({ where: { counter: { like: '%4' } } }), [{
556
+ id: '4',
557
+ title: 'atmosphere',
558
+ longText: 'The atmosphere is not a sphere',
559
+ counter: 4
560
+ },
561
+ {
562
+ id: '5',
563
+ title: 'planet',
564
+ longText: 'The planet have atmosphere',
565
+ counter: 14
566
+ }], 'where: { counter: { like: \'%4\' } }')
567
+
568
+ same(await entity.find({ where: { counter: { like: '4%' } } }), [{
569
+ id: '4',
570
+ title: 'atmosphere',
571
+ longText: 'The atmosphere is not a sphere',
572
+ counter: 4
573
+ }], 'where: { counter: { like: \'4%\' } }')
574
+
575
+ same(await entity.find({ where: { counter: { like: null } } }), [], 'where: { counter: { like: null } }')
576
+ })