From 70b0dac3c552d43eb8b31779518795b3c667c90e Mon Sep 17 00:00:00 2001 From: Sawangg Date: Fri, 10 Nov 2023 21:01:18 +0100 Subject: [PATCH 1/3] core: Added database schemas --- drizzle.config.ts | 6 ++-- package.json | 2 -- pnpm-lock.yaml | 8 ------ src/db/index.ts | 4 --- src/db/schema.ts | 58 ++++++++++++++++++++++++++++++++++++++ src/db/schemas/messages.ts | 9 ------ src/env.mjs | 2 +- 7 files changed, 62 insertions(+), 27 deletions(-) create mode 100644 src/db/schema.ts delete mode 100644 src/db/schemas/messages.ts diff --git a/drizzle.config.ts b/drizzle.config.ts index 76202c3..0a061b8 100644 --- a/drizzle.config.ts +++ b/drizzle.config.ts @@ -1,11 +1,11 @@ -import "dotenv/config"; import type { Config } from "drizzle-kit"; +import { env } from "@src/env.mjs"; export default { - schema: "./src/db/schemas/*", + schema: "./src/db/schema.ts", out: "./src/db/migrations", driver: "pg", dbCredentials: { - connectionString: process.env.DB_URL!, + connectionString: env.DB_URL, }, } satisfies Config; diff --git a/package.json b/package.json index 1a5c7e3..b664634 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,6 @@ "unlighthouse:dev": "pnpm dlx unlighthouse --site localhost:3000", "db:push": "drizzle-kit push:pg", "db:pull": "drizzle-kit introspect:pg", - "db:drop": "drizzle-kit drop", "db:studio": "drizzle-kit studio --host 127.0.0.1 --port 3001" }, "dependencies": { @@ -45,7 +44,6 @@ "@typescript-eslint/eslint-plugin": "^6.10.0", "@typescript-eslint/parser": "^6.10.0", "autoprefixer": "^10.4.16", - "dotenv": "^16.3.1", "drizzle-kit": "^0.20.0", "eslint": "^8.53.0", "eslint-config-next": "^14.0.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 16ec935..86e70e8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -88,9 +88,6 @@ devDependencies: autoprefixer: specifier: ^10.4.16 version: 10.4.16(postcss@8.4.31) - dotenv: - specifier: ^16.3.1 - version: 16.3.1 drizzle-kit: specifier: ^0.20.0 version: 0.20.0 @@ -1983,11 +1980,6 @@ packages: esutils: 2.0.3 dev: true - /dotenv@16.3.1: - resolution: {integrity: sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==} - engines: {node: '>=12'} - dev: true - /dreamopt@0.8.0: resolution: {integrity: sha512-vyJTp8+mC+G+5dfgsY+r3ckxlz+QMX40VjPQsZc5gxVAxLmi64TBoVkP54A/pRAXMXsbu2GMMBrZPxNv23waMg==} engines: {node: '>=0.4.0'} diff --git a/src/db/index.ts b/src/db/index.ts index 4d1d663..7f059b6 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -1,10 +1,6 @@ import { drizzle, type PostgresJsDatabase } from "drizzle-orm/postgres-js"; -// import { migrate } from "drizzle-orm/postgres-js/migrator"; import postgres from "postgres"; import { env } from "@src/env.mjs"; -// const migrationClient = postgres(env.DB_URL, { max: 1 }); -// void migrate(drizzle(migrationClient), { migrationsFolder: "./migrations" }); - const queryClient = postgres(env.DB_URL); export const db: PostgresJsDatabase = drizzle(queryClient); diff --git a/src/db/schema.ts b/src/db/schema.ts new file mode 100644 index 0000000..8200ebf --- /dev/null +++ b/src/db/schema.ts @@ -0,0 +1,58 @@ +import { boolean, index, integer, pgEnum, pgTable, serial, varchar } from "drizzle-orm/pg-core"; + +// Users table +export const users = pgTable("users", { + id: serial("id").primaryKey().notNull(), + email: varchar("email").notNull(), + password: varchar("password").notNull(), + admin: boolean("admin").default(false).notNull(), +}); + +export const difficultyEnum = pgEnum("difficulty", ["easy", "medium", "hard"]); +export const typeEnum = pgEnum("type", ["qcm", "program", "personality"]); + +// Questions table +export const questions = pgTable("questions", { + id: serial("id").primaryKey().notNull(), + statement: varchar("statement").notNull(), + difficulty: difficultyEnum("difficulty").notNull(), + type: typeEnum("type").notNull(), +}); + +// Game table +export const games = pgTable( + "games", + { + id: serial("id").primaryKey().notNull(), + userId: integer("user_id") + .references(() => users.id) + .notNull(), + progress: integer("progress").default(0).notNull(), // -1 if done else X where X is the question number + }, + (table) => { + return { + userIdx: index("user_idx").on(table.userId), + }; + }, +); + +// Answers table +export const answers = pgTable( + "answers", + { + id: serial("id").primaryKey().notNull(), + answer: varchar("answer").notNull(), + grade: integer("grade"), // Between 0 and 100 & null if personality question + questionId: integer("question_id") + .references(() => questions.id) + .notNull(), + gameId: integer("game_id") + .references(() => games.id) + .notNull(), + }, + (table) => { + return { + gameId: index("game_idx").on(table.gameId), + }; + }, +); diff --git a/src/db/schemas/messages.ts b/src/db/schemas/messages.ts deleted file mode 100644 index 4634da7..0000000 --- a/src/db/schemas/messages.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { pgTable, serial, varchar } from "drizzle-orm/pg-core"; - -export const messages = pgTable("messages", { - id: serial("id").primaryKey().notNull(), - name: varchar("name").notNull(), - email: varchar("email").notNull(), - company: varchar("company"), - message: varchar("message").notNull(), -}); diff --git a/src/env.mjs b/src/env.mjs index 18bda60..20df734 100644 --- a/src/env.mjs +++ b/src/env.mjs @@ -3,7 +3,7 @@ import { z } from "zod"; export const env = createEnv({ server: { - NODE_ENV: z.string(), + NODE_ENV: z.enum(["development", "test", "production"]).default("development"), DB_URL: z.string(), }, client: {}, From bb25c3969990dd3e951e5016f4e1b4aacdf62977 Mon Sep 17 00:00:00 2001 From: Sawangg Date: Fri, 10 Nov 2023 21:04:21 +0100 Subject: [PATCH 2/3] fix: Fix github actions --- .github/workflows/format.yml | 2 +- .github/workflows/lint.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 03e37f8..c6aef05 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -12,7 +12,7 @@ jobs: - uses: actions/setup-node@v3 with: node-version: latest - - run: npm ci + - run: npm i - run: npm run format - name: Commit changes uses: stefanzweifel/git-auto-commit-action@v4 diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 5152117..e64f4e3 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -12,5 +12,5 @@ jobs: - uses: actions/setup-node@v3 with: node-version: latest - - run: npm ci + - run: npm i - run: npm run lint From c3b92a1773e0ef2b4b5d131ef9ec6162276971c1 Mon Sep 17 00:00:00 2001 From: Sawangg Date: Fri, 10 Nov 2023 21:20:39 +0100 Subject: [PATCH 3/3] chore: Added pnpm in github actions --- .github/workflows/format.yml | 44 ++++++++++++++++++++++++++++-------- .github/workflows/lint.yml | 41 ++++++++++++++++++++++++++++----- package.json | 3 ++- src/env.mjs | 1 + 4 files changed, 72 insertions(+), 17 deletions(-) diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index c6aef05..fcd376f 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -1,21 +1,45 @@ name: Format on: pull_request: - branches: [master] + branches: ["*"] + +env: + DB_URL: fake + jobs: format: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - name: Checkout repo + uses: actions/checkout@v3 with: ref: ${{ github.head_ref }} - - uses: actions/setup-node@v3 + + - name: Setup Node 20 + uses: actions/setup-node@v3 with: - node-version: latest - - run: npm i - - run: npm run format - - name: Commit changes - uses: stefanzweifel/git-auto-commit-action@v4 + node-version: 20 + + - name: Setup pnpm + uses: pnpm/action-setup@v2.2.4 with: - commit_message: Apply formatting changes - branch: ${{ github.head_ref }} + version: 8.6.1 + + - name: Get pnpm store directory + id: pnpm-cache + run: | + echo "pnpm_cache_dir=$(pnpm store path)" >> $GITHUB_OUTPUT + + - name: Setup pnpm cache + uses: actions/cache@v3 + with: + path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }} + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} + restore-keys: | + ${{ runner.os }}-pnpm-store- + + - name: Install deps (with cache) + run: pnpm install + + - name: Run Format Check + run: pnpm format:check diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index e64f4e3..ffd7d9f 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -1,16 +1,45 @@ name: Lint on: pull_request: - branches: [master] + branches: ["*"] + +env: + DB_URL: fake + jobs: format: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - name: Checkout repo + uses: actions/checkout@v3 with: ref: ${{ github.head_ref }} - - uses: actions/setup-node@v3 + + - name: Setup Node 20 + uses: actions/setup-node@v3 with: - node-version: latest - - run: npm i - - run: npm run lint + node-version: 20 + + - name: Setup pnpm + uses: pnpm/action-setup@v2.2.4 + with: + version: 8.6.1 + + - name: Get pnpm store directory + id: pnpm-cache + run: | + echo "pnpm_cache_dir=$(pnpm store path)" >> $GITHUB_OUTPUT + + - name: Setup pnpm cache + uses: actions/cache@v3 + with: + path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }} + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} + restore-keys: | + ${{ runner.os }}-pnpm-store- + + - name: Install deps (with cache) + run: pnpm install + + - name: Run lint + run: pnpm lint diff --git a/package.json b/package.json index b664634..4f76c79 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "build": "next build", "start": "next start", "lint": "next lint", - "format": "prettier --config ./prettier.config.cjs --write .", + "format:fix": "prettier --write \"**/*.{ts,tsx,mdx}\" --cache", + "format:check": "prettier --check \"**/*.{ts,tsx,mdx}\" --cache", "unlighthouse:dev": "pnpm dlx unlighthouse --site localhost:3000", "db:push": "drizzle-kit push:pg", "db:pull": "drizzle-kit introspect:pg", diff --git a/src/env.mjs b/src/env.mjs index 20df734..30d53b3 100644 --- a/src/env.mjs +++ b/src/env.mjs @@ -11,4 +11,5 @@ export const env = createEnv({ NODE_ENV: process.env.NODE_ENV, DB_URL: process.env.DB_URL, }, + skipValidation: !!process.env.SKIP_ENV_VALIDATION, });