pwi-plata-type 0.4.166 → 0.4.167

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.
Files changed (101) hide show
  1. package/README.md +2 -2
  2. package/__BUILD__/libs/sql.js +4 -1
  3. package/__BUILD__/libs/sql.js.map +1 -1
  4. package/package.json +53 -53
  5. package/src/@types/build.d.ts +26 -26
  6. package/src/@types/cluster.actions.d.ts +27 -27
  7. package/src/@types/cluster.d.ts +55 -55
  8. package/src/@types/create-cli.d.ts +8 -8
  9. package/src/@types/exported.d.ts +31 -31
  10. package/src/@types/fs.d.ts +6 -6
  11. package/src/@types/global.d.ts +113 -113
  12. package/src/@types/knex.d.ts +13 -13
  13. package/src/@types/model.d.ts +143 -143
  14. package/src/@types/router.d.ts +107 -107
  15. package/src/@types/sql.d.ts +75 -75
  16. package/src/@types/swagger.d.ts +22 -22
  17. package/src/bin/extras/create-api-cli.ts +218 -218
  18. package/src/bin/extras/create-cluster-cli.ts +160 -160
  19. package/src/bin/extras/create-lib-cli.ts +84 -84
  20. package/src/bin/extras/plata-create-tools.ts +97 -97
  21. package/src/bin/plata-api.ts +189 -189
  22. package/src/bin/plata-build.ts +17 -17
  23. package/src/bin/plata-cluster.ts +42 -42
  24. package/src/bin/plata-cmd.ts +18 -18
  25. package/src/bin/plata-create.ts +49 -49
  26. package/src/bin/plata-runtime.ts +131 -131
  27. package/src/bin/plata-swagger.ts +99 -99
  28. package/src/bin/runtime/_setupRuntime.ts +48 -48
  29. package/src/bin/runtime/_types.d.ts +7 -7
  30. package/src/bin/runtime/class.ts +20 -20
  31. package/src/bin/runtime/dirs.ts +25 -25
  32. package/src/bin/runtime/effect.ts +60 -60
  33. package/src/bin/runtime/fs.ts +10 -10
  34. package/src/bin/runtime/promises.ts +18 -18
  35. package/src/bin/runtime/require.ts +113 -113
  36. package/src/bin/runtime/sql.ts +12 -12
  37. package/src/bin/runtime/switch.ts +60 -60
  38. package/src/index.ts +19 -19
  39. package/src/libs/axios.ts +51 -51
  40. package/src/libs/build.ts +162 -162
  41. package/src/libs/cluster/cluster-controller.ts +93 -93
  42. package/src/libs/cluster.ts +330 -330
  43. package/src/libs/custom.ts +15 -15
  44. package/src/libs/env.ts +10 -10
  45. package/src/libs/fs.ts +399 -399
  46. package/src/libs/hot-reload.ts +63 -63
  47. package/src/libs/models/index.ts +4 -4
  48. package/src/libs/models/model-template-combine.ts +93 -93
  49. package/src/libs/models/model-template-env.ts +58 -58
  50. package/src/libs/models/model-template-union.ts +83 -83
  51. package/src/libs/models/model-template.ts +107 -107
  52. package/src/libs/models/model-validations.ts +443 -443
  53. package/src/libs/models/tools.ts +194 -194
  54. package/src/libs/router.ts +185 -185
  55. package/src/libs/sql.ts +792 -788
  56. package/src/libs/swagger.ts +81 -81
  57. package/src/libs/tools.ts +240 -240
  58. package/src/scripts/post-install/api.ts +32 -32
  59. package/src/scripts/post-install/cluster.ts +28 -28
  60. package/src/scripts/post-install/lib.ts +25 -25
  61. package/src/scripts/post-install.ts +64 -64
  62. package/src/scripts/pre-test.ts +33 -33
  63. package/src/tests/assets/sql/example.sql +76 -76
  64. package/src/tests/config.test.ts +12 -12
  65. package/src/tests/effect.test.ts +62 -62
  66. package/src/tests/model.env.test.ts +66 -66
  67. package/src/tests/model.test.ts +141 -141
  68. package/src/tests/model.union.test.ts +160 -160
  69. package/src/tests/router.test.ts +44 -44
  70. package/src/tests/sql.test.ts +502 -502
  71. package/src/tests/switch.test.ts +76 -76
  72. package/templates/create-cli/class.ts +13 -13
  73. package/templates/create-cli/cluster.ts +14 -14
  74. package/templates/create-cli/cmd.ts +3 -3
  75. package/templates/create-cli/config.ts +2 -2
  76. package/templates/create-cli/lib.ts +3 -3
  77. package/templates/create-cli/model.ts +14 -14
  78. package/templates/create-cli/route.ts +8 -8
  79. package/templates/create-cli/task.ts +13 -13
  80. package/templates/postinstall/api/.vscode/launch.json +20 -20
  81. package/templates/postinstall/api/Dockerfile +24 -24
  82. package/templates/postinstall/api/configs/env.ts +2 -2
  83. package/templates/postinstall/api/envs/debug.env +15 -15
  84. package/templates/postinstall/api/envs/homolog.env +15 -15
  85. package/templates/postinstall/api/envs/prod.env +15 -15
  86. package/templates/postinstall/api/tsconfig.json +24 -24
  87. package/templates/postinstall/cluster/.vscode/launch.json +20 -20
  88. package/templates/postinstall/cluster/Dockerfile +24 -24
  89. package/templates/postinstall/cluster/configs/env.ts +2 -2
  90. package/templates/postinstall/cluster/envs/debug.env +10 -10
  91. package/templates/postinstall/cluster/envs/homolog.env +10 -10
  92. package/templates/postinstall/cluster/envs/prod.env +10 -10
  93. package/templates/postinstall/cluster/tsconfig.json +24 -24
  94. package/templates/postinstall/lib/postinstall-tools.mjs +111 -111
  95. package/templates/postinstall/lib/postinstall.js +33 -33
  96. package/templates/postinstall/lib/tsconfig.json +22 -22
  97. package/templates/swagger-cli/header.json +21 -21
  98. package/templates/swagger-cli/route/body.json +14 -14
  99. package/templates/swagger-cli/route/header.json +18 -18
  100. package/templates/swagger-cli/route/response.json +53 -53
  101. package/tsconfig.json +28 -28
