@travetto/model-sql 5.0.20 → 6.0.0-rc.0
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/connection/base.ts +20 -16
- package/src/dialect/base.ts +3 -5
- package/src/table-manager.ts +7 -8
- package/LICENSE +0 -21
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-sql",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0-rc.0",
|
|
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": "^
|
|
31
|
-
"@travetto/config": "^
|
|
32
|
-
"@travetto/context": "^
|
|
33
|
-
"@travetto/model": "^
|
|
34
|
-
"@travetto/model-query": "^
|
|
30
|
+
"@travetto/cli": "^6.0.0-rc.0",
|
|
31
|
+
"@travetto/config": "^6.0.0-rc.0",
|
|
32
|
+
"@travetto/context": "^6.0.0-rc.0",
|
|
33
|
+
"@travetto/model": "^6.0.0-rc.0",
|
|
34
|
+
"@travetto/model-query": "^6.0.0-rc.0"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"@travetto/test": "^
|
|
37
|
+
"@travetto/test": "^6.0.0-rc.0"
|
|
38
38
|
},
|
|
39
39
|
"peerDependenciesMeta": {
|
|
40
40
|
"@travetto/test": {
|
package/src/connection/base.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import { castTo, Util } from '@travetto/runtime';
|
|
2
|
-
import { AsyncContext } from '@travetto/context';
|
|
3
|
-
|
|
4
|
-
const ContextActiveSymbol: unique symbol = Symbol.for('@travetto/model:sql-active');
|
|
5
|
-
const TxActiveSymbol: unique symbol = Symbol.for('@travetto/model:sql-transaction');
|
|
2
|
+
import { AsyncContext, AsyncContextValue } from '@travetto/context';
|
|
6
3
|
|
|
7
4
|
export type TransactionType = 'required' | 'isolated' | 'force';
|
|
8
5
|
|
|
@@ -26,22 +23,27 @@ export abstract class Connection<C = unknown> {
|
|
|
26
23
|
commitNested: 'RELEASE SAVEPOINT $1;'
|
|
27
24
|
};
|
|
28
25
|
|
|
29
|
-
|
|
26
|
+
readonly context: AsyncContext;
|
|
27
|
+
|
|
28
|
+
#active = new AsyncContextValue<C>(this);
|
|
29
|
+
#activeTx = new AsyncContextValue<boolean>(this);
|
|
30
30
|
|
|
31
|
+
constructor(context: AsyncContext) {
|
|
32
|
+
this.context = context;
|
|
31
33
|
}
|
|
32
34
|
|
|
33
35
|
/**
|
|
34
36
|
* Get active connection
|
|
35
37
|
*/
|
|
36
|
-
get active(): C {
|
|
37
|
-
return this.
|
|
38
|
+
get active(): C | undefined {
|
|
39
|
+
return this.#active.get();
|
|
38
40
|
}
|
|
39
41
|
|
|
40
42
|
/**
|
|
41
43
|
* Get active tx state
|
|
42
44
|
*/
|
|
43
45
|
get activeTx(): boolean {
|
|
44
|
-
return !!this.
|
|
46
|
+
return !!this.#activeTx.get();
|
|
45
47
|
}
|
|
46
48
|
|
|
47
49
|
/**
|
|
@@ -78,12 +80,14 @@ export abstract class Connection<C = unknown> {
|
|
|
78
80
|
}
|
|
79
81
|
|
|
80
82
|
return this.context.run(async () => {
|
|
83
|
+
let conn;
|
|
81
84
|
try {
|
|
82
|
-
|
|
85
|
+
conn = await this.acquire();
|
|
86
|
+
this.#active.set(conn);
|
|
83
87
|
return await op();
|
|
84
88
|
} finally {
|
|
85
|
-
if (
|
|
86
|
-
this.release(
|
|
89
|
+
if (conn) {
|
|
90
|
+
this.release(conn);
|
|
87
91
|
}
|
|
88
92
|
}
|
|
89
93
|
});
|
|
@@ -103,7 +107,7 @@ export abstract class Connection<C = unknown> {
|
|
|
103
107
|
const self = castTo<Connection>(this);
|
|
104
108
|
yield* this.context.iterate(async function* () {
|
|
105
109
|
try {
|
|
106
|
-
self.
|
|
110
|
+
self.#active.set(await self.acquire());
|
|
107
111
|
yield* op();
|
|
108
112
|
} finally {
|
|
109
113
|
if (self.active) {
|
|
@@ -121,12 +125,12 @@ export abstract class Connection<C = unknown> {
|
|
|
121
125
|
if (mode === 'isolated' || mode === 'force') {
|
|
122
126
|
const txId = mode === 'isolated' ? `tx${Util.uuid()}` : undefined;
|
|
123
127
|
try {
|
|
124
|
-
await this.startTx(this.active
|
|
128
|
+
await this.startTx(this.active!, txId);
|
|
125
129
|
const res = await op();
|
|
126
|
-
await this.commitTx(this.active
|
|
130
|
+
await this.commitTx(this.active!, txId);
|
|
127
131
|
return res;
|
|
128
132
|
} catch (err) {
|
|
129
|
-
try { await this.rollbackTx(this.active
|
|
133
|
+
try { await this.rollbackTx(this.active!, txId); } catch { }
|
|
130
134
|
throw err;
|
|
131
135
|
}
|
|
132
136
|
} else {
|
|
@@ -134,7 +138,7 @@ export abstract class Connection<C = unknown> {
|
|
|
134
138
|
}
|
|
135
139
|
} else {
|
|
136
140
|
return this.runWithActive(() => {
|
|
137
|
-
this.
|
|
141
|
+
this.#activeTx.set(true);
|
|
138
142
|
return this.runWithTransaction('force', op);
|
|
139
143
|
});
|
|
140
144
|
}
|
package/src/dialect/base.ts
CHANGED
|
@@ -134,15 +134,13 @@ export abstract class SQLDialect implements DialectState {
|
|
|
134
134
|
rootAlias = '_ROOT';
|
|
135
135
|
|
|
136
136
|
aliasCache = new Map<Class, Map<string, Alias>>();
|
|
137
|
+
ns: string;
|
|
137
138
|
|
|
138
|
-
constructor(
|
|
139
|
+
constructor(ns: string) {
|
|
139
140
|
this.namespace = this.namespace.bind(this);
|
|
140
141
|
this.table = this.table.bind(this);
|
|
141
142
|
this.ident = this.ident.bind(this);
|
|
142
|
-
|
|
143
|
-
if (this.ns) {
|
|
144
|
-
this.ns = `${this.ns}_`;
|
|
145
|
-
}
|
|
143
|
+
this.ns = ns ? `${ns}_` : ns;
|
|
146
144
|
}
|
|
147
145
|
|
|
148
146
|
/**
|
package/src/table-manager.ts
CHANGED
|
@@ -15,12 +15,11 @@ import { Connection } from './connection/base';
|
|
|
15
15
|
export class TableManager {
|
|
16
16
|
|
|
17
17
|
#dialect: SQLDialect;
|
|
18
|
+
context: AsyncContext;
|
|
18
19
|
|
|
19
|
-
constructor(
|
|
20
|
-
public context: AsyncContext,
|
|
21
|
-
dialect: SQLDialect
|
|
22
|
-
) {
|
|
20
|
+
constructor(context: AsyncContext, dialect: SQLDialect) {
|
|
23
21
|
this.#dialect = dialect;
|
|
22
|
+
this.context = context;
|
|
24
23
|
}
|
|
25
24
|
|
|
26
25
|
#exec<T = unknown>(sql: string): Promise<{ records: T[], count: number }> {
|
|
@@ -47,7 +46,7 @@ export class TableManager {
|
|
|
47
46
|
/**
|
|
48
47
|
* Create all needed tables for a given class
|
|
49
48
|
*/
|
|
50
|
-
@WithAsyncContext(
|
|
49
|
+
@WithAsyncContext()
|
|
51
50
|
@Connected()
|
|
52
51
|
@Transactional()
|
|
53
52
|
async createTables(cls: Class): Promise<void> {
|
|
@@ -74,7 +73,7 @@ export class TableManager {
|
|
|
74
73
|
/**
|
|
75
74
|
* Drop all tables for a given class
|
|
76
75
|
*/
|
|
77
|
-
@WithAsyncContext(
|
|
76
|
+
@WithAsyncContext()
|
|
78
77
|
@Connected()
|
|
79
78
|
@Transactional()
|
|
80
79
|
async dropTables(cls: Class): Promise<void> {
|
|
@@ -86,7 +85,7 @@ export class TableManager {
|
|
|
86
85
|
/**
|
|
87
86
|
* Drop all tables for a given class
|
|
88
87
|
*/
|
|
89
|
-
@WithAsyncContext(
|
|
88
|
+
@WithAsyncContext()
|
|
90
89
|
@Connected()
|
|
91
90
|
@Transactional()
|
|
92
91
|
async truncateTables(cls: Class): Promise<void> {
|
|
@@ -105,7 +104,7 @@ export class TableManager {
|
|
|
105
104
|
/**
|
|
106
105
|
* When the schema changes, update SQL
|
|
107
106
|
*/
|
|
108
|
-
@WithAsyncContext(
|
|
107
|
+
@WithAsyncContext()
|
|
109
108
|
@Transactional()
|
|
110
109
|
@Connected()
|
|
111
110
|
async changeSchema(cls: Class, change: SchemaChange): Promise<void> {
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2020 ArcSine Technologies
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|