Skip to content

Commit

Permalink
refactor(db): Migrate to Prisma
Browse files Browse the repository at this point in the history
Moves database interactions from raw SQL to Prisma ORM.
  • Loading branch information
nin0-dev authored Nov 12, 2024
2 parents 19146fd + e440908 commit 0e0bedc
Show file tree
Hide file tree
Showing 14 changed files with 323 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ NODE_ENV=production
HOST=localhost
PORT=3000
# Postgres connection string
POSTGRES_URL="postgresql://postgres:postgres@localhost:5432/nin0chat"
POSTGRES_URL="postgresql://postgres:postgres@localhost:5432/nin0chat?schema=public"
# Cloudflare Turnstile site secret
TURNSTILE_SECRET=0x0000000000000000000000000000000000
# SMTP2Go API key
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"scripts": {
"dev": "tsx watch --env-file=.env --include \"**/*\" src/index.ts",
"build": "tsc",
"start": "node dist/index.js"
"start": "node dist/index.js",
"db:markinit": "prisma migrate resolve --applied 0_init",
"db:migrate:prod": "prisma migrate deploy",
"db:migrate:dev": "prisma migrate dev"
},
"devDependencies": {
"@types/bcrypt": "^5.0.2",
Expand All @@ -14,15 +17,17 @@
"eslint": "^9.13.0",
"pino-pretty": "^11.3.0",
"prettier": "^3.3.3",
"prisma": "^5.21.1",
"tsx": "^4.19.1",
"typescript": "^5.6.3"
},
"dependencies": {
"@prisma/client": "^5.21.1",
"bcrypt": "^5.1.1",
"fastify": "^5.0.0",
"fastify-decorators": "^3.16.1",
"fastify-file-routes": "^1.1.2",
"pg": "^8.13.0",
"pg-ipc": "^1.0.5"
}
}
}
66 changes: 66 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions prisma/migrations/0_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
CREATE TABLE IF NOT EXISTS botguilds (
channel_id TEXT NOT NULL,
guild_id TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS bots (
id TEXT NOT NULL,
owner_id TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS email_verifications (
id TEXT NOT NULL,
token TEXT
);

CREATE TABLE IF NOT EXISTS klines (
user_id TEXT,
ip TEXT,
reason TEXT
);

CREATE TABLE IF NOT EXISTS tokens (
id TEXT NOT NULL,
token TEXT NOT NULL,
seed TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS users (
id TEXT NOT NULL,
username TEXT NOT NULL,
pfp TEXT,
email TEXT,
password TEXT,
activated BOOLEAN DEFAULT false NOT NULL,
role BIGINT DEFAULT 2 NOT NULL,
bot BOOLEAN DEFAULT false NOT NULL
);
33 changes: 33 additions & 0 deletions prisma/migrations/20241108083501_set_unique_fields/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- AlterTable
ALTER TABLE botguilds
ADD PRIMARY KEY (channel_id);

-- AlterTable
ALTER TABLE bots
ADD PRIMARY KEY (id);

-- AlterTable
ALTER TABLE email_verifications
ADD PRIMARY KEY (id);

-- AlterTable
ALTER TABLE klines
ADD COLUMN id SERIAL,
ADD PRIMARY KEY (id);

-- AlterTable
ALTER TABLE tokens
ADD PRIMARY KEY (seed);

-- AlterTable
ALTER TABLE users
ADD PRIMARY KEY (id);

-- CreateIndex
CREATE UNIQUE INDEX "users_username_key" ON "users"("username");

-- CreateIndex
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");

-- CreateIndex
CREATE UNIQUE INDEX "email_verifications_token_key" ON "email_verifications"("token");
27 changes: 27 additions & 0 deletions prisma/migrations/20241109014759_bigint_ids/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- AlterTable
ALTER TABLE botguilds
ALTER COLUMN channel_id TYPE BIGINT USING channel_id::BIGINT,
ALTER COLUMN guild_id TYPE BIGINT USING guild_id::BIGINT;

-- AlterTable
ALTER TABLE bots
ALTER COLUMN id TYPE BIGINT USING id::BIGINT,
ALTER COLUMN owner_id TYPE BIGINT USING owner_id::BIGINT;

-- AlterTable
ALTER TABLE email_verifications
ALTER COLUMN id TYPE BIGINT USING id::BIGINT;

-- AlterTable
ALTER TABLE klines
ALTER COLUMN user_id TYPE BIGINT USING user_id::BIGINT;

-- AlterTable
ALTER TABLE tokens
ALTER COLUMN id TYPE BIGINT USING id::BIGINT;

-- AlterTable
ALTER TABLE users
ALTER COLUMN id TYPE BIGINT USING id::BIGINT,
ALTER COLUMN role TYPE INT USING role::INT;
ALTER TABLE users RENAME COLUMN pfp TO avatar;
5 changes: 5 additions & 0 deletions prisma/migrations/20241109023859_bot_relations/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- AddForeignKey
ALTER TABLE "bots" ADD CONSTRAINT "bots_owner_id_fkey" FOREIGN KEY ("owner_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "bots" ADD CONSTRAINT "bots_id_fkey" FOREIGN KEY ("id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
3 changes: 3 additions & 0 deletions prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
64 changes: 64 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("POSTGRES_URL")
}

model User {
id BigInt @id
username String @unique
avatar String?
email String? @unique
password String?
activated Boolean @default(false)
role Int @default(2)
bot Boolean @default(false)
bots Bot[] @relation("bot_owner")
bot_info Bot? @relation("bot_user_connection")
@@map("users")
}

model Bot {
id BigInt @id
owner_id BigInt
owner User @relation("bot_owner", fields: [owner_id], references: [id])
user User @relation("bot_user_connection", fields: [id], references: [id])
@@map("bots")
}

model Token {
id BigInt
token String
seed String @id
@@map("tokens")
}

model EmailVerification {
id BigInt @id
token String? @unique
@@map("email_verifications")
}

model KLine {
id Int @id @default(autoincrement())
user_id BigInt?
ip String?
reason String?
@@map("klines")
}

// Only used for the bot, not really needed in the backend
model BotGuild {
channel_id BigInt @id
guild_id BigInt
@@map("botguilds")
}
Loading

0 comments on commit 0e0bedc

Please sign in to comment.