initial schema and migrations

This commit is contained in:
2023-02-28 15:29:49 +01:00
parent 5f327c0272
commit b0a40d0971
9 changed files with 403 additions and 6 deletions
+43
View File
@@ -0,0 +1,43 @@
import { Kysely, PostgresDialect } from "kysely";
import { Pool } from "pg";
import migrateToLatest from "./migrations";
import Database from "./model";
migrateToLatest();
// You'd create one of these when you start your app.
const db = new Kysely<Database>({
// Use MysqlDialect for MySQL and SqliteDialect for SQLite.
dialect: new PostgresDialect({
pool: new Pool({
host: process.env.POSTGRES_HOST,
database: process.env.POSTGRES_DATABASE,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
}),
}),
});
/*
async function demo() {
const { id } = await db
.insertInto("person")
.values({ first_name: "Jennifer", gender: "female" })
.returning("id")
.executeTakeFirstOrThrow();
await db
.insertInto("pet")
.values({ name: "Catto", species: "cat", owner_id: id })
.execute();
const person = await db
.selectFrom("person")
.innerJoin("pet", "pet.owner_id", "person.id")
.select(["first_name", "pet.name as pet_name"])
.where("person.id", "=", id)
.executeTakeFirst();
if (person) {
person.pet_name;
}
}
*/
export default "e";
+53
View File
@@ -0,0 +1,53 @@
import { promises as fs } from "fs";
import {
FileMigrationProvider,
Kysely,
Migrator,
PostgresDialect,
} from "kysely";
import * as path from "path";
import { Pool } from "pg";
import Database from "./model";
console.log(process.env.POSTGRES_HOST);
async function migrateToLatest() {
const db = new Kysely<Database>({
dialect: new PostgresDialect({
pool: new Pool({
host: process.env.POSTGRES_HOST,
database: process.env.POSTGRES_DATABASE,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
}),
}),
});
const migrator = new Migrator({
db,
provider: new FileMigrationProvider({
fs,
path,
migrationFolder: path.join(__dirname, "./migrations"),
}),
});
const { error, results } = await migrator.migrateToLatest();
results?.forEach((it) => {
if (it.status === "Success") {
console.log(`migration "${it.migrationName}" was executed successfully`);
} else if (it.status === "Error") {
console.error(`failed to execute migration "${it.migrationName}"`);
}
});
if (error) {
console.error("failed to migrate");
console.error(error);
process.exit(1);
}
await db.destroy();
}
export default migrateToLatest;
+76
View File
@@ -0,0 +1,76 @@
import { Kysely, sql } from "kysely";
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.createTable("entity")
.addColumn("id", "integer", (col) => col.unique().primaryKey())
.addColumn("name", "varchar")
.execute();
await db.schema
.createTable("weapon")
.addColumn("id", "varchar", (col) => col.unique().primaryKey())
.addColumn("name", "varchar")
.addColumn("description", "varchar")
.addColumn("image", "varchar")
.execute();
await db.schema
.createTable("map")
.addColumn("id", "varchar", (col) => col.unique().primaryKey())
.addColumn("name", "varchar")
.addColumn("description", "varchar")
.addColumn("image", "varchar")
.execute();
await db.schema
.createTable("server")
.addColumn("name", "varchar", (col) => col.unique().primaryKey())
.addColumn("description", "varchar")
.addColumn("token", "varchar")
.execute();
await db.schema
.createTable("kill")
.addColumn("id", "serial", (col) => col.primaryKey())
.addColumn("server", "varchar", (col) =>
col.references("server.name").notNull()
)
.addColumn("attacker", "integer", (col) =>
col.references("entity.id").notNull()
)
.addColumn("victim", "integer", (col) =>
col.references("entity.id").notNull()
)
.addColumn("weapon", "varchar", (col) =>
col.references("weapon.id").notNull()
)
.addColumn("map", "varchar", (col) => col.references("map.id").notNull())
.addColumn("distance", "integer", (col) => col.notNull())
.addColumn("date", "varchar", (col) => col.defaultTo(sql`now()`).notNull())
.execute();
/*await db.schema
.createTable("pet")
.addColumn("id", "serial", (col) => col.primaryKey())
.addColumn("name", "varchar", (col) => col.notNull().unique())
.addColumn("owner_id", "integer", (col) =>
col.references("person.id").onDelete("cascade").notNull()
)
.addColumn("species", "varchar", (col) => col.notNull())
.execute();*/
/*
await db.schema
.createIndex("kill")
.on("pet")
.column("owner_id")
.execute();*/
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema.dropTable("kill").execute();
await db.schema.dropTable("entity").execute();
await db.schema.dropTable("weapon").execute();
await db.schema.dropTable("map").execute();
await db.schema.dropTable("server").execute();
}
+43
View File
@@ -0,0 +1,43 @@
import { Generated } from "kysely";
interface KillTable {
id: Generated<number>;
server: string;
attacker: number;
victim: number;
weapon: string;
map: string;
distance: number;
date: Date;
}
interface EntityTable {
id: number;
name: string;
}
interface WeaponTable {
id: string;
name: string;
description: string;
image: string;
}
interface MapTable {
id: string;
name: string;
description: string;
image: string;
}
interface ServerTable {
id: string;
name: string;
description: string;
}
interface Database {
kill: KillTable;
entity: EntityTable;
weapon: WeaponTable;
maps: MapTable;
server: ServerTable;
}
export default Database;
+6
View File
@@ -1,7 +1,13 @@
import * as dotenv from "dotenv";
dotenv.config();
import express from "express";
import client from "./client";
import db from "./db/db";
import server from "./server";
console.log("dotenv");
console.log(db);
const app = express();
const port = 3000;