@platformatic/sql-mapper 0.46.1 → 0.47.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.
package/test/or.test.js DELETED
@@ -1,193 +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 fakeLogger = {
7
- trace: () => { },
8
- error: () => { }
9
- }
10
-
11
- test('where clause with or operation', async ({ pass, teardown, same }) => {
12
- const mapper = await connect({
13
- ...connInfo,
14
- autoTimestamp: true,
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 if (isMysql) {
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
- } else {
37
- await db.query(sql`CREATE TABLE posts (
38
- id SERIAL PRIMARY KEY,
39
- title VARCHAR(42),
40
- long_text TEXT,
41
- counter INTEGER
42
- );`)
43
- }
44
- }
45
- })
46
-
47
- const entity = mapper.entities.post
48
-
49
- const posts = [{
50
- title: 'Dog',
51
- longText: 'Foo',
52
- counter: 10
53
- }, {
54
- title: 'Cat',
55
- longText: 'Bar',
56
- counter: 20
57
- }, {
58
- title: 'Mouse',
59
- longText: 'Baz',
60
- counter: 30
61
- }, {
62
- title: 'Duck',
63
- longText: 'A duck tale',
64
- counter: 40
65
- }]
66
-
67
- await entity.insert({
68
- inputs: posts
69
- })
70
-
71
- {
72
- const data = await entity.find({
73
- where: {
74
- or: [
75
- {
76
- counter: {
77
- eq: 10
78
- }
79
- },
80
- {
81
- counter: {
82
- eq: 20
83
- }
84
- }
85
- ]
86
- }
87
- })
88
-
89
- same(data, [
90
- { id: '1', title: 'Dog', longText: 'Foo', counter: 10 },
91
- { id: '2', title: 'Cat', longText: 'Bar', counter: 20 }
92
- ])
93
- }
94
-
95
- {
96
- const data = await entity.find({
97
- where: {
98
- or: [
99
- {
100
- counter: {
101
- eq: 20
102
- }
103
- },
104
- {
105
- counter: {
106
- gte: 30
107
- }
108
- }
109
- ]
110
- }
111
- })
112
-
113
- same(data, [
114
- { id: '2', title: 'Cat', longText: 'Bar', counter: 20 },
115
- { id: '3', title: 'Mouse', longText: 'Baz', counter: 30 },
116
- { id: '4', title: 'Duck', longText: 'A duck tale', counter: 40 }
117
- ])
118
- }
119
-
120
- {
121
- const data = await entity.find({
122
- where: {
123
- or: [
124
- {
125
- title: {
126
- eq: 'Dog'
127
- }
128
- },
129
- {
130
- title: {
131
- eq: 'Duck'
132
- }
133
- }
134
- ]
135
- }
136
- })
137
-
138
- same(data, [
139
- { id: '1', title: 'Dog', longText: 'Foo', counter: 10 },
140
- { id: '4', title: 'Duck', longText: 'A duck tale', counter: 40 }
141
- ])
142
- }
143
-
144
- {
145
- const data = await entity.find({
146
- where: {
147
- or: [
148
- {
149
- title: {
150
- eq: 'Dog'
151
- }
152
- },
153
- {
154
- longText: {
155
- eq: 'Baz'
156
- }
157
- }
158
- ]
159
- }
160
- })
161
-
162
- same(data, [
163
- { id: '1', title: 'Dog', longText: 'Foo', counter: 10 },
164
- { id: '3', title: 'Mouse', longText: 'Baz', counter: 30 }
165
- ])
166
- }
167
-
168
- {
169
- const data = await entity.find({
170
- where: {
171
- counter: {
172
- in: [10, 20]
173
- },
174
- or: [
175
- {
176
- title: {
177
- eq: 'Dog'
178
- }
179
- },
180
- {
181
- longText: {
182
- eq: 'Baz'
183
- }
184
- }
185
- ]
186
- }
187
- })
188
-
189
- same(data, [
190
- { id: '1', title: 'Dog', longText: 'Foo', counter: 10 }
191
- ])
192
- }
193
- })
@@ -1,474 +0,0 @@
1
- const { test } = require('tap')
2
- const { connect } = require('..')
3
-
4
- const { connInfo, isSQLite, isMysql, isMysql8, isPg, clear } = require('./helper')
5
-
6
- const fakeLogger = {
7
- trace: () => {},
8
- error: () => {}
9
- }
10
-
11
- test('uses tables from different schemas', { skip: isSQLite }, async ({ pass, teardown, equal }) => {
12
- async function onDatabaseLoad (db, sql) {
13
- await clear(db, sql)
14
- teardown(() => db.dispose())
15
-
16
- await db.query(sql`CREATE SCHEMA IF NOT EXISTS test1;`)
17
- if (isMysql || isMysql8) {
18
- await db.query(sql`CREATE TABLE IF NOT EXISTS \`test1\`.\`pages\` (
19
- id SERIAL PRIMARY KEY,
20
- title VARCHAR(255) NOT NULL
21
- );`)
22
- } else {
23
- await db.query(sql`CREATE TABLE IF NOT EXISTS "test1"."pages" (
24
- id SERIAL PRIMARY KEY,
25
- title VARCHAR(255) NOT NULL
26
- );`)
27
- }
28
-
29
- await db.query(sql`CREATE SCHEMA IF NOT EXISTS test2;`)
30
-
31
- if (isMysql || isMysql8) {
32
- await db.query(sql`CREATE TABLE IF NOT EXISTS \`test2\`.\`users\` (
33
- id SERIAL PRIMARY KEY,
34
- username VARCHAR(255) NOT NULL
35
- );`)
36
- } else {
37
- await db.query(sql`CREATE TABLE IF NOT EXISTS "test2"."users" (
38
- id SERIAL PRIMARY KEY,
39
- username VARCHAR(255) NOT NULL
40
- );`)
41
- }
42
- }
43
- const mapper = await connect({
44
- connectionString: connInfo.connectionString,
45
- log: fakeLogger,
46
- onDatabaseLoad,
47
- ignore: {},
48
- hooks: {},
49
- schema: ['test1', 'test2']
50
- })
51
- const pageEntity = mapper.entities.test1Page
52
- equal(pageEntity.name, 'Test1Page')
53
- equal(pageEntity.singularName, 'test1Page')
54
- equal(pageEntity.pluralName, 'test1Pages')
55
- equal(pageEntity.schema, 'test1')
56
- const userEntity = mapper.entities.test2User
57
- equal(userEntity.name, 'Test2User')
58
- equal(userEntity.singularName, 'test2User')
59
- equal(userEntity.pluralName, 'test2Users')
60
- equal(userEntity.schema, 'test2')
61
- pass()
62
- })
63
-
64
- test('find enums correctly using schemas', { skip: isSQLite }, async ({ pass, teardown, equal, match }) => {
65
- async function onDatabaseLoad (db, sql) {
66
- await clear(db, sql)
67
- teardown(() => db.dispose())
68
-
69
- await db.query(sql`CREATE SCHEMA IF NOT EXISTS test1;`)
70
- if (isMysql || isMysql8) {
71
- await db.query(sql`
72
- CREATE TABLE IF NOT EXISTS \`test1\`.\`pages\` (
73
- id SERIAL PRIMARY KEY,
74
- title VARCHAR(255) NOT NULL,
75
- type ENUM ('blank', 'non-blank')
76
- );`)
77
- } else if (isPg) {
78
- await db.query(sql`
79
- CREATE TYPE pagetype as enum ('blank', 'non-blank');
80
- CREATE TABLE IF NOT EXISTS "test1"."pages" (
81
- id SERIAL PRIMARY KEY,
82
- title VARCHAR(255) NOT NULL,
83
- type pagetype
84
- );`)
85
- } else {
86
- await db.query(sql`CREATE TABLE IF NOT EXISTS "test1"."pages" (
87
- id SERIAL PRIMARY KEY,
88
- title VARCHAR(255) NOT NULL,
89
- type ENUM ('blank', 'non-blank')
90
- );`)
91
- }
92
- }
93
- const mapper = await connect({
94
- connectionString: connInfo.connectionString,
95
- log: fakeLogger,
96
- onDatabaseLoad,
97
- ignore: {},
98
- hooks: {},
99
- schema: ['test1']
100
- })
101
- const pageEntity = mapper.entities.test1Page
102
- equal(pageEntity.name, 'Test1Page')
103
- equal(pageEntity.singularName, 'test1Page')
104
- equal(pageEntity.pluralName, 'test1Pages')
105
- match(mapper.dbschema, [
106
- {
107
- schema: 'test1',
108
- table: 'pages',
109
- constraints: [
110
- {
111
- constraint_type: isMysql8 ? 'UNIQUE' : 'PRIMARY KEY'
112
- }
113
- ],
114
- columns: [
115
- {
116
- column_name: 'id',
117
- is_nullable: 'NO'
118
- },
119
- {
120
- column_name: 'title',
121
- is_nullable: 'NO'
122
- },
123
- {
124
- column_name: 'type',
125
- is_nullable: 'YES'
126
- }
127
- ]
128
- }
129
- ])
130
- pass()
131
- })
132
-
133
- test('if schema is empty array, should not load entities from tables in explicit schema', { skip: isSQLite }, async ({ pass, teardown, equal }) => {
134
- async function onDatabaseLoad (db, sql) {
135
- await clear(db, sql)
136
- teardown(() => db.dispose())
137
-
138
- await db.query(sql`CREATE SCHEMA IF NOT EXISTS test1;`)
139
- if (isMysql || isMysql8) {
140
- await db.query(sql`CREATE TABLE IF NOT EXISTS \`test1\`.\`pages\` (
141
- id SERIAL PRIMARY KEY,
142
- title VARCHAR(255) NOT NULL
143
- );`)
144
- } else {
145
- await db.query(sql`CREATE TABLE IF NOT EXISTS "test1"."pages" (
146
- id SERIAL PRIMARY KEY,
147
- title VARCHAR(255) NOT NULL
148
- );`)
149
- }
150
-
151
- await db.query(sql`CREATE SCHEMA IF NOT EXISTS test2;`)
152
-
153
- if (isMysql || isMysql8) {
154
- await db.query(sql`CREATE TABLE IF NOT EXISTS \`test2\`.\`users\` (
155
- id SERIAL PRIMARY KEY,
156
- username VARCHAR(255) NOT NULL
157
- );`)
158
- } else {
159
- await db.query(sql`CREATE TABLE IF NOT EXISTS "test2"."users" (
160
- id SERIAL PRIMARY KEY,
161
- username VARCHAR(255) NOT NULL
162
- );`)
163
- }
164
- }
165
- const mapper = await connect({
166
- connectionString: connInfo.connectionString,
167
- log: fakeLogger,
168
- onDatabaseLoad,
169
- ignore: {},
170
- hooks: {},
171
- schema: []
172
- })
173
-
174
- equal(Object.keys(mapper.entities).length, 0)
175
- pass()
176
- })
177
-
178
- test('[pg] if schema is empty array, should find entities only in default \'public\' schema', { skip: !isPg }, async ({ pass, teardown, equal }) => {
179
- async function onDatabaseLoad (db, sql) {
180
- await clear(db, sql)
181
- teardown(() => db.dispose())
182
-
183
- await db.query(sql`CREATE TABLE IF NOT EXISTS pages (
184
- id SERIAL PRIMARY KEY,
185
- title VARCHAR(255) NOT NULL
186
- );`)
187
-
188
- await db.query(sql`CREATE SCHEMA IF NOT EXISTS test2;`)
189
- await db.query(sql`CREATE TABLE IF NOT EXISTS "test2"."users" (
190
- id SERIAL PRIMARY KEY,
191
- username VARCHAR(255) NOT NULL
192
- );`)
193
- }
194
- const mapper = await connect({
195
- connectionString: connInfo.connectionString,
196
- log: fakeLogger,
197
- onDatabaseLoad,
198
- ignore: {},
199
- hooks: {},
200
- schema: []
201
- })
202
- equal(Object.keys(mapper.entities).length, 1)
203
- const pageEntity = mapper.entities.page
204
- equal(pageEntity.name, 'Page')
205
- equal(pageEntity.singularName, 'page')
206
- equal(pageEntity.pluralName, 'pages')
207
- equal(pageEntity.schema, 'public')
208
- pass()
209
- })
210
-
211
- test('[sqlite] if sqllite, ignores schema information', { skip: !isSQLite }, async ({ pass, teardown, equal }) => {
212
- async function onDatabaseLoad (db, sql) {
213
- await clear(db, sql)
214
- teardown(() => db.dispose())
215
- await db.query(sql`CREATE TABLE "pages" (
216
- "id" INTEGER PRIMARY KEY,
217
- "title" TEXT NOT NULL
218
- );`)
219
- }
220
- const mapper = await connect({
221
- connectionString: connInfo.connectionString,
222
- log: fakeLogger,
223
- onDatabaseLoad,
224
- ignore: {},
225
- hooks: {},
226
- schema: ['ignored', 'also_ignored']
227
- })
228
-
229
- equal(Object.keys(mapper.entities).length, 1)
230
- const pageEntity = mapper.entities.page
231
- equal(pageEntity.name, 'Page')
232
- equal(pageEntity.singularName, 'page')
233
- equal(pageEntity.pluralName, 'pages')
234
- equal(pageEntity.schema, null)
235
- pass()
236
- })
237
-
238
- test('addEntityHooks in entities with schema', { skip: isSQLite }, async ({ pass, teardown, same, equal, plan, fail, throws, end }) => {
239
- async function onDatabaseLoad (db, sql) {
240
- await clear(db, sql)
241
- teardown(() => db.dispose())
242
-
243
- await db.query(sql`CREATE SCHEMA IF NOT EXISTS test1;`)
244
- if (isMysql || isMysql8) {
245
- await db.query(sql`CREATE TABLE IF NOT EXISTS \`test1\`.\`pages\` (
246
- id SERIAL PRIMARY KEY,
247
- title VARCHAR(255) NOT NULL
248
- );`)
249
- } else {
250
- await db.query(sql`CREATE TABLE IF NOT EXISTS "test1"."pages" (
251
- id SERIAL PRIMARY KEY,
252
- title VARCHAR(255) NOT NULL
253
- );`)
254
- }
255
- }
256
-
257
- const mapper = await connect({
258
- ...connInfo,
259
- log: fakeLogger,
260
- onDatabaseLoad,
261
- schema: ['test1']
262
- })
263
-
264
- throws(() => mapper.addEntityHooks('user', {}), 'Cannot find entity user')
265
-
266
- mapper.addEntityHooks('test1Page', {
267
- noKey () {
268
- fail('noKey should never be called')
269
- },
270
- async save (original, { input, ctx, fields }) {
271
- pass('save called')
272
-
273
- if (!input.id) {
274
- same(input, {
275
- title: 'Hello'
276
- })
277
-
278
- return original({
279
- input: {
280
- title: 'Hello from hook'
281
- },
282
- fields
283
- })
284
- } else {
285
- same(input, {
286
- id: 1,
287
- title: 'Hello World'
288
- })
289
-
290
- return original({
291
- input: {
292
- id: 1,
293
- title: 'Hello from hook 2'
294
- },
295
- fields
296
- })
297
- }
298
- },
299
- async find (original, args) {
300
- pass('find called')
301
-
302
- same(args.where, {
303
- id: {
304
- eq: '1'
305
- }
306
- })
307
- args.where = {
308
- id: {
309
- eq: '2'
310
- }
311
- }
312
- same(args.fields, ['id', 'title'])
313
- return original(args)
314
- },
315
- async insert (original, args) {
316
- pass('insert called')
317
-
318
- same(args.inputs, [{
319
- title: 'hello'
320
- }, {
321
- title: 'world'
322
- }])
323
- same(args.fields, ['id', 'title'])
324
- return original(args)
325
- }
326
- })
327
-
328
- const entity = mapper.entities.test1Page
329
-
330
- same(await entity.save({ input: { title: 'Hello' } }), {
331
- id: 1,
332
- title: 'Hello from hook'
333
- })
334
-
335
- same(await entity.find({ where: { id: { eq: 1 } }, fields: ['id', 'title'] }), [])
336
-
337
- same(await entity.save({ input: { id: 1, title: 'Hello World' } }), {
338
- id: 1,
339
- title: 'Hello from hook 2'
340
- })
341
-
342
- await entity.insert({ inputs: [{ title: 'hello' }, { title: 'world' }], fields: ['id', 'title'] })
343
- end()
344
- })
345
-
346
- test('uses tables from different schemas with FK', { skip: isSQLite }, async ({ pass, teardown, equal }) => {
347
- async function onDatabaseLoad (db, sql) {
348
- await clear(db, sql)
349
- teardown(() => db.dispose())
350
-
351
- await db.query(sql`CREATE SCHEMA IF NOT EXISTS test1;`)
352
- if (isMysql || isMysql8) {
353
- await db.query(sql`CREATE TABLE IF NOT EXISTS \`test1\`.\`pages\` (
354
- id SERIAL PRIMARY KEY,
355
- title VARCHAR(255) NOT NULL
356
- );`)
357
- } else {
358
- await db.query(sql`CREATE TABLE IF NOT EXISTS "test1"."pages" (
359
- id SERIAL PRIMARY KEY,
360
- title VARCHAR(255) NOT NULL
361
- );`)
362
- }
363
-
364
- await db.query(sql`CREATE SCHEMA IF NOT EXISTS test2;`)
365
-
366
- if (isMysql || isMysql8) {
367
- await db.query(sql`CREATE TABLE IF NOT EXISTS \`test2\`.\`users\` (
368
- id SERIAL PRIMARY KEY,
369
- username VARCHAR(255) NOT NULL,
370
- page_id BIGINT UNSIGNED,
371
- FOREIGN KEY(page_id) REFERENCES test1.pages(id) ON DELETE CASCADE
372
- );`)
373
- } else {
374
- await db.query(sql`CREATE TABLE IF NOT EXISTS "test2"."users" (
375
- id SERIAL PRIMARY KEY,
376
- username VARCHAR(255) NOT NULL,
377
- page_id integer REFERENCES test1.pages(id)
378
- );`)
379
- }
380
- }
381
- const mapper = await connect({
382
- connectionString: connInfo.connectionString,
383
- log: fakeLogger,
384
- onDatabaseLoad,
385
- ignore: {},
386
- hooks: {},
387
- schema: ['test1', 'test2']
388
- })
389
- const pageEntity = mapper.entities.test1Page
390
- equal(pageEntity.name, 'Test1Page')
391
- equal(pageEntity.singularName, 'test1Page')
392
- equal(pageEntity.pluralName, 'test1Pages')
393
- equal(pageEntity.schema, 'test1')
394
- equal(pageEntity.relations.length, 0)
395
-
396
- const userEntity = mapper.entities.test2User
397
- equal(userEntity.name, 'Test2User')
398
- equal(userEntity.singularName, 'test2User')
399
- equal(userEntity.pluralName, 'test2Users')
400
- equal(userEntity.schema, 'test2')
401
- equal(userEntity.relations.length, 1)
402
- const userRelation = userEntity.relations[0]
403
- equal(userRelation.foreignEntityName, 'test1Page')
404
- equal(userRelation.entityName, 'test2User')
405
- pass()
406
- })
407
-
408
- test('recreate mapper from schema', async ({ pass, teardown, equal, match, fail }) => {
409
- async function onDatabaseLoad (db, sql) {
410
- await clear(db, sql)
411
- teardown(() => db.dispose())
412
-
413
- if (isMysql || isMysql8) {
414
- await db.query(sql`
415
- CREATE TABLE IF NOT EXISTS \`pages\` (
416
- id SERIAL PRIMARY KEY,
417
- title VARCHAR(255) NOT NULL
418
- );`)
419
- } else if (isPg) {
420
- await db.query(sql`
421
- CREATE TABLE IF NOT EXISTS "pages" (
422
- id SERIAL PRIMARY KEY,
423
- title VARCHAR(255) NOT NULL
424
- );`)
425
- } else if (isSQLite) {
426
- await db.query(sql`
427
- CREATE TABLE IF NOT EXISTS "pages" (
428
- id INTEGER PRIMARY KEY,
429
- title VARCHAR(255) NOT NULL
430
- );`)
431
- } else {
432
- await db.query(sql`CREATE TABLE IF NOT EXISTS "pages" (
433
- id SERIAL PRIMARY KEY,
434
- title VARCHAR(255) NOT NULL,
435
- );`)
436
- }
437
- }
438
- const mapper = await connect({
439
- connectionString: connInfo.connectionString,
440
- log: fakeLogger,
441
- onDatabaseLoad,
442
- ignore: {},
443
- hooks: {}
444
- })
445
- const dbschema = mapper.dbschema
446
- const knownQueries = [
447
- 'SELECT VERSION()'
448
- ]
449
- const mapper2 = await connect({
450
- connectionString: connInfo.connectionString,
451
- log: {
452
- trace (msg) {
453
- if (knownQueries.indexOf(msg.query?.text) < 0) {
454
- console.log(msg)
455
- fail('no trace')
456
- }
457
- },
458
- error (...msg) {
459
- console.log(...msg)
460
- fail('no error')
461
- }
462
- },
463
- dbschema,
464
- ignore: {},
465
- hooks: {}
466
- })
467
- teardown(() => mapper2.db.dispose())
468
-
469
- const pageEntity = mapper2.entities.page
470
- equal(pageEntity.name, 'Page')
471
- equal(pageEntity.singularName, 'page')
472
- equal(pageEntity.pluralName, 'pages')
473
- pass()
474
- })