@travetto/model-sql 7.0.0-rc.1 → 7.0.0-rc.3
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/package.json +7 -7
- package/src/config.ts +5 -5
- package/src/connection/base.ts +30 -30
- package/src/connection/decorator.ts +17 -17
- package/src/dialect/base.ts +225 -157
- package/src/service.ts +52 -59
- package/src/table-manager.ts +91 -61
- package/src/types.ts +1 -1
- package/src/util.ts +75 -75
- package/support/test/query.ts +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-sql",
|
|
3
|
-
"version": "7.0.0-rc.
|
|
3
|
+
"version": "7.0.0-rc.3",
|
|
4
4
|
"description": "SQL backing for the travetto model module, with real-time modeling support for SQL schemas.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sql",
|
|
@@ -27,14 +27,14 @@
|
|
|
27
27
|
"directory": "module/model-sql"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@travetto/cli": "^7.0.0-rc.
|
|
31
|
-
"@travetto/config": "^7.0.0-rc.
|
|
32
|
-
"@travetto/context": "^7.0.0-rc.
|
|
33
|
-
"@travetto/model": "^7.0.0-rc.
|
|
34
|
-
"@travetto/model-query": "^7.0.0-rc.
|
|
30
|
+
"@travetto/cli": "^7.0.0-rc.3",
|
|
31
|
+
"@travetto/config": "^7.0.0-rc.3",
|
|
32
|
+
"@travetto/context": "^7.0.0-rc.3",
|
|
33
|
+
"@travetto/model": "^7.0.0-rc.3",
|
|
34
|
+
"@travetto/model-query": "^7.0.0-rc.3"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"@travetto/test": "^7.0.0-rc.
|
|
37
|
+
"@travetto/test": "^7.0.0-rc.3"
|
|
38
38
|
},
|
|
39
39
|
"peerDependenciesMeta": {
|
|
40
40
|
"@travetto/test": {
|
package/src/config.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Config } from '@travetto/config';
|
|
2
|
-
import { asFull } from '@travetto/runtime';
|
|
2
|
+
import { asFull, Runtime } from '@travetto/runtime';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* SQL Model Config
|
|
@@ -17,11 +17,11 @@ export class SQLModelConfig<T extends {} = {}> {
|
|
|
17
17
|
/**
|
|
18
18
|
* Username
|
|
19
19
|
*/
|
|
20
|
-
user = '';
|
|
20
|
+
user = Runtime.production ? '' : 'travetto';
|
|
21
21
|
/**
|
|
22
22
|
* Password
|
|
23
23
|
*/
|
|
24
|
-
password = '';
|
|
24
|
+
password = Runtime.production ? '' : 'travetto';
|
|
25
25
|
/**
|
|
26
26
|
* Table prefix
|
|
27
27
|
*/
|
|
@@ -31,9 +31,9 @@ export class SQLModelConfig<T extends {} = {}> {
|
|
|
31
31
|
*/
|
|
32
32
|
database = 'app';
|
|
33
33
|
/**
|
|
34
|
-
*
|
|
34
|
+
* Allow storage modification at runtime
|
|
35
35
|
*/
|
|
36
|
-
|
|
36
|
+
modifyStorage?: boolean;
|
|
37
37
|
/**
|
|
38
38
|
* Db version
|
|
39
39
|
*/
|
package/src/connection/base.ts
CHANGED
|
@@ -71,23 +71,23 @@ export abstract class Connection<C = unknown> {
|
|
|
71
71
|
/**
|
|
72
72
|
* Run operation with active connection
|
|
73
73
|
* @param context
|
|
74
|
-
* @param
|
|
74
|
+
* @param operation
|
|
75
75
|
* @param args
|
|
76
76
|
*/
|
|
77
|
-
async runWithActive<R>(
|
|
77
|
+
async runWithActive<R>(operation: () => Promise<R>): Promise<R> {
|
|
78
78
|
if (this.active) {
|
|
79
|
-
return
|
|
79
|
+
return operation();
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
return this.context.run(async () => {
|
|
83
|
-
let
|
|
83
|
+
let connection;
|
|
84
84
|
try {
|
|
85
|
-
|
|
86
|
-
this.#active.set(
|
|
87
|
-
return await
|
|
85
|
+
connection = await this.acquire();
|
|
86
|
+
this.#active.set(connection);
|
|
87
|
+
return await operation();
|
|
88
88
|
} finally {
|
|
89
|
-
if (
|
|
90
|
-
this.release(
|
|
89
|
+
if (connection) {
|
|
90
|
+
this.release(connection);
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
93
|
});
|
|
@@ -96,19 +96,19 @@ export abstract class Connection<C = unknown> {
|
|
|
96
96
|
/**
|
|
97
97
|
* Iterate with active connection
|
|
98
98
|
* @param context
|
|
99
|
-
* @param
|
|
99
|
+
* @param operation
|
|
100
100
|
* @param args
|
|
101
101
|
*/
|
|
102
|
-
async * iterateWithActive<R>(
|
|
102
|
+
async * iterateWithActive<R>(operation: () => AsyncIterable<R>): AsyncIterable<R> {
|
|
103
103
|
if (this.active) {
|
|
104
|
-
yield*
|
|
104
|
+
yield* operation();
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
const self = castTo<Connection>(this);
|
|
108
108
|
yield* this.context.iterate(async function* () {
|
|
109
109
|
try {
|
|
110
110
|
self.#active.set(await self.acquire());
|
|
111
|
-
yield*
|
|
111
|
+
yield* operation();
|
|
112
112
|
} finally {
|
|
113
113
|
if (self.active) {
|
|
114
114
|
self.release(self.active);
|
|
@@ -120,26 +120,26 @@ export abstract class Connection<C = unknown> {
|
|
|
120
120
|
/**
|
|
121
121
|
* Run a function within a valid sql transaction. Relies on @travetto/context.
|
|
122
122
|
*/
|
|
123
|
-
async runWithTransaction<R>(mode: TransactionType,
|
|
123
|
+
async runWithTransaction<R>(mode: TransactionType, operation: () => Promise<R>): Promise<R> {
|
|
124
124
|
if (this.activeTx) {
|
|
125
125
|
if (mode === 'isolated' || mode === 'force') {
|
|
126
126
|
const txId = mode === 'isolated' ? `tx${Util.uuid()}` : undefined;
|
|
127
127
|
try {
|
|
128
128
|
await this.startTx(this.active!, txId);
|
|
129
|
-
const
|
|
129
|
+
const result = await operation();
|
|
130
130
|
await this.commitTx(this.active!, txId);
|
|
131
|
-
return
|
|
132
|
-
} catch (
|
|
131
|
+
return result;
|
|
132
|
+
} catch (error) {
|
|
133
133
|
try { await this.rollbackTx(this.active!, txId); } catch { }
|
|
134
|
-
throw
|
|
134
|
+
throw error;
|
|
135
135
|
}
|
|
136
136
|
} else {
|
|
137
|
-
return await
|
|
137
|
+
return await operation();
|
|
138
138
|
}
|
|
139
139
|
} else {
|
|
140
140
|
return this.runWithActive(() => {
|
|
141
141
|
this.#activeTx.set(true);
|
|
142
|
-
return this.runWithTransaction('force',
|
|
142
|
+
return this.runWithTransaction('force', operation);
|
|
143
143
|
});
|
|
144
144
|
}
|
|
145
145
|
}
|
|
@@ -147,42 +147,42 @@ export abstract class Connection<C = unknown> {
|
|
|
147
147
|
/**
|
|
148
148
|
* Start a transaction
|
|
149
149
|
*/
|
|
150
|
-
async startTx(
|
|
150
|
+
async startTx(connection: C, transactionId?: string): Promise<void> {
|
|
151
151
|
if (transactionId) {
|
|
152
152
|
if (this.nestedTransactions) {
|
|
153
|
-
await this.execute(
|
|
153
|
+
await this.execute(connection, this.transactionDialect.beginNested, [transactionId]);
|
|
154
154
|
}
|
|
155
155
|
} else {
|
|
156
156
|
if (this.isolatedTransactions) {
|
|
157
|
-
await this.execute(
|
|
157
|
+
await this.execute(connection, this.transactionDialect.isolate);
|
|
158
158
|
}
|
|
159
|
-
await this.execute(
|
|
159
|
+
await this.execute(connection, this.transactionDialect.begin);
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
162
|
|
|
163
163
|
/**
|
|
164
164
|
* Commit active transaction
|
|
165
165
|
*/
|
|
166
|
-
async commitTx(
|
|
166
|
+
async commitTx(connection: C, transactionId?: string): Promise<void> {
|
|
167
167
|
if (transactionId) {
|
|
168
168
|
if (this.nestedTransactions) {
|
|
169
|
-
await this.execute(
|
|
169
|
+
await this.execute(connection, this.transactionDialect.commitNested, [transactionId]);
|
|
170
170
|
}
|
|
171
171
|
} else {
|
|
172
|
-
await this.execute(
|
|
172
|
+
await this.execute(connection, this.transactionDialect.commit);
|
|
173
173
|
}
|
|
174
174
|
}
|
|
175
175
|
|
|
176
176
|
/**
|
|
177
177
|
* Rollback active transaction
|
|
178
178
|
*/
|
|
179
|
-
async rollbackTx(
|
|
179
|
+
async rollbackTx(connection: C, transactionId?: string): Promise<void> {
|
|
180
180
|
if (transactionId) {
|
|
181
181
|
if (this.isolatedTransactions) {
|
|
182
|
-
await this.execute(
|
|
182
|
+
await this.execute(connection, this.transactionDialect.rollbackNested, [transactionId]);
|
|
183
183
|
}
|
|
184
184
|
} else {
|
|
185
|
-
await this.execute(
|
|
185
|
+
await this.execute(connection, this.transactionDialect.rollback);
|
|
186
186
|
}
|
|
187
187
|
}
|
|
188
188
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AsyncIterableMethodDescriptor, AsyncMethodDescriptor } from '@travetto/runtime';
|
|
2
2
|
import { Connection, TransactionType } from './base.ts';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Indicating something is aware of connections
|
|
6
6
|
*/
|
|
7
7
|
export interface ConnectionAware<C = unknown> {
|
|
8
|
-
|
|
8
|
+
connection: Connection<C>;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
/**
|
|
@@ -13,12 +13,12 @@ export interface ConnectionAware<C = unknown> {
|
|
|
13
13
|
* @kind decorator
|
|
14
14
|
*/
|
|
15
15
|
export function Connected() {
|
|
16
|
-
return function <T extends {
|
|
17
|
-
target: T,
|
|
16
|
+
return function <T extends { connection?: Connection }>(
|
|
17
|
+
target: T, property: string, descriptor: AsyncMethodDescriptor<T>
|
|
18
18
|
): void {
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
return this.
|
|
19
|
+
const handle = descriptor.value!;
|
|
20
|
+
descriptor.value = function (...args: unknown[]): ReturnType<typeof handle> {
|
|
21
|
+
return this.connection!.runWithActive(() => handle.call(this, ...args));
|
|
22
22
|
};
|
|
23
23
|
};
|
|
24
24
|
}
|
|
@@ -28,12 +28,12 @@ export function Connected() {
|
|
|
28
28
|
* @kind decorator
|
|
29
29
|
*/
|
|
30
30
|
export function ConnectedIterator() {
|
|
31
|
-
return function <T extends {
|
|
32
|
-
target: T,
|
|
31
|
+
return function <T extends { connection?: Connection }>(
|
|
32
|
+
target: T, property: string, descriptor: AsyncIterableMethodDescriptor<T>
|
|
33
33
|
): void {
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
yield* this.
|
|
34
|
+
const handle = descriptor.value!;
|
|
35
|
+
descriptor.value = async function* (...args: unknown[]): ReturnType<typeof handle> {
|
|
36
|
+
yield* this.connection!.iterateWithActive(() => handle.call(this, ...args));
|
|
37
37
|
};
|
|
38
38
|
};
|
|
39
39
|
}
|
|
@@ -43,12 +43,12 @@ export function ConnectedIterator() {
|
|
|
43
43
|
* @kind decorator
|
|
44
44
|
*/
|
|
45
45
|
export function Transactional(mode: TransactionType = 'required') {
|
|
46
|
-
return function <T extends {
|
|
47
|
-
target: unknown,
|
|
46
|
+
return function <T extends { connection?: Connection }>(
|
|
47
|
+
target: unknown, property: string, descriptor: AsyncMethodDescriptor<T>
|
|
48
48
|
): void {
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
return this.
|
|
49
|
+
const handle = descriptor.value!;
|
|
50
|
+
descriptor.value = function (...args: unknown[]): ReturnType<typeof handle> {
|
|
51
|
+
return this.connection!.runWithTransaction(mode, () => handle.call(this, ...args));
|
|
52
52
|
};
|
|
53
53
|
};
|
|
54
54
|
}
|