@zpg6-test-pkgs/better-auth
Version:
The most comprehensive authentication library for TypeScript.
159 lines (156 loc) • 4.37 kB
text/typescript
import { a as Adapter, W as Where, b as AdapterInstance, B as BetterAuthOptions } from '../../shared/better-auth.eDxSywzK.mjs';
import 'kysely';
import 'better-call';
import 'zod/v4';
import '../../shared/better-auth.DTtXpZYr.mjs';
import '../../shared/better-auth.B_ZH8D8u.mjs';
import 'jose';
import 'zod/v4/core';
import 'zod';
import 'better-sqlite3';
import 'bun:sqlite';
/**
* Parameters passed to adapter router callbacks
*/
type AdapterRouterParams = {
modelName: string;
fallbackAdapter: Adapter;
} & ({
operation: "create";
data?: {
model: string;
data: Omit<Record<string, any>, "id">;
select?: string[];
forceAllowId?: boolean;
};
} | {
operation: "findOne";
data?: {
model: string;
where: Where[];
select?: string[];
};
} | {
operation: "findMany";
data?: {
model: string;
where?: Where[];
limit?: number;
sortBy?: {
field: string;
direction: "asc" | "desc";
};
offset?: number;
};
} | {
operation: "update";
data?: {
model: string;
where: Where[];
update: Record<string, any>;
};
} | {
operation: "updateMany";
data?: {
model: string;
where: Where[];
update: Record<string, any>;
};
} | {
operation: "delete";
data?: {
model: string;
where: Where[];
};
} | {
operation: "deleteMany";
data?: {
model: string;
where: Where[];
};
} | {
operation: "count";
data?: {
model: string;
where?: Where[];
};
});
/**
* Adapter router callback function
* Returns an adapter instance, Promise<adapter instance>, or null/undefined to continue to next route
*/
type AdapterRouterCallback = (params: AdapterRouterParams) => AdapterInstance | Promise<AdapterInstance | null | undefined> | null | undefined;
/**
* Configuration for the simplified adapter router
*/
interface AdapterRouterConfig {
/**
* The fallback/main adapter that handles all models by default
*/
fallbackAdapter: AdapterInstance;
/**
* Array of routing callbacks evaluated in order
* Each callback receives `{ modelName, data, operation, fallbackAdapter }` and can return:
* - An Adapter instance to use for this request
* - null/undefined to continue to the next route
*
* First matching route wins. If no routes match, fallbackAdapter is used.
*
* @example
* ```ts
* [
* // Premium users get premium storage
* ({ data }) => data?.tier === 'premium' ? premiumAdapter : null,
*
* // Sessions go to cache
* ({ modelName }) => modelName === 'session' ? cacheAdapter : null,
*
* // Reads go to replica, writes to primary
* ({ operation }) => ['findOne', 'findMany'].includes(operation) ? replicaAdapter : null,
* ]
* ```
*/
routes?: AdapterRouterCallback[];
/**
* Optional debug logging
*/
debugLogs?: boolean;
}
/**
* Creates an adapter router that routes requests to different adapters based on the model
*
* @param config - Router configuration
* @returns Adapter instance function
*
* @example
* ```ts
* import { betterAuth } from "better-auth";
* import { adapterRouter } from "better-auth/adapters/adapter-router";
* import { prismaAdapter } from "better-auth/adapters/prisma";
* import { redisAdapter } from "better-auth/adapters/redis";
*
* const prisma = prismaAdapter(prismaClient);
* const redis = redisAdapter(redisClient);
* const premium = premiumAdapter(premiumDb);
*
* export const auth = betterAuth({
* database: adapterRouter({
* fallbackAdapter: prisma,
* routes: [
* // Premium users get premium storage
* ({ data }) => data?.tier === 'premium' ? premium : null,
*
* // Sessions go to cache
* ({ modelName }) => modelName === 'session' ? redis : null,
*
* // Reads go to replica
* ({ operation }) => ['findOne', 'findMany'].includes(operation) ? readReplica : null,
* ]
* })
* });
*
* ```
*/
declare const adapterRouter: (config: AdapterRouterConfig) => (options: BetterAuthOptions) => Adapter;
export { adapterRouter };
export type { AdapterRouterCallback, AdapterRouterConfig, AdapterRouterParams };