Skip to content

Commit

Permalink
feat(cli): pass migrate database options
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Jan 11, 2024
1 parent a659c6a commit 590c133
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 17 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ You can check more filter conditions [FilterOptions](https://github.com/yjl9903/

Follow [CONTRIBUTING.md](./CONTRIBUTING.md) to setup the environment and start developing.

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

```bash
docker compose --file=docker-compose.dev.yml up
```

Then migrate the dev postgres database.

```bash
pnpm animegarden db migrate --uri "postgres://root:example@0.0.0.0:5432/animegarden"
```

## Related Projects

+ [AnimeSpace](https://github.com/yjl9903/AnimeSpace): Keep following your favourite anime
Expand Down
File renamed without changes.
36 changes: 32 additions & 4 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,40 @@ cli
cli
.command('database migrate', 'Migrate database')
.alias('db migrate')
.action(async () => {
// TODO: pass options
.option('--uri <string>', 'Postgres database connection URI')
.option('--host <string>', 'Postgres database host')
.option('--port <string>', 'Postgres database port')
.option('--username <string>', 'Postgres database username')
.option('--password <string>', 'Postgres database password')
.option('--database <string>', 'Postgres database name')
.action(async (options) => {
await import('dotenv/config');

const uri = options.uri ?? process.env.POSTGRES_URI ?? process.env.POSTGRES_CONNECTION_STRING;
const host = options.host ?? process.env.POSTGRES_HOST;
const port = options.port ?? process.env.POSTGRES_PORT;
const username = options.username ?? process.env.POSTGRES_USERNAME;
const password = options.password ?? process.env.POSTGRES_PASSWORD;
const databaseName = options.database ?? process.env.POSTGRES_DATABASE ?? 'animegarden';

if (!uri && !host) {
return;
}

const { connectDatabase, migrateDatabase } = await import('@animegarden/database');
const { connection, db } = connectDatabase(`postgres://root:example@0.0.0.0:5432/animegarden`);
await migrateDatabase(db);

const { connection, database } = uri
? connectDatabase(uri, { max: 1 })
: connectDatabase({
host,
port: port ? +port : undefined,
username,
password,
database: databaseName,
max: 1
});

await migrateDatabase(database);
await connection.end();
});

Expand Down
8 changes: 0 additions & 8 deletions packages/database/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@

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
11 changes: 7 additions & 4 deletions packages/database/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import { resources } from './schema/resource';

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

export const connectDatabase = (...args: Parameters<typeof postgres>) => {
const queryClient = postgres(...args);
export function connectDatabase(
uri: string | postgres.Options<{}>,
options?: postgres.Options<{}>
) {
const queryClient = typeof uri === 'string' ? postgres(uri, options) : postgres(uri);
return {
connection: queryClient,
db: drizzle(queryClient, { schema: { resources, users, teams } })
database: drizzle(queryClient, { schema: { resources, users, teams } })
};
};
}
4 changes: 3 additions & 1 deletion packages/database/src/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { fileURLToPath } from 'node:url';

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;
const migrationsFolder = fileURLToPath(new URL('../drizzle', import.meta.url));
await migrate(db, { migrationsFolder });
}

0 comments on commit 590c133

Please sign in to comment.