feat: move auth schema to a shared schema

This commit is contained in:
Yadunand Prem 2025-07-10 22:32:18 -04:00
parent 558280f342
commit 5e1d72bf2c
No known key found for this signature in database
9 changed files with 64 additions and 32 deletions

11
api/drizzle.config.ts Normal file
View File

@ -0,0 +1,11 @@
import { defineConfig } from "drizzle-kit";
import { env } from "@/env";
export default defineConfig({
out: "./drizzle",
schema: "./src/db/schema/*",
dialect: "postgresql",
dbCredentials: {
url: env.DATABASE_URL,
},
});

View File

@ -1,4 +1,6 @@
CREATE TABLE "account" (
CREATE SCHEMA "shared";
--> statement-breakpoint
CREATE TABLE "shared"."account" (
"id" text PRIMARY KEY NOT NULL,
"account_id" text NOT NULL,
"provider_id" text NOT NULL,
@ -14,7 +16,7 @@ CREATE TABLE "account" (
"updated_at" timestamp NOT NULL
);
--> statement-breakpoint
CREATE TABLE "apikey" (
CREATE TABLE "shared"."apikey" (
"id" text PRIMARY KEY NOT NULL,
"name" text,
"start" text,
@ -38,7 +40,7 @@ CREATE TABLE "apikey" (
"metadata" text
);
--> statement-breakpoint
CREATE TABLE "session" (
CREATE TABLE "shared"."session" (
"id" text PRIMARY KEY NOT NULL,
"expires_at" timestamp NOT NULL,
"token" text NOT NULL,
@ -50,7 +52,7 @@ CREATE TABLE "session" (
CONSTRAINT "session_token_unique" UNIQUE("token")
);
--> statement-breakpoint
CREATE TABLE "user" (
CREATE TABLE "shared"."user" (
"id" text PRIMARY KEY NOT NULL,
"name" text NOT NULL,
"email" text NOT NULL,
@ -64,7 +66,7 @@ CREATE TABLE "user" (
CONSTRAINT "user_username_unique" UNIQUE("username")
);
--> statement-breakpoint
CREATE TABLE "verification" (
CREATE TABLE "shared"."verification" (
"id" text PRIMARY KEY NOT NULL,
"identifier" text NOT NULL,
"value" text NOT NULL,
@ -73,6 +75,6 @@ CREATE TABLE "verification" (
"updated_at" timestamp
);
--> statement-breakpoint
ALTER TABLE "account" ADD CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "apikey" ADD CONSTRAINT "apikey_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "session" ADD CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
ALTER TABLE "shared"."account" ADD CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "shared"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "shared"."apikey" ADD CONSTRAINT "apikey_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "shared"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "shared"."session" ADD CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "shared"."user"("id") ON DELETE cascade ON UPDATE no action;

View File

@ -1,12 +1,12 @@
{
"id": "284c4b80-7cd3-4277-8c67-f346be8d83c2",
"id": "033383bb-5508-4523-b1f5-d7b7f0c00a33",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.account": {
"shared.account": {
"name": "account",
"schema": "",
"schema": "shared",
"columns": {
"id": {
"name": "id",
@ -93,6 +93,7 @@
"name": "account_user_id_user_id_fk",
"tableFrom": "account",
"tableTo": "user",
"schemaTo": "shared",
"columnsFrom": [
"user_id"
],
@ -109,9 +110,9 @@
"checkConstraints": {},
"isRLSEnabled": false
},
"public.apikey": {
"shared.apikey": {
"name": "apikey",
"schema": "",
"schema": "shared",
"columns": {
"id": {
"name": "id",
@ -250,6 +251,7 @@
"name": "apikey_user_id_user_id_fk",
"tableFrom": "apikey",
"tableTo": "user",
"schemaTo": "shared",
"columnsFrom": [
"user_id"
],
@ -266,9 +268,9 @@
"checkConstraints": {},
"isRLSEnabled": false
},
"public.session": {
"shared.session": {
"name": "session",
"schema": "",
"schema": "shared",
"columns": {
"id": {
"name": "id",
@ -325,6 +327,7 @@
"name": "session_user_id_user_id_fk",
"tableFrom": "session",
"tableTo": "user",
"schemaTo": "shared",
"columnsFrom": [
"user_id"
],
@ -349,9 +352,9 @@
"checkConstraints": {},
"isRLSEnabled": false
},
"public.user": {
"shared.user": {
"name": "user",
"schema": "",
"schema": "shared",
"columns": {
"id": {
"name": "id",
@ -431,9 +434,9 @@
"checkConstraints": {},
"isRLSEnabled": false
},
"public.verification": {
"shared.verification": {
"name": "verification",
"schema": "",
"schema": "shared",
"columns": {
"id": {
"name": "id",
@ -482,7 +485,9 @@
}
},
"enums": {},
"schemas": {},
"schemas": {
"shared": "shared"
},
"sequences": {},
"roles": {},
"policies": {},

View File

@ -5,8 +5,8 @@
{
"idx": 0,
"version": "7",
"when": 1752191664960,
"tag": "0000_deep_maelstrom",
"when": 1752200400975,
"tag": "0000_long_puff_adder",
"breakpoints": true
}
]

View File

@ -1,4 +1,5 @@
import { drizzle } from "drizzle-orm/bun-sql";
import { env } from "@/env";
import * as authSchema from "./schema/auth";
export const db = drizzle(env.DATABASE_URL);
export const db = drizzle(env.DATABASE_URL, { schema: { ...authSchema } });

View File

@ -1,12 +1,14 @@
import {
boolean,
integer,
pgTable,
pgSchema,
text,
timestamp,
} from "drizzle-orm/pg-core";
export const user = pgTable("user", {
export const shared = pgSchema("shared");
export const user = shared.table("user", {
id: text("id").primaryKey(),
name: text("name").notNull(),
email: text("email").notNull().unique(),
@ -24,7 +26,7 @@ export const user = pgTable("user", {
displayUsername: text("display_username"),
});
export const session = pgTable("session", {
export const session = shared.table("session", {
id: text("id").primaryKey(),
expiresAt: timestamp("expires_at").notNull(),
token: text("token").notNull().unique(),
@ -37,7 +39,7 @@ export const session = pgTable("session", {
.references(() => user.id, { onDelete: "cascade" }),
});
export const account = pgTable("account", {
export const account = shared.table("account", {
id: text("id").primaryKey(),
accountId: text("account_id").notNull(),
providerId: text("provider_id").notNull(),
@ -55,7 +57,7 @@ export const account = pgTable("account", {
updatedAt: timestamp("updated_at").notNull(),
});
export const verification = pgTable("verification", {
export const verification = shared.table("verification", {
id: text("id").primaryKey(),
identifier: text("identifier").notNull(),
value: text("value").notNull(),
@ -64,7 +66,7 @@ export const verification = pgTable("verification", {
updatedAt: timestamp("updated_at").$defaultFn(() => new Date()),
});
export const apikey = pgTable("apikey", {
export const apikey = shared.table("apikey", {
id: text("id").primaryKey(),
name: text("name"),
start: text("start"),

View File

@ -1,14 +1,14 @@
import { Hono } from "hono";
import { logger } from "hono/logger";
import { env } from "./env";
import { auth } from "./lib/auth";
const app = new Hono();
app.use(logger());
app.on(["POST", "GET"], "/api/auth/**", (c) => auth.handler(c.req.raw));
app.get("/", (c) => {
return c.text("Hello Hono!");
});
console.log(`using env: ${JSON.stringify(env, null, 2)}`);
export default app;

11
api/src/lib/authClient.ts Normal file
View File

@ -0,0 +1,11 @@
// mainly for testing
//
import { createAuthClient } from "better-auth/client";
import { apiKeyClient } from "better-auth/client/plugins";
import { env } from "@/env";
export const authClient = createAuthClient({
baseURL: env.BETTER_AUTH_URL,
plugins: [apiKeyClient()],
});