@creator.co/wapi 1.3.19-alpha1 → 1.4.0-alpha1

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 (72) hide show
  1. package/dist/package.json +2 -1
  2. package/dist/src/API/Request.js.map +1 -1
  3. package/dist/src/API/Response.js.map +1 -1
  4. package/dist/src/API/Utils.js.map +1 -1
  5. package/dist/src/BaseEvent/EventProcessor.js.map +1 -1
  6. package/dist/src/BaseEvent/Transaction.d.ts +1 -1
  7. package/dist/src/Config/Configuration.js.map +1 -1
  8. package/dist/src/Config/EnvironmentVar.js.map +1 -1
  9. package/dist/src/Crypto/JWT.js.map +1 -1
  10. package/dist/src/Database/Database.d.ts +8 -5
  11. package/dist/src/Database/Database.js +5 -3
  12. package/dist/src/Database/Database.js.map +1 -1
  13. package/dist/src/Database/DatabaseManager.d.ts +30 -9
  14. package/dist/src/Database/DatabaseManager.js +25 -10
  15. package/dist/src/Database/DatabaseManager.js.map +1 -1
  16. package/dist/src/Database/DatabaseTransaction.d.ts +66 -31
  17. package/dist/src/Database/DatabaseTransaction.js +51 -30
  18. package/dist/src/Database/DatabaseTransaction.js.map +1 -1
  19. package/dist/src/Database/integrations/knex/KnexDatabase.d.ts +22 -8
  20. package/dist/src/Database/integrations/knex/KnexDatabase.js +18 -13
  21. package/dist/src/Database/integrations/knex/KnexDatabase.js.map +1 -1
  22. package/dist/src/Database/integrations/knex/KnexTransaction.d.ts +39 -16
  23. package/dist/src/Database/integrations/knex/KnexTransaction.js +83 -17
  24. package/dist/src/Database/integrations/knex/KnexTransaction.js.map +1 -1
  25. package/dist/src/Database/integrations/kysely/KyselyDatabase.d.ts +55 -0
  26. package/dist/src/Database/integrations/kysely/KyselyDatabase.js +126 -0
  27. package/dist/src/Database/integrations/kysely/KyselyDatabase.js.map +1 -0
  28. package/dist/src/Database/integrations/kysely/KyselyTransaction.d.ts +70 -0
  29. package/dist/src/Database/integrations/kysely/KyselyTransaction.js +186 -0
  30. package/dist/src/Database/integrations/kysely/KyselyTransaction.js.map +1 -0
  31. package/dist/src/Database/integrations/pgsql/PostgresDatabase.d.ts +16 -11
  32. package/dist/src/Database/integrations/pgsql/PostgresDatabase.js +19 -18
  33. package/dist/src/Database/integrations/pgsql/PostgresDatabase.js.map +1 -1
  34. package/dist/src/Database/integrations/pgsql/PostgresTransaction.d.ts +42 -16
  35. package/dist/src/Database/integrations/pgsql/PostgresTransaction.js +84 -18
  36. package/dist/src/Database/integrations/pgsql/PostgresTransaction.js.map +1 -1
  37. package/dist/src/Database/types.d.ts +47 -26
  38. package/dist/src/Logger/Logger.d.ts +6 -6
  39. package/dist/src/Logger/Logger.js +12 -15
  40. package/dist/src/Logger/Logger.js.map +1 -1
  41. package/dist/src/Publisher/Publisher.js.map +1 -1
  42. package/dist/src/Server/RouteResolver.js.map +1 -1
  43. package/dist/src/Server/lib/ContainerServer.js.map +1 -1
  44. package/dist/src/Server/lib/Server.js.map +1 -1
  45. package/dist/src/Server/lib/container/GenericHandler.js +2 -2
  46. package/dist/src/Server/lib/container/GenericHandler.js.map +1 -1
  47. package/dist/src/Server/lib/container/GenericHandlerEvent.js +1 -1
  48. package/dist/src/Server/lib/container/GenericHandlerEvent.js.map +1 -1
  49. package/dist/src/Server/lib/container/Utils.js.map +1 -1
  50. package/dist/src/Validation/Validator.js.map +1 -1
  51. package/package.json +2 -1
  52. package/src/BaseEvent/Transaction.ts +2 -2
  53. package/src/Database/Database.ts +8 -5
  54. package/src/Database/DatabaseManager.ts +34 -17
  55. package/src/Database/DatabaseTransaction.ts +79 -35
  56. package/src/Database/integrations/knex/KnexDatabase.ts +22 -11
  57. package/src/Database/integrations/knex/KnexTransaction.ts +52 -19
  58. package/src/Database/integrations/kysely/KyselyDatabase.ts +87 -0
  59. package/src/Database/integrations/kysely/KyselyTransaction.ts +172 -0
  60. package/src/Database/integrations/pgsql/PostgresDatabase.ts +26 -14
  61. package/src/Database/integrations/pgsql/PostgresTransaction.ts +55 -22
  62. package/src/Database/types.ts +53 -31
  63. package/src/Logger/Logger.ts +12 -15
  64. package/src/Server/lib/container/GenericHandler.ts +2 -2
  65. package/tests/Database/DatabaseManager.test.ts +16 -1
  66. package/tests/Database/integrations/knex/KnexDatabase.test.ts +2 -2
  67. package/tests/Database/integrations/knex/KnexTransaction.test.ts +52 -45
  68. package/tests/Database/integrations/kysely/KyselyDatabase.test.ts +109 -0
  69. package/tests/Database/integrations/kysely/KyselyTransaction.test.ts +118 -0
  70. package/tests/Database/integrations/pg/PostgresDatabase.test.ts +32 -6
  71. package/tests/Database/integrations/pg/PostgresTransaction.test.ts +75 -9
  72. package/tsconfig.json +1 -0
