Skip to content

Commit

Permalink
feat(db): update migration db
Browse files Browse the repository at this point in the history
  • Loading branch information
harrytran998 committed May 10, 2024
1 parent 36f156a commit febd402
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 15 deletions.
4 changes: 3 additions & 1 deletion packages/db/moon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"kysely": "0.27.3"
},
"devDependencies": {
"pg": "^8.11.5",
"tsup": "8.0.2"
},
"publishConfig": {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<any>) {
export async function up(db: Kysely<DB>) {
await db.schema
.createType("user_status")
.asEnum(["NEWBIE", "VERIFIED", "BLACKLIST", "INACTIVE", "ACTIVE", "CLOSED"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function up(db: Kysely<any>) {
.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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Kysely } from "kysely";

export async function up(db: Kysely<any>) {
await db.insertInto("meta_schemas").values({});
}

export async function down(db: Kysely<any>) {}
Empty file removed packages/db/src/seeds/index.ts
Empty file.
Empty file.
28 changes: 16 additions & 12 deletions scripts/generateMigrateFileAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<any>) {}
export async function down(db: Kysely<any>) {}`,
) {
// @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);

0 comments on commit febd402

Please sign in to comment.