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, "id" text PRIMARY KEY NOT NULL,
"account_id" text NOT NULL, "account_id" text NOT NULL,
"provider_id" text NOT NULL, "provider_id" text NOT NULL,
@ -14,7 +16,7 @@ CREATE TABLE "account" (
"updated_at" timestamp NOT NULL "updated_at" timestamp NOT NULL
); );
--> statement-breakpoint --> statement-breakpoint
CREATE TABLE "apikey" ( CREATE TABLE "shared"."apikey" (
"id" text PRIMARY KEY NOT NULL, "id" text PRIMARY KEY NOT NULL,
"name" text, "name" text,
"start" text, "start" text,
@ -38,7 +40,7 @@ CREATE TABLE "apikey" (
"metadata" text "metadata" text
); );
--> statement-breakpoint --> statement-breakpoint
CREATE TABLE "session" ( CREATE TABLE "shared"."session" (
"id" text PRIMARY KEY NOT NULL, "id" text PRIMARY KEY NOT NULL,
"expires_at" timestamp NOT NULL, "expires_at" timestamp NOT NULL,
"token" text NOT NULL, "token" text NOT NULL,
@ -50,7 +52,7 @@ CREATE TABLE "session" (
CONSTRAINT "session_token_unique" UNIQUE("token") CONSTRAINT "session_token_unique" UNIQUE("token")
); );
--> statement-breakpoint --> statement-breakpoint
CREATE TABLE "user" ( CREATE TABLE "shared"."user" (
"id" text PRIMARY KEY NOT NULL, "id" text PRIMARY KEY NOT NULL,
"name" text NOT NULL, "name" text NOT NULL,
"email" text NOT NULL, "email" text NOT NULL,
@ -64,7 +66,7 @@ CREATE TABLE "user" (
CONSTRAINT "user_username_unique" UNIQUE("username") CONSTRAINT "user_username_unique" UNIQUE("username")
); );
--> statement-breakpoint --> statement-breakpoint
CREATE TABLE "verification" ( CREATE TABLE "shared"."verification" (
"id" text PRIMARY KEY NOT NULL, "id" text PRIMARY KEY NOT NULL,
"identifier" text NOT NULL, "identifier" text NOT NULL,
"value" text NOT NULL, "value" text NOT NULL,
@ -73,6 +75,6 @@ CREATE TABLE "verification" (
"updated_at" timestamp "updated_at" timestamp
); );
--> statement-breakpoint --> 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 "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 "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 "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 "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"."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", "prevId": "00000000-0000-0000-0000-000000000000",
"version": "7", "version": "7",
"dialect": "postgresql", "dialect": "postgresql",
"tables": { "tables": {
"public.account": { "shared.account": {
"name": "account", "name": "account",
"schema": "", "schema": "shared",
"columns": { "columns": {
"id": { "id": {
"name": "id", "name": "id",
@ -93,6 +93,7 @@
"name": "account_user_id_user_id_fk", "name": "account_user_id_user_id_fk",
"tableFrom": "account", "tableFrom": "account",
"tableTo": "user", "tableTo": "user",
"schemaTo": "shared",
"columnsFrom": [ "columnsFrom": [
"user_id" "user_id"
], ],
@ -109,9 +110,9 @@
"checkConstraints": {}, "checkConstraints": {},
"isRLSEnabled": false "isRLSEnabled": false
}, },
"public.apikey": { "shared.apikey": {
"name": "apikey", "name": "apikey",
"schema": "", "schema": "shared",
"columns": { "columns": {
"id": { "id": {
"name": "id", "name": "id",
@ -250,6 +251,7 @@
"name": "apikey_user_id_user_id_fk", "name": "apikey_user_id_user_id_fk",
"tableFrom": "apikey", "tableFrom": "apikey",
"tableTo": "user", "tableTo": "user",
"schemaTo": "shared",
"columnsFrom": [ "columnsFrom": [
"user_id" "user_id"
], ],
@ -266,9 +268,9 @@
"checkConstraints": {}, "checkConstraints": {},
"isRLSEnabled": false "isRLSEnabled": false
}, },
"public.session": { "shared.session": {
"name": "session", "name": "session",
"schema": "", "schema": "shared",
"columns": { "columns": {
"id": { "id": {
"name": "id", "name": "id",
@ -325,6 +327,7 @@
"name": "session_user_id_user_id_fk", "name": "session_user_id_user_id_fk",
"tableFrom": "session", "tableFrom": "session",
"tableTo": "user", "tableTo": "user",
"schemaTo": "shared",
"columnsFrom": [ "columnsFrom": [
"user_id" "user_id"
], ],
@ -349,9 +352,9 @@
"checkConstraints": {}, "checkConstraints": {},
"isRLSEnabled": false "isRLSEnabled": false
}, },
"public.user": { "shared.user": {
"name": "user", "name": "user",
"schema": "", "schema": "shared",
"columns": { "columns": {
"id": { "id": {
"name": "id", "name": "id",
@ -431,9 +434,9 @@
"checkConstraints": {}, "checkConstraints": {},
"isRLSEnabled": false "isRLSEnabled": false
}, },
"public.verification": { "shared.verification": {
"name": "verification", "name": "verification",
"schema": "", "schema": "shared",
"columns": { "columns": {
"id": { "id": {
"name": "id", "name": "id",
@ -482,7 +485,9 @@
} }
}, },
"enums": {}, "enums": {},
"schemas": {}, "schemas": {
"shared": "shared"
},
"sequences": {}, "sequences": {},
"roles": {}, "roles": {},
"policies": {}, "policies": {},

View File

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

View File

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

View File

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