@goodie-ts/kysely 1.0.0 → 2.0.1
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/dist/abstract-migration.d.ts +2 -2
- package/dist/abstract-migration.js +2 -2
- package/dist/{beans.json → components.json} +786 -62
- package/dist/decorators/migration.d.ts +1 -1
- package/dist/decorators/migration.js +1 -1
- package/dist/dialects/d1.d.ts.map +1 -1
- package/dist/dialects/d1.js +22 -15
- package/dist/dialects/d1.js.map +1 -1
- package/dist/dialects/libsql.d.ts.map +1 -1
- package/dist/dialects/libsql.js +6 -6
- package/dist/dialects/libsql.js.map +1 -1
- package/dist/dialects/mysql.d.ts.map +1 -1
- package/dist/dialects/mysql.js +6 -6
- package/dist/dialects/mysql.js.map +1 -1
- package/dist/dialects/neon.d.ts.map +1 -1
- package/dist/dialects/neon.js +6 -6
- package/dist/dialects/neon.js.map +1 -1
- package/dist/dialects/planetscale.d.ts.map +1 -1
- package/dist/dialects/planetscale.js +6 -6
- package/dist/dialects/planetscale.js.map +1 -1
- package/dist/dialects/postgres.d.ts.map +1 -1
- package/dist/dialects/postgres.js +6 -6
- package/dist/dialects/postgres.js.map +1 -1
- package/dist/dialects/sqlite.d.ts.map +1 -1
- package/dist/dialects/sqlite.js +6 -6
- package/dist/dialects/sqlite.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/kysely-database.d.ts +2 -2
- package/dist/kysely-database.js +2 -2
- package/dist/kysely-transformer-plugin.d.ts +2 -5
- package/dist/kysely-transformer-plugin.d.ts.map +1 -1
- package/dist/kysely-transformer-plugin.js +18 -197
- package/dist/kysely-transformer-plugin.js.map +1 -1
- package/dist/migration-post-processor.d.ts +18 -0
- package/dist/migration-post-processor.d.ts.map +1 -0
- package/dist/migration-post-processor.js +117 -0
- package/dist/migration-post-processor.js.map +1 -0
- package/dist/pool-config.d.ts.map +1 -1
- package/dist/pool-config.js +2 -2
- package/dist/pool-config.js.map +1 -1
- package/dist/transaction-manager.d.ts +6 -4
- package/dist/transaction-manager.d.ts.map +1 -1
- package/dist/transaction-manager.js +166 -119
- package/dist/transaction-manager.js.map +1 -1
- package/dist/transactional-interceptor.d.ts.map +1 -1
- package/dist/transactional-interceptor.js +68 -17
- package/dist/transactional-interceptor.js.map +1 -1
- package/package.json +6 -6
- package/dist/migration-runner.d.ts +0 -16
- package/dist/migration-runner.d.ts.map +0 -1
- package/dist/migration-runner.js +0 -45
- package/dist/migration-runner.js.map +0 -1
|
@@ -1,33 +1,81 @@
|
|
|
1
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
2
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
3
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
4
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
5
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
6
|
+
var _, done = false;
|
|
7
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
8
|
+
var context = {};
|
|
9
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
10
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
11
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
12
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
13
|
+
if (kind === "accessor") {
|
|
14
|
+
if (result === void 0) continue;
|
|
15
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
16
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
17
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
18
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
19
|
+
}
|
|
20
|
+
else if (_ = accept(result)) {
|
|
21
|
+
if (kind === "field") initializers.unshift(_);
|
|
22
|
+
else descriptor[key] = _;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
26
|
+
done = true;
|
|
27
|
+
};
|
|
28
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
29
|
+
var useValue = arguments.length > 2;
|
|
30
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
31
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
32
|
+
}
|
|
33
|
+
return useValue ? value : void 0;
|
|
34
|
+
};
|
|
1
35
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
36
|
+
import { Singleton } from '@goodie-ts/core';
|
|
2
37
|
/**
|
|
3
38
|
* Manages database transactions using AsyncLocalStorage.
|
|
4
39
|
*
|
|
5
40
|
* Provides transaction propagation across async call chains without
|
|
6
41
|
* explicitly threading a transaction object through every method.
|
|
7
42
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
43
|
+
* Library component — constructor receives the `KyselyDatabase` component
|
|
44
|
+
* and reads its `.kysely` and `.supportsReturning` properties.
|
|
45
|
+
* For tests, call `new TransactionManager()` with no args + `configure()`.
|
|
10
46
|
*/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
47
|
+
let TransactionManager = (() => {
|
|
48
|
+
let _classDecorators = [Singleton()];
|
|
49
|
+
let _classDescriptor;
|
|
50
|
+
let _classExtraInitializers = [];
|
|
51
|
+
let _classThis;
|
|
52
|
+
var TransactionManager = class {
|
|
53
|
+
static { _classThis = this; }
|
|
54
|
+
static {
|
|
55
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
56
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
57
|
+
TransactionManager = _classThis = _classDescriptor.value;
|
|
58
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
59
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
60
|
+
}
|
|
61
|
+
storage = new AsyncLocalStorage();
|
|
62
|
+
kyselyRef;
|
|
63
|
+
testTransactionActive = false;
|
|
64
|
+
_supportsReturning;
|
|
65
|
+
constructor(kyselyDatabase) {
|
|
66
|
+
if (kyselyDatabase) {
|
|
67
|
+
// Capture the current value (may already be set if @OnInit ran)
|
|
68
|
+
this.kyselyRef = kyselyDatabase.kysely;
|
|
69
|
+
// Read supportsReturning from the database (e.g. KyselyDatabase subclass)
|
|
70
|
+
if (kyselyDatabase.supportsReturning !== undefined) {
|
|
71
|
+
this._supportsReturning = kyselyDatabase.supportsReturning;
|
|
24
72
|
}
|
|
25
|
-
// Make the
|
|
26
|
-
// Any code accessing
|
|
27
|
-
//
|
|
28
|
-
// The setter ensures @
|
|
73
|
+
// Make the database's .kysely property transaction-aware.
|
|
74
|
+
// Any code accessing database.kysely will automatically use the
|
|
75
|
+
// active transaction when inside one.
|
|
76
|
+
// The setter ensures @OnInit can still assign the value.
|
|
29
77
|
const tm = this;
|
|
30
|
-
Object.defineProperty(
|
|
78
|
+
Object.defineProperty(kyselyDatabase, 'kysely', {
|
|
31
79
|
get() {
|
|
32
80
|
return tm.getConnection();
|
|
33
81
|
},
|
|
@@ -37,113 +85,112 @@ export class TransactionManager {
|
|
|
37
85
|
configurable: true,
|
|
38
86
|
});
|
|
39
87
|
}
|
|
40
|
-
|
|
41
|
-
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Configure the Kysely instance used for transactions.
|
|
91
|
+
* Called manually or after @OnInit creates the Kysely instance.
|
|
92
|
+
*/
|
|
93
|
+
configure(kysely, supportsReturning) {
|
|
94
|
+
this.kyselyRef = kysely;
|
|
95
|
+
if (supportsReturning !== undefined) {
|
|
96
|
+
this._supportsReturning = supportsReturning;
|
|
42
97
|
}
|
|
43
98
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
configure(kysely, supportsReturning) {
|
|
50
|
-
this.kyselyRef = kysely;
|
|
51
|
-
if (supportsReturning !== undefined) {
|
|
52
|
-
this._supportsReturning = supportsReturning;
|
|
99
|
+
get kysely() {
|
|
100
|
+
if (!this.kyselyRef) {
|
|
101
|
+
throw new Error('TransactionManager not configured. Call transactionManager.configure(kysely) or use createKyselyPlugin({ database: "YourDatabase" }).');
|
|
102
|
+
}
|
|
103
|
+
return this.kyselyRef;
|
|
53
104
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
105
|
+
/**
|
|
106
|
+
* Run a function inside a transaction.
|
|
107
|
+
*
|
|
108
|
+
* - If already in a transaction (REQUIRED propagation), reuses it.
|
|
109
|
+
* - If `requiresNew` is true, always starts a fresh transaction.
|
|
110
|
+
*/
|
|
111
|
+
async runInTransaction(fn, requiresNew = false) {
|
|
112
|
+
// Inside a test transaction, all queries already use the test transaction.
|
|
113
|
+
// Skip creating new transactions to avoid Kysely's nested transaction error.
|
|
114
|
+
if (this.testTransactionActive) {
|
|
115
|
+
return fn();
|
|
116
|
+
}
|
|
117
|
+
const existing = this.storage.getStore();
|
|
118
|
+
if (existing && !requiresNew) {
|
|
119
|
+
return fn();
|
|
120
|
+
}
|
|
121
|
+
return this.kysely.transaction().execute(async (trx) => {
|
|
122
|
+
return this.storage.run(trx, fn);
|
|
123
|
+
});
|
|
58
124
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
* Run a function inside a transaction.
|
|
63
|
-
*
|
|
64
|
-
* - If already in a transaction (REQUIRED propagation), reuses it.
|
|
65
|
-
* - If `requiresNew` is true, always starts a fresh transaction.
|
|
66
|
-
*/
|
|
67
|
-
async runInTransaction(fn, requiresNew = false) {
|
|
68
|
-
// Inside a test transaction, all queries already use the test transaction.
|
|
69
|
-
// Skip creating new transactions to avoid Kysely's nested transaction error.
|
|
70
|
-
if (this.testTransactionActive) {
|
|
71
|
-
return fn();
|
|
125
|
+
/** Get the current transaction, or undefined if not in one. */
|
|
126
|
+
currentTransaction() {
|
|
127
|
+
return this.storage.getStore();
|
|
72
128
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
129
|
+
/**
|
|
130
|
+
* Get the current database connection — transaction-aware.
|
|
131
|
+
* Returns the active transaction if inside one, otherwise the raw Kysely instance.
|
|
132
|
+
*/
|
|
133
|
+
getConnection() {
|
|
134
|
+
return this.currentTransaction() ?? this.kysely;
|
|
76
135
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
* Get the current database connection — transaction-aware.
|
|
87
|
-
* Returns the active transaction if inside one, otherwise the raw Kysely instance.
|
|
88
|
-
*/
|
|
89
|
-
getConnection() {
|
|
90
|
-
return this.currentTransaction() ?? this.kysely;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Whether the underlying dialect supports RETURNING clauses.
|
|
94
|
-
* Derived from the KyselyDatabase provider at construction / configure time.
|
|
95
|
-
*/
|
|
96
|
-
get supportsReturning() {
|
|
97
|
-
if (this._supportsReturning === undefined) {
|
|
98
|
-
throw new Error('TransactionManager not configured. Call configure(kysely) or pass a KyselyProvider to the constructor.');
|
|
136
|
+
/**
|
|
137
|
+
* Whether the underlying dialect supports RETURNING clauses.
|
|
138
|
+
* Derived from the KyselyDatabase provider at construction / configure time.
|
|
139
|
+
*/
|
|
140
|
+
get supportsReturning() {
|
|
141
|
+
if (this._supportsReturning === undefined) {
|
|
142
|
+
throw new Error('TransactionManager not configured. Call configure(kysely) or pass a KyselyProvider to the constructor.');
|
|
143
|
+
}
|
|
144
|
+
return this._supportsReturning;
|
|
99
145
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
const readyPromise = new Promise((resolve) => {
|
|
120
|
-
transactionReady = resolve;
|
|
121
|
-
});
|
|
122
|
-
const rollbackPromise = this.kysely
|
|
123
|
-
.transaction()
|
|
124
|
-
.execute(async (trx) => {
|
|
125
|
-
this.kyselyRef = trx;
|
|
126
|
-
this.testTransactionActive = true;
|
|
127
|
-
transactionReady();
|
|
128
|
-
await new Promise((_resolve, reject) => {
|
|
129
|
-
triggerRollback = () => reject(new TestRollbackSignal());
|
|
146
|
+
/**
|
|
147
|
+
* Start a test-scoped transaction that ALL queries will use.
|
|
148
|
+
*
|
|
149
|
+
* Replaces the internal Kysely reference with the transaction object,
|
|
150
|
+
* so any code calling `getConnection()` or `database.kysely` (via the
|
|
151
|
+
* provider proxy) will automatically use this transaction.
|
|
152
|
+
*
|
|
153
|
+
* Designed for test frameworks (e.g. vitest fixtures) where
|
|
154
|
+
* AsyncLocalStorage context may not propagate through `use()`.
|
|
155
|
+
*
|
|
156
|
+
* @returns A rollback function — call it to roll back the transaction
|
|
157
|
+
* and restore the original Kysely instance.
|
|
158
|
+
*/
|
|
159
|
+
async startTestTransaction() {
|
|
160
|
+
const original = this.kyselyRef;
|
|
161
|
+
let triggerRollback;
|
|
162
|
+
let transactionReady;
|
|
163
|
+
const readyPromise = new Promise((resolve) => {
|
|
164
|
+
transactionReady = resolve;
|
|
130
165
|
});
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
166
|
+
const rollbackPromise = this.kysely
|
|
167
|
+
.transaction()
|
|
168
|
+
.execute(async (trx) => {
|
|
169
|
+
this.kyselyRef = trx;
|
|
170
|
+
this.testTransactionActive = true;
|
|
171
|
+
transactionReady();
|
|
172
|
+
await new Promise((_resolve, reject) => {
|
|
173
|
+
triggerRollback = () => reject(new TestRollbackSignal());
|
|
174
|
+
});
|
|
175
|
+
})
|
|
176
|
+
.catch((e) => {
|
|
177
|
+
if (!(e instanceof TestRollbackSignal))
|
|
178
|
+
throw e;
|
|
179
|
+
})
|
|
180
|
+
.finally(() => {
|
|
181
|
+
this.testTransactionActive = false;
|
|
182
|
+
this.kyselyRef = original;
|
|
183
|
+
});
|
|
184
|
+
await readyPromise;
|
|
185
|
+
return async () => {
|
|
186
|
+
triggerRollback();
|
|
187
|
+
await rollbackPromise;
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
return TransactionManager = _classThis;
|
|
192
|
+
})();
|
|
193
|
+
export { TransactionManager };
|
|
147
194
|
/** @internal Sentinel error for test transaction rollback. */
|
|
148
195
|
class TestRollbackSignal extends Error {
|
|
149
196
|
constructor() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transaction-manager.js","sourceRoot":"","sources":["../src/transaction-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"transaction-manager.js","sourceRoot":"","sources":["../src/transaction-manager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;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sourceRoot":"","sources":["../src/transactional-interceptor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"transactional-interceptor.d.ts","sourceRoot":"","sources":["../src/transactional-interceptor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAE5E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAOnE;;;;;GAKG;AACH,qBACa,wBAAyB,YAAW,iBAAiB;IACpD,OAAO,CAAC,QAAQ,CAAC,kBAAkB;gBAAlB,kBAAkB,EAAE,kBAAkB;IAEnE,SAAS,CAAC,GAAG,EAAE,iBAAiB,GAAG,OAAO;CAY3C"}
|
|
@@ -1,24 +1,75 @@
|
|
|
1
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
2
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
3
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
4
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
5
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
6
|
+
var _, done = false;
|
|
7
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
8
|
+
var context = {};
|
|
9
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
10
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
11
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
12
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
13
|
+
if (kind === "accessor") {
|
|
14
|
+
if (result === void 0) continue;
|
|
15
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
16
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
17
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
18
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
19
|
+
}
|
|
20
|
+
else if (_ = accept(result)) {
|
|
21
|
+
if (kind === "field") initializers.unshift(_);
|
|
22
|
+
else descriptor[key] = _;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
26
|
+
done = true;
|
|
27
|
+
};
|
|
28
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
29
|
+
var useValue = arguments.length > 2;
|
|
30
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
31
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
32
|
+
}
|
|
33
|
+
return useValue ? value : void 0;
|
|
34
|
+
};
|
|
35
|
+
import { Singleton } from '@goodie-ts/core';
|
|
1
36
|
/**
|
|
2
37
|
* AOP interceptor that wraps method execution in a database transaction.
|
|
3
38
|
*
|
|
4
39
|
* Reads propagation strategy from `ctx.metadata` (set by the kysely
|
|
5
40
|
* transformer plugin).
|
|
6
41
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
42
|
+
let TransactionalInterceptor = (() => {
|
|
43
|
+
let _classDecorators = [Singleton()];
|
|
44
|
+
let _classDescriptor;
|
|
45
|
+
let _classExtraInitializers = [];
|
|
46
|
+
let _classThis;
|
|
47
|
+
var TransactionalInterceptor = class {
|
|
48
|
+
static { _classThis = this; }
|
|
49
|
+
static {
|
|
50
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
51
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
52
|
+
TransactionalInterceptor = _classThis = _classDescriptor.value;
|
|
53
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
54
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
55
|
+
}
|
|
56
|
+
transactionManager;
|
|
57
|
+
constructor(transactionManager) {
|
|
58
|
+
this.transactionManager = transactionManager;
|
|
59
|
+
}
|
|
60
|
+
intercept(ctx) {
|
|
61
|
+
const meta = ctx.metadata;
|
|
62
|
+
if (!meta)
|
|
63
|
+
return ctx.proceed();
|
|
64
|
+
const requiresNew = meta.propagation === 'REQUIRES_NEW';
|
|
65
|
+
return this.transactionManager.runInTransaction(async () => {
|
|
66
|
+
// Must await so rejected promises propagate within the transaction scope,
|
|
67
|
+
// ensuring Kysely rolls back on failure.
|
|
68
|
+
return await ctx.proceed();
|
|
69
|
+
}, requiresNew);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
return TransactionalInterceptor = _classThis;
|
|
73
|
+
})();
|
|
74
|
+
export { TransactionalInterceptor };
|
|
24
75
|
//# sourceMappingURL=transactional-interceptor.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transactional-interceptor.js","sourceRoot":"","sources":["../src/transactional-interceptor.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"transactional-interceptor.js","sourceRoot":"","sources":["../src/transactional-interceptor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAQ5C;;;;;GAKG;IAEU,wBAAwB;4BADpC,SAAS,EAAE;;;;;;;;YACZ,6KAeC;;;YAfY,uDAAwB;;QACN,kBAAkB;QAA/C,YAA6B,kBAAsC;YAAtC,uBAAkB,GAAlB,kBAAkB,CAAoB;QAAG,CAAC;QAEvE,SAAS,CAAC,GAAsB;YAC9B,MAAM,IAAI,GAAG,GAAG,CAAC,QAA6C,CAAC;YAC/D,IAAI,CAAC,IAAI;gBAAE,OAAO,GAAG,CAAC,OAAO,EAAE,CAAC;YAEhC,MAAM,WAAW,GAAG,IAAI,CAAC,WAAW,KAAK,cAAc,CAAC;YAExD,OAAO,IAAI,CAAC,kBAAkB,CAAC,gBAAgB,CAAC,KAAK,IAAI,EAAE;gBACzD,0EAA0E;gBAC1E,yCAAyC;gBACzC,OAAO,MAAM,GAAG,CAAC,OAAO,EAAE,CAAC;YAC7B,CAAC,EAAE,WAAW,CAAC,CAAC;QAClB,CAAC;;;;SAdU,wBAAwB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@goodie-ts/kysely",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Kysely integration for goodie-ts — @Transactional decorator, TransactionManager, KyselyDatabase",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"access": "public"
|
|
14
14
|
},
|
|
15
15
|
"goodie": {
|
|
16
|
-
"
|
|
16
|
+
"components": "dist/components.json",
|
|
17
17
|
"plugin": "./dist/kysely-transformer-plugin.js"
|
|
18
18
|
},
|
|
19
19
|
"main": "dist/index.js",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
}
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
|
-
"@goodie-ts/core": ">=
|
|
28
|
+
"@goodie-ts/core": ">=2.0.0",
|
|
29
29
|
"kysely": ">=0.27.0",
|
|
30
30
|
"pg": ">=8.0.0",
|
|
31
31
|
"mysql2": ">=3.0.0",
|
|
@@ -67,9 +67,9 @@
|
|
|
67
67
|
"mysql2": "^3.0.0",
|
|
68
68
|
"pg": "^8.0.0",
|
|
69
69
|
"ts-morph": "^24.0.0",
|
|
70
|
-
"@goodie-ts/
|
|
71
|
-
"@goodie-ts/
|
|
72
|
-
"@goodie-ts/transformer": "
|
|
70
|
+
"@goodie-ts/cli": "2.1.0",
|
|
71
|
+
"@goodie-ts/core": "2.0.0",
|
|
72
|
+
"@goodie-ts/transformer": "2.0.0"
|
|
73
73
|
},
|
|
74
74
|
"files": [
|
|
75
75
|
"dist"
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { AbstractMigration } from './abstract-migration.js';
|
|
2
|
-
import type { KyselyProvider } from './transaction-manager.js';
|
|
3
|
-
/**
|
|
4
|
-
* Runs Kysely migrations at application startup.
|
|
5
|
-
*
|
|
6
|
-
* Auto-wired by the Kysely transformer plugin as an eager singleton
|
|
7
|
-
* with `@PostConstruct` on `migrate()`. Constructor receives the
|
|
8
|
-
* KyselyProvider and all discovered @Migration instances.
|
|
9
|
-
*/
|
|
10
|
-
export declare class MigrationRunner {
|
|
11
|
-
private readonly kyselyProvider;
|
|
12
|
-
private readonly migrations;
|
|
13
|
-
constructor(kyselyProvider: KyselyProvider, ...migrations: AbstractMigration[]);
|
|
14
|
-
migrate(): Promise<void>;
|
|
15
|
-
}
|
|
16
|
-
//# sourceMappingURL=migration-runner.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"migration-runner.d.ts","sourceRoot":"","sources":["../src/migration-runner.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAEjE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAE/D;;;;;;GAMG;AACH,qBAAa,eAAe;IAC1B,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAiB;IAChD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAsB;gBAG/C,cAAc,EAAE,cAAc,EAC9B,GAAG,UAAU,EAAE,iBAAiB,EAAE;IAM9B,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAgC/B"}
|
package/dist/migration-runner.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { Migrator } from 'kysely';
|
|
2
|
-
import { getMigrationName } from './decorators/migration.js';
|
|
3
|
-
/**
|
|
4
|
-
* Runs Kysely migrations at application startup.
|
|
5
|
-
*
|
|
6
|
-
* Auto-wired by the Kysely transformer plugin as an eager singleton
|
|
7
|
-
* with `@PostConstruct` on `migrate()`. Constructor receives the
|
|
8
|
-
* KyselyProvider and all discovered @Migration instances.
|
|
9
|
-
*/
|
|
10
|
-
export class MigrationRunner {
|
|
11
|
-
kyselyProvider;
|
|
12
|
-
migrations;
|
|
13
|
-
constructor(kyselyProvider, ...migrations) {
|
|
14
|
-
this.kyselyProvider = kyselyProvider;
|
|
15
|
-
this.migrations = migrations;
|
|
16
|
-
}
|
|
17
|
-
async migrate() {
|
|
18
|
-
const migrationMap = {};
|
|
19
|
-
for (const m of this.migrations) {
|
|
20
|
-
const name = getMigrationName(m);
|
|
21
|
-
if (!name) {
|
|
22
|
-
throw new Error(`MigrationRunner received an object without @Migration metadata: ${m.constructor.name}`);
|
|
23
|
-
}
|
|
24
|
-
// Kysely's Migrator spreads migration objects (...migration), which
|
|
25
|
-
// drops prototype methods from class instances. Bind explicitly.
|
|
26
|
-
migrationMap[name] = {
|
|
27
|
-
up: m.up.bind(m),
|
|
28
|
-
down: m.down?.bind(m),
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
const migrator = new Migrator({
|
|
32
|
-
db: this.kyselyProvider.kysely,
|
|
33
|
-
provider: { getMigrations: async () => migrationMap },
|
|
34
|
-
});
|
|
35
|
-
const { error, results } = await migrator.migrateToLatest();
|
|
36
|
-
for (const r of results ?? []) {
|
|
37
|
-
if (r.status === 'Error') {
|
|
38
|
-
console.error(`Migration "${r.migrationName}" failed`);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
if (error)
|
|
42
|
-
throw error;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
//# sourceMappingURL=migration-runner.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"migration-runner.js","sourceRoot":"","sources":["../src/migration-runner.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAElC,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAG7D;;;;;;GAMG;AACH,MAAM,OAAO,eAAe;IACT,cAAc,CAAiB;IAC/B,UAAU,CAAsB;IAEjD,YACE,cAA8B,EAC9B,GAAG,UAA+B;QAElC,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;QACrC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;IAED,KAAK,CAAC,OAAO;QACX,MAAM,YAAY,GAAoC,EAAE,CAAC;QACzD,KAAK,MAAM,CAAC,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YAChC,MAAM,IAAI,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAAC;YACjC,IAAI,CAAC,IAAI,EAAE,CAAC;gBACV,MAAM,IAAI,KAAK,CACb,mEAAmE,CAAC,CAAC,WAAW,CAAC,IAAI,EAAE,CACxF,CAAC;YACJ,CAAC;YACD,oEAAoE;YACpE,iEAAiE;YACjE,YAAY,CAAC,IAAI,CAAC,GAAG;gBACnB,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;gBAChB,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;aACtB,CAAC;QACJ,CAAC;QAED,MAAM,QAAQ,GAAG,IAAI,QAAQ,CAAC;YAC5B,EAAE,EAAE,IAAI,CAAC,cAAc,CAAC,MAAM;YAC9B,QAAQ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