diff --git a/bun.lockb b/bun.lockb index 975d250..b1220bf 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/docker-compose.yaml b/docker-compose.yaml index 6beeec5..637acfe 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -5,6 +5,8 @@ services: dockerfile: Dockerfile ports: - "3000:3000" + env_file: + - .env environment: - NODE_ENV=production - OTEL_EXPORTER_OTLP_ENDPOINT=http://tracing:4318 @@ -27,10 +29,14 @@ services: db: image: postgres:latest + env_file: + - .env environment: POSTGRES_USER: ${DB_USER} POSTGRES_PASSWORD: ${DB_PASSWORD} POSTGRES_DB: ${DB_NAME} + ports: + - "5432:5432" volumes: - postgres_data:/var/lib/postgresql/data networks: diff --git a/drizzle/0000_elite_ben_parker.sql b/drizzle/0000_rich_reavers.sql similarity index 68% rename from drizzle/0000_elite_ben_parker.sql rename to drizzle/0000_rich_reavers.sql index 64f5981..7a0e6df 100644 --- a/drizzle/0000_elite_ben_parker.sql +++ b/drizzle/0000_rich_reavers.sql @@ -1,4 +1,6 @@ -CREATE TABLE "account" ( +CREATE SCHEMA "auth"; +--> statement-breakpoint +CREATE TABLE "auth"."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 "session" ( +CREATE TABLE "auth"."session" ( "id" text PRIMARY KEY NOT NULL, "expires_at" timestamp NOT NULL, "token" text NOT NULL, @@ -26,7 +28,7 @@ CREATE TABLE "session" ( CONSTRAINT "session_token_unique" UNIQUE("token") ); --> statement-breakpoint -CREATE TABLE "user" ( +CREATE TABLE "auth"."user" ( "id" text PRIMARY KEY NOT NULL, "name" text NOT NULL, "email" text NOT NULL, @@ -37,7 +39,7 @@ CREATE TABLE "user" ( CONSTRAINT "user_email_unique" UNIQUE("email") ); --> statement-breakpoint -CREATE TABLE "verification" ( +CREATE TABLE "auth"."verification" ( "id" text PRIMARY KEY NOT NULL, "identifier" text NOT NULL, "value" text NOT NULL, @@ -46,5 +48,5 @@ 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 no action 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 no action ON UPDATE no action; \ No newline at end of file +ALTER TABLE "auth"."account" ADD CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint +ALTER TABLE "auth"."session" ADD CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE no action ON UPDATE no action; \ No newline at end of file diff --git a/drizzle/meta/0000_snapshot.json b/drizzle/meta/0000_snapshot.json index df9dd9b..6fff69e 100644 --- a/drizzle/meta/0000_snapshot.json +++ b/drizzle/meta/0000_snapshot.json @@ -1,12 +1,12 @@ { - "id": "f4717319-16e8-43a1-8b25-12a12964c98f", + "id": "1ada386b-032f-4c0c-a867-e25f2e921167", "prevId": "00000000-0000-0000-0000-000000000000", "version": "7", "dialect": "postgresql", "tables": { - "public.account": { + "auth.account": { "name": "account", - "schema": "", + "schema": "auth", "columns": { "id": { "name": "id", @@ -93,6 +93,7 @@ "name": "account_user_id_user_id_fk", "tableFrom": "account", "tableTo": "user", + "schemaTo": "auth", "columnsFrom": [ "user_id" ], @@ -109,9 +110,9 @@ "checkConstraints": {}, "isRLSEnabled": false }, - "public.session": { + "auth.session": { "name": "session", - "schema": "", + "schema": "auth", "columns": { "id": { "name": "id", @@ -168,6 +169,7 @@ "name": "session_user_id_user_id_fk", "tableFrom": "session", "tableTo": "user", + "schemaTo": "auth", "columnsFrom": [ "user_id" ], @@ -192,9 +194,9 @@ "checkConstraints": {}, "isRLSEnabled": false }, - "public.user": { + "auth.user": { "name": "user", - "schema": "", + "schema": "auth", "columns": { "id": { "name": "id", @@ -255,9 +257,9 @@ "checkConstraints": {}, "isRLSEnabled": false }, - "public.verification": { + "auth.verification": { "name": "verification", - "schema": "", + "schema": "auth", "columns": { "id": { "name": "id", @@ -306,7 +308,9 @@ } }, "enums": {}, - "schemas": {}, + "schemas": { + "auth": "auth" + }, "sequences": {}, "roles": {}, "policies": {}, diff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.json index 4e586cb..0ca736c 100644 --- a/drizzle/meta/_journal.json +++ b/drizzle/meta/_journal.json @@ -5,8 +5,8 @@ { "idx": 0, "version": "7", - "when": 1736255248333, - "tag": "0000_elite_ben_parker", + "when": 1736605568502, + "tag": "0000_rich_reavers", "breakpoints": true } ] diff --git a/package.json b/package.json index 39ae0d1..f5fa11e 100644 --- a/package.json +++ b/package.json @@ -5,9 +5,13 @@ "test": "echo \"Error: no test specified\" && exit 1", "dev": "bun run --watch src/index.ts", "email": "email dev --dir src/emails", + "db:studio": "drizzle-kit studio", + "db:generate": "drizzle-kit generate", + "db:migrate": "drizzle-kit migrate", "build": "bun build --compile --minify-whitespace --minify-syntax --target bun --outfile server ./src/index.ts" }, "dependencies": { + "@elysiajs/cors": "^1.2.0", "@elysiajs/opentelemetry": "^1.2.0", "@elysiajs/server-timing": "^1.2.0", "@elysiajs/swagger": "^1.2.0", @@ -37,4 +41,4 @@ "tsx": "^4.19.2" }, "module": "src/index.js" -} \ No newline at end of file +} diff --git a/src/api/user/user.model.ts b/src/api/user/user.model.ts deleted file mode 100644 index dbb100d..0000000 --- a/src/api/user/user.model.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { pgTable, varchar, timestamp } from "drizzle-orm/pg-core"; - -import { createId } from "@paralleldrive/cuid2"; -import { createInsertSchema } from "drizzle-typebox"; -import { t } from "elysia"; - -export const user = pgTable("user", { - id: varchar("id") - .$defaultFn(() => createId()) - .primaryKey(), - username: varchar("username").notNull().unique(), - password: varchar("password").notNull(), - email: varchar("email").notNull().unique(), - salt: varchar("salt", { length: 64 }).notNull(), - createdAt: timestamp("created_at").defaultNow().notNull(), -}); - -const _createUser = createInsertSchema(user, { - email: t.String({ format: "email" }), -}); - -// ✅ This works, by referencing the type from `drizzle-typebox` -export const createUserType = t.Omit(_createUser, ["id", "salt", "createdAt"]); diff --git a/src/db/index.ts b/src/db/index.ts index eeef779..53dcac8 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -1,4 +1,4 @@ import "dotenv/config"; import { drizzle } from "drizzle-orm/node-postgres"; -const db = drizzle(process.env.DATABASE_URL!); +export const db = drizzle(process.env.DATABASE_URL!); diff --git a/src/db/model.ts b/src/db/model.ts deleted file mode 100644 index d50a294..0000000 --- a/src/db/model.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { createUserType } from "../api/user/user.model"; - -export const db = { - insert: { - user: createUserType, - }, -}; diff --git a/src/db/schema.ts b/src/db/schema.ts index 4214f12..61600f0 100644 --- a/src/db/schema.ts +++ b/src/db/schema.ts @@ -1,6 +1,8 @@ -import { pgTable, text, timestamp, boolean } from "drizzle-orm/pg-core"; +import { pgTable, pgSchema, text, timestamp, boolean } from "drizzle-orm/pg-core"; -export const user = pgTable("user", { +export const auth_schema = pgSchema("auth") + +export const user = auth_schema.table("user", { id: text("id").primaryKey(), name: text("name").notNull(), email: text("email").notNull().unique(), @@ -10,7 +12,7 @@ export const user = pgTable("user", { updatedAt: timestamp("updated_at").notNull(), }); -export const session = pgTable("session", { +export const session = auth_schema.table("session", { id: text("id").primaryKey(), expiresAt: timestamp("expires_at").notNull(), token: text("token").notNull().unique(), @@ -23,7 +25,7 @@ export const session = pgTable("session", { .references(() => user.id), }); -export const account = pgTable("account", { +export const account = auth_schema.table("account", { id: text("id").primaryKey(), accountId: text("account_id").notNull(), providerId: text("provider_id").notNull(), @@ -41,7 +43,7 @@ export const account = pgTable("account", { updatedAt: timestamp("updated_at").notNull(), }); -export const verification = pgTable("verification", { +export const verification = auth_schema.table("verification", { id: text("id").primaryKey(), identifier: text("identifier").notNull(), value: text("value").notNull(), diff --git a/src/index.ts b/src/index.ts index 11fd30f..669072b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,22 +2,27 @@ import { Elysia } from "elysia"; import { swagger } from "@elysiajs/swagger"; import { opentelemetry } from "@elysiajs/opentelemetry"; import { serverTiming } from "@elysiajs/server-timing"; - +import { cors } from '@elysiajs/cors' import { note } from "./api/note/note.route"; import { betterAuthView } from "./lib/auth/auth-view"; import { userMiddleware, userInfo } from "./middlewares/auth-middleware"; const app = new Elysia() + .use(cors()) .use(opentelemetry()) - .use(swagger()) + .use(swagger({ + path: "/docs", + })) .use(serverTiming()) .onError(({ error, code }) => { if (code === "NOT_FOUND") return "Not Found :("; console.error(error); }) - .use(note) .derive(({ request }) => userMiddleware(request)) .all("/api/auth/*", betterAuthView) + .use(note) .get("/user", ({ user, session }) => userInfo(user, session)); app.listen(3000); + +console.log("Server is running on: http://localhost:3000") diff --git a/src/lib/auth/auth-view.ts b/src/lib/auth/auth-view.ts index 5bca005..0a76a8a 100644 --- a/src/lib/auth/auth-view.ts +++ b/src/lib/auth/auth-view.ts @@ -1,4 +1,4 @@ -import { Context, Elysia } from "elysia"; +import { Context } from "elysia"; import { auth } from "./auth"; export const betterAuthView = (context: Context) => { diff --git a/src/lib/auth/auth.ts b/src/lib/auth/auth.ts index dc116b6..226c052 100644 --- a/src/lib/auth/auth.ts +++ b/src/lib/auth/auth.ts @@ -1,14 +1,28 @@ import { betterAuth } from "better-auth"; import { drizzleAdapter } from "better-auth/adapters/drizzle"; -import { db } from "../../db/model"; +import { db } from "../../db/index"; +import { openAPI } from "better-auth/plugins" +import { user, account, verification, session } from "../../db/schema"; + export const auth = betterAuth({ database: drizzleAdapter(db, { // We're using Drizzle as our database provider: "pg", + schema: { + user: user, + session: session, + account: account, + verification: verification + } }), emailAndPassword: { enabled: true, // If you want to use email and password auth }, + plugins: [ + openAPI({ + path: "/docs", + }), + ], socialProviders: { /* * We're using Google and Github as our social provider,