🎉 initial commit

This commit is contained in:
Robert Kunze
2026-01-12 17:44:56 +01:00
commit 7fb0512f65
8 changed files with 822 additions and 0 deletions

104
dbmorph.ts Normal file
View File

@@ -0,0 +1,104 @@
if (Deno.args[0] == "--help" || Deno.args[0] == "-h") {
console.log(`dbmorph.ts dbml|migrate|seed|make[:seed] [options]
=================
dbml - convert all dbml files to corresponding SQL files
migrate - runs all open migrations
seed - runs all (or the given) seeds
make - creates a template for a new migration
make:seed - creates a new template for seeding data
`);
Deno.exit(0);
}
if (Deno.args[0] == "dbml") {
const dbml = await import("./dbml.ts");
dbml.default(Deno.args.slice(1));
Deno.exit(0);
}
if (Deno.args[0] == "migrate") {
const migrate = await import("./migrate.ts");
await migrate.default(Deno.args.slice(1));
Deno.exit(0);
}
if (Deno.args[0] == "seed") {
const seed = await import("./seed.ts");
await seed.default(Deno.args.slice(1));
Deno.exit(0);
}
if (Deno.args[0] == "make") {
Deno.mkdirSync("./db/migrations", { recursive: true });
if (!Deno.args[1]) {
console.log("Please provide a name for the migration.");
Deno.exit(1);
}
let suffix = ".sql";
const dt = new Date().toISOString();
const upFile = `db/migrations/${dt}_up_${Deno.args[1]}`;
const downFile = `db/migrations/${dt}_down_${Deno.args[1]}`;
if (!Deno.args[2]) {
Deno.args[2] = "-t";
Deno.args[3] = "sql";
}
if (Deno.args[2] == "-t") {
if (Deno.args[3] == "ts") {
suffix = ".ts";
Deno.writeTextFileSync(
upFile + suffix,
`export default async function(sql) {\n\t// run your sql queries like so: await sql${"`"}SELECT * FROM mytable${"`"};\n}`,
);
Deno.writeTextFileSync(
downFile + suffix,
`export default async function(sql) {\n\t// run your sql queries like so: await sql${"`"}SELECT * FROM mytable${"`"};\n}`,
);
} else if (Deno.args[3] == "sql") {
// suffix is already correct
Deno.writeTextFileSync(upFile + suffix, "CREATE TABLE mytable ();");
Deno.writeTextFileSync(downFile + suffix, "DROP TABLE mytable;");
} else {
console.log("Unknown migration type: ", Deno.args[3]);
Deno.exit(1);
}
}
Deno.exit(0);
}
if (Deno.args[0] == "make:seed") {
Deno.mkdirSync("./db/seeds", { recursive: true });
if (!Deno.args[1]) {
console.log("Please provide a name for the seed");
Deno.exit(1);
}
const upFile = `db/seeds/${Deno.args[1]}`;
if (!Deno.args[2]) {
console.log("Assuming SQL seed...");
Deno.writeTextFileSync(upFile + ".sql", "INSERT INTO () VALUES ();");
} else if (Deno.args[2] == "-t") {
if (Deno.args[3] == "ts") {
Deno.writeTextFileSync(
upFile + ".ts",
`export default async function(sql) {\n\t// run your sql queries like so: await sql${"`"}SELECT * FROM mytable${"`"};\n}`,
);
} else if (Deno.args[3] == "sql") {
// suffix is already correct
Deno.writeTextFileSync(
upFile + ".sql",
"INSERT INTO () VALUES ();",
);
} else if (Deno.args[3] == "csv") {
Deno.writeTextFileSync(
upFile + ".csv",
"mytable\r\ncol1,col2,col3\r\nvalue1,value2,value3\r\n\r\nmyothertable\r\ncola,colb,colc\r\n1\r\n2\r\n3",
);
} else {
console.log("Unknown seed type: ", Deno.args[3]);
Deno.exit(1);
}
}
Deno.exit(0);
}