Skip to content

Commit

Permalink
feat(database): do database migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Jan 11, 2024
1 parent 7f5ebf1 commit ad9143d
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 14 deletions.
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@animegarden/scraper": "workspace:*",
"@animegarden/database": "workspace:*",
"@animegarden/scraper": "workspace:*",
"@breadc/color": "^0.9.7",
"animegarden": "workspace:*",
"breadc": "^0.9.7",
"dotenv": "^16.3.1",
"fs-extra": "^11.2.0",
"simptrad": "^0.1.0",
"undici": "^6.3.0",
Expand Down
12 changes: 12 additions & 0 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@ cli
}
});

cli
.command('database migrate', 'Migrate database')
.alias('db migrate')
.action(async () => {
// TODO: pass options
await import('dotenv/config');
const { connectDatabase, migrateDatabase } = await import('@animegarden/database');
const { connection, db } = connectDatabase(`postgres://root:[email protected]:5432/animegarden`);
await migrateDatabase(db);
await connection.end();
});

cli.run(process.argv.slice(2)).catch((err) => console.error(err));
8 changes: 8 additions & 0 deletions packages/database/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@

The database for [AnimeGarden](https://garden.onekuma.cn/).

## Local Development

Start dev postgres and redis with `docker-compose.yml`

```bash
docker compose up --file packages/database/docker-compose.yml
```

## Credits

+ [動漫花園](https://share.dmhy.org/)
Expand Down
29 changes: 29 additions & 0 deletions packages/database/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: '3.9'

services:
postgresql:
image: postgres
container_name: animegarden_postgres
restart: always
ports:
- '5432:5432'
# volumes:
# - ./.animegarden/database:/var/lib/postgresql/data
environment:
TZ: Asia/Shanghai
PGTZ: Asia/Shanghai
POSTGRES_DB: animegarden
POSTGRES_USER: root
POSTGRES_PASSWORD: example
# postgres://root:[email protected]:5432/animegarden

redis:
image: redis
container_name: animegarden_redis
restart: always
ports:
- '6379:6379'
# volumes:
# - ./.animegarden/redis:/data
environment:
TZ: Asia/Shanghai
10 changes: 2 additions & 8 deletions packages/database/drizzle.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ import type { Config } from 'drizzle-kit';
import 'dotenv/config';

export default {
schema: './src/schema/',
out: './drizzle',
driver: 'pg',
dbCredentials: {
host: process.env.DB_HOST!,
database: process.env.DB_NAME!,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD
}
out: './drizzle',
schema: './src/schema/'
} satisfies Config;
7 changes: 5 additions & 2 deletions packages/database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"files": [
"dist"
"dist",
"drizzle"
],
"scripts": {
"build": "unbuild",
Expand All @@ -44,10 +45,12 @@
},
"dependencies": {
"animegarden": "workspace:*",
"dotenv": "^16.3.1",
"drizzle-orm": "^0.29.3",
"postgres": "^3.4.3"
},
"devDependencies": {
"dotenv": "^16.3.1"
},
"engines": {
"node": ">=v18.16.0"
}
Expand Down
16 changes: 16 additions & 0 deletions packages/database/src/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import postgres from 'postgres';
import { drizzle } from 'drizzle-orm/postgres-js';

import { users } from './schema/user';
import { teams } from './schema/team';
import { resources } from './schema/resource';

export interface DatabaseConnectionConfig extends postgres.Options<{}> {}

export const connectDatabase = (...args: Parameters<typeof postgres>) => {
const queryClient = postgres(...args);
return {
connection: queryClient,
db: drizzle(queryClient, { schema: { resources, users, teams } })
};
};
4 changes: 4 additions & 0 deletions packages/database/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export * from './schema';

export * from './connection';

export * from './migrate';
6 changes: 6 additions & 0 deletions packages/database/src/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { migrate } from 'drizzle-orm/postgres-js/migrator';

export async function migrateDatabase(db: Parameters<typeof migrate>[0]) {
const migrationsFolder = new URL('../drizzle', import.meta.url).pathname;
await migrate(db, { migrationsFolder });
}
1 change: 1 addition & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@animegarden/database": "workspace:*",
"@animegarden/scraper": "workspace:*",
"animegarden": "workspace:*",
"dotenv": "^16.3.1",
"simptrad": "^0.1.0",
"zod": "^3.22.4"
},
Expand Down
13 changes: 10 additions & 3 deletions pnpm-lock.yaml

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

3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"@animegarden/scraper": [
"./packages/scraper/src/index.ts"
],
"@animegarden/database": [
"./packages/database/src/index.ts"
],
"@animegarden/cli": [
"./packages/cli/src/index.ts"
]
Expand Down

0 comments on commit ad9143d

Please sign in to comment.