@guren/orm 2.7.0 → 2.7.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/dist/index.d.ts +2 -0
- package/dist/index.js +75 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -324,6 +324,7 @@ interface PaginatedResult<TRecord extends PlainObject = PlainObject> {
|
|
|
324
324
|
}
|
|
325
325
|
/** Interface for ORM adapters that power the Model class; DrizzleAdapter by default. */
|
|
326
326
|
interface ORMAdapter {
|
|
327
|
+
/** Must not commit before the callback's promise settles: drizzle's bun-sqlite does. */
|
|
327
328
|
transaction?<TResult>(callback: (trx: unknown) => Promise<TResult>): Promise<TResult>;
|
|
328
329
|
findMany<TRecord extends PlainObject = PlainObject>(table: unknown, options?: FindManyOptions<TRecord>, queryOptions?: AdapterQueryOptions): Promise<TRecord[]>;
|
|
329
330
|
findUnique<TRecord extends PlainObject = PlainObject>(table: unknown, where: WhereClause<TRecord>, queryOptions?: AdapterQueryOptions): Promise<TRecord | null>;
|
|
@@ -860,6 +861,7 @@ type DrizzleDatabase = {
|
|
|
860
861
|
insert(table: unknown): DrizzleLikeInsert;
|
|
861
862
|
update?(table: unknown): DrizzleLikeUpdate;
|
|
862
863
|
delete?(table: unknown): DrizzleLikeDelete;
|
|
864
|
+
run?(query: unknown): Promise<unknown>;
|
|
863
865
|
transaction?<TResult>(callback: (trx: unknown) => Promise<TResult>): Promise<TResult>;
|
|
864
866
|
};
|
|
865
867
|
declare const DrizzleAdapter: ORMAdapterAdvanced & {
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { and, asc, count, desc, eq, gt, gte, inArray, isNotNull, isNull, like, lt, lte, ne, notInArray, or } from "drizzle-orm";
|
|
1
|
+
import { and, asc, count, desc, eq, gt, gte, inArray, isNotNull, isNull, like, lt, lte, ne, notInArray, or, sql } from "drizzle-orm";
|
|
2
2
|
import { extname, relative, resolve } from "node:path";
|
|
3
3
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
4
4
|
import { existsSync, readdirSync } from "node:fs";
|
|
@@ -138,6 +138,9 @@ function buildDrizzleConditions(table, conditions) {
|
|
|
138
138
|
//#endregion
|
|
139
139
|
//#region src/adapters/drizzle-adapter.ts
|
|
140
140
|
let database;
|
|
141
|
+
let transactionAwaitsCallback;
|
|
142
|
+
let transactionQueue = Promise.resolve();
|
|
143
|
+
let transactionScope;
|
|
141
144
|
function ensureDatabase() {
|
|
142
145
|
if (!database) throw new Error("DrizzleAdapter: database has not been configured. Call DrizzleAdapter.configure(db).");
|
|
143
146
|
return database;
|
|
@@ -217,9 +220,78 @@ async function resolveWithReturning(query) {
|
|
|
217
220
|
row: void 0
|
|
218
221
|
};
|
|
219
222
|
}
|
|
223
|
+
const NOOP = () => void 0;
|
|
224
|
+
/**
|
|
225
|
+
* Whether `db.transaction()` awaits its callback before committing: drizzle's
|
|
226
|
+
* bun-sqlite COMMITs on whatever the callback returns, d1 and every pg/mysql
|
|
227
|
+
* driver await it. Probed rather than matched against a driver list, because
|
|
228
|
+
* this adapter takes any drizzle-shaped handle and one a list never named gets
|
|
229
|
+
* the wrong path silently. Costs one empty transaction per configured database.
|
|
230
|
+
*/
|
|
231
|
+
async function awaitsItsCallback(db) {
|
|
232
|
+
if (transactionAwaitsCallback === void 0) {
|
|
233
|
+
const probe = db.transaction?.(NOOP);
|
|
234
|
+
transactionAwaitsCallback = isPromiseLike(probe);
|
|
235
|
+
if (isPromiseLike(probe)) await probe.catch(NOOP);
|
|
236
|
+
}
|
|
237
|
+
return transactionAwaitsCallback;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Imported on demand so `node:async_hooks` stays off the module graph of a
|
|
241
|
+
* Workers or Lambda bundle, whose drivers all await their callback and never
|
|
242
|
+
* reach this branch. The promise is what is memoized, not the storage: two
|
|
243
|
+
* concurrent first callers must not end up asking different instances.
|
|
244
|
+
*/
|
|
245
|
+
function loadTransactionScope() {
|
|
246
|
+
transactionScope ??= import("node:async_hooks").then(({ AsyncLocalStorage }) => {
|
|
247
|
+
const store = new AsyncLocalStorage();
|
|
248
|
+
return {
|
|
249
|
+
run: (fn) => store.run(true, fn),
|
|
250
|
+
isInside: () => store.getStore() === true
|
|
251
|
+
};
|
|
252
|
+
});
|
|
253
|
+
return transactionScope;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Serializes what `runExclusively` drives, since one connection takes one
|
|
257
|
+
* transaction. Waiting is only safe for a caller that is not already inside one:
|
|
258
|
+
* that caller would be queued behind itself. Async context is what separates the
|
|
259
|
+
* two, and it is the only thing that can — arrival order cannot tell a nested
|
|
260
|
+
* call from an unrelated concurrent one, and refusing both punishes the second.
|
|
261
|
+
*/
|
|
262
|
+
async function runOwnTransaction(db, callback) {
|
|
263
|
+
if (typeof db.run !== "function") throw new Error("DrizzleAdapter: the configured database commits before its transaction callback has awaited anything, and exposes no run() to drive BEGIN/COMMIT with, so transactions on it cannot be made atomic.");
|
|
264
|
+
const scope = await loadTransactionScope();
|
|
265
|
+
if (scope.isInside()) throw new Error("DrizzleAdapter: cannot begin a transaction inside another one. This driver holds a single connection, which takes one transaction at a time, so the inner transaction would wait on the outer one to finish and the outer on the inner.");
|
|
266
|
+
const run = db.run.bind(db);
|
|
267
|
+
const slot = transactionQueue.then(() => runExclusively(db, run, scope, callback));
|
|
268
|
+
transactionQueue = slot.then(NOOP, NOOP);
|
|
269
|
+
return slot;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* BEGIN/COMMIT/ROLLBACK driven here so an async callback is honoured on a driver
|
|
273
|
+
* that would otherwise commit before awaiting it. The handle itself is the
|
|
274
|
+
* transaction scope: these drivers hold one connection, so every statement
|
|
275
|
+
* between BEGIN and COMMIT is inside it. Callers reach this one at a time.
|
|
276
|
+
*/
|
|
277
|
+
async function runExclusively(db, run, scope, callback) {
|
|
278
|
+
await run(sql.raw("begin"));
|
|
279
|
+
try {
|
|
280
|
+
const result = await scope.run(() => callback(db));
|
|
281
|
+
await run(sql.raw("commit"));
|
|
282
|
+
return result;
|
|
283
|
+
} catch (error) {
|
|
284
|
+
try {
|
|
285
|
+
await run(sql.raw("rollback"));
|
|
286
|
+
} catch {}
|
|
287
|
+
throw error;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
220
290
|
const DrizzleAdapter = {
|
|
221
291
|
configure(db) {
|
|
222
292
|
database = db;
|
|
293
|
+
transactionAwaitsCallback = void 0;
|
|
294
|
+
transactionQueue = Promise.resolve();
|
|
223
295
|
},
|
|
224
296
|
getDatabase() {
|
|
225
297
|
return ensureDatabase();
|
|
@@ -339,7 +411,8 @@ const DrizzleAdapter = {
|
|
|
339
411
|
async transaction(callback) {
|
|
340
412
|
const db = ensureDatabase();
|
|
341
413
|
if (typeof db.transaction !== "function") throw new Error("DrizzleAdapter: configured database does not support transactions.");
|
|
342
|
-
return db.transaction(callback);
|
|
414
|
+
if (await awaitsItsCallback(db)) return db.transaction(callback);
|
|
415
|
+
return runOwnTransaction(db, callback);
|
|
343
416
|
}
|
|
344
417
|
};
|
|
345
418
|
//#endregion
|