-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-database.ts
More file actions
42 lines (37 loc) · 1.09 KB
/
create-database.ts
File metadata and controls
42 lines (37 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import knex from "knex";
import fs from "node:fs/promises"
await fs.rm("./mydb.sqlite");
const db = knex({
client: "better-sqlite3",
connection: {
filename: "./mydb.sqlite",
},
useNullAsDefault: true,
});
try {
const tables = await db("sqlite_master")
.where("type", "table")
.andWhereNot("name", "sqlite_sequence");
await Promise.all(tables.map((table) => db.schema.dropTable(table.name)));
await db.schema
.createTable("track", (table) => {
table.increments();
table.string("name").unique();
})
.createTable("transition_type", (table) => {
table.increments();
table.string("name").unique();
})
.createTable("transition", (table) => {
table.increments();
table.integer("track_a_id").notNullable().references("track.id");
table.string("type_id").notNullable().references("transition_type.id");
table.integer("track_b_id").notNullable().references("track.id");
table.unique(["track_a_id", "type_id", "track_b_id"]);
table.string("note");
});
} catch (e) {
console.error(e);
} finally {
db.destroy();
}