@creator.co/wapi 1.3.19 → 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.
- package/dist/package.json +2 -1
- package/dist/src/API/Request.js.map +1 -1
- package/dist/src/API/Response.js.map +1 -1
- package/dist/src/API/Utils.js.map +1 -1
- package/dist/src/BaseEvent/EventProcessor.js.map +1 -1
- package/dist/src/BaseEvent/Transaction.d.ts +1 -1
- package/dist/src/Config/Configuration.js.map +1 -1
- package/dist/src/Config/EnvironmentVar.js.map +1 -1
- package/dist/src/Crypto/JWT.js.map +1 -1
- package/dist/src/Database/Database.d.ts +8 -5
- package/dist/src/Database/Database.js +5 -3
- package/dist/src/Database/Database.js.map +1 -1
- package/dist/src/Database/DatabaseManager.d.ts +30 -9
- package/dist/src/Database/DatabaseManager.js +25 -10
- package/dist/src/Database/DatabaseManager.js.map +1 -1
- package/dist/src/Database/DatabaseTransaction.d.ts +66 -31
- package/dist/src/Database/DatabaseTransaction.js +51 -30
- package/dist/src/Database/DatabaseTransaction.js.map +1 -1
- package/dist/src/Database/integrations/knex/KnexDatabase.d.ts +22 -8
- package/dist/src/Database/integrations/knex/KnexDatabase.js +18 -13
- package/dist/src/Database/integrations/knex/KnexDatabase.js.map +1 -1
- package/dist/src/Database/integrations/knex/KnexTransaction.d.ts +39 -16
- package/dist/src/Database/integrations/knex/KnexTransaction.js +83 -17
- package/dist/src/Database/integrations/knex/KnexTransaction.js.map +1 -1
- package/dist/src/Database/integrations/kysely/KyselyDatabase.d.ts +55 -0
- package/dist/src/Database/integrations/kysely/KyselyDatabase.js +126 -0
- package/dist/src/Database/integrations/kysely/KyselyDatabase.js.map +1 -0
- package/dist/src/Database/integrations/kysely/KyselyTransaction.d.ts +70 -0
- package/dist/src/Database/integrations/kysely/KyselyTransaction.js +186 -0
- package/dist/src/Database/integrations/kysely/KyselyTransaction.js.map +1 -0
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.d.ts +16 -11
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.js +19 -18
- package/dist/src/Database/integrations/pgsql/PostgresDatabase.js.map +1 -1
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.d.ts +42 -16
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.js +84 -18
- package/dist/src/Database/integrations/pgsql/PostgresTransaction.js.map +1 -1
- package/dist/src/Database/types.d.ts +47 -26
- package/dist/src/Logger/Logger.d.ts +6 -6
- package/dist/src/Logger/Logger.js +12 -15
- package/dist/src/Logger/Logger.js.map +1 -1
- package/dist/src/Publisher/Publisher.js.map +1 -1
- package/dist/src/Server/RouteResolver.js.map +1 -1
- package/dist/src/Server/lib/ContainerServer.js.map +1 -1
- package/dist/src/Server/lib/Server.js.map +1 -1
- package/dist/src/Server/lib/container/GenericHandler.js.map +1 -1
- package/dist/src/Server/lib/container/GenericHandlerEvent.js +1 -1
- package/dist/src/Server/lib/container/GenericHandlerEvent.js.map +1 -1
- package/dist/src/Server/lib/container/Utils.js.map +1 -1
- package/dist/src/Validation/Validator.js.map +1 -1
- package/package.json +2 -1
- package/src/BaseEvent/Transaction.ts +2 -2
- package/src/Database/Database.ts +8 -5
- package/src/Database/DatabaseManager.ts +34 -17
- package/src/Database/DatabaseTransaction.ts +79 -35
- package/src/Database/integrations/knex/KnexDatabase.ts +22 -11
- package/src/Database/integrations/knex/KnexTransaction.ts +52 -19
- package/src/Database/integrations/kysely/KyselyDatabase.ts +87 -0
- package/src/Database/integrations/kysely/KyselyTransaction.ts +172 -0
- package/src/Database/integrations/pgsql/PostgresDatabase.ts +26 -14
- package/src/Database/integrations/pgsql/PostgresTransaction.ts +55 -22
- package/src/Database/types.ts +53 -31
- package/src/Logger/Logger.ts +12 -15
- package/tests/Database/DatabaseManager.test.ts +16 -1
- package/tests/Database/integrations/knex/KnexDatabase.test.ts +2 -2
- package/tests/Database/integrations/knex/KnexTransaction.test.ts +52 -45
- package/tests/Database/integrations/kysely/KyselyDatabase.test.ts +109 -0
- package/tests/Database/integrations/kysely/KyselyTransaction.test.ts +118 -0
- package/tests/Database/integrations/pg/PostgresDatabase.test.ts +32 -6
- package/tests/Database/integrations/pg/PostgresTransaction.test.ts +75 -9
- package/tsconfig.json +1 -0
package/src/Logger/Logger.ts
CHANGED
|
@@ -33,8 +33,10 @@ const PURE_CONSOLE = console['notGlobalLogger'] ? console['origin'] : console
|
|
|
33
33
|
const DEFAULT_LOG_FUNCTION = PURE_CONSOLE.log.bind(PURE_CONSOLE)
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
|
-
*
|
|
37
|
-
*
|
|
36
|
+
* Creates a blacklist array by mapping each string in the given array to its lowercase form.
|
|
37
|
+
* The resulting blacklist array is used to filter out sensitive information.
|
|
38
|
+
* @type {string[]} blacklist - An array of strings to be converted to lowercase and used as a blacklist.
|
|
39
|
+
* @returns {string[]} - An array of lowercase strings representing the blacklist.
|
|
38
40
|
*/
|
|
39
41
|
const blacklist = ['password', 'token', 'accounts'].map(s => s.toLowerCase())
|
|
40
42
|
|
|
@@ -214,29 +216,24 @@ export default class Logger {
|
|
|
214
216
|
}
|
|
215
217
|
|
|
216
218
|
/**
|
|
217
|
-
*
|
|
218
|
-
* @param {number} index - The index of the caller in the
|
|
219
|
-
* @returns {string} The name of the caller
|
|
219
|
+
* Retrieves the name of the caller function at the specified index in the stack trace.
|
|
220
|
+
* @param {number} index - The index of the caller function in the stack trace.
|
|
221
|
+
* @returns {string} The name of the caller function along with the file path and line number.
|
|
220
222
|
*/
|
|
221
223
|
private callerName(index: number): string {
|
|
222
224
|
const safeIndex = Math.min(index, stackTrace.get().length)
|
|
223
225
|
if (stackTrace.get()[safeIndex]) {
|
|
224
|
-
let callerName = stackTrace
|
|
225
|
-
|
|
226
|
-
: null
|
|
227
|
-
if (!callerName) {
|
|
228
|
-
callerName = stackTrace?.get()?.[safeIndex]?.getFileName()?.split('/')
|
|
229
|
-
callerName = callerName?.slice(callerName?.indexOf('src'))?.join('/')
|
|
230
|
-
}
|
|
226
|
+
let callerName = stackTrace?.get()?.[safeIndex]?.getFileName()?.split('/')
|
|
227
|
+
callerName = callerName?.slice(callerName?.indexOf('src'))?.join('/')
|
|
231
228
|
return callerName + ':' + stackTrace?.get()?.[safeIndex]?.getLineNumber()
|
|
232
229
|
}
|
|
233
230
|
return ''
|
|
234
231
|
}
|
|
235
232
|
|
|
236
233
|
/**
|
|
237
|
-
* Processes
|
|
238
|
-
* @param {LOG_LEVELS} level - The
|
|
239
|
-
* @param {any
|
|
234
|
+
* Processes log messages based on the specified log level.
|
|
235
|
+
* @param {LOG_LEVELS} level - The level of the log message.
|
|
236
|
+
* @param {any} args - The arguments to be logged.
|
|
240
237
|
* @returns None
|
|
241
238
|
*/
|
|
242
239
|
private processLog(level: LOG_LEVELS, args: any): void {
|
|
@@ -3,7 +3,7 @@ import { expect } from 'chai'
|
|
|
3
3
|
import { DatabaseManager } from '../../src/Database'
|
|
4
4
|
import type { DatabaseType, DbConfig } from '../../src/Database/types'
|
|
5
5
|
|
|
6
|
-
type FakeDatabase = { host: string; type: 'knex' | 'pg' }
|
|
6
|
+
type FakeDatabase = { host: string; type: 'knex' | 'pg' | 'kysely' }
|
|
7
7
|
|
|
8
8
|
const fakeConfig = (host: string, type: DatabaseType) => {
|
|
9
9
|
return {
|
|
@@ -23,6 +23,9 @@ describe('Database Manager', () => {
|
|
|
23
23
|
pg: jest.fn(config => {
|
|
24
24
|
return { host: config.host, type: 'pg' } as any
|
|
25
25
|
}) as any,
|
|
26
|
+
kysely: jest.fn(config => {
|
|
27
|
+
return { host: config.host, type: 'kysely' } as any
|
|
28
|
+
}) as any,
|
|
26
29
|
}
|
|
27
30
|
|
|
28
31
|
const create = config => underTest.create({ ...config }) as unknown as FakeDatabase
|
|
@@ -31,6 +34,8 @@ describe('Database Manager', () => {
|
|
|
31
34
|
const knexConfig2 = fakeConfig('other', 'knex')
|
|
32
35
|
const pgConfig1 = fakeConfig('localhost', 'pg')
|
|
33
36
|
const pgConfig2 = fakeConfig('other', 'pg')
|
|
37
|
+
const kyselyConfig1 = fakeConfig('localhost', 'pg')
|
|
38
|
+
const kyselyConfig2 = fakeConfig('other', 'pg')
|
|
34
39
|
|
|
35
40
|
const knex1 = create(knexConfig1)
|
|
36
41
|
const knex2 = create(knexConfig1)
|
|
@@ -40,6 +45,10 @@ describe('Database Manager', () => {
|
|
|
40
45
|
const pg2 = create(pgConfig1)
|
|
41
46
|
const pg3 = create(pgConfig2)
|
|
42
47
|
|
|
48
|
+
const kysely1 = create(kyselyConfig1)
|
|
49
|
+
const kysely2 = create(kyselyConfig1)
|
|
50
|
+
const kysely3 = create(kyselyConfig2)
|
|
51
|
+
|
|
43
52
|
expect(knex1).to.equal(knex2).to.not.equal(knex3).to.not.equal(pg1)
|
|
44
53
|
|
|
45
54
|
expect(knexConfig1).to.be.deep.equal(knex1).to.be.deep.equal(knex2)
|
|
@@ -51,5 +60,11 @@ describe('Database Manager', () => {
|
|
|
51
60
|
expect(pgConfig1).to.be.deep.equal(pg1).to.be.deep.equal(pg2)
|
|
52
61
|
|
|
53
62
|
expect(pg3).to.be.deep.equal(pgConfig2)
|
|
63
|
+
|
|
64
|
+
expect(kysely1).to.equal(kysely2).to.not.equal(kysely3)
|
|
65
|
+
|
|
66
|
+
expect(kyselyConfig1).to.be.deep.equal(kysely1).to.be.deep.equal(kysely2)
|
|
67
|
+
|
|
68
|
+
expect(kysely3).to.be.deep.equal(kyselyConfig2)
|
|
54
69
|
})
|
|
55
70
|
})
|
|
@@ -47,7 +47,7 @@ describe('KnexDatabase', () => {
|
|
|
47
47
|
const trans = await underTest.transaction()
|
|
48
48
|
|
|
49
49
|
expect(trans).toBeInstanceOf(Function)
|
|
50
|
-
expect(trans
|
|
50
|
+
expect(trans.transaction).toBe(mockTrans)
|
|
51
51
|
})
|
|
52
52
|
|
|
53
53
|
test('KnexDatabase - convert camel to snake', async () => {
|
|
@@ -70,6 +70,6 @@ describe('KnexDatabase', () => {
|
|
|
70
70
|
const trans = await underTest.transaction()
|
|
71
71
|
|
|
72
72
|
expect(trans).toBeInstanceOf(Function)
|
|
73
|
-
expect(trans
|
|
73
|
+
expect(trans.transaction).toBe(mockTrans)
|
|
74
74
|
})
|
|
75
75
|
})
|
|
@@ -4,7 +4,7 @@ import { DbConfig } from '../../../../src/Database'
|
|
|
4
4
|
import { KnexDatabase } from '../../../../src/Database/integrations/knex/KnexDatabase'
|
|
5
5
|
import { KnexTransactionImpl } from '../../../../src/Database/integrations/knex/KnexTransaction'
|
|
6
6
|
|
|
7
|
-
const testResources = (config: Partial<DbConfig<'knex'>>) => {
|
|
7
|
+
const testResources = async (config: Partial<DbConfig<'knex'>>) => {
|
|
8
8
|
const database = {
|
|
9
9
|
config: {
|
|
10
10
|
autoCommit: true,
|
|
@@ -12,25 +12,31 @@ const testResources = (config: Partial<DbConfig<'knex'>>) => {
|
|
|
12
12
|
} as DbConfig<'knex'>,
|
|
13
13
|
} as KnexDatabase
|
|
14
14
|
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
const txMock = jest.fn() as any
|
|
16
|
+
txMock.commit = jest.fn()
|
|
17
|
+
txMock.rollback = jest.fn()
|
|
18
|
+
txMock.select = jest.fn()
|
|
19
19
|
|
|
20
|
-
const
|
|
20
|
+
const wMock = jest.fn() as any
|
|
21
|
+
wMock.transaction = jest.fn(() => txMock)
|
|
22
|
+
|
|
23
|
+
const writer = wMock as Knex
|
|
24
|
+
const transaction = txMock as Knex.Transaction
|
|
21
25
|
|
|
22
26
|
return {
|
|
23
|
-
|
|
24
|
-
|
|
27
|
+
transaction,
|
|
28
|
+
writer,
|
|
29
|
+
underTest: await KnexTransactionImpl.newTransaction(writer, database),
|
|
25
30
|
}
|
|
26
31
|
}
|
|
27
32
|
|
|
28
33
|
describe('KnexTransaction', () => {
|
|
29
34
|
test('Can only commit once', async () => {
|
|
30
|
-
const {
|
|
35
|
+
const { writer, transaction, underTest } = await testResources({})
|
|
31
36
|
await underTest.commit()
|
|
32
37
|
expect(underTest.isOpen()).toBe(false)
|
|
33
|
-
expect(
|
|
38
|
+
expect(writer.transaction).toBeCalled()
|
|
39
|
+
expect(transaction.commit).toBeCalled()
|
|
34
40
|
|
|
35
41
|
await expect(underTest.commit).rejects.toThrowError(
|
|
36
42
|
'Cannot commit, transaction is already closed!'
|
|
@@ -38,28 +44,29 @@ describe('KnexTransaction', () => {
|
|
|
38
44
|
})
|
|
39
45
|
|
|
40
46
|
test("Can't commit after rollback", async () => {
|
|
41
|
-
const {
|
|
47
|
+
const { writer, transaction, underTest } = await testResources({})
|
|
42
48
|
await underTest.rollback()
|
|
43
49
|
expect(underTest.isOpen()).toBe(false)
|
|
44
|
-
expect(
|
|
50
|
+
expect(writer.transaction).toBeCalled()
|
|
51
|
+
expect(transaction.rollback).toBeCalled()
|
|
45
52
|
|
|
46
53
|
await expect(underTest.commit).rejects.toThrowError(
|
|
47
54
|
'Cannot commit, transaction is already closed!'
|
|
48
55
|
)
|
|
49
56
|
})
|
|
50
57
|
|
|
51
|
-
test('Forwards calls to
|
|
52
|
-
const {
|
|
58
|
+
test('Forwards calls to transaction', async () => {
|
|
59
|
+
const { transaction, underTest } = await testResources({})
|
|
53
60
|
await underTest.select('name')
|
|
54
|
-
expect(
|
|
61
|
+
expect(transaction.select).toBeCalledWith('name')
|
|
55
62
|
})
|
|
56
63
|
|
|
57
|
-
test('Blocks
|
|
58
|
-
const {
|
|
64
|
+
test('Blocks transaction calls after commit', async () => {
|
|
65
|
+
const { transaction, underTest } = await testResources({})
|
|
59
66
|
expect(underTest.select).toBeDefined()
|
|
60
67
|
|
|
61
68
|
await underTest.commit()
|
|
62
|
-
expect(
|
|
69
|
+
expect(transaction.commit).toBeCalled()
|
|
63
70
|
expect(underTest.isOpen()).toBe(false)
|
|
64
71
|
|
|
65
72
|
expect(underTest.select).toBeUndefined()
|
|
@@ -68,74 +75,74 @@ describe('KnexTransaction', () => {
|
|
|
68
75
|
|
|
69
76
|
describe('KnexTransaction - closeSuccess', () => {
|
|
70
77
|
test('Close success commits', async () => {
|
|
71
|
-
const {
|
|
78
|
+
const { transaction, underTest } = await testResources({})
|
|
72
79
|
await underTest.closeSuccess()
|
|
73
|
-
expect(
|
|
80
|
+
expect(transaction.commit).toBeCalled()
|
|
74
81
|
})
|
|
75
82
|
|
|
76
83
|
test("Close success, doesn't commit again if already committed", async () => {
|
|
77
|
-
const {
|
|
84
|
+
const { transaction, underTest } = await testResources({})
|
|
78
85
|
await underTest.commit()
|
|
79
|
-
expect(
|
|
86
|
+
expect(transaction.commit).toBeCalledTimes(1)
|
|
80
87
|
await underTest.closeSuccess()
|
|
81
|
-
expect(
|
|
88
|
+
expect(transaction.commit).toBeCalledTimes(1)
|
|
82
89
|
})
|
|
83
90
|
|
|
84
91
|
test('Close success, rolls back if not autocommit', async () => {
|
|
85
|
-
const {
|
|
92
|
+
const { transaction, underTest } = await testResources({ autoCommit: false })
|
|
86
93
|
await underTest.closeSuccess()
|
|
87
|
-
expect(
|
|
94
|
+
expect(transaction.rollback).toBeCalled()
|
|
88
95
|
})
|
|
89
96
|
|
|
90
97
|
test("Close success, doesn't rollback if already committed", async () => {
|
|
91
|
-
const {
|
|
98
|
+
const { transaction, underTest } = await testResources({ autoCommit: false })
|
|
92
99
|
await underTest.commit()
|
|
93
|
-
expect(
|
|
100
|
+
expect(transaction.commit).toBeCalledTimes(1)
|
|
94
101
|
await underTest.closeSuccess()
|
|
95
|
-
expect(
|
|
96
|
-
expect(
|
|
102
|
+
expect(transaction.rollback).not.toBeCalled()
|
|
103
|
+
expect(transaction.commit).toBeCalledTimes(1)
|
|
97
104
|
})
|
|
98
105
|
|
|
99
106
|
test("Close success, doesn't rollback if already rolled back", async () => {
|
|
100
|
-
const {
|
|
107
|
+
const { transaction, underTest } = await testResources({ autoCommit: false })
|
|
101
108
|
await underTest.rollback()
|
|
102
|
-
expect(
|
|
109
|
+
expect(transaction.rollback).toBeCalledTimes(1)
|
|
103
110
|
await underTest.closeSuccess()
|
|
104
|
-
expect(
|
|
105
|
-
expect(
|
|
111
|
+
expect(transaction.rollback).toBeCalledTimes(1)
|
|
112
|
+
expect(transaction.commit).not.toBeCalled()
|
|
106
113
|
})
|
|
107
114
|
})
|
|
108
115
|
|
|
109
116
|
describe('KnexTransaction - closeFailure', () => {
|
|
110
117
|
test('Close failure rolls back', async () => {
|
|
111
|
-
const {
|
|
118
|
+
const { transaction, underTest } = await testResources({})
|
|
112
119
|
await underTest.closeFailure()
|
|
113
|
-
expect(
|
|
120
|
+
expect(transaction.rollback).toBeCalled()
|
|
114
121
|
})
|
|
115
122
|
|
|
116
123
|
test("Close failure, doesn't rollback again if already rolled back", async () => {
|
|
117
|
-
const {
|
|
124
|
+
const { transaction, underTest } = await testResources({})
|
|
118
125
|
await underTest.rollback()
|
|
119
|
-
expect(
|
|
126
|
+
expect(transaction.rollback).toBeCalledTimes(1)
|
|
120
127
|
await underTest.closeFailure()
|
|
121
|
-
expect(
|
|
128
|
+
expect(transaction.rollback).toBeCalledTimes(1)
|
|
122
129
|
})
|
|
123
130
|
|
|
124
131
|
test("Close failure, doesn't rollback if already committed", async () => {
|
|
125
|
-
const {
|
|
132
|
+
const { transaction, underTest } = await testResources({ autoCommit: false })
|
|
126
133
|
await underTest.commit()
|
|
127
|
-
expect(
|
|
134
|
+
expect(transaction.commit).toBeCalledTimes(1)
|
|
128
135
|
await underTest.closeFailure()
|
|
129
|
-
expect(
|
|
130
|
-
expect(
|
|
136
|
+
expect(transaction.rollback).not.toBeCalled()
|
|
137
|
+
expect(transaction.commit).toBeCalledTimes(1)
|
|
131
138
|
})
|
|
132
139
|
})
|
|
133
140
|
|
|
134
141
|
describe('KnexTransaction - call direct', () => {
|
|
135
|
-
test('Calls
|
|
136
|
-
const {
|
|
142
|
+
test('Calls transaction', async () => {
|
|
143
|
+
const { transaction, underTest } = await testResources({ autoCommit: false })
|
|
137
144
|
|
|
138
145
|
underTest('test')
|
|
139
|
-
expect(
|
|
146
|
+
expect(transaction).toBeCalledWith('test')
|
|
140
147
|
})
|
|
141
148
|
})
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { expect as c_expect } from 'chai'
|
|
2
|
+
import { PostgresDialect } from 'kysely'
|
|
3
|
+
import { PoolClient } from 'pg'
|
|
4
|
+
|
|
5
|
+
import type { DbConfig } from '../../../../src/Database'
|
|
6
|
+
import { KyselyDatabase } from '../../../../src/Database/integrations/kysely/KyselyDatabase'
|
|
7
|
+
|
|
8
|
+
const config: DbConfig<'kysely'> = {
|
|
9
|
+
type: 'kysely',
|
|
10
|
+
username: 'username',
|
|
11
|
+
password: 'password',
|
|
12
|
+
host: 'host',
|
|
13
|
+
port: 1234,
|
|
14
|
+
database: 'database',
|
|
15
|
+
maxConnections: 1,
|
|
16
|
+
autoCommit: true,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const config2: DbConfig<'kysely'> = {
|
|
20
|
+
...config,
|
|
21
|
+
readReplica: config,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const expectedImplConfig = {
|
|
25
|
+
host: config.host,
|
|
26
|
+
port: config.port,
|
|
27
|
+
user: config.username,
|
|
28
|
+
password: config.password,
|
|
29
|
+
database: config.database,
|
|
30
|
+
max: config.maxConnections,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
describe('KyselyDatabase', () => {
|
|
34
|
+
let mockTrans
|
|
35
|
+
let mockClient
|
|
36
|
+
let mockDriver
|
|
37
|
+
let mockDialect
|
|
38
|
+
let mockPgTrans
|
|
39
|
+
let mockPgClient
|
|
40
|
+
beforeEach(() => {
|
|
41
|
+
mockTrans = {
|
|
42
|
+
execute: jest.fn(async cb => {
|
|
43
|
+
cb(mockTrans)
|
|
44
|
+
}),
|
|
45
|
+
} as any
|
|
46
|
+
mockClient = jest.fn(
|
|
47
|
+
() =>
|
|
48
|
+
({
|
|
49
|
+
transaction: () => mockTrans,
|
|
50
|
+
}) as any as PostgresDialect
|
|
51
|
+
)
|
|
52
|
+
KyselyDatabase['kyselyProvider'] = mockClient as any
|
|
53
|
+
|
|
54
|
+
mockDriver = {
|
|
55
|
+
execute: jest.fn(async () => {}),
|
|
56
|
+
} as any
|
|
57
|
+
mockDialect = jest.fn(
|
|
58
|
+
() =>
|
|
59
|
+
({
|
|
60
|
+
createDriver: () => mockDriver,
|
|
61
|
+
}) as any as PostgresDialect
|
|
62
|
+
)
|
|
63
|
+
KyselyDatabase['kyselyPgProvider'] = mockDialect as any
|
|
64
|
+
|
|
65
|
+
mockPgTrans = {
|
|
66
|
+
query: jest.fn(() => {}),
|
|
67
|
+
} as any
|
|
68
|
+
mockPgClient = jest.fn(
|
|
69
|
+
() =>
|
|
70
|
+
({
|
|
71
|
+
connect: async () => mockPgTrans,
|
|
72
|
+
}) as PoolClient
|
|
73
|
+
)
|
|
74
|
+
KyselyDatabase['pgProvider'] = mockPgClient as any
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('KyselyDatabase', async () => {
|
|
78
|
+
const underTest = new KyselyDatabase(config)
|
|
79
|
+
expect(mockPgClient).toHaveBeenNthCalledWith(1, expectedImplConfig)
|
|
80
|
+
expect(mockDialect).toHaveBeenCalledTimes(1)
|
|
81
|
+
expect(mockClient).toHaveBeenNthCalledWith(1, { dialect: underTest.pgClient })
|
|
82
|
+
|
|
83
|
+
const trans = await underTest.transaction()
|
|
84
|
+
|
|
85
|
+
expect(mockTrans.execute).toHaveBeenCalledTimes(1)
|
|
86
|
+
expect(trans.reader).toBeUndefined()
|
|
87
|
+
expect(trans.writer).toBeInstanceOf(Object)
|
|
88
|
+
expect(trans.writer.transaction).toBeInstanceOf(Function)
|
|
89
|
+
expect(trans).toBeInstanceOf(Function)
|
|
90
|
+
expect(trans['transaction']).toBe(mockTrans)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
test('KyselyDatabase - read replica', async () => {
|
|
94
|
+
const underTest = new KyselyDatabase(config2)
|
|
95
|
+
expect(mockPgClient).toHaveBeenNthCalledWith(2, expectedImplConfig)
|
|
96
|
+
expect(mockDialect).toHaveBeenCalledTimes(2)
|
|
97
|
+
expect(mockClient).toHaveBeenNthCalledWith(1, { dialect: underTest.pgClient })
|
|
98
|
+
expect(mockClient).toHaveBeenNthCalledWith(2, { dialect: underTest.pgReadClient })
|
|
99
|
+
|
|
100
|
+
const trans = await underTest.transaction()
|
|
101
|
+
|
|
102
|
+
expect(mockTrans.execute).toHaveBeenCalledTimes(1)
|
|
103
|
+
expect(trans.writer).toBeInstanceOf(Object)
|
|
104
|
+
expect(trans.writer.transaction).toBeInstanceOf(Function)
|
|
105
|
+
expect(trans.reader).toBeInstanceOf(Object)
|
|
106
|
+
expect(trans).toBeInstanceOf(Function)
|
|
107
|
+
expect(trans['transaction']).toBe(mockTrans)
|
|
108
|
+
})
|
|
109
|
+
})
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { Kysely, PostgresDialect, Transaction } from 'kysely'
|
|
2
|
+
|
|
3
|
+
import type { DbConfig } from '../../../../src/Database'
|
|
4
|
+
import { KyselyDatabase } from '../../../../src/Database/integrations/kysely/KyselyDatabase'
|
|
5
|
+
import { KyselyTransactionImpl } from '../../../../src/Database/integrations/kysely/KyselyTransaction'
|
|
6
|
+
|
|
7
|
+
const testResources = async (config: Partial<DbConfig<'kysely'>>) => {
|
|
8
|
+
type DB = {
|
|
9
|
+
view: {
|
|
10
|
+
id: number
|
|
11
|
+
}
|
|
12
|
+
secondView: {
|
|
13
|
+
title: string
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const database = {
|
|
18
|
+
config: {
|
|
19
|
+
autoCommit: true,
|
|
20
|
+
...config,
|
|
21
|
+
} as DbConfig<'kysely'>,
|
|
22
|
+
} as KyselyDatabase<DB>
|
|
23
|
+
|
|
24
|
+
const transaction = jest.fn().mockImplementation(() => {
|
|
25
|
+
return {
|
|
26
|
+
execute: jest.fn(async cb => {
|
|
27
|
+
try {
|
|
28
|
+
await cb(transaction)
|
|
29
|
+
} catch (e) {
|
|
30
|
+
/* empty */
|
|
31
|
+
}
|
|
32
|
+
}),
|
|
33
|
+
executeQuery: jest.fn(),
|
|
34
|
+
}
|
|
35
|
+
})() as any as Transaction<DB>
|
|
36
|
+
|
|
37
|
+
const writer = jest.fn().mockImplementation(() => {
|
|
38
|
+
return {
|
|
39
|
+
transaction: jest.fn(() => transaction),
|
|
40
|
+
}
|
|
41
|
+
})() as any as Kysely<DB>
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
transaction,
|
|
45
|
+
writer,
|
|
46
|
+
underTest: await KyselyTransactionImpl.newTransaction(writer, database),
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
describe('KyselyTransaction', () => {
|
|
51
|
+
test('Transaction starts opened', async () => {
|
|
52
|
+
const { transaction, underTest } = await testResources({})
|
|
53
|
+
expect(underTest.isOpen()).toBe(true)
|
|
54
|
+
expect(transaction['execute']).toHaveBeenCalledTimes(1)
|
|
55
|
+
|
|
56
|
+
await expect(underTest.begin).rejects.toThrowError(
|
|
57
|
+
'Cannot begin, transaction is already opened!'
|
|
58
|
+
)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test('Can only commit once', async () => {
|
|
62
|
+
const { underTest } = await testResources({})
|
|
63
|
+
await underTest.commit()
|
|
64
|
+
expect(underTest.isOpen()).toBe(false)
|
|
65
|
+
|
|
66
|
+
await expect(underTest.commit).rejects.toThrowError(
|
|
67
|
+
'Cannot commit, transaction is already closed!'
|
|
68
|
+
)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
test('Can only rollback once', async () => {
|
|
72
|
+
const { underTest } = await testResources({})
|
|
73
|
+
await underTest.rollback()
|
|
74
|
+
expect(underTest.isOpen()).toBe(false)
|
|
75
|
+
await expect(underTest.rollback).rejects.toThrowError(
|
|
76
|
+
'Cannot rollback, transaction is already closed!'
|
|
77
|
+
)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test("Can't open transaction twice", async () => {
|
|
81
|
+
const { underTest } = await testResources({})
|
|
82
|
+
expect(underTest.isOpen()).toBe(true)
|
|
83
|
+
|
|
84
|
+
await expect(underTest.begin).rejects.toThrowError(
|
|
85
|
+
'Cannot begin, transaction is already opened!'
|
|
86
|
+
)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
test('Can commit, begin and commit again ', async () => {
|
|
90
|
+
const { underTest } = await testResources({})
|
|
91
|
+
await underTest.commit()
|
|
92
|
+
expect(underTest.isOpen()).toBe(false)
|
|
93
|
+
await underTest.begin()
|
|
94
|
+
expect(underTest.isOpen()).toBe(true)
|
|
95
|
+
await underTest.commit()
|
|
96
|
+
expect(underTest.isOpen()).toBe(false)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
test('Can commit, begin and rollback ', async () => {
|
|
100
|
+
const { underTest } = await testResources({})
|
|
101
|
+
await underTest.commit()
|
|
102
|
+
expect(underTest.isOpen()).toBe(false)
|
|
103
|
+
await underTest.begin()
|
|
104
|
+
expect(underTest.isOpen()).toBe(true)
|
|
105
|
+
await underTest.rollback()
|
|
106
|
+
expect(underTest.isOpen()).toBe(false)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test('Can rollback, begin and commit ', async () => {
|
|
110
|
+
const { underTest } = await testResources({})
|
|
111
|
+
await underTest.rollback()
|
|
112
|
+
expect(underTest.isOpen()).toBe(false)
|
|
113
|
+
await underTest.begin()
|
|
114
|
+
expect(underTest.isOpen()).toBe(true)
|
|
115
|
+
await underTest.commit()
|
|
116
|
+
expect(underTest.isOpen()).toBe(false)
|
|
117
|
+
})
|
|
118
|
+
})
|
|
@@ -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
|
-
|
|
29
|
-
|
|
32
|
+
let mockTrans
|
|
33
|
+
let mockClient
|
|
34
|
+
beforeEach(() => {
|
|
35
|
+
mockTrans = {
|
|
30
36
|
query: jest.fn(() => {}),
|
|
31
37
|
} as any
|
|
32
|
-
|
|
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).
|
|
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['
|
|
73
|
+
expect(trans['transaction']).toBe(mockTrans)
|
|
48
74
|
})
|
|
49
75
|
})
|