Skip to content

Commit a48993d

Browse files
committed
fix: pass false to transaction if is not supported in engine
1 parent 823a582 commit a48993d

2 files changed

Lines changed: 60 additions & 67 deletions

File tree

packages/surreal-better-auth/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "surreal-better-auth",
3-
"version": "2.0.0-beta.4",
3+
"version": "2.0.0-beta.6",
44
"author": {
55
"name": "Oskar Gmerek",
66
"url": "https://oskargmerek.com",
@@ -51,8 +51,6 @@
5151
"scripts": {
5252
"build": "tsdown",
5353
"build:watch": "tsdown --watch",
54-
"test": "vitest run tests/ --fileParallelism=false",
55-
"test:plugins": "vitest run tests/plugins/",
5654
"postinstall": "node welcome.js"
5755
},
5856
"type": "module",

packages/surreal-better-auth/src/adapter.ts

Lines changed: 59 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -302,74 +302,69 @@ export const surrealdbAdapter = (db: Surreal, config?: SurrealDBAdapterConfig) =
302302
};
303303
};
304304

305-
const adapterOptions: AdapterFactoryOptions = {
306-
config: {
307-
adapterId: "surrealdb",
308-
adapterName: "SurrealDB",
309-
usePlural: config?.usePlural ?? false,
310-
debugLogs: config?.debugLogs ?? false,
311-
supportsJSON: true,
312-
supportsArrays: true,
313-
supportsUUIDs: false,
314-
// @ts-expect-error
315-
supportsJoin: true,
316-
supportsDates: true,
317-
supportsBooleans: true,
318-
supportsNumericIds: true,
319-
customIdGenerator: config?.idGenerator
320-
? () => {
321-
return "__surreal__";
305+
return (options: any): DBAdapter => {
306+
const supportsTx = db.isFeatureSupported(Features.Transactions);
307+
308+
const adapterOptions: AdapterFactoryOptions = {
309+
config: {
310+
adapterId: "surrealdb",
311+
adapterName: "SurrealDB",
312+
usePlural: config?.usePlural ?? false,
313+
debugLogs: config?.debugLogs ?? false,
314+
supportsJSON: true,
315+
supportsArrays: true,
316+
supportsUUIDs: false,
317+
// @ts-expect-error
318+
supportsJoin: true,
319+
supportsDates: true,
320+
supportsBooleans: true,
321+
supportsNumericIds: true,
322+
customIdGenerator: config?.idGenerator
323+
? () => {
324+
return "__surreal__";
325+
}
326+
: undefined,
327+
328+
/**
329+
* Transforms SurrealDB specific types back to standard JavaScript types.
330+
* Converts RecordId to string and Surreal DateTime to native Date.
331+
*/
332+
customTransformOutput: ({ data }) => {
333+
if (data instanceof RecordId || data instanceof StringRecordId) {
334+
return data.toString();
322335
}
323-
: undefined,
324-
/**
325-
* Transforms SurrealDB specific types back to standard JavaScript types.
326-
* Converts RecordId to string and Surreal DateTime to native Date.
327-
*/
328-
customTransformOutput: ({ data }) => {
329-
if (data instanceof RecordId || data instanceof StringRecordId) {
330-
return data.toString();
331-
}
332336

333-
if (data instanceof DateTime) {
334-
return data.toDate();
335-
}
336-
337-
return data;
338-
},
339-
/**
340-
* Wraps adapter operations in a SurrealDB transaction.
341-
*/
342-
transaction: async (cb) => {
343-
// Check if transactions are supported by the engine.
344-
if (!db.isFeatureSupported(Features.Transactions)) {
345-
// If not supported, use a base adapter and execute sequentially.
346-
const baseAdapter = lazyAdapter(authOptions);
347-
return await cb(baseAdapter);
348-
}
337+
if (data instanceof DateTime) {
338+
return data.toDate();
339+
}
349340

350-
// If transactions are supported, use a transactional adapter.
351-
const txn = await db.beginTransaction();
352-
try {
353-
const transactionalAdapter = createAdapterFactory({
354-
config: adapterOptions.config,
355-
adapter: createCustomAdapter(txn),
356-
})(authOptions);
357-
358-
const result = await cb(transactionalAdapter);
359-
await txn.commit();
360-
return result;
361-
} catch (err) {
362-
await txn.cancel();
363-
throw err;
364-
}
341+
return data;
342+
},
343+
/**
344+
* Wraps adapter operations in a SurrealDB transaction.
345+
*/
346+
transaction: supportsTx
347+
? async (cb) => {
348+
const txn = await db.beginTransaction();
349+
try {
350+
const transactionalAdapter = createAdapterFactory({
351+
config: adapterOptions.config,
352+
adapter: createCustomAdapter(txn),
353+
})(authOptions);
354+
355+
const result = await cb(transactionalAdapter);
356+
await txn.commit();
357+
return result;
358+
} catch (err) {
359+
await txn.cancel();
360+
throw err;
361+
}
362+
}
363+
: false,
365364
},
366-
},
367-
adapter: createCustomAdapter(db),
368-
};
369-
370-
const lazyAdapter = createAdapterFactory(adapterOptions);
365+
adapter: createCustomAdapter(db),
366+
};
371367

372-
return (options: any): DBAdapter => {
373-
return lazyAdapter(options);
368+
return createAdapterFactory(adapterOptions)(options);
374369
};
375370
};

0 commit comments

Comments
 (0)