@contember/database 1.0.0-rc.1 → 1.0.0-rc.12
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/src/client/Client.d.ts +2 -2
- package/dist/src/client/Client.d.ts.map +1 -1
- package/dist/src/client/Client.js +10 -7
- package/dist/src/client/Client.js.map +1 -1
- package/dist/src/client/Connection.d.ts +8 -3
- package/dist/src/client/Connection.d.ts.map +1 -1
- package/dist/src/client/Connection.js +7 -6
- package/dist/src/client/Connection.js.map +1 -1
- package/dist/src/client/EventManager.d.ts +2 -5
- package/dist/src/client/EventManager.d.ts.map +1 -1
- package/dist/src/client/EventManager.js +3 -4
- package/dist/src/client/EventManager.js.map +1 -1
- package/dist/src/client/SingleConnection.d.ts +3 -1
- package/dist/src/client/SingleConnection.d.ts.map +1 -1
- package/dist/src/client/SingleConnection.js +7 -5
- package/dist/src/client/SingleConnection.js.map +1 -1
- package/dist/src/client/Transaction.d.ts +3 -1
- package/dist/src/client/Transaction.d.ts.map +1 -1
- package/dist/src/client/Transaction.js +11 -8
- package/dist/src/client/Transaction.js.map +1 -1
- package/dist/src/tsconfig.tsbuildinfo +1 -1
- package/dist/tests/cases/unit/eventManagerTest.d.ts +2 -0
- package/dist/tests/cases/unit/eventManagerTest.d.ts.map +1 -0
- package/dist/tests/cases/unit/eventManagerTest.js +62 -0
- package/dist/tests/cases/unit/eventManagerTest.js.map +1 -0
- package/dist/tests/cases/unit/queryBuilderTest.js +1 -1
- package/dist/tests/cases/unit/queryBuilderTest.js.map +1 -1
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/client/Client.ts +22 -8
- package/src/client/Connection.ts +12 -7
- package/src/client/EventManager.ts +4 -3
- package/src/client/SingleConnection.ts +14 -9
- package/src/client/Transaction.ts +13 -7
- package/tests/cases/unit/eventManagerTest.ts +63 -0
- package/tests/cases/unit/queryBuilderTest.ts +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contember/database",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.12",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "dist/src/index.js",
|
|
6
6
|
"typings": "dist/src/index.d.ts",
|
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
"test": "uvu dist/tests/cases/ \\.js$"
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@contember/queryable": "^1.0.0-rc.
|
|
11
|
+
"@contember/queryable": "^1.0.0-rc.12"
|
|
12
12
|
},
|
|
13
13
|
"devDependencies": {
|
|
14
|
-
"@contember/database-tester": "^1.0.0-rc.
|
|
14
|
+
"@contember/database-tester": "^1.0.0-rc.12",
|
|
15
15
|
"@types/node": "^17.0.5",
|
|
16
16
|
"pg": "^8.5.0",
|
|
17
17
|
"uvu": "^0.5.1"
|
package/src/client/Client.ts
CHANGED
|
@@ -9,19 +9,25 @@ class Client<ConnectionType extends Connection.ConnectionLike = Connection.Conne
|
|
|
9
9
|
public readonly connection: ConnectionType,
|
|
10
10
|
public readonly schema: string,
|
|
11
11
|
public readonly queryMeta: Record<string, any>,
|
|
12
|
+
public readonly eventManager: EventManager = new EventManager(connection.eventManager),
|
|
12
13
|
) {}
|
|
13
14
|
|
|
14
|
-
get eventManager(): EventManager {
|
|
15
|
-
return this.connection.eventManager
|
|
16
|
-
}
|
|
17
|
-
|
|
18
15
|
public forSchema(schema: string): Client<ConnectionType> {
|
|
19
|
-
|
|
16
|
+
const eventManager = new EventManager(this.eventManager.parent)
|
|
17
|
+
return new Client<ConnectionType>(this.connection, schema, this.queryMeta, eventManager)
|
|
20
18
|
}
|
|
21
19
|
|
|
22
20
|
async transaction<T>(transactionScope: (wrapper: Client<Connection.TransactionLike>) => Promise<T> | T): Promise<T> {
|
|
23
|
-
return await this.connection.transaction(
|
|
24
|
-
|
|
21
|
+
return await this.connection.transaction(
|
|
22
|
+
transaction =>
|
|
23
|
+
transactionScope(
|
|
24
|
+
new Client<Connection.TransactionLike>(
|
|
25
|
+
transaction,
|
|
26
|
+
this.schema,
|
|
27
|
+
this.queryMeta,
|
|
28
|
+
new EventManager(transaction.eventManager),
|
|
29
|
+
)),
|
|
30
|
+
{ eventManager: this.eventManager },
|
|
25
31
|
)
|
|
26
32
|
}
|
|
27
33
|
|
|
@@ -47,7 +53,15 @@ class Client<ConnectionType extends Connection.ConnectionLike = Connection.Conne
|
|
|
47
53
|
meta: Record<string, any> = {},
|
|
48
54
|
config: Connection.QueryConfig = {},
|
|
49
55
|
): Promise<Connection.Result<Row>> {
|
|
50
|
-
return this.connection.query<Row>(
|
|
56
|
+
return this.connection.query<Row>(
|
|
57
|
+
sql,
|
|
58
|
+
parameters,
|
|
59
|
+
{ ...this.queryMeta, ...meta },
|
|
60
|
+
{
|
|
61
|
+
eventManager: this.eventManager,
|
|
62
|
+
...config,
|
|
63
|
+
},
|
|
64
|
+
)
|
|
51
65
|
}
|
|
52
66
|
|
|
53
67
|
createQueryHandler(): QueryHandler<DatabaseQueryable> {
|
package/src/client/Connection.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Pool, PoolConfig } from 'pg'
|
|
2
|
-
import { EventManager
|
|
2
|
+
import { EventManager } from './EventManager'
|
|
3
3
|
import { Client } from './Client'
|
|
4
4
|
import { Transaction } from './Transaction'
|
|
5
5
|
import { executeClientOperation, executeQuery } from './execution'
|
|
@@ -10,7 +10,7 @@ class Connection implements Connection.ConnectionLike, Connection.ClientFactory,
|
|
|
10
10
|
constructor(
|
|
11
11
|
public readonly config: PoolConfig,
|
|
12
12
|
private readonly queryConfig: Connection.QueryConfig,
|
|
13
|
-
public readonly eventManager: EventManager = new
|
|
13
|
+
public readonly eventManager: EventManager = new EventManager(null),
|
|
14
14
|
) {
|
|
15
15
|
this.pool = new Pool(config)
|
|
16
16
|
this.pool.on('error', err => {
|
|
@@ -21,14 +21,15 @@ class Connection implements Connection.ConnectionLike, Connection.ClientFactory,
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
public createClient(schema: string, queryMeta: Record<string, any>): Client {
|
|
24
|
-
return new Client(this, schema, queryMeta)
|
|
24
|
+
return new Client(this, schema, queryMeta, new EventManager(this.eventManager))
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
async transaction<Result>(
|
|
28
28
|
callback: (connection: Connection.TransactionLike) => Promise<Result> | Result,
|
|
29
|
+
options: { eventManager?: EventManager } = {},
|
|
29
30
|
): Promise<Result> {
|
|
30
31
|
const client = await executeClientOperation(() => this.pool.connect())
|
|
31
|
-
const eventManager = new
|
|
32
|
+
const eventManager = new EventManager(options.eventManager ?? this.eventManager)
|
|
32
33
|
await executeQuery(client, eventManager, {
|
|
33
34
|
sql: 'BEGIN',
|
|
34
35
|
...this.queryConfig,
|
|
@@ -56,12 +57,12 @@ class Connection implements Connection.ConnectionLike, Connection.ClientFactory,
|
|
|
56
57
|
sql: string,
|
|
57
58
|
parameters: any[] = [],
|
|
58
59
|
meta: Record<string, any> = {},
|
|
59
|
-
config: Connection.QueryConfig = {},
|
|
60
|
+
{ eventManager, ...config }: Connection.QueryConfig = {},
|
|
60
61
|
): Promise<Connection.Result<Row>> {
|
|
61
62
|
const client = await executeClientOperation(() => this.pool.connect())
|
|
62
63
|
const query: Connection.Query = { sql, parameters, meta, ...this.queryConfig, ...config }
|
|
63
64
|
try {
|
|
64
|
-
const result = await executeQuery<Row>(client, this.eventManager, query, {})
|
|
65
|
+
const result = await executeQuery<Row>(client, eventManager ?? this.eventManager, query, {})
|
|
65
66
|
client.release()
|
|
66
67
|
return result
|
|
67
68
|
} catch (e) {
|
|
@@ -83,6 +84,7 @@ class Connection implements Connection.ConnectionLike, Connection.ClientFactory,
|
|
|
83
84
|
namespace Connection {
|
|
84
85
|
export interface QueryConfig {
|
|
85
86
|
timing?: boolean
|
|
87
|
+
eventManager?: EventManager
|
|
86
88
|
}
|
|
87
89
|
|
|
88
90
|
export interface Queryable {
|
|
@@ -97,7 +99,10 @@ namespace Connection {
|
|
|
97
99
|
}
|
|
98
100
|
|
|
99
101
|
export interface Transactional {
|
|
100
|
-
transaction<Result>(
|
|
102
|
+
transaction<Result>(
|
|
103
|
+
trx: (connection: TransactionLike) => Promise<Result> | Result,
|
|
104
|
+
options?: { eventManager?: EventManager },
|
|
105
|
+
): Promise<Result>
|
|
101
106
|
}
|
|
102
107
|
|
|
103
108
|
export interface ConnectionLike extends Transactional, Queryable {}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Connection } from './Connection'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
class EventManager {
|
|
4
4
|
private readonly listeners = {
|
|
5
5
|
[EventManager.Event.queryStart]: [] as EventManager.QueryStartCallback[],
|
|
6
6
|
[EventManager.Event.queryEnd]: [] as EventManager.QueryEndCallback[],
|
|
@@ -8,7 +8,9 @@ export class EventManagerImpl {
|
|
|
8
8
|
[EventManager.Event.clientError]: [] as EventManager.ClientErrorCallback[],
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
constructor(
|
|
11
|
+
constructor(
|
|
12
|
+
public readonly parent: EventManager | null = null,
|
|
13
|
+
) {}
|
|
12
14
|
|
|
13
15
|
on<Event extends keyof EventManager.ListenerTypes>(event: Event, cb: EventManager.ListenerTypes[Event]): void {
|
|
14
16
|
(this.listeners[event] as EventManager.ListenerTypes[Event][]).push(cb)
|
|
@@ -31,7 +33,6 @@ export class EventManagerImpl {
|
|
|
31
33
|
}
|
|
32
34
|
}
|
|
33
35
|
|
|
34
|
-
type EventManager = { [P in keyof EventManagerImpl]: EventManagerImpl[P] }
|
|
35
36
|
|
|
36
37
|
namespace EventManager {
|
|
37
38
|
export enum Event {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Client as PgClient } from 'pg'
|
|
2
|
-
import { EventManager
|
|
2
|
+
import { EventManager } from './EventManager'
|
|
3
3
|
import { Client } from './Client'
|
|
4
4
|
import { Transaction } from './Transaction'
|
|
5
5
|
import { executeQuery } from './execution'
|
|
@@ -9,26 +9,31 @@ class SingleConnection implements Connection.ConnectionLike, Connection.ClientFa
|
|
|
9
9
|
constructor(
|
|
10
10
|
private readonly pgClient: PgClient,
|
|
11
11
|
private readonly queryConfig: Connection.QueryConfig,
|
|
12
|
-
public readonly eventManager: EventManager = new
|
|
12
|
+
public readonly eventManager: EventManager = new EventManager(null),
|
|
13
13
|
private isConnected = false,
|
|
14
14
|
) {}
|
|
15
15
|
|
|
16
16
|
public createClient(schema: string, queryMeta: Record<string, any>): Client {
|
|
17
|
-
return new Client(this, schema, queryMeta)
|
|
17
|
+
return new Client(this, schema, queryMeta, new EventManager(this.eventManager))
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
async transaction<Result>(
|
|
21
21
|
callback: (connection: Connection.TransactionLike) => Promise<Result> | Result,
|
|
22
|
+
options: { eventManager?: EventManager } = {},
|
|
22
23
|
): Promise<Result> {
|
|
23
24
|
if (!this.isConnected) {
|
|
24
25
|
await this.pgClient.connect()
|
|
25
26
|
this.isConnected = true
|
|
26
27
|
}
|
|
27
|
-
const eventManager = new
|
|
28
|
-
await executeQuery(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
28
|
+
const eventManager = new EventManager(options.eventManager ?? this.eventManager)
|
|
29
|
+
await executeQuery(
|
|
30
|
+
this.pgClient,
|
|
31
|
+
eventManager,
|
|
32
|
+
{
|
|
33
|
+
sql: 'BEGIN',
|
|
34
|
+
...this.queryConfig,
|
|
35
|
+
},
|
|
36
|
+
)
|
|
32
37
|
const transaction = new Transaction(this.pgClient, eventManager, this.queryConfig)
|
|
33
38
|
try {
|
|
34
39
|
const result = await callback(transaction)
|
|
@@ -59,7 +64,7 @@ class SingleConnection implements Connection.ConnectionLike, Connection.ClientFa
|
|
|
59
64
|
}
|
|
60
65
|
const query: Connection.Query = { sql, parameters, meta, ...this.queryConfig, ...config }
|
|
61
66
|
try {
|
|
62
|
-
return await executeQuery<Row>(this.pgClient, this.eventManager, query, {})
|
|
67
|
+
return await executeQuery<Row>(this.pgClient, config.eventManager ?? this.eventManager, query, {})
|
|
63
68
|
} catch (e) {
|
|
64
69
|
throw e
|
|
65
70
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Connection } from './Connection'
|
|
2
2
|
import { EventManager } from './EventManager'
|
|
3
|
-
import { ClientBase
|
|
3
|
+
import { ClientBase } from 'pg'
|
|
4
4
|
import { wrapIdentifier } from '../utils'
|
|
5
5
|
import { executeQuery } from './execution'
|
|
6
6
|
|
|
@@ -23,10 +23,16 @@ export class Transaction implements Connection.TransactionLike {
|
|
|
23
23
|
|
|
24
24
|
async transaction<Result>(
|
|
25
25
|
callback: (connection: Connection.TransactionLike) => Promise<Result> | Result,
|
|
26
|
+
options?: { eventManager?: EventManager },
|
|
26
27
|
): Promise<Result> {
|
|
27
28
|
const savepointName = `savepoint_${this.savepointCounter++}`
|
|
28
29
|
await this.query(`SAVEPOINT ${wrapIdentifier(savepointName)}`)
|
|
29
|
-
const savepoint = new SavePoint(
|
|
30
|
+
const savepoint = new SavePoint(
|
|
31
|
+
savepointName,
|
|
32
|
+
this,
|
|
33
|
+
this.pgClient,
|
|
34
|
+
new EventManager(options?.eventManager ?? this.eventManager),
|
|
35
|
+
)
|
|
30
36
|
try {
|
|
31
37
|
const result = await callback(savepoint)
|
|
32
38
|
if (!savepoint.isClosed) {
|
|
@@ -93,20 +99,20 @@ class SavePoint implements Connection.TransactionLike {
|
|
|
93
99
|
return this._isClosed
|
|
94
100
|
}
|
|
95
101
|
|
|
96
|
-
public get eventManager(): EventManager {
|
|
97
|
-
return this.transactionInst.eventManager
|
|
98
|
-
}
|
|
99
|
-
|
|
100
102
|
constructor(
|
|
101
103
|
public readonly savepointName: string,
|
|
102
104
|
private readonly transactionInst: Transaction,
|
|
103
105
|
private readonly pgClient: ClientBase,
|
|
106
|
+
public readonly eventManager: EventManager,
|
|
104
107
|
) {}
|
|
105
108
|
|
|
106
109
|
async transaction<Result>(
|
|
107
110
|
callback: (connection: Connection.TransactionLike) => Promise<Result> | Result,
|
|
111
|
+
options?: { eventManager?: EventManager },
|
|
108
112
|
): Promise<Result> {
|
|
109
|
-
return await this.transactionInst.transaction(callback
|
|
113
|
+
return await this.transactionInst.transaction(callback, {
|
|
114
|
+
eventManager: options?.eventManager ?? this.eventManager,
|
|
115
|
+
})
|
|
110
116
|
}
|
|
111
117
|
|
|
112
118
|
async query<Row extends Record<string, any>>(
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { EventManager } from '../../../src'
|
|
2
|
+
import { createConnectionMock } from '@contember/database-tester'
|
|
3
|
+
import { test } from 'uvu'
|
|
4
|
+
import assert from 'uvu/assert'
|
|
5
|
+
|
|
6
|
+
const simpleQuery = (sql: string) => ({
|
|
7
|
+
sql,
|
|
8
|
+
parameters: [],
|
|
9
|
+
response: { rows: [] },
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
test('event manager: connection and client', async () => {
|
|
13
|
+
const connection = createConnectionMock([
|
|
14
|
+
simpleQuery('SELECT 1'),
|
|
15
|
+
simpleQuery('SELECT 2'),
|
|
16
|
+
])
|
|
17
|
+
const events: { source: string; sql: string }[] = []
|
|
18
|
+
const client = connection.createClient('public', {})
|
|
19
|
+
|
|
20
|
+
connection.eventManager.on(EventManager.Event.queryStart, ({ sql }) => events.push({ sql, source: 'connection' }))
|
|
21
|
+
client.eventManager.on(EventManager.Event.queryStart, ({ sql }) => events.push({ sql, source: 'client' }))
|
|
22
|
+
await client.query('SELECT 1')
|
|
23
|
+
await connection.query('SELECT 2')
|
|
24
|
+
assert.equal(events, [
|
|
25
|
+
{ sql: 'SELECT 1', source: 'client' },
|
|
26
|
+
{ sql: 'SELECT 1', source: 'connection' },
|
|
27
|
+
{ sql: 'SELECT 2', source: 'connection' },
|
|
28
|
+
])
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
test('event manager: connection and client with transaction', async () => {
|
|
33
|
+
const connection = createConnectionMock([
|
|
34
|
+
simpleQuery('BEGIN;'),
|
|
35
|
+
simpleQuery('SELECT 1'),
|
|
36
|
+
simpleQuery('COMMIT;'),
|
|
37
|
+
simpleQuery('SELECT 2'),
|
|
38
|
+
simpleQuery('SELECT 3'),
|
|
39
|
+
])
|
|
40
|
+
const events: { source: string; sql: string }[] = []
|
|
41
|
+
const client = connection.createClient('public', {})
|
|
42
|
+
|
|
43
|
+
connection.eventManager.on(EventManager.Event.queryStart, ({ sql }) => events.push({ sql, source: 'connection' }))
|
|
44
|
+
client.eventManager.on(EventManager.Event.queryStart, ({ sql }) => events.push({ sql, source: 'client' }))
|
|
45
|
+
await client.transaction(async trx => {
|
|
46
|
+
trx.eventManager.on(EventManager.Event.queryStart, ({ sql }) => events.push({ sql, source: 'transaction' }))
|
|
47
|
+
await trx.query('SELECT 1')
|
|
48
|
+
|
|
49
|
+
})
|
|
50
|
+
await client.query('SELECT 2')
|
|
51
|
+
await connection.query('SELECT 3')
|
|
52
|
+
assert.equal(events, [
|
|
53
|
+
{ sql: 'BEGIN;', source: 'connection' },
|
|
54
|
+
{ sql: 'SELECT 1', source: 'transaction' },
|
|
55
|
+
{ sql: 'SELECT 1', source: 'client' },
|
|
56
|
+
{ sql: 'SELECT 1', source: 'connection' },
|
|
57
|
+
{ sql: 'COMMIT;', source: 'connection' },
|
|
58
|
+
{ sql: 'SELECT 2', source: 'client' },
|
|
59
|
+
{ sql: 'SELECT 2', source: 'connection' },
|
|
60
|
+
{ sql: 'SELECT 3', source: 'connection' },
|
|
61
|
+
])
|
|
62
|
+
})
|
|
63
|
+
test.run()
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Client, ConflictActionType, LimitByGroupWrapper, LockType, Operator } from '../../../src'
|
|
1
|
+
import { Client, ConflictActionType, EventManager, LimitByGroupWrapper, LockType, Operator } from '../../../src'
|
|
2
2
|
import { createConnectionMock } from '@contember/database-tester'
|
|
3
3
|
import { SQL } from '../../src/tags'
|
|
4
4
|
import { test } from 'uvu'
|
|
@@ -17,7 +17,7 @@ const execute = async (test: Test) => {
|
|
|
17
17
|
response: { rows: [] },
|
|
18
18
|
},
|
|
19
19
|
])
|
|
20
|
-
const wrapper = new Client(connection, 'public', {})
|
|
20
|
+
const wrapper = new Client(connection, 'public', {}, new EventManager(null))
|
|
21
21
|
|
|
22
22
|
await test.query(wrapper)
|
|
23
23
|
}
|