@@ -10,11 +10,15 @@ const config: DbConfig<'pg'> = {
10
10
  host: 'host',
11
11
  port: 1234,
12
12
  database: 'database',
13
- driver: 'driver',
14
13
  maxConnections: 1,
15
14
  autoCommit: true,
16
15
  }
17
16
 
17
+ const config2: DbConfig<'pg'> = {
18
+ ...config,
19
+ readReplica: config,
20
+ }
21
+
18
22
  const expectedImplConfig = {
19
23
  host: config.host,
20
24
  port: config.port,
@@ -25,25 +29,47 @@ const expectedImplConfig = {
25
29
  }
26
30
 
27
31
  describe('PostgresDatabase', () => {
28
- test('PostgresDatabase', async () => {
29
- const mockTrans = {
32
+ let mockTrans
33
+ let mockClient
34
+ beforeEach(() => {
35
+ mockTrans = {
30
36
  query: jest.fn(() => {}),
31
37
  } as any
32
- const mockClient = jest.fn(
38
+ mockClient = jest.fn(
33
39
  () =>
34
40
  ({
35
41
  connect: async () => mockTrans,
36
42
  }) as PoolClient
37
43
  )
38
44
  PostgresDatabase['pgProvider'] = mockClient as any
45
+ })
39
46
 
47
+ test('PostgresDatabase', async () => {
40
48
  const underTest = new PostgresDatabase(config)
41
- expect(mockClient).toBeCalledWith(expectedImplConfig)
49
+ expect(mockClient).toHaveBeenNthCalledWith(1, expectedImplConfig)
50
+
51
+ const trans = await underTest.transaction()
52
+
53
+ expect(mockTrans.query).toBeCalledWith('BEGIN')
54
+ expect(trans.reader).toBeUndefined()
55
+ expect(trans.writer).toBeInstanceOf(Object)
56
+ expect(trans.writer.connect).toBeInstanceOf(Function)
57
+ expect(trans).toBeInstanceOf(Function)
58
+ expect(trans['transaction']).toBe(mockTrans)
59
+ })
60
+
61
+ test('PostgresDatabase - read replica', async () => {
62
+ const underTest = new PostgresDatabase(config2)
63
+ expect(mockClient).toHaveBeenNthCalledWith(2, expectedImplConfig)
42
64
 
43
65
  const trans = await underTest.transaction()
44
66
 
45
67
  expect(mockTrans.query).toBeCalledWith('BEGIN')
68
+ expect(trans.reader).toBeInstanceOf(Object)
69
+ expect(trans.reader.connect).toBeInstanceOf(Function)
70
+ expect(trans.writer).toBeInstanceOf(Object)
71
+ expect(trans.writer.connect).toBeInstanceOf(Function)
46
72
  expect(trans).toBeInstanceOf(Function)
47
- expect(trans['delegate']).toBe(mockTrans)
73
+ expect(trans['transaction']).toBe(mockTrans)
48
74
  })
49
75
  })
@@ -1,10 +1,10 @@
1
- import { PoolClient } from 'pg'
1
+ import { Pool, PoolClient } from 'pg'
2
2
 
3
3
  import type { DbConfig } from '../../../../src/Database'
4
4
  import { PostgresDatabase } from '../../../../src/Database/integrations/pgsql/PostgresDatabase'
5
5
  import { PostgresTransactionImpl } from '../../../../src/Database/integrations/pgsql/PostgresTransaction'
6
6
 
7
- const testResources = (config: Partial<DbConfig<'pg'>>) => {
7
+ const testResources = async (config: Partial<DbConfig<'pg'>>) => {
8
8
  const database = {
9
9
  config: {
10
10
  autoCommit: true,
@@ -12,7 +12,7 @@ const testResources = (config: Partial<DbConfig<'pg'>>) => {
12
12
  } as DbConfig<'pg'>,
13
13
  } as PostgresDatabase
14
14
 
15
- const delegate = jest.fn().mockImplementation(() => {
15
+ const transaction = jest.fn().mockImplementation(() => {
16
16
  return {
17
17
  commit: jest.fn(),
18
18
  rollback: jest.fn(),
@@ -20,18 +20,35 @@ const testResources = (config: Partial<DbConfig<'pg'>>) => {
20
20
  }
21
21
  })() as any as PoolClient
22
22
 
23
+ const writer = jest.fn().mockImplementation(() => {
24
+ return {
25
+ connect: jest.fn(() => transaction),
26
+ }
27
+ })() as any as Pool
28
+
23
29
  return {
24
- delegate,
25
- underTest: PostgresTransactionImpl.wrapDelegate(delegate, database),
30
+ transaction,
31
+ writer,
32
+ underTest: await PostgresTransactionImpl.newTransaction(writer, database),
26
33
  }
27
34
  }
28
35
 
29
36
  describe('PostgresTransaction', () => {
37
+ test('Transaction starts opened', async () => {
38
+ const { transaction, underTest } = await testResources({})
39
+ expect(underTest.isOpen()).toBe(true)
40
+ expect(transaction.query).toHaveBeenNthCalledWith(1, 'BEGIN')
41
+
42
+ await expect(underTest.begin).rejects.toThrowError(
43
+ 'Cannot begin, transaction is already opened!'
44
+ )
45
+ })
46
+
30
47
  test('Can only commit once', async () => {
31
- const { delegate, underTest } = testResources({})
48
+ const { transaction, underTest } = await testResources({})
32
49
  await underTest.commit()
33
50
  expect(underTest.isOpen()).toBe(false)
34
- expect(delegate.query).toBeCalledWith('COMMIT')
51
+ expect(transaction.query).toBeCalledWith('COMMIT')
35
52
 
36
53
  await expect(underTest.commit).rejects.toThrowError(
37
54
  'Cannot commit, transaction is already closed!'
@@ -39,13 +56,62 @@ describe('PostgresTransaction', () => {
39
56
  })
40
57
 
41
58
  test('Can only rollback once', async () => {
42
- const { delegate, underTest } = testResources({})
59
+ const { transaction, underTest } = await testResources({})
43
60
  await underTest.rollback()
44
61
  expect(underTest.isOpen()).toBe(false)
45
- expect(delegate.query).toBeCalledWith('ROLLBACK')
62
+ expect(transaction.query).toBeCalledWith('ROLLBACK')
46
63
 
47
64
  await expect(underTest.rollback).rejects.toThrowError(
48
65
  'Cannot rollback, transaction is already closed!'
49
66
  )
50
67
  })
68
+
69
+ test("Can't open transaction twice", async () => {
70
+ const { transaction, underTest } = await testResources({})
71
+ expect(underTest.isOpen()).toBe(true)
72
+ expect(transaction.query).toHaveBeenNthCalledWith(1, 'BEGIN')
73
+
74
+ await expect(underTest.begin).rejects.toThrowError(
75
+ 'Cannot begin, transaction is already opened!'
76
+ )
77
+ })
78
+
79
+ test('Can commit, begin and commit again ', async () => {
80
+ const { transaction, underTest } = await testResources({})
81
+ await underTest.commit()
82
+ expect(underTest.isOpen()).toBe(false)
83
+ expect(transaction.query).toBeCalledWith('COMMIT')
84
+ await underTest.begin()
85
+ expect(underTest.isOpen()).toBe(true)
86
+ expect(transaction.query).toHaveBeenNthCalledWith(3, 'BEGIN')
87
+ await underTest.commit()
88
+ expect(underTest.isOpen()).toBe(false)
89
+ expect(transaction.query).toHaveBeenNthCalledWith(4, 'COMMIT')
90
+ })
91
+
92
+ test('Can commit, begin and rollback ', async () => {
93
+ const { transaction, underTest } = await testResources({})
94
+ await underTest.commit()
95
+ expect(underTest.isOpen()).toBe(false)
96
+ expect(transaction.query).toBeCalledWith('COMMIT')
97
+ await underTest.begin()
98
+ expect(underTest.isOpen()).toBe(true)
99
+ expect(transaction.query).toHaveBeenNthCalledWith(3, 'BEGIN')
100
+ await underTest.rollback()
101
+ expect(underTest.isOpen()).toBe(false)
102
+ expect(transaction.query).toHaveBeenNthCalledWith(4, 'ROLLBACK')
103
+ })
104
+
105
+ test('Can rollback, begin and commit ', async () => {
106
+ const { transaction, underTest } = await testResources({})
107
+ await underTest.rollback()
108
+ expect(underTest.isOpen()).toBe(false)
109
+ expect(transaction.query).toBeCalledWith('ROLLBACK')
110
+ await underTest.begin()
111
+ expect(underTest.isOpen()).toBe(true)
112
+ expect(transaction.query).toHaveBeenNthCalledWith(3, 'BEGIN')
113
+ await underTest.commit()
114
+ expect(underTest.isOpen()).toBe(false)
115
+ expect(transaction.query).toHaveBeenNthCalledWith(4, 'COMMIT')
116
+ })
51
117
  })
package/tsconfig.json CHANGED
@@ -15,6 +15,7 @@
15
15
  "target": "es5",
16
16
  "outDir": "dist",
17
17
  "declaration": true,
18
+ "skipLibCheck": true,
18
19
  "strictNullChecks": true
19
20
  }
20
21
  }