diff --git a/apps/backend/drizzle.config.ts b/apps/backend/drizzle.config.ts new file mode 100644 index 000000000..a07528dc2 --- /dev/null +++ b/apps/backend/drizzle.config.ts @@ -0,0 +1,7 @@ +import { defineConfig } from "drizzle-kit"; + +export default defineConfig({ + dialect: "postgresql", + schema: "./src/db/schema/index.ts", + out: "./drizzle", +}); \ No newline at end of file diff --git a/apps/backend/package.json b/apps/backend/package.json index aaa80ea20..b83330f77 100644 --- a/apps/backend/package.json +++ b/apps/backend/package.json @@ -7,7 +7,9 @@ "build": "node scripts/build.mjs", "start": "npm run dev", "format": "prettier --write src", - "lint": "eslint --fix src" + "lint": "eslint --fix src", + "generate": "drizzle-kit generate", + "migrate": "tsx scripts/migrate.ts" }, "dependencies": { "@packages/antalmanac-types": "workspace:**", diff --git a/apps/backend/scripts/migrate.js b/apps/backend/scripts/migrate.js deleted file mode 100644 index 5ea6e9951..000000000 --- a/apps/backend/scripts/migrate.js +++ /dev/null @@ -1,20 +0,0 @@ -import { DynamoDBClient, DescribeTableCommand } from '@aws-sdk/client-dynamodb'; // ES Modules import -import env from '../src/env'; - -async function main() { - const client = new DynamoDBClient({ - region: env.AWS_REGION, - }); - - const input = { - // DescribeTableInput - TableName: env.USERDATA_TABLE_NAME, // required - }; - - const command = new DescribeTableCommand(input); - const response = await client.send(command); - - console.log(response); -} - -main(); diff --git a/apps/backend/scripts/migrate.ts b/apps/backend/scripts/migrate.ts new file mode 100644 index 000000000..6b372244d --- /dev/null +++ b/apps/backend/scripts/migrate.ts @@ -0,0 +1,45 @@ +/** + * To run this script, run "pnpm run migrate". + */ + +import { drizzle } from 'drizzle-orm/postgres-js'; +import { migrate } from 'drizzle-orm/postgres-js/migrator'; + +import { ddbClient } from '../src/db/ddb.ts'; +import { db, client } from '../src/db/index.ts'; +import { user } from '../src/db/schema/index.ts'; + +/** + * Migrates the current drizzle schema to the PostgreSQL database associated + * with the drizzle client. Before migrating, run "pnpm run generate" to generate + * the migration files, and make sure the PostgreSQL database exists beforehand. + */ +async function migrateToPostgres() { + await migrate(drizzle(client), { + migrationsFolder: './drizzle' + }); +} + +/** + * Migrates user data from DynamoDB to the PostgreSQL database associated + * with the drizzle client. + */ +async function insertUsersToPostgres() { + const userIds = await ddbClient.getUserIds(); + const usersToInsert = userIds.map((userId) => ({ id: userId })); + await db.insert(user).values(usersToInsert); + console.log(await db.query.user.findMany()); +} + +async function main() { + try { + await migrateToPostgres(); + await insertUsersToPostgres(); + } catch (error) { + console.log(error); + } finally { + await client.end(); + } +} + +main(); diff --git a/apps/backend/src/db/ddb.ts b/apps/backend/src/db/ddb.ts index b6f7a2ee4..03bb9e271 100644 --- a/apps/backend/src/db/ddb.ts +++ b/apps/backend/src/db/ddb.ts @@ -137,7 +137,6 @@ class DDBClient>> { } async getUserData(id: string) { - this.migrate(); return (await ddbClient.get('id', id))?.userData; } @@ -175,7 +174,7 @@ class DDBClient>> { } - async migrate () { + async getUserIds () { const params = { TableName: this.tableName, } @@ -195,7 +194,7 @@ class DDBClient>> { } } while (typeof items.LastEvaluatedKey !== 'undefined'); - console.log(scanResults); + return scanResults; } } diff --git a/apps/backend/src/db/index.ts b/apps/backend/src/db/index.ts index fda86503b..65a724625 100644 --- a/apps/backend/src/db/index.ts +++ b/apps/backend/src/db/index.ts @@ -1,6 +1,7 @@ import { drizzle } from 'drizzle-orm/postgres-js'; import postgres from 'postgres'; +import * as schema from './schema/index.js'; export const client = postgres('postgres://postgres:postgres@localhost:5432/antalmanac'); -export const db = drizzle(client); +export const db = drizzle(client, { schema });