Skip to content

Commit

Permalink
✨ Configure Drizzle ORM
Browse files Browse the repository at this point in the history
  • Loading branch information
injoonH committed May 14, 2024
1 parent 581385d commit 8fc705e
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DB_HOST=localhost
DB_PORT=3306
DB_USER=root
DB_PASSWORD=
DB_NAME=ara
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
node_modules

.env
.volume
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Ara API

Restful API for Ara, KAIST's official community service
Restful API for Ara, KAIST's official community service.

## Getting Started

Copy the environment variables:

```bash
cp .env.example .env
```

To install dependencies:

```bash
Expand Down
Binary file modified bun.lockb
Binary file not shown.
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
services:
db:
image: mysql:8
ports:
- '${DB_PORT:?err}:3306'
environment:
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD:?err}
- MYSQL_DATABASE=${DB_NAME:?err}
volumes:
- ./.volume/db:/var/lib/mysql
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
16 changes: 16 additions & 0 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineConfig } from 'drizzle-kit'

import { env } from '@/env'

export default defineConfig({
dialect: 'mysql',
schema: './src/db/schema/*',
out: './src/db/migration',
dbCredentials: {
host: env.DB_HOST,
port: env.DB_PORT,
user: env.DB_USER,
password: env.DB_PASSWORD,
database: env.DB_NAME,
},
})
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"type": "module",
"scripts": {
"db:generate": "drizzle-kit generate",
"db:migrate": "drizzle-kit migrate",
"db:studio": "drizzle-kit studio",
"dev": "bun run --hot src/index.ts",
"prepare": "husky"
},
Expand Down
14 changes: 14 additions & 0 deletions src/db/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { drizzle } from 'drizzle-orm/mysql2'
import { createPool } from 'mysql2/promise'

import { env } from '@/env'

const connectionPool = createPool({
host: env.DB_HOST,
port: env.DB_PORT,
user: env.DB_USER,
password: env.DB_PASSWORD,
database: env.DB_NAME,
})

export const db = drizzle(connectionPool)
1 change: 1 addition & 0 deletions src/db/migration/meta/_journal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "version": "6", "dialect": "mysql", "entries": [] }
Empty file added src/db/schema/index.ts
Empty file.
11 changes: 11 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { z } from 'zod'

export const env = z
.object({
DB_HOST: z.string(),
DB_PORT: z.coerce.number(),
DB_USER: z.string(),
DB_PASSWORD: z.string(),
DB_NAME: z.string(),
})
.parse(process.env)

0 comments on commit 8fc705e

Please sign in to comment.