UNPKG

better-auth

Version:

The most comprehensive authentication framework for TypeScript.

148 lines (146 loc) 4.92 kB
import { getCurrentAdapter, getCurrentAuthContext } from "@better-auth/core/context"; //#region src/db/with-hooks.ts function getWithHooks(adapter, ctx) { const hooks = ctx.hooks; async function createWithHooks(data, model, customCreateFn) { const context = await getCurrentAuthContext().catch(() => null); let actualData = data; for (const hook of hooks || []) { const toRun = hook[model]?.create?.before; if (toRun) { const result = await toRun(actualData, context); if (result === false) return null; if (typeof result === "object" && "data" in result) actualData = { ...actualData, ...result.data }; } } const customCreated = customCreateFn ? await customCreateFn.fn(actualData) : null; const created = !customCreateFn || customCreateFn.executeMainFn ? await (await getCurrentAdapter(adapter)).create({ model, data: actualData, forceAllowId: true }) : customCreated; for (const hook of hooks || []) { const toRun = hook[model]?.create?.after; if (toRun) await toRun(created, context); } return created; } async function updateWithHooks(data, where, model, customUpdateFn) { const context = await getCurrentAuthContext().catch(() => null); let actualData = data; for (const hook of hooks || []) { const toRun = hook[model]?.update?.before; if (toRun) { const result = await toRun(data, context); if (result === false) return null; if (typeof result === "object" && "data" in result) actualData = { ...actualData, ...result.data }; } } const customUpdated = customUpdateFn ? await customUpdateFn.fn(actualData) : null; const updated = !customUpdateFn || customUpdateFn.executeMainFn ? await (await getCurrentAdapter(adapter)).update({ model, update: actualData, where }) : customUpdated; for (const hook of hooks || []) { const toRun = hook[model]?.update?.after; if (toRun) await toRun(updated, context); } return updated; } async function updateManyWithHooks(data, where, model, customUpdateFn) { const context = await getCurrentAuthContext().catch(() => null); let actualData = data; for (const hook of hooks || []) { const toRun = hook[model]?.update?.before; if (toRun) { const result = await toRun(data, context); if (result === false) return null; if (typeof result === "object" && "data" in result) actualData = { ...actualData, ...result.data }; } } const customUpdated = customUpdateFn ? await customUpdateFn.fn(actualData) : null; const updated = !customUpdateFn || customUpdateFn.executeMainFn ? await (await getCurrentAdapter(adapter)).updateMany({ model, update: actualData, where }) : customUpdated; for (const hook of hooks || []) { const toRun = hook[model]?.update?.after; if (toRun) await toRun(updated, context); } return updated; } async function deleteWithHooks(where, model, customDeleteFn) { const context = await getCurrentAuthContext().catch(() => null); let entityToDelete = null; try { entityToDelete = (await (await getCurrentAdapter(adapter)).findMany({ model, where, limit: 1 }))[0] || null; } catch {} if (entityToDelete) for (const hook of hooks || []) { const toRun = hook[model]?.delete?.before; if (toRun) { if (await toRun(entityToDelete, context) === false) return null; } } const customDeleted = customDeleteFn ? await customDeleteFn.fn(where) : null; const deleted = !customDeleteFn || customDeleteFn.executeMainFn ? await (await getCurrentAdapter(adapter)).delete({ model, where }) : customDeleted; if (entityToDelete) for (const hook of hooks || []) { const toRun = hook[model]?.delete?.after; if (toRun) await toRun(entityToDelete, context); } return deleted; } async function deleteManyWithHooks(where, model, customDeleteFn) { const context = await getCurrentAuthContext().catch(() => null); let entitiesToDelete = []; try { entitiesToDelete = await (await getCurrentAdapter(adapter)).findMany({ model, where }); } catch {} for (const entity of entitiesToDelete) for (const hook of hooks || []) { const toRun = hook[model]?.delete?.before; if (toRun) { if (await toRun(entity, context) === false) return null; } } const customDeleted = customDeleteFn ? await customDeleteFn.fn(where) : null; const deleted = !customDeleteFn || customDeleteFn.executeMainFn ? await (await getCurrentAdapter(adapter)).deleteMany({ model, where }) : customDeleted; for (const entity of entitiesToDelete) for (const hook of hooks || []) { const toRun = hook[model]?.delete?.after; if (toRun) await toRun(entity, context); } return deleted; } return { createWithHooks, updateWithHooks, updateManyWithHooks, deleteWithHooks, deleteManyWithHooks }; } //#endregion export { getWithHooks }; //# sourceMappingURL=with-hooks.mjs.map