mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-27 01:16:55 -05:00
fix: drizzle mysql unsupported returning (#128)
fix: drizzle mysql unsupported returning (#128) --------- Co-authored-by: Riley Barabash <admin@rileybarabash.com>
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
"scripts": {
|
||||
"build": "NODE_OPTIONS='--max-old-space-size=4000' tsup --clean --dts",
|
||||
"dev": "NODE_OPTIONS='--max-old-space-size=4000' tsup --watch --sourcemap",
|
||||
"dev:dts": "NODE_OPTIONS='--max-old-space-size=4000' tsup --watch --dts",
|
||||
"dev:dts": "NODE_OPTIONS='--max-old-space-size=8192' tsup --watch --dts",
|
||||
"test": "pnpm prisma:push && pnpm typecheck && vitest",
|
||||
"prisma:push": "prisma db push --schema src/adapters/prisma-adapter/test/schema.prisma",
|
||||
"test:adapters": "pnpm prisma:push && vitest adapters",
|
||||
|
||||
@@ -90,8 +90,14 @@ export const drizzleAdapter = (
|
||||
schema,
|
||||
usePlural: options.usePlural,
|
||||
});
|
||||
const res = await db.insert(schemaModel).values(val).returning();
|
||||
const mutation = db.insert(schemaModel).values(val);
|
||||
if (databaseType !== "mysql") return (await mutation.returning())[0];
|
||||
|
||||
await mutation;
|
||||
const res = await db
|
||||
.select()
|
||||
.from(schemaModel)
|
||||
.where(eq(schemaModel.id, (data.data as { id: string }).id));
|
||||
return res[0];
|
||||
},
|
||||
async findOne(data) {
|
||||
@@ -150,11 +156,17 @@ export const drizzleAdapter = (
|
||||
usePlural: options.usePlural,
|
||||
});
|
||||
const wheres = whereConvertor(where, schemaModel);
|
||||
const res = await db
|
||||
const mutation = db
|
||||
.update(schemaModel)
|
||||
.set(update)
|
||||
.where(...wheres)
|
||||
.returning();
|
||||
.where(...wheres);
|
||||
if (databaseType !== "mysql") return (await mutation.returning())[0];
|
||||
|
||||
await mutation;
|
||||
const res = await db
|
||||
.select()
|
||||
.from(schemaModel)
|
||||
.where(eq(schemaModel.id, (data.update as { id: string }).id));
|
||||
return res[0];
|
||||
},
|
||||
async delete(data) {
|
||||
|
||||
@@ -12,25 +12,6 @@ import type {
|
||||
Prettify,
|
||||
} from "../../types";
|
||||
|
||||
/**
|
||||
* Generate a unique key for the request to cache the
|
||||
* request for 5 seconds for this specific request.
|
||||
*
|
||||
* This is to prevent reaching to database if getSession is
|
||||
* called multiple times for the same request
|
||||
*/
|
||||
function getRequestUniqueKey(ctx: Context<any, any>, token: string): string {
|
||||
if (!ctx.request) {
|
||||
return "";
|
||||
}
|
||||
const { method, url, headers } = ctx.request;
|
||||
const userAgent = ctx.request.headers.get("User-Agent") || "";
|
||||
const ip = getIp(ctx.request) || "";
|
||||
const headerString = JSON.stringify(headers);
|
||||
const uniqueString = `${method}:${url}:${headerString}:${userAgent}:${ip}:${token}`;
|
||||
return uniqueString;
|
||||
}
|
||||
|
||||
export const getSession = <Option extends BetterAuthOptions>() =>
|
||||
createAuthEndpoint(
|
||||
"/session",
|
||||
@@ -87,8 +68,7 @@ export const getSession = <Option extends BetterAuthOptions>() =>
|
||||
*
|
||||
* e.g. ({expiry date} - 30 days) + 1 hour
|
||||
*
|
||||
* inspired by: https://github.com/nextauthjs/next-auth/blob/main/packages/core/src/lib/
|
||||
* actions/session.ts
|
||||
* inspired by: https://github.com/nextauthjs/next-auth/blob/main/packages/core/src/lib/actions/session.ts
|
||||
*/
|
||||
const sessionIsDueToBeUpdatedDate =
|
||||
session.session.expiresAt.valueOf() -
|
||||
|
||||
@@ -65,6 +65,9 @@ export function matchType(
|
||||
fieldType: FieldType,
|
||||
dbType: KyselyDatabaseType,
|
||||
) {
|
||||
if (fieldType === "string[]" || fieldType === "number[]") {
|
||||
return columnDataType.toLowerCase().includes("json");
|
||||
}
|
||||
const types = map[dbType];
|
||||
const type = types[fieldType].map((t) => t.toLowerCase());
|
||||
const matches = type.includes(columnDataType.toLowerCase());
|
||||
@@ -169,6 +172,12 @@ export async function getMigrations(config: BetterAuthOptions) {
|
||||
if (dbType === "mysql" && type === "string") {
|
||||
return "varchar(255)";
|
||||
}
|
||||
if (dbType === "sqlite" && (type === "string[]" || type === "number[]")) {
|
||||
return "text";
|
||||
}
|
||||
if (type === "string[]" || type === "number[]") {
|
||||
return "jsonb";
|
||||
}
|
||||
return typeMap[type];
|
||||
}
|
||||
if (toBeAdded.length) {
|
||||
|
||||
@@ -17,7 +17,12 @@ export type FieldAttribute<T extends FieldType = FieldType> = {
|
||||
type: T;
|
||||
} & FieldAttributeConfig<T>;
|
||||
|
||||
export type FieldType = "string" | "number" | "boolean" | "date";
|
||||
export type FieldType =
|
||||
| "string"
|
||||
| "number"
|
||||
| "boolean"
|
||||
| "date"
|
||||
| `${"string" | "number"}[]`;
|
||||
|
||||
export type InferValueType<T extends FieldType> = T extends "string"
|
||||
? string
|
||||
@@ -25,8 +30,10 @@ export type InferValueType<T extends FieldType> = T extends "string"
|
||||
? number
|
||||
: T extends "boolean"
|
||||
? boolean
|
||||
: T extends "date"
|
||||
? Date
|
||||
: T extends `${infer T}[]`
|
||||
? T extends "string"
|
||||
? string[]
|
||||
: number[]
|
||||
: never;
|
||||
|
||||
export type InferFieldOutput<T extends FieldAttribute> =
|
||||
@@ -58,7 +65,7 @@ export type FieldAttributeConfig<T extends FieldType = FieldType> = {
|
||||
* Note: This will not create a default value on the database level. It will only
|
||||
* be used when creating a new record.
|
||||
*/
|
||||
defaultValue?: InferValueType<T> | (() => InferValueType<T>);
|
||||
defaultValue?: any;
|
||||
/**
|
||||
* transform the value before storing it.
|
||||
*/
|
||||
|
||||
@@ -8,6 +8,12 @@ export function toZodSchema(fields: Record<string, FieldAttribute>) {
|
||||
if (!field) {
|
||||
return acc;
|
||||
}
|
||||
if (field.type === "string[]" || field.type === "number[]") {
|
||||
return {
|
||||
...acc,
|
||||
[key]: z.array(field.type === "string[]" ? z.string() : z.number()),
|
||||
};
|
||||
}
|
||||
let schema: ZodSchema = z[field.type]();
|
||||
if (field?.required === false) {
|
||||
schema = schema.optional();
|
||||
|
||||
Reference in New Issue
Block a user