@platformatic/sql-mapper 0.45.1 → 0.46.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,319 +0,0 @@
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
-
8
- const fakeLogger = {
9
- trace: () => {},
10
- error: () => {}
11
- }
12
-
13
- test('updateMany successful', async ({ pass, teardown, same }) => {
14
- const mapper = await connect({
15
- ...connInfo,
16
- log: fakeLogger,
17
- async onDatabaseLoad (db, sql) {
18
- teardown(() => db.dispose())
19
- pass('onDatabaseLoad called')
20
-
21
- await clear(db, sql)
22
-
23
- if (isSQLite) {
24
- await db.query(sql`CREATE TABLE posts (
25
- id INTEGER PRIMARY KEY,
26
- title VARCHAR(42),
27
- long_text TEXT,
28
- counter INTEGER
29
- );`)
30
- } else {
31
- await db.query(sql`CREATE TABLE posts (
32
- id SERIAL PRIMARY KEY,
33
- title VARCHAR(42),
34
- long_text TEXT,
35
- counter INTEGER
36
- );`)
37
- }
38
- }
39
- })
40
-
41
- const entity = mapper.entities.post
42
-
43
- const posts = [{
44
- title: 'Dog',
45
- longText: 'Foo',
46
- counter: 10
47
- }, {
48
- title: 'Cat',
49
- longText: 'Bar',
50
- counter: 20
51
- }, {
52
- title: 'Mouse',
53
- longText: 'Baz',
54
- counter: 30
55
- }, {
56
- title: 'Duck',
57
- longText: 'A duck tale',
58
- counter: 40
59
- }]
60
-
61
- await entity.insert({
62
- inputs: posts
63
- })
64
-
65
- await entity.updateMany({
66
- where: {
67
- counter: {
68
- gte: 30
69
- }
70
- },
71
- input: {
72
- title: 'Updated title'
73
- }
74
- })
75
-
76
- const updatedPosts = await entity.find({})
77
-
78
- same(updatedPosts, [{
79
- id: '1',
80
- title: 'Dog',
81
- longText: 'Foo',
82
- counter: 10
83
- }, {
84
- id: '2',
85
- title: 'Cat',
86
- longText: 'Bar',
87
- counter: 20
88
- }, {
89
- id: '3',
90
- title: 'Updated title',
91
- longText: 'Baz',
92
- counter: 30
93
- }, {
94
- id: '4',
95
- title: 'Updated title',
96
- longText: 'A duck tale',
97
- counter: 40
98
- }])
99
- })
100
-
101
- test('updateMany will return the updated values', async ({ pass, teardown, same }) => {
102
- const mapper = await connect({
103
- ...connInfo,
104
- log: fakeLogger,
105
- async onDatabaseLoad (db, sql) {
106
- teardown(() => db.dispose())
107
- pass('onDatabaseLoad called')
108
-
109
- await clear(db, sql)
110
-
111
- if (isSQLite) {
112
- await db.query(sql`CREATE TABLE posts (
113
- id INTEGER PRIMARY KEY,
114
- title VARCHAR(42),
115
- long_text TEXT,
116
- counter INTEGER
117
- );`)
118
- } else {
119
- await db.query(sql`CREATE TABLE posts (
120
- id SERIAL PRIMARY KEY,
121
- title VARCHAR(42),
122
- long_text TEXT,
123
- counter INTEGER
124
- );`)
125
- }
126
- }
127
- })
128
-
129
- const entity = mapper.entities.post
130
-
131
- const posts = [{
132
- title: 'Dog',
133
- longText: 'Foo',
134
- counter: 10
135
- }, {
136
- title: 'Cat',
137
- longText: 'Bar',
138
- counter: 20
139
- }, {
140
- title: 'Mouse',
141
- longText: 'Baz',
142
- counter: 30
143
- }, {
144
- title: 'Duck',
145
- longText: 'A duck tale',
146
- counter: 40
147
- }]
148
-
149
- await entity.insert({
150
- inputs: posts
151
- })
152
-
153
- const updatedPosts = await entity.updateMany({
154
- where: {
155
- counter: {
156
- gte: 30
157
- }
158
- },
159
- input: {
160
- title: 'Updated title'
161
- },
162
- fields: ['id', 'counter']
163
- })
164
-
165
- same(updatedPosts, [{
166
- id: '3',
167
- counter: 30
168
- }, {
169
- id: '4',
170
- counter: 40
171
- }])
172
- })
173
-
174
- test('updateMany missing input', async ({ pass, teardown, rejects }) => {
175
- const mapper = await connect({
176
- ...connInfo,
177
- log: fakeLogger,
178
- async onDatabaseLoad (db, sql) {
179
- teardown(() => db.dispose())
180
- pass('onDatabaseLoad called')
181
-
182
- await clear(db, sql)
183
-
184
- if (isSQLite) {
185
- await db.query(sql`CREATE TABLE posts (
186
- id INTEGER PRIMARY KEY,
187
- title VARCHAR(42),
188
- long_text TEXT,
189
- counter INTEGER
190
- );`)
191
- } else {
192
- await db.query(sql`CREATE TABLE posts (
193
- id SERIAL PRIMARY KEY,
194
- title VARCHAR(42),
195
- long_text TEXT,
196
- counter INTEGER
197
- );`)
198
- }
199
- }
200
- })
201
-
202
- const entity = mapper.entities.post
203
-
204
- const posts = [{
205
- title: 'Dog',
206
- longText: 'Foo',
207
- counter: 10
208
- }, {
209
- title: 'Cat',
210
- longText: 'Bar',
211
- counter: 20
212
- }, {
213
- title: 'Mouse',
214
- longText: 'Baz',
215
- counter: 30
216
- }, {
217
- title: 'Duck',
218
- longText: 'A duck tale',
219
- counter: 40
220
- }]
221
-
222
- await entity.insert({
223
- inputs: posts
224
- })
225
-
226
- rejects(entity.updateMany({
227
- where: {
228
- counter: {
229
- gte: 30
230
- }
231
- }
232
- }), new Error('Input not provided.'))
233
- })
234
-
235
- test('updateMany successful and update updated_at', async ({ pass, teardown, same, notSame }) => {
236
- const mapper = await connect({
237
- ...connInfo,
238
- autoTimestamp: true,
239
- log: fakeLogger,
240
- async onDatabaseLoad (db, sql) {
241
- teardown(() => db.dispose())
242
- pass('onDatabaseLoad called')
243
-
244
- await clear(db, sql)
245
-
246
- if (isSQLite) {
247
- await db.query(sql`CREATE TABLE posts (
248
- id INTEGER PRIMARY KEY,
249
- title VARCHAR(42),
250
- long_text TEXT,
251
- counter INTEGER,
252
- created_at TIMESTAMP,
253
- updated_at TIMESTAMP
254
- );`)
255
- } else if (isMysql) {
256
- await db.query(sql`CREATE TABLE posts (
257
- id SERIAL PRIMARY KEY,
258
- title VARCHAR(42),
259
- long_text TEXT,
260
- counter INTEGER,
261
- created_at TIMESTAMP NULL DEFAULT NULL,
262
- updated_at TIMESTAMP NULL DEFAULT NULL
263
- );`)
264
- } else {
265
- await db.query(sql`CREATE TABLE posts (
266
- id SERIAL PRIMARY KEY,
267
- title VARCHAR(42),
268
- long_text TEXT,
269
- counter INTEGER,
270
- created_at TIMESTAMP,
271
- updated_at TIMESTAMP
272
- );`)
273
- }
274
- }
275
- })
276
-
277
- const entity = mapper.entities.post
278
-
279
- const posts = [{
280
- title: 'Dog',
281
- longText: 'Foo',
282
- counter: 10
283
- }, {
284
- title: 'Cat',
285
- longText: 'Bar',
286
- counter: 20
287
- }, {
288
- title: 'Mouse',
289
- longText: 'Baz',
290
- counter: 30
291
- }, {
292
- title: 'Duck',
293
- longText: 'A duck tale',
294
- counter: 40
295
- }]
296
-
297
- await entity.insert({
298
- inputs: posts
299
- })
300
- const createdPost3 = (await entity.find({ where: { id: { eq: '3' } } }))[0]
301
-
302
- await setTimeout(1000) // await 1s
303
-
304
- await entity.updateMany({
305
- where: {
306
- counter: {
307
- gte: 30
308
- }
309
- },
310
- input: {
311
- title: 'Updated title'
312
- }
313
- })
314
-
315
- const updatedPost3 = (await entity.find({ where: { id: { eq: '3' } } }))[0]
316
- same(updatedPost3.title, 'Updated title')
317
- same(createdPost3.createdAt, updatedPost3.createdAt)
318
- notSame(createdPost3.updatedAt, updatedPost3.updatedAt)
319
- })