@@ -1,502 +1,502 @@
1
- import { describe, it, before, after } from "node:test"
2
- import assert from "node:assert"
3
- import path from "node:path"
4
- import fs from "node:fs"
5
- import { PlataModels, PlataSql } from ".."
6
- import { loadRuntimeLibs } from "../bin/runtime/_setupRuntime"
7
-
8
- describe('PlataSql', () => {
9
- const originalDatabasePath = path.join(__dirname, 'assets', 'sql', 'example.db')
10
- const tmpDatabasePath = path.join(__dirname, 'assets', 'sql', 'example.dummy.db')
11
- const tmpAuxDatabasePath = path.join(__dirname, 'assets', 'sql', 'aux.dummy.db')
12
-
13
- let sql: PlataSql.Driver<false>
14
- let aux: PlataSql.Driver<false>
15
-
16
- fs.rmSync(tmpDatabasePath, {
17
- force: true,
18
- recursive: false,
19
- })
20
-
21
- fs.rmSync(tmpAuxDatabasePath, {
22
- force: true,
23
- recursive: false,
24
- })
25
-
26
- const plataPromise = (async () => {
27
- const g = global as any
28
-
29
- g.Plata = await loadRuntimeLibs()
30
- g.Plata.config = Object.create(null)
31
- })()
32
-
33
- const modelSql = new PlataModels.ModelTemplate({
34
- PK_ID: [ PlataModels.Required(), PlataModels.Int() ],
35
- VL_PRECO: [ PlataModels.Required(), PlataModels.Int() ],
36
- DS_OBS: [ PlataModels.Required(), PlataModels.VarChar(Infinity) ],
37
- TB_JOINTEST2: [{
38
- PK_ID: [ PlataModels.Required(), PlataModels.Int() ],
39
- FK_JOIN: [ PlataModels.Required(), PlataModels.Int() ],
40
- }]
41
- } as const, {} as const)
42
-
43
- const testFunc: Sql.SqlGetFunc<typeof modelSql> = async (builder, sql) => {
44
- const result = await sql.select('TABLE', modelSql).build(
45
- s => builder(s)
46
- ).toListAsync()
47
-
48
- if (result.errorID !== undefined) {
49
- return result
50
- }
51
-
52
- if (result.errors !== undefined) {
53
- return {
54
- errorID: '',
55
- msg: '',
56
- error: {}
57
- }
58
- }
59
-
60
- return result.itens
61
- }
62
-
63
- const getSqlite3 = () => {
64
- if (!fs.existsSync(tmpDatabasePath)) {
65
- fs.copyFileSync(originalDatabasePath, tmpDatabasePath)
66
- }
67
-
68
- return new PlataSql.Driver({
69
- client: 'sqlite3',
70
- connection: {
71
- filename: tmpDatabasePath
72
- }
73
- }, false)
74
- }
75
-
76
- const getSqlite3Aux = () => {
77
- if (!fs.existsSync(tmpAuxDatabasePath)) {
78
- fs.copyFileSync(originalDatabasePath, tmpAuxDatabasePath)
79
- }
80
-
81
- return new PlataSql.Driver({
82
- client: 'sqlite3',
83
- connection: {
84
- filename: tmpAuxDatabasePath
85
- }
86
- }, false)
87
- }
88
-
89
- before(async () => {
90
- await plataPromise
91
- Plata.config.ENV = 'test'
92
-
93
- sql = getSqlite3()
94
- aux = getSqlite3Aux()
95
- })
96
-
97
- it('Insert', async () => {
98
- const tabela = 'TB_INSERT'
99
-
100
- const antes = await sql.select(tabela, modelSql).build(
101
- s => s
102
- ).toListAsync()
103
-
104
- assert.strictEqual(antes.errorID, undefined)
105
- assert.strictEqual(antes.errors, undefined)
106
-
107
- const insert = await sql.insert(tabela, modelSql, [{
108
- VL_PRECO: 2,
109
- DS_OBS: 'B'
110
- }])
111
-
112
- assert.strictEqual(insert.errorID, undefined)
113
- assert.strictEqual(insert.length, 1)
114
-
115
- const depois = await sql.select(tabela, modelSql).build(
116
- s => s
117
- ).toListAsync()
118
-
119
- assert.strictEqual(depois.errorID, undefined)
120
- assert.strictEqual(depois.errors, undefined)
121
-
122
- // Add TB_JOINTEST2 in insert itens
123
- const i = insert.map(r => {
124
- return {
125
- ...r,
126
- TB_JOINTEST2: []
127
- }
128
- })
129
-
130
- const esperado = [...antes.itens, ...i]
131
-
132
- for (let i = 0; i < depois.itens.length; i++) {
133
- assert.deepEqual(depois.itens[i], esperado[i])
134
- }
135
- })
136
-
137
- it('autoInsert', async () => {
138
- const tabela = 'TB_AUTOINSERT'
139
-
140
- const antes = await sql.select(tabela, modelSql).build(
141
- s => s
142
- ).toListAsync()
143
-
144
- assert.strictEqual(antes.errorID, undefined)
145
- assert.strictEqual(antes.errors, undefined)
146
-
147
- const insert = await sql.autoInsert(tabela, modelSql, [{}], [ 'PK_ID' ])
148
-
149
- assert.strictEqual(insert.errorID, undefined)
150
- assert.strictEqual(insert.length, 1)
151
-
152
- {
153
- const inserted = insert[0]
154
-
155
- for (const key in inserted) {
156
- assert.notStrictEqual(inserted[key], null)
157
- }
158
- }
159
-
160
- const depois = await sql.select(tabela, modelSql).build(
161
- s => s
162
- ).toListAsync()
163
-
164
- assert.strictEqual(depois.errorID, undefined)
165
- assert.strictEqual(depois.errors, undefined)
166
-
167
- // Add TB_JOINTEST2 in insert itens
168
- const i = insert.map(r => {
169
- return {
170
- ...r,
171
- TB_JOINTEST2: []
172
- }
173
- })
174
-
175
- const esperado = [...antes.itens, ...i]
176
-
177
- for (let i = 0; i < depois.itens.length; i++) {
178
- assert.deepEqual(depois.itens[i], esperado[i])
179
- }
180
- })
181
-
182
- it('Update', async () => {
183
- const tabela = 'TB_UPDATE'
184
-
185
- const antes = await sql.select(tabela, modelSql).build(
186
- s => s
187
- ).toListAsync()
188
-
189
- assert.strictEqual(antes.errorID, undefined)
190
- assert.strictEqual(antes.errors, undefined)
191
-
192
- const update = await sql.update(tabela, modelSql).build(
193
- s => s.where('PK_ID', 1), {
194
- VL_PRECO: 3,
195
- DS_OBS: 'C'
196
- }
197
- )
198
-
199
- assert.strictEqual(update.errorID, undefined)
200
- assert.strictEqual(update, 1)
201
-
202
- const depois = await sql.select(tabela, modelSql).build(
203
- s => s
204
- ).toListAsync()
205
-
206
- assert.strictEqual(depois.errorID, undefined)
207
- assert.strictEqual(depois.errors, undefined)
208
-
209
- const alterado = depois.itens.filter(d => d.PK_ID === 1)
210
-
211
- assert.strictEqual(alterado.length, 1)
212
-
213
- assert.strictEqual(alterado[0].DS_OBS, 'C')
214
- assert.strictEqual(alterado[0].VL_PRECO, 3)
215
- })
216
-
217
- it('Transaction Okay', async () => {
218
- const tabela = 'TB_TRANSACTION_OKAY'
219
-
220
- const antes = await sql.select(tabela, modelSql).build(
221
- s => s
222
- ).toListAsync()
223
-
224
- assert.strictEqual(antes.errorID, undefined)
225
- assert.strictEqual(antes.errors, undefined)
226
-
227
- const error = await sql.transaction(async trx => {
228
- const insert = await trx.insert(tabela, modelSql, [{
229
- DS_OBS: 'B',
230
- VL_PRECO: 2
231
- }])
232
-
233
- assert.strictEqual(insert.errorID, undefined)
234
-
235
- return true
236
- })
237
-
238
- assert.strictEqual(error, true)
239
-
240
- const depois = await sql.select(tabela, modelSql).build(
241
- s => s
242
- ).toListAsync()
243
-
244
- assert.strictEqual(depois.errorID, undefined)
245
- assert.strictEqual(depois.errors, undefined)
246
-
247
- assert.strictEqual(depois.itens.length, antes.itens.length + 1)
248
- })
249
-
250
- it('Transaction Multiquery Okay', async () => {
251
- const tabela = 'TB_TRANSACTION_MULTIPLES_QUERY_OK'
252
-
253
- const error = await sql.transaction(async trx => {
254
- await Promise.all([
255
- Plata.FastPromise(() => trx.update(tabela, modelSql).build(
256
- s => s.where('PK_ID', '=', 1),
257
- {
258
- DS_OBS: 'F',
259
- VL_PRECO: 99
260
- }
261
- )).then(() => assert.strictEqual(trx.conn.isTransaction, true, 'Transação fechada antes da hora')),
262
- Plata.FastPromise(() => trx.update(tabela, modelSql).build(
263
- s => s.where('PK_ID', '=', 2),
264
- {
265
- DS_OBS: 'F',
266
- VL_PRECO: 99
267
- }
268
- )).then(() => assert.strictEqual(trx.conn.isTransaction, true, 'Transação fechada antes da hora')),
269
- Plata.FastPromise(() => trx.update(tabela, modelSql).build(
270
- s => s.where('PK_ID', '=', 3),
271
- {
272
- DS_OBS: 'F',
273
- VL_PRECO: 99
274
- }
275
- )).then(() => assert.strictEqual(trx.conn.isTransaction, true, 'Transação fechada antes da hora')),
276
- Plata.FastPromise(() => trx.update(tabela, modelSql).build(
277
- s => s.where('PK_ID', '=', 4),
278
- {
279
- DS_OBS: 'F',
280
- VL_PRECO: 99
281
- }
282
- )).then(() => assert.strictEqual(trx.conn.isTransaction, true, 'Transação fechada antes da hora')),
283
- Plata.FastPromise(() => trx.update(tabela, modelSql).build(
284
- s => s.where('PK_ID', '=', 5),
285
- {
286
- DS_OBS: 'F',
287
- VL_PRECO: 99
288
- }
289
- )).then(() => assert.strictEqual(trx.conn.isTransaction, true, 'Transação fechada antes da hora')),
290
- ])
291
-
292
- return true
293
- })
294
-
295
- assert.strictEqual(sql.conn.isTransaction ?? false, false, 'Transação ainda aberta')
296
- assert.strictEqual(error, true)
297
-
298
- const depois = await sql.select(tabela, modelSql).build(
299
- s => s
300
- ).toListAsync()
301
-
302
- assert.strictEqual(depois.errorID, undefined)
303
- assert.strictEqual(depois.errors, undefined)
304
- assert.strictEqual(depois.itens.length, 5)
305
-
306
- for (let i = 0; i < depois.itens.length; i++) {
307
- assert.deepEqual(depois.itens[i], {
308
- PK_ID: i+1,
309
- DS_OBS: 'F',
310
- VL_PRECO: 99,
311
- TB_JOINTEST2: []
312
- })
313
- }
314
- })
315
-
316
- it('Transaction Not Okay', async () => {
317
- const tabela = 'TB_TRANSACTION_NOKAY'
318
-
319
- const antes = await sql.select(tabela, modelSql).build(
320
- s => s
321
- ).toListAsync()
322
-
323
- assert.strictEqual(antes.errorID, undefined)
324
- assert.strictEqual(antes.errors, undefined)
325
-
326
- const error = await sql.transaction(async trx => {
327
- const insert = await trx.insert(tabela, modelSql, [{
328
- DS_OBS: 'B',
329
- VL_PRECO: 2
330
- }])
331
-
332
- assert.strictEqual(insert.errorID, undefined)
333
-
334
- return Plata.BuildPlataError({
335
- errorID: 'PTSQLTRX001',
336
- msg: 'Erro esperado'
337
- })
338
- })
339
-
340
- assert.strictEqual(error.errorID, 'PTSQLTRX001')
341
-
342
- const depois = await sql.select(tabela, modelSql).build(
343
- s => s
344
- ).toListAsync()
345
-
346
- assert.strictEqual(depois.errorID, undefined)
347
- assert.strictEqual(depois.errors, undefined)
348
-
349
- assert.strictEqual(depois.itens.length, antes.itens.length)
350
- })
351
-
352
- it('Join Found', async () => {
353
- const table1 = 'TB_JOINTEST1'
354
- const table2 = 'TB_JOINTEST2'
355
-
356
- const selectJoin = await sql.select(table1, modelSql).build(
357
- s => s
358
- .limit(1)
359
- .includeOne(table2, 'LEFT JOIN', j => j.on(`${table1}.PK_ID`, '=', `${table2}.FK_JOIN`))
360
- ).toListAsync()
361
-
362
- assert.strictEqual(selectJoin.errorID, undefined)
363
- assert.strictEqual(selectJoin.errors, undefined)
364
-
365
- assert.deepEqual(selectJoin.itens[0], {
366
- PK_ID: 1,
367
- VL_PRECO: 1,
368
- DS_OBS: 'A',
369
- TB_JOINTEST2: [{
370
- PK_ID: 1,
371
- FK_JOIN: 1
372
- }]
373
- })
374
- })
375
-
376
- it('Join Not Found', async () => {
377
- const table1 = 'TB_JOINTEST1'
378
- const table2 = 'TB_JOINTEST2'
379
-
380
- const selectJoin = await sql.select(table1, modelSql).build(
381
- s => s
382
- .limit(1)
383
- .includeOne(table2, 'LEFT JOIN', j => j.onVal(`${table2}.PK_ID`, '=', 2))
384
- ).toListAsync()
385
-
386
- assert.strictEqual(selectJoin.errorID, undefined)
387
- assert.notStrictEqual(selectJoin.errors, undefined)
388
- })
389
-
390
- it('MultiBanco', async () => {
391
- const tabela = 'TB_MULTIBANCO'
392
-
393
- const sqlInsertPromise = Plata.FastPromise(() => sql.insert(tabela, modelSql, [
394
- {
395
- VL_PRECO: 1,
396
- DS_OBS: 'A'
397
- }
398
- ]))
399
-
400
- const auxInsertPromise = Plata.FastPromise(() => aux.insert(tabela, modelSql, [
401
- {
402
- VL_PRECO: 2,
403
- DS_OBS: 'B'
404
- }
405
- ]))
406
-
407
- const [ sqlResult, auxResult ] = await Promise.all([ sqlInsertPromise, auxInsertPromise ])
408
-
409
- assert.strictEqual(sqlResult.errorID, undefined)
410
- assert.strictEqual(auxResult.errorID, undefined)
411
-
412
- const sqlSelectPromise = Plata.FastPromise(() => sql.select(tabela, modelSql).build(
413
- s => s
414
- ).toListAsync())
415
-
416
- const auxSelectPromise = Plata.FastPromise(() => aux.select(tabela, modelSql).build(
417
- s => s
418
- ).toListAsync())
419
-
420
- const [ sqlSelect, auxSelect ] = await Promise.all([ sqlSelectPromise , auxSelectPromise ])
421
-
422
- assert.strictEqual(sqlSelect.errorID, undefined)
423
- assert.strictEqual(sqlSelect.errors, undefined)
424
-
425
- assert.strictEqual(auxSelect.errorID, undefined)
426
- assert.strictEqual(auxSelect.errors, undefined)
427
-
428
- assert.strictEqual(sqlSelect.itens.length, 1)
429
- assert.strictEqual(auxSelect.itens.length, 1)
430
-
431
- assert.strictEqual(sqlSelect.itens[0].VL_PRECO, 1)
432
- assert.strictEqual(sqlSelect.itens[0].DS_OBS, 'A')
433
-
434
- assert.strictEqual(auxSelect.itens[0].VL_PRECO, 2)
435
- assert.strictEqual(auxSelect.itens[0].DS_OBS, 'B')
436
- })
437
-
438
- it('Multiquery',async () => {
439
- const tabela = 'TB_MULTIQUERY'
440
-
441
- let promises: Promise<any>[] = []
442
- const rows: any[] = []
443
-
444
- for (let i = 0; i < 50; i++) {
445
- const row = {
446
- VL_PRECO: i,
447
- DS_OBS: `${i}`,
448
- }
449
-
450
- promises.push(Plata.FastPromise(() => sql.insert(
451
- tabela,
452
- modelSql,
453
- [row]
454
- )))
455
-
456
- rows.push([{
457
- PK_ID: i + 1,
458
- ...row,
459
- }])
460
- }
461
-
462
- const resultInsert = await Promise.all(promises)
463
-
464
- assert.deepEqual(resultInsert, rows)
465
-
466
- promises = []
467
-
468
- for (let i = 0; i < 50; i++) {
469
- promises.push(Plata.FastPromise(() =>
470
- sql.select(tabela,modelSql).build(
471
- s => s
472
- .where('PK_ID', i + 1)
473
- ).toListAsync())
474
- )
475
- }
476
-
477
- const resultSelect = await Promise.all(promises)
478
-
479
- assert.deepEqual(resultSelect, rows.flatMap(row => Plata.BuildObject({
480
- errors: undefined,
481
- itens: [{
482
- ...row[0],
483
- TB_JOINTEST2: [],
484
- }]
485
- })))
486
- })
487
-
488
- after(async () => { // Requerido para Sqlite3
489
- const sqlError = await sql.conn.destroy().then(
490
- () => undefined,
491
- err => err
492
- )
493
-
494
- const auxError = await aux.conn.destroy().then(
495
- () => undefined,
496
- err => err
497
- )
498
-
499
- assert.strictEqual(sqlError, undefined)
500
- assert.strictEqual(auxError, undefined)
501
- })
502
- })
1
+ import { describe, it, before, after } from "node:test"
2
+ import assert from "node:assert"
3
+ import path from "node:path"
4
+ import fs from "node:fs"
5
+ import { PlataModels, PlataSql } from ".."
6
+ import { loadRuntimeLibs } from "../bin/runtime/_setupRuntime"
7
+
8
+ describe('PlataSql', () => {
9
+ const originalDatabasePath = path.join(__dirname, 'assets', 'sql', 'example.db')
10
+ const tmpDatabasePath = path.join(__dirname, 'assets', 'sql', 'example.dummy.db')
11
+ const tmpAuxDatabasePath = path.join(__dirname, 'assets', 'sql', 'aux.dummy.db')
12
+
13
+ let sql: PlataSql.Driver<false>
14
+ let aux: PlataSql.Driver<false>
15
+
16
+ fs.rmSync(tmpDatabasePath, {
17
+ force: true,
18
+ recursive: false,
19
+ })
20
+
21
+ fs.rmSync(tmpAuxDatabasePath, {
22
+ force: true,
23
+ recursive: false,
24
+ })
25
+
26
+ const plataPromise = (async () => {
27
+ const g = global as any
28
+
29
+ g.Plata = await loadRuntimeLibs()
30
+ g.Plata.config = Object.create(null)
31
+ })()
32
+
33
+ const modelSql = new PlataModels.ModelTemplate({
34
+ PK_ID: [ PlataModels.Required(), PlataModels.Int() ],
35
+ VL_PRECO: [ PlataModels.Required(), PlataModels.Int() ],
36
+ DS_OBS: [ PlataModels.Required(), PlataModels.VarChar(Infinity) ],
37
+ TB_JOINTEST2: [{
38
+ PK_ID: [ PlataModels.Required(), PlataModels.Int() ],
39
+ FK_JOIN: [ PlataModels.Required(), PlataModels.Int() ],
40
+ }]
41
+ } as const, {} as const)
42
+
43
+ const testFunc: Sql.SqlGetFunc<typeof modelSql> = async (builder, sql) => {
44
+ const result = await sql.select('TABLE', modelSql).build(
45
+ s => builder(s)
46
+ ).toListAsync()
47
+
48
+ if (result.errorID !== undefined) {
49
+ return result
50
+ }
51
+
52
+ if (result.errors !== undefined) {
53
+ return {
54
+ errorID: '',
55
+ msg: '',
56
+ error: {}
57
+ }
58
+ }
59
+
60
+ return result.itens
61
+ }
62
+
63
+ const getSqlite3 = () => {
64
+ if (!fs.existsSync(tmpDatabasePath)) {
65
+ fs.copyFileSync(originalDatabasePath, tmpDatabasePath)
66
+ }
67
+
68
+ return new PlataSql.Driver({
69
+ client: 'sqlite3',
70
+ connection: {
71
+ filename: tmpDatabasePath
72
+ }
73
+ }, false)
74
+ }
75
+
76
+ const getSqlite3Aux = () => {
77
+ if (!fs.existsSync(tmpAuxDatabasePath)) {
78
+ fs.copyFileSync(originalDatabasePath, tmpAuxDatabasePath)
79
+ }
80
+
81
+ return new PlataSql.Driver({
82
+ client: 'sqlite3',
83
+ connection: {
84
+ filename: tmpAuxDatabasePath
85
+ }
86
+ }, false)
87
+ }
88
+
89
+ before(async () => {
90
+ await plataPromise
91
+ Plata.config.ENV = 'test'
92
+
93
+ sql = getSqlite3()
94
+ aux = getSqlite3Aux()
95
+ })
96
+
97
+ it('Insert', async () => {
98
+ const tabela = 'TB_INSERT'
99
+
100
+ const antes = await sql.select(tabela, modelSql).build(
101
+ s => s
102
+ ).toListAsync()
103
+
104
+ assert.strictEqual(antes.errorID, undefined)
105
+ assert.strictEqual(antes.errors, undefined)
106
+
107
+ const insert = await sql.insert(tabela, modelSql, [{
108
+ VL_PRECO: 2,
109
+ DS_OBS: 'B'
110
+ }])
111
+
112
+ assert.strictEqual(insert.errorID, undefined)
113
+ assert.strictEqual(insert.length, 1)
114
+
115
+ const depois = await sql.select(tabela, modelSql).build(
116
+ s => s
117
+ ).toListAsync()
118
+
119
+ assert.strictEqual(depois.errorID, undefined)
120
+ assert.strictEqual(depois.errors, undefined)
121
+
122
+ // Add TB_JOINTEST2 in insert itens
123
+ const i = insert.map(r => {
124
+ return {
125
+ ...r,
126
+ TB_JOINTEST2: []
127
+ }
128
+ })
129
+
130
+ const esperado = [...antes.itens, ...i]
131
+
132
+ for (let i = 0; i < depois.itens.length; i++) {
133
+ assert.deepEqual(depois.itens[i], esperado[i])
134
+ }
135
+ })
136
+
137
+ it('autoInsert', async () => {
138
+ const tabela = 'TB_AUTOINSERT'
139
+
140
+ const antes = await sql.select(tabela, modelSql).build(
141
+ s => s
142
+ ).toListAsync()
143
+
144
+ assert.strictEqual(antes.errorID, undefined)
145
+ assert.strictEqual(antes.errors, undefined)
146
+
147
+ const insert = await sql.autoInsert(tabela, modelSql, [{}], [ 'PK_ID' ])
148
+
149
+ assert.strictEqual(insert.errorID, undefined)
150
+ assert.strictEqual(insert.length, 1)
151
+
152
+ {
153
+ const inserted = insert[0]
154
+
155
+ for (const key in inserted) {
156
+ assert.notStrictEqual(inserted[key], null)
157
+ }
158
+ }
159
+
160
+ const depois = await sql.select(tabela, modelSql).build(
161
+ s => s
162
+ ).toListAsync()
163
+
164
+ assert.strictEqual(depois.errorID, undefined)
165
+ assert.strictEqual(depois.errors, undefined)
166
+
167
+ // Add TB_JOINTEST2 in insert itens
168
+ const i = insert.map(r => {
169
+ return {
170
+ ...r,
171
+ TB_JOINTEST2: []
172
+ }
173
+ })
174
+
175
+ const esperado = [...antes.itens, ...i]
176
+
177
+ for (let i = 0; i < depois.itens.length; i++) {
178
+ assert.deepEqual(depois.itens[i], esperado[i])
179
+ }
180
+ })
181
+
182
+ it('Update', async () => {
183
+ const tabela = 'TB_UPDATE'
184
+
185
+ const antes = await sql.select(tabela, modelSql).build(
186
+ s => s
187
+ ).toListAsync()
188
+
189
+ assert.strictEqual(antes.errorID, undefined)
190
+ assert.strictEqual(antes.errors, undefined)
191
+
192
+ const update = await sql.update(tabela, modelSql).build(
193
+ s => s.where('PK_ID', 1), {
194
+ VL_PRECO: 3,
195
+ DS_OBS: 'C'
196
+ }
197
+ )
198
+
199
+ assert.strictEqual(update.errorID, undefined)
200
+ assert.strictEqual(update, 1)
201
+
202
+ const depois = await sql.select(tabela, modelSql).build(
203
+ s => s
204
+ ).toListAsync()
205
+
206
+ assert.strictEqual(depois.errorID, undefined)
207
+ assert.strictEqual(depois.errors, undefined)
208
+
209
+ const alterado = depois.itens.filter(d => d.PK_ID === 1)
210
+
211
+ assert.strictEqual(alterado.length, 1)
212
+
213
+ assert.strictEqual(alterado[0].DS_OBS, 'C')
214
+ assert.strictEqual(alterado[0].VL_PRECO, 3)
215
+ })
216
+
217
+ it('Transaction Okay', async () => {
218
+ const tabela = 'TB_TRANSACTION_OKAY'
219
+
220
+ const antes = await sql.select(tabela, modelSql).build(
221
+ s => s
222
+ ).toListAsync()
223
+
224
+ assert.strictEqual(antes.errorID, undefined)
225
+ assert.strictEqual(antes.errors, undefined)
226
+
227
+ const error = await sql.transaction(async trx => {
228
+ const insert = await trx.insert(tabela, modelSql, [{
229
+ DS_OBS: 'B',
230
+ VL_PRECO: 2
231
+ }])
232
+
233
+ assert.strictEqual(insert.errorID, undefined)
234
+
235
+ return true
236
+ })
237
+
238
+ assert.strictEqual(error, true)
239
+
240
+ const depois = await sql.select(tabela, modelSql).build(
241
+ s => s
242
+ ).toListAsync()
243
+
244
+ assert.strictEqual(depois.errorID, undefined)
245
+ assert.strictEqual(depois.errors, undefined)
246
+
247
+ assert.strictEqual(depois.itens.length, antes.itens.length + 1)
248
+ })
249
+
250
+ it('Transaction Multiquery Okay', async () => {
251
+ const tabela = 'TB_TRANSACTION_MULTIPLES_QUERY_OK'
252
+
253
+ const error = await sql.transaction(async trx => {
254
+ await Promise.all([
255
+ Plata.FastPromise(() => trx.update(tabela, modelSql).build(
256
+ s => s.where('PK_ID', '=', 1),
257
+ {
258
+ DS_OBS: 'F',
259
+ VL_PRECO: 99
260
+ }
261
+ )).then(() => assert.strictEqual(trx.conn.isTransaction, true, 'Transação fechada antes da hora')),
262
+ Plata.FastPromise(() => trx.update(tabela, modelSql).build(
263
+ s => s.where('PK_ID', '=', 2),
264
+ {
265
+ DS_OBS: 'F',
266
+ VL_PRECO: 99
267
+ }
268
+ )).then(() => assert.strictEqual(trx.conn.isTransaction, true, 'Transação fechada antes da hora')),
269
+ Plata.FastPromise(() => trx.update(tabela, modelSql).build(
270
+ s => s.where('PK_ID', '=', 3),
271
+ {
272
+ DS_OBS: 'F',
273
+ VL_PRECO: 99
274
+ }
275
+ )).then(() => assert.strictEqual(trx.conn.isTransaction, true, 'Transação fechada antes da hora')),
276
+ Plata.FastPromise(() => trx.update(tabela, modelSql).build(
277
+ s => s.where('PK_ID', '=', 4),
278
+ {
279
+ DS_OBS: 'F',
280
+ VL_PRECO: 99
281
+ }
282
+ )).then(() => assert.strictEqual(trx.conn.isTransaction, true, 'Transação fechada antes da hora')),
283
+ Plata.FastPromise(() => trx.update(tabela, modelSql).build(
284
+ s => s.where('PK_ID', '=', 5),
285
+ {
286
+ DS_OBS: 'F',
287
+ VL_PRECO: 99
288
+ }
289
+ )).then(() => assert.strictEqual(trx.conn.isTransaction, true, 'Transação fechada antes da hora')),
290
+ ])
291
+
292
+ return true
293
+ })
294
+
295
+ assert.strictEqual(sql.conn.isTransaction ?? false, false, 'Transação ainda aberta')
296
+ assert.strictEqual(error, true)
297
+
298
+ const depois = await sql.select(tabela, modelSql).build(
299
+ s => s
300
+ ).toListAsync()
301
+
302
+ assert.strictEqual(depois.errorID, undefined)
303
+ assert.strictEqual(depois.errors, undefined)
304
+ assert.strictEqual(depois.itens.length, 5)
305
+
306
+ for (let i = 0; i < depois.itens.length; i++) {
307
+ assert.deepEqual(depois.itens[i], {
308
+ PK_ID: i+1,
309
+ DS_OBS: 'F',
310
+ VL_PRECO: 99,
311
+ TB_JOINTEST2: []
312
+ })
313
+ }
314
+ })
315
+
316
+ it('Transaction Not Okay', async () => {
317
+ const tabela = 'TB_TRANSACTION_NOKAY'
318
+
319
+ const antes = await sql.select(tabela, modelSql).build(
320
+ s => s
321
+ ).toListAsync()
322
+
323
+ assert.strictEqual(antes.errorID, undefined)
324
+ assert.strictEqual(antes.errors, undefined)
325
+
326
+ const error = await sql.transaction(async trx => {
327
+ const insert = await trx.insert(tabela, modelSql, [{
328
+ DS_OBS: 'B',
329
+ VL_PRECO: 2
330
+ }])
331
+
332
+ assert.strictEqual(insert.errorID, undefined)
333
+
334
+ return Plata.BuildPlataError({
335
+ errorID: 'PTSQLTRX001',
336
+ msg: 'Erro esperado'
337
+ })
338
+ })
339
+
340
+ assert.strictEqual(error.errorID, 'PTSQLTRX001')
341
+
342
+ const depois = await sql.select(tabela, modelSql).build(
343
+ s => s
344
+ ).toListAsync()
345
+
346
+ assert.strictEqual(depois.errorID, undefined)
347
+ assert.strictEqual(depois.errors, undefined)
348
+
349
+ assert.strictEqual(depois.itens.length, antes.itens.length)
350
+ })
351
+
352
+ it('Join Found', async () => {
353
+ const table1 = 'TB_JOINTEST1'
354
+ const table2 = 'TB_JOINTEST2'
355
+
356
+ const selectJoin = await sql.select(table1, modelSql).build(
357
+ s => s
358
+ .limit(1)
359
+ .includeOne(table2, 'LEFT JOIN', j => j.on(`${table1}.PK_ID`, '=', `${table2}.FK_JOIN`))
360
+ ).toListAsync()
361
+
362
+ assert.strictEqual(selectJoin.errorID, undefined)
363
+ assert.strictEqual(selectJoin.errors, undefined)
364
+
365
+ assert.deepEqual(selectJoin.itens[0], {
366
+ PK_ID: 1,
367
+ VL_PRECO: 1,
368
+ DS_OBS: 'A',
369
+ TB_JOINTEST2: [{
370
+ PK_ID: 1,
371
+ FK_JOIN: 1
372
+ }]
373
+ })
374
+ })
375
+
376
+ it('Join Not Found', async () => {
377
+ const table1 = 'TB_JOINTEST1'
378
+ const table2 = 'TB_JOINTEST2'
379
+
380
+ const selectJoin = await sql.select(table1, modelSql).build(
381
+ s => s
382
+ .limit(1)
383
+ .includeOne(table2, 'LEFT JOIN', j => j.onVal(`${table2}.PK_ID`, '=', 2))
384
+ ).toListAsync()
385
+
386
+ assert.strictEqual(selectJoin.errorID, undefined)
387
+ assert.notStrictEqual(selectJoin.errors, undefined)
388
+ })
389
+
390
+ it('MultiBanco', async () => {
391
+ const tabela = 'TB_MULTIBANCO'
392
+
393
+ const sqlInsertPromise = Plata.FastPromise(() => sql.insert(tabela, modelSql, [
394
+ {
395
+ VL_PRECO: 1,
396
+ DS_OBS: 'A'
397
+ }
398
+ ]))
399
+
400
+ const auxInsertPromise = Plata.FastPromise(() => aux.insert(tabela, modelSql, [
401
+ {
402
+ VL_PRECO: 2,
403
+ DS_OBS: 'B'
404
+ }
405
+ ]))
406
+
407
+ const [ sqlResult, auxResult ] = await Promise.all([ sqlInsertPromise, auxInsertPromise ])
408
+
409
+ assert.strictEqual(sqlResult.errorID, undefined)
410
+ assert.strictEqual(auxResult.errorID, undefined)
411
+
412
+ const sqlSelectPromise = Plata.FastPromise(() => sql.select(tabela, modelSql).build(
413
+ s => s
414
+ ).toListAsync())
415
+
416
+ const auxSelectPromise = Plata.FastPromise(() => aux.select(tabela, modelSql).build(
417
+ s => s
418
+ ).toListAsync())
419
+
420
+ const [ sqlSelect, auxSelect ] = await Promise.all([ sqlSelectPromise , auxSelectPromise ])
421
+
422
+ assert.strictEqual(sqlSelect.errorID, undefined)
423
+ assert.strictEqual(sqlSelect.errors, undefined)
424
+
425
+ assert.strictEqual(auxSelect.errorID, undefined)
426
+ assert.strictEqual(auxSelect.errors, undefined)
427
+
428
+ assert.strictEqual(sqlSelect.itens.length, 1)
429
+ assert.strictEqual(auxSelect.itens.length, 1)
430
+
431
+ assert.strictEqual(sqlSelect.itens[0].VL_PRECO, 1)
432
+ assert.strictEqual(sqlSelect.itens[0].DS_OBS, 'A')
433
+
434
+ assert.strictEqual(auxSelect.itens[0].VL_PRECO, 2)
435
+ assert.strictEqual(auxSelect.itens[0].DS_OBS, 'B')
436
+ })
437
+
438
+ it('Multiquery',async () => {
439
+ const tabela = 'TB_MULTIQUERY'
440
+
441
+ let promises: Promise<any>[] = []
442
+ const rows: any[] = []
443
+
444
+ for (let i = 0; i < 50; i++) {
445
+ const row = {
446
+ VL_PRECO: i,
447
+ DS_OBS: `${i}`,
448
+ }
449
+
450
+ promises.push(Plata.FastPromise(() => sql.insert(
451
+ tabela,
452
+ modelSql,
453
+ [row]
454
+ )))
455
+
456
+ rows.push([{
457
+ PK_ID: i + 1,
458
+ ...row,
459
+ }])
460
+ }
461
+
462
+ const resultInsert = await Promise.all(promises)
463
+
464
+ assert.deepEqual(resultInsert, rows)
465
+
466
+ promises = []
467
+
468
+ for (let i = 0; i < 50; i++) {
469
+ promises.push(Plata.FastPromise(() =>
470
+ sql.select(tabela,modelSql).build(
471
+ s => s
472
+ .where('PK_ID', i + 1)
473
+ ).toListAsync())
474
+ )
475
+ }
476
+
477
+ const resultSelect = await Promise.all(promises)
478
+
479
+ assert.deepEqual(resultSelect, rows.flatMap(row => Plata.BuildObject({
480
+ errors: undefined,
481
+ itens: [{
482
+ ...row[0],
483
+ TB_JOINTEST2: [],
484
+ }]
485
+ })))
486
+ })
487
+
488
+ after(async () => { // Requerido para Sqlite3
489
+ const sqlError = await sql.conn.destroy().then(
490
+ () => undefined,
491
+ err => err
492
+ )
493
+
494
+ const auxError = await aux.conn.destroy().then(
495
+ () => undefined,
496
+ err => err
497
+ )
498
+
499
+ assert.strictEqual(sqlError, undefined)
500
+ assert.strictEqual(auxError, undefined)
501
+ })
502
+ })