diff --git a/packages/db/moon.yml b/packages/db/moon.yml index 59e3dd6f..ce043629 100644 --- a/packages/db/moon.yml +++ b/packages/db/moon.yml @@ -5,4 +5,6 @@ tasks: build: options: mergeArgs: "replace" - args: "src/**/*.ts --clean --dts --format esm --tsconfig tsconfig.build.json" + args: "src/index.ts --clean --dts --format esm --tsconfig tsconfig.build.json" + gen.model: + command: "kysely-codegen" diff --git a/packages/db/package.json b/packages/db/package.json index 7489e48a..0020876f 100644 --- a/packages/db/package.json +++ b/packages/db/package.json @@ -21,6 +21,7 @@ "kysely": "0.27.3" }, "devDependencies": { + "pg": "^8.11.5", "tsup": "8.0.2" }, "publishConfig": { diff --git a/packages/db/src/migrations/20240504T110341.739Z-create-users-table.ts b/packages/db/src/migrations/20240504T110341.739Z-create-users-table.ts index 8d49d38b..b7519425 100644 --- a/packages/db/src/migrations/20240504T110341.739Z-create-users-table.ts +++ b/packages/db/src/migrations/20240504T110341.739Z-create-users-table.ts @@ -1,8 +1,9 @@ import type { Kysely } from "kysely"; import { withTimestamps } from "../utils"; import { sql } from "kysely"; +import type { DB } from "kysely-codegen"; -export async function up(db: Kysely) { +export async function up(db: Kysely) { await db.schema .createType("user_status") .asEnum(["NEWBIE", "VERIFIED", "BLACKLIST", "INACTIVE", "ACTIVE", "CLOSED"]) diff --git a/packages/db/src/migrations/20240504T111004.750Z-create-tenant-table.ts b/packages/db/src/migrations/20240504T111004.750Z-create-tenant-table.ts index 51e12a7b..567ac943 100644 --- a/packages/db/src/migrations/20240504T111004.750Z-create-tenant-table.ts +++ b/packages/db/src/migrations/20240504T111004.750Z-create-tenant-table.ts @@ -8,7 +8,7 @@ export async function up(db: Kysely) { .addColumn("name", "varchar(255)", (col) => col.notNull()) .addColumn("slug", "varchar(255)", (col) => col.unique().notNull()) .addColumn("description", "text", (col) => col.notNull().defaultTo("")) - .addColumn("verified", "boolean", (col) => col.notNull().defaultTo(false)) + .addColumn("is_verified", "boolean", (col) => col.notNull().defaultTo(false)) .addColumn("owner_id", "text", (col) => col.references("users.id").onDelete("set null")) .addColumn("metadata", "jsonb") .$call(withTimestamps) diff --git a/packages/db/src/seeds/20240510T093124.653Z-seed-meta-schema.ts b/packages/db/src/seeds/20240510T093124.653Z-seed-meta-schema.ts new file mode 100644 index 00000000..8834afe8 --- /dev/null +++ b/packages/db/src/seeds/20240510T093124.653Z-seed-meta-schema.ts @@ -0,0 +1,7 @@ +import type { Kysely } from "kysely"; + +export async function up(db: Kysely) { + await db.insertInto("meta_schemas").values({}); +} + +export async function down(db: Kysely) {} diff --git a/packages/db/src/seeds/index.ts b/packages/db/src/seeds/index.ts deleted file mode 100644 index e69de29b..00000000 diff --git a/packages/db/src/seeds/meta-schema.ts b/packages/db/src/seeds/meta-schema.ts deleted file mode 100644 index e69de29b..00000000 diff --git a/scripts/generateMigrateFileAPI.ts b/scripts/generateMigrateFileAPI.ts index b3f4e2f1..56de741f 100644 --- a/scripts/generateMigrateFileAPI.ts +++ b/scripts/generateMigrateFileAPI.ts @@ -3,12 +3,24 @@ import fs from "node:fs"; import path from "node:path"; +const fileName = process.argv[2]; +const folder = process.argv[3] || "migrations"; + +if (!fileName) { + console.error("Please provide a filename"); + process.exit(1); +} + +const filePath = generateFilePath(fileName); +createFile(filePath); +writeFileContent(filePath); + function generateFilePath(name: string) { const currentDate = new Date(); const isoDate = currentDate.toISOString(); const fileName = `${isoDate.replaceAll(":", "").replaceAll("-", "")}-${name}.ts`; - return path.join(process.cwd(), "packages/db/src/migrations", fileName); + return path.join(process.cwd(), "packages/db/src", folder, fileName); } function createFile(fileName: string) { @@ -23,19 +35,11 @@ function createFile(fileName: string) { async function writeFileContent( filePath: string, - content = `import { Kysely } from "kysely"; + content = ` +import type { Kysely } from "kysely"; export async function up(db: Kysely) {} export async function down(db: Kysely) {}`, ) { + // @ts-expect-error await Bun.write(filePath, content, { encoding: "utf-8" }); } - -const fileName = process.argv[2]; -if (!fileName) { - console.error("Please provide a filename"); - process.exit(1); -} - -const filePath = generateFilePath(fileName); -createFile(filePath); -writeFileContent(filePath);