Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Added database schemas #46

Merged
merged 3 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -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 ci
- 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
41 changes: 35 additions & 6 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -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 ci
- 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
6 changes: 3 additions & 3 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
"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",
"db:drop": "drizzle-kit drop",
"db:studio": "drizzle-kit studio --host 127.0.0.1 --port 3001"
},
"dependencies": {
Expand Down Expand Up @@ -45,7 +45,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",
Expand Down
8 changes: 0 additions & 8 deletions pnpm-lock.yaml

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

4 changes: 0 additions & 4 deletions src/db/index.ts
Original file line number Diff line number Diff line change
@@ -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);
58 changes: 58 additions & 0 deletions src/db/schema.ts
Original file line number Diff line number Diff line change
@@ -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),
};
},
);
9 changes: 0 additions & 9 deletions src/db/schemas/messages.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ 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: {},
runtimeEnv: {
NODE_ENV: process.env.NODE_ENV,
DB_URL: process.env.DB_URL,
},
skipValidation: !!process.env.SKIP_ENV_VALIDATION,
});