@travetto/model-sql 8.0.0-alpha.24 → 8.0.0-alpha.25
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/README.md +2 -2
- package/__index__.ts +2 -2
- package/package.json +8 -8
- package/src/config.ts +1 -1
- package/src/connection/base.ts +8 -7
- package/src/connection/decorator.ts +5 -10
- package/src/dialect/base.ts +152 -112
- package/src/internal/types.ts +2 -2
- package/src/service.ts +139 -116
- package/src/table-manager.ts +20 -12
- package/src/types.ts +1 -1
- package/src/util.ts +43 -22
- package/support/test/query.ts +8 -5
package/README.md
CHANGED
|
@@ -23,8 +23,8 @@ The current SQL client support stands at:
|
|
|
23
23
|
**Note**: Wider client support will roll out as usage increases.
|
|
24
24
|
|
|
25
25
|
## Assumed Behavior
|
|
26
|
-
The [SQL Model Service](https://github.com/travetto/travetto/tree/main/module/model-sql#readme "SQL backing for the travetto model module, with real-time modeling support for SQL schemas.") works quite a bit different than the average [Object Relationship Mapping](https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping) in that it makes assertions about how data is stored in the database.
|
|
26
|
+
The [SQL Model Service](https://github.com/travetto/travetto/tree/main/module/model-sql#readme "SQL backing for the travetto model module, with real-time modeling support for SQL schemas.") works quite a bit different than the average [Object Relationship Mapping](https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping) in that it makes assertions about how data is stored in the database. The primary goal of the [SQL](https://en.wikipedia.org/wiki/SQL) support is not to handle every scenario that a relational database can provide, but to integrate with the [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations.") structure, while leverage relational datastores to the best of their abilities.
|
|
27
27
|
|
|
28
|
-
The primary difference is around unique identifiers, and how parent/child relationships are managed.
|
|
28
|
+
The primary difference is around unique identifiers, and how parent/child relationships are managed. In a normal database primary keys could be composite values between various fields. For example a unique identifier could be a combination of `date` + `orderNumber` + `customerNumber`. This is perfectly normal in a relational model, but [SQL Model Service](https://github.com/travetto/travetto/tree/main/module/model-sql#readme "SQL backing for the travetto model module, with real-time modeling support for SQL schemas.") assumes unique identifiers ([UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier)) as 32-character hexadecimal values. In addition to these unique values, the parent's identifier is required in all children values. This allows for some fairly optimized querying, updates, and deletions on changes.
|
|
29
29
|
|
|
30
30
|
What this translates to, is that the framework here dictates the final schema, and doesn't support adapting to existing relational data stores. In greenfield projects, this is not an issue, but will most likely preclude its use in adapting to existing relational data sets.
|
package/__index__.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export * from './src/config.ts';
|
|
2
|
-
export * from './src/service.ts';
|
|
3
2
|
export * from './src/connection/base.ts';
|
|
4
3
|
export * from './src/connection/decorator.ts';
|
|
5
4
|
export * from './src/dialect/base.ts';
|
|
5
|
+
export * from './src/service.ts';
|
|
6
|
+
export * from './src/types.ts';
|
|
6
7
|
export * from './src/util.ts';
|
|
7
|
-
export * from './src/types.ts';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-sql",
|
|
3
|
-
"version": "8.0.0-alpha.
|
|
3
|
+
"version": "8.0.0-alpha.25",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "SQL backing for the travetto model module, with real-time modeling support for SQL schemas.",
|
|
6
6
|
"keywords": [
|
|
@@ -28,15 +28,15 @@
|
|
|
28
28
|
"directory": "module/model-sql"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@travetto/config": "^8.0.0-alpha.
|
|
32
|
-
"@travetto/context": "^8.0.0-alpha.
|
|
33
|
-
"@travetto/model": "^8.0.0-alpha.
|
|
34
|
-
"@travetto/model-indexed": "^8.0.0-alpha.
|
|
35
|
-
"@travetto/model-query": "^8.0.0-alpha.
|
|
31
|
+
"@travetto/config": "^8.0.0-alpha.22",
|
|
32
|
+
"@travetto/context": "^8.0.0-alpha.20",
|
|
33
|
+
"@travetto/model": "^8.0.0-alpha.23",
|
|
34
|
+
"@travetto/model-indexed": "^8.0.0-alpha.25",
|
|
35
|
+
"@travetto/model-query": "^8.0.0-alpha.24"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"@travetto/cli": "^8.0.0-alpha.
|
|
39
|
-
"@travetto/test": "^8.0.0-alpha.
|
|
38
|
+
"@travetto/cli": "^8.0.0-alpha.28",
|
|
39
|
+
"@travetto/test": "^8.0.0-alpha.21"
|
|
40
40
|
},
|
|
41
41
|
"peerDependenciesMeta": {
|
|
42
42
|
"@travetto/cli": {
|
package/src/config.ts
CHANGED
package/src/connection/base.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { castTo, Util } from '@travetto/runtime';
|
|
2
1
|
import { type AsyncContext, AsyncContextValue } from '@travetto/context';
|
|
2
|
+
import { castTo, Util } from '@travetto/runtime';
|
|
3
3
|
|
|
4
4
|
export type TransactionType = 'required' | 'isolated' | 'force';
|
|
5
5
|
|
|
@@ -9,7 +9,6 @@ export type TransactionType = 'required' | 'isolated' | 'force';
|
|
|
9
9
|
* vs querying.
|
|
10
10
|
*/
|
|
11
11
|
export abstract class Connection<C = unknown> {
|
|
12
|
-
|
|
13
12
|
isolatedTransactions = true;
|
|
14
13
|
nestedTransactions = true;
|
|
15
14
|
|
|
@@ -56,7 +55,7 @@ export abstract class Connection<C = unknown> {
|
|
|
56
55
|
* @param rawConnection
|
|
57
56
|
* @param query
|
|
58
57
|
*/
|
|
59
|
-
abstract execute<T = unknown>(rawConnection: C, query: string, values?: unknown[]): Promise<{ records: T[]
|
|
58
|
+
abstract execute<T = unknown>(rawConnection: C, query: string, values?: unknown[]): Promise<{ records: T[]; count: number }>;
|
|
60
59
|
|
|
61
60
|
/**
|
|
62
61
|
* Acquire new connection
|
|
@@ -80,7 +79,7 @@ export abstract class Connection<C = unknown> {
|
|
|
80
79
|
}
|
|
81
80
|
|
|
82
81
|
return this.context.run(async () => {
|
|
83
|
-
let connection;
|
|
82
|
+
let connection: C | undefined;
|
|
84
83
|
try {
|
|
85
84
|
connection = await this.acquire();
|
|
86
85
|
this.#active.set(connection);
|
|
@@ -99,7 +98,7 @@ export abstract class Connection<C = unknown> {
|
|
|
99
98
|
* @param operation
|
|
100
99
|
* @param args
|
|
101
100
|
*/
|
|
102
|
-
async *
|
|
101
|
+
async *iterateWithActive<R>(operation: () => AsyncIterable<R>): AsyncIterable<R> {
|
|
103
102
|
if (this.active) {
|
|
104
103
|
yield* operation();
|
|
105
104
|
}
|
|
@@ -130,7 +129,9 @@ export abstract class Connection<C = unknown> {
|
|
|
130
129
|
await this.commitTx(this.active!, txId);
|
|
131
130
|
return result;
|
|
132
131
|
} catch (error) {
|
|
133
|
-
try {
|
|
132
|
+
try {
|
|
133
|
+
await this.rollbackTx(this.active!, txId);
|
|
134
|
+
} catch {}
|
|
134
135
|
throw error;
|
|
135
136
|
}
|
|
136
137
|
} else {
|
|
@@ -185,4 +186,4 @@ export abstract class Connection<C = unknown> {
|
|
|
185
186
|
await this.execute(connection, this.transactionDialect.rollback);
|
|
186
187
|
}
|
|
187
188
|
}
|
|
188
|
-
}
|
|
189
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { AsyncIterableMethodDescriptor, AsyncMethodDescriptor } from '@travetto/runtime';
|
|
2
|
+
|
|
2
3
|
import type { Connection, TransactionType } from './base.ts';
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -13,9 +14,7 @@ export interface ConnectionAware<C = unknown> {
|
|
|
13
14
|
* @kind decorator
|
|
14
15
|
*/
|
|
15
16
|
export function Connected() {
|
|
16
|
-
return function <T extends { connection?: Connection }>(
|
|
17
|
-
target: T, property: string, descriptor: AsyncMethodDescriptor<T>
|
|
18
|
-
): void {
|
|
17
|
+
return function <T extends { connection?: Connection }>(target: T, property: string, descriptor: AsyncMethodDescriptor<T>): void {
|
|
19
18
|
const handle = descriptor.value!;
|
|
20
19
|
descriptor.value = function (...args: unknown[]): ReturnType<typeof handle> {
|
|
21
20
|
return this.connection!.runWithActive(() => handle.call(this, ...args));
|
|
@@ -28,9 +27,7 @@ export function Connected() {
|
|
|
28
27
|
* @kind decorator
|
|
29
28
|
*/
|
|
30
29
|
export function ConnectedIterator() {
|
|
31
|
-
return function <T extends { connection?: Connection }>(
|
|
32
|
-
target: T, property: string, descriptor: AsyncIterableMethodDescriptor<T>
|
|
33
|
-
): void {
|
|
30
|
+
return function <T extends { connection?: Connection }>(target: T, property: string, descriptor: AsyncIterableMethodDescriptor<T>): void {
|
|
34
31
|
const handle = descriptor.value!;
|
|
35
32
|
descriptor.value = async function* (...args: unknown[]): ReturnType<typeof handle> {
|
|
36
33
|
yield* this.connection!.iterateWithActive(() => handle.call(this, ...args));
|
|
@@ -43,12 +40,10 @@ export function ConnectedIterator() {
|
|
|
43
40
|
* @kind decorator
|
|
44
41
|
*/
|
|
45
42
|
export function Transactional(mode: TransactionType = 'required') {
|
|
46
|
-
return function <T extends { connection?: Connection }>(
|
|
47
|
-
target: unknown, property: string, descriptor: AsyncMethodDescriptor<T>
|
|
48
|
-
): void {
|
|
43
|
+
return function <T extends { connection?: Connection }>(target: unknown, property: string, descriptor: AsyncMethodDescriptor<T>): void {
|
|
49
44
|
const handle = descriptor.value!;
|
|
50
45
|
descriptor.value = function (...args: unknown[]): ReturnType<typeof handle> {
|
|
51
46
|
return this.connection!.runWithTransaction(mode, () => handle.call(this, ...args));
|
|
52
47
|
};
|
|
53
48
|
};
|
|
54
|
-
}
|
|
49
|
+
}
|