@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.
- package/lib/entity.js +9 -0
- package/mapper.js +13 -0
- package/package.json +3 -2
- package/test/composite.test.js +0 -162
- package/test/create-connection.test.js +0 -39
- package/test/entity.test.js +0 -1125
- package/test/entity_transaction.test.js +0 -132
- package/test/helper.js +0 -112
- package/test/hooks.test.js +0 -325
- package/test/inserted_at_updated_at.test.js +0 -132
- package/test/mapper.test.js +0 -325
- package/test/no-primary-key.test.js +0 -79
- package/test/or.test.js +0 -193
- package/test/schema.test.js +0 -474
- package/test/types/mapper.test-d.ts +0 -139
- package/test/updateMany.test.js +0 -319
- package/test/where.test.js +0 -889
package/test/schema.test.js
DELETED
|
@@ -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
|
-
})
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import { expectType } from 'tsd'
|
|
2
|
-
import { SQL, SQLQuery } from '@databases/sql'
|
|
3
|
-
import { fastify, FastifyInstance, FastifyReply } from 'fastify'
|
|
4
|
-
import { FastifyError } from '@fastify/error'
|
|
5
|
-
import {
|
|
6
|
-
connect,
|
|
7
|
-
plugin,
|
|
8
|
-
utils,
|
|
9
|
-
Entity,
|
|
10
|
-
DBEntityField,
|
|
11
|
-
Database,
|
|
12
|
-
SQLMapperPluginInterface,
|
|
13
|
-
EntityHooks,
|
|
14
|
-
createConnectionPool,
|
|
15
|
-
Entities,
|
|
16
|
-
errors
|
|
17
|
-
} from '../../mapper'
|
|
18
|
-
|
|
19
|
-
const log = {
|
|
20
|
-
trace() { },
|
|
21
|
-
error() { },
|
|
22
|
-
warn() { }
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
declare module 'fastify' {
|
|
26
|
-
interface FastifyInstance {
|
|
27
|
-
platformatic: SQLMapperPluginInterface<Entities>
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const pluginOptions: SQLMapperPluginInterface<Entities> = await connect<Entities>({ connectionString: '', log })
|
|
32
|
-
expectType<Database>(pluginOptions.db)
|
|
33
|
-
expectType<SQL>(pluginOptions.sql)
|
|
34
|
-
expectType<{ [entityName: string]: Entity }>(pluginOptions.entities)
|
|
35
|
-
|
|
36
|
-
expectType<Promise<void>>(pluginOptions.cleanUpAllEntities())
|
|
37
|
-
|
|
38
|
-
interface EntityFields {
|
|
39
|
-
id: number,
|
|
40
|
-
name: string,
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const entity: Entity<EntityFields> = pluginOptions.entities.entityName
|
|
44
|
-
expectType<string>(entity.name)
|
|
45
|
-
expectType<string>(entity.singularName)
|
|
46
|
-
expectType<string>(entity.pluralName)
|
|
47
|
-
expectType<string>(entity.primaryKey)
|
|
48
|
-
expectType<string>(entity.table)
|
|
49
|
-
expectType<any[]>(entity.relations)
|
|
50
|
-
expectType<{ [columnName: string]: DBEntityField }>(entity.fields)
|
|
51
|
-
expectType<{ [columnName: string]: DBEntityField }>(entity.camelCasedFields)
|
|
52
|
-
expectType<(input: { [columnName: string]: any }) => { [columnName: string]: any }>(entity.fixInput)
|
|
53
|
-
expectType<(input: { [columnName: string]: any }) => { [columnName: string]: any }>(entity.fixOutput)
|
|
54
|
-
expectType<Partial<EntityFields>[]>(await entity.find())
|
|
55
|
-
expectType<Partial<EntityFields>[]>(await entity.insert({ inputs: [{ id: 1, name: 'test' }] }))
|
|
56
|
-
expectType<Partial<EntityFields>>(await entity.save({ input: { id: 1, name: 'test' } }))
|
|
57
|
-
expectType<Partial<EntityFields>[]>(await entity.delete())
|
|
58
|
-
expectType<number>(await entity.count())
|
|
59
|
-
|
|
60
|
-
const entityHooks: EntityHooks = {
|
|
61
|
-
async find(originalFind: typeof entity.find, ...options: Parameters<typeof entity.find>): ReturnType<typeof entity.find> { return [] },
|
|
62
|
-
async insert(originalInsert: typeof entity.insert, ...options: Parameters<typeof entity.insert>): ReturnType<typeof entity.insert> { return [] },
|
|
63
|
-
async save(originalSave: typeof entity.save, ...options: Parameters<typeof entity.save>): ReturnType<typeof entity.save> { return {} },
|
|
64
|
-
async delete(originalDelete: typeof entity.delete, ...options: Parameters<typeof entity.delete>): ReturnType<typeof entity.delete> { return [] },
|
|
65
|
-
async count(originalCount: typeof entity.count, ...options: Parameters<typeof entity.count>): ReturnType<typeof entity.count> { return 0 },
|
|
66
|
-
}
|
|
67
|
-
expectType<EntityHooks>(entityHooks)
|
|
68
|
-
expectType<SQLMapperPluginInterface<Entities>>(await connect<Entities>({ connectionString: '', log }))
|
|
69
|
-
expectType<SQLMapperPluginInterface<Entities>>(await connect<Entities>({ connectionString: '', autoTimestamp: true, log }))
|
|
70
|
-
expectType<SQLMapperPluginInterface<Entities>>(await connect<Entities>({ connectionString: '', hooks: {}, log }))
|
|
71
|
-
expectType<SQLMapperPluginInterface<Entities>>(await connect<Entities>({
|
|
72
|
-
connectionString: '', hooks: {
|
|
73
|
-
Page: entityHooks
|
|
74
|
-
},
|
|
75
|
-
log
|
|
76
|
-
}))
|
|
77
|
-
expectType<SQLMapperPluginInterface<Entities>>(await connect<Entities>({ connectionString: '', ignore: {}, log }))
|
|
78
|
-
expectType<SQLMapperPluginInterface<Entities>>(await connect<Entities>({
|
|
79
|
-
connectionString: '', log, onDatabaseLoad(db: Database, sql: SQL) {
|
|
80
|
-
expectType<(query: SQLQuery) => Promise<any[]>>(db.query)
|
|
81
|
-
expectType<() => Promise<void>>(db.dispose)
|
|
82
|
-
expectType<boolean | undefined>(pluginOptions.db.isMySql)
|
|
83
|
-
expectType<boolean | undefined>(pluginOptions.db.isMariaDB)
|
|
84
|
-
expectType<boolean | undefined>(pluginOptions.db.isSQLite)
|
|
85
|
-
expectType<boolean | undefined>(pluginOptions.db.isPg)
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
}))
|
|
89
|
-
|
|
90
|
-
const instance: FastifyInstance = fastify()
|
|
91
|
-
instance.register(plugin, { connectionString: '', autoTimestamp: true })
|
|
92
|
-
instance.register((instance) => {
|
|
93
|
-
expectType<SQLMapperPluginInterface<Entities>>(instance.platformatic)
|
|
94
|
-
|
|
95
|
-
instance.platformatic.addEntityHooks<EntityFields>('something', {
|
|
96
|
-
async find(originalFind, options) {
|
|
97
|
-
expectType<Partial<EntityFields>[]>(await originalFind())
|
|
98
|
-
expectType<Parameters<typeof entity.find>[0]>(options)
|
|
99
|
-
|
|
100
|
-
return [{
|
|
101
|
-
id: 42
|
|
102
|
-
}]
|
|
103
|
-
}
|
|
104
|
-
})
|
|
105
|
-
|
|
106
|
-
instance.get('/', async (request, reply) => {
|
|
107
|
-
const ctx = request.platformaticContext
|
|
108
|
-
expectType<FastifyInstance>(ctx.app)
|
|
109
|
-
expectType<FastifyReply>(ctx.reply)
|
|
110
|
-
await instance.platformatic.cleanUpAllEntities()
|
|
111
|
-
})
|
|
112
|
-
})
|
|
113
|
-
|
|
114
|
-
expectType<(str: string) => string>(utils.toSingular)
|
|
115
|
-
|
|
116
|
-
expectType<Promise<{ db: Database, sql: SQL }>>(createConnectionPool({ connectionString: '', log }))
|
|
117
|
-
|
|
118
|
-
// Errors
|
|
119
|
-
type ErrorWithNoParams = () => FastifyError
|
|
120
|
-
type ErrorWithOneParam = (param: string) => FastifyError
|
|
121
|
-
type ErrorWithOneAnyParam = (param: any) => FastifyError
|
|
122
|
-
type ErrorWithTwoParams = (param1: string, param2: string) => FastifyError
|
|
123
|
-
|
|
124
|
-
expectType<ErrorWithOneParam>(errors.CannotFindEntityError)
|
|
125
|
-
expectType<ErrorWithNoParams>(errors.SpecifyProtocolError)
|
|
126
|
-
expectType<ErrorWithNoParams>(errors.ConnectionStringRequiredError)
|
|
127
|
-
expectType<ErrorWithOneAnyParam>(errors.TableMustBeAStringError)
|
|
128
|
-
expectType<ErrorWithOneParam>(errors.UnknownFieldError)
|
|
129
|
-
expectType<ErrorWithNoParams>(errors.InputNotProvidedError)
|
|
130
|
-
expectType<ErrorWithOneParam>(errors.UnsupportedWhereClauseError)
|
|
131
|
-
expectType<ErrorWithNoParams>(errors.UnsupportedOperatorForArrayFieldError)
|
|
132
|
-
expectType<ErrorWithNoParams>(errors.UnsupportedOperatorForNonArrayFieldError)
|
|
133
|
-
expectType<ErrorWithOneParam>(errors.ParamNotAllowedError)
|
|
134
|
-
expectType<ErrorWithTwoParams>(errors.InvalidPrimaryKeyTypeError)
|
|
135
|
-
expectType<ErrorWithTwoParams>(errors.ParamLimitNotAllowedError)
|
|
136
|
-
expectType<ErrorWithOneParam>(errors.ParamLimitMustBeNotNegativeError)
|
|
137
|
-
expectType<ErrorWithOneParam>(errors.MissingValueForPrimaryKeyError)
|
|
138
|
-
expectType<ErrorWithNoParams>(errors.SQLiteOnlySupportsAutoIncrementOnOneColumnError)
|
|
139
|
-
|