diff --git a/README.md b/README.md index 8e3a6cd..08b682c 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,8 @@ Login is possible via Username and TOTP. 1. Install the database-system-package you like (e.g. mysql, mariadb, postgresql, sqlite3) 2. Configure type inside src/data-source.ts to run the database-system you like. -3. Set migrationsRun to false and synchronize to true. (Migrations are build to suit mysql) +3. Set migrationsRun to false and synchronize to true for rapid prototyping 4. Building the schema via CLI: - Run `npm run update-database` to build the schema using the migrations without starting the application - Run `npm run synchronize-database` to build the schema without using migrations without starting the application -5. Run `npm run dev` to run inside dev-environment +5. Run `npm run dev` to run inside dev-environment (runs migrations if migrationsRun is set to true) diff --git a/src/migrations/1724317398939-initial.ts b/src/migrations/1724317398939-initial.ts index 25ef8c6..695cd7a 100644 --- a/src/migrations/1724317398939-initial.ts +++ b/src/migrations/1724317398939-initial.ts @@ -1,18 +1,93 @@ -import { MigrationInterface, QueryRunner } from "typeorm"; +import { MigrationInterface, QueryRunner, Table, TableForeignKey } from "typeorm"; export class Initial1724317398939 implements MigrationInterface { - name = 'Initial1724317398939' + name = "Initial1724317398939"; - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(`CREATE TABLE \`user\` (\`id\` int NOT NULL AUTO_INCREMENT, \`mail\` varchar(255) NOT NULL, \`username\` varchar(255) NOT NULL, \`secret\` varchar(255) NOT NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`); - await queryRunner.query(`CREATE TABLE \`refresh\` (\`id\` int NOT NULL AUTO_INCREMENT, \`token\` varchar(255) NOT NULL, \`expiry\` datetime NOT NULL, \`userId\` int NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`); - await queryRunner.query(`ALTER TABLE \`refresh\` ADD CONSTRAINT \`FK_b39e4ed3bfa789758e476870ec2\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); - } + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.createTable( + new Table({ + name: "user", + columns: [ + { + name: "id", + type: "int", + isPrimary: true, + isNullable: false, + isGenerated: true, + generationStrategy: "increment", + }, + { + name: "mail", + type: "varchar", + length: "255", + isNullable: false, + }, + { + name: "username", + type: "varchar", + length: "255", + isNullable: false, + }, + { + name: "secret", + type: "varchar", + length: "255", + isNullable: false, + }, + ], + }), + true + ); - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE \`refresh\` DROP FOREIGN KEY \`FK_b39e4ed3bfa789758e476870ec2\``); - await queryRunner.query(`DROP TABLE \`refresh\``); - await queryRunner.query(`DROP TABLE \`user\``); - } + await queryRunner.createTable( + new Table({ + name: "refresh", + columns: [ + { + name: "id", + type: "int", + isPrimary: true, + isNullable: false, + isGenerated: true, + generationStrategy: "increment", + }, + { + name: "token", + type: "varchar", + length: "255", + isNullable: false, + }, + { + name: "expiry", + type: "datetime", + isNullable: false, + }, + { + name: "userId", + type: "int", + isNullable: true, + }, + ], + }), + true + ); + await queryRunner.createForeignKey( + "refresh", + new TableForeignKey({ + columnNames: ["userId"], + referencedColumnNames: ["id"], + referencedTableName: "user", + onDelete: "No Action", + }) + ); + } + + public async down(queryRunner: QueryRunner): Promise { + const table = await queryRunner.getTable("refresh"); + const foreignKey = table.foreignKeys.find((fk) => fk.columnNames.indexOf("userId") !== -1); + await queryRunner.dropForeignKey("refresh", foreignKey); + await queryRunner.dropTable("refresh"); + await queryRunner.dropTable("user"); + } } diff --git a/src/migrations/1724573307851-refreshPrimaryChange.ts b/src/migrations/1724573307851-refreshPrimaryChange.ts index 5fe722a..a55d194 100644 --- a/src/migrations/1724573307851-refreshPrimaryChange.ts +++ b/src/migrations/1724573307851-refreshPrimaryChange.ts @@ -1,26 +1,25 @@ -import { MigrationInterface, QueryRunner } from "typeorm"; +import { MigrationInterface, QueryRunner, TableColumn } from "typeorm"; export class RefreshPrimaryChange1724573307851 implements MigrationInterface { - name = 'RefreshPrimaryChange1724573307851' + name = "RefreshPrimaryChange1724573307851"; - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE \`refresh\` CHANGE \`id\` \`id\` int NOT NULL`); - await queryRunner.query(`ALTER TABLE \`refresh\` DROP PRIMARY KEY`); - await queryRunner.query(`ALTER TABLE \`refresh\` DROP COLUMN \`id\``); - await queryRunner.query(`ALTER TABLE \`refresh\` ADD PRIMARY KEY (\`token\`, \`userId\`)`); - await queryRunner.query(`ALTER TABLE \`refresh\` DROP FOREIGN KEY \`FK_b39e4ed3bfa789758e476870ec2\``); - await queryRunner.query(`ALTER TABLE \`refresh\` CHANGE \`userId\` \`userId\` int NOT NULL`); - await queryRunner.query(`ALTER TABLE \`refresh\` ADD CONSTRAINT \`FK_b39e4ed3bfa789758e476870ec2\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); - } - - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE \`refresh\` DROP FOREIGN KEY \`FK_b39e4ed3bfa789758e476870ec2\``); - await queryRunner.query(`ALTER TABLE \`refresh\` CHANGE \`userId\` \`userId\` int NULL DEFAULT 'NULL'`); - await queryRunner.query(`ALTER TABLE \`refresh\` ADD CONSTRAINT \`FK_b39e4ed3bfa789758e476870ec2\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); - await queryRunner.query(`ALTER TABLE \`refresh\` DROP PRIMARY KEY`); - await queryRunner.query(`ALTER TABLE \`refresh\` ADD \`id\` int NOT NULL AUTO_INCREMENT`); - await queryRunner.query(`ALTER TABLE \`refresh\` ADD PRIMARY KEY (\`id\`)`); - await queryRunner.query(`ALTER TABLE \`refresh\` CHANGE \`id\` \`id\` int NOT NULL AUTO_INCREMENT`); - } + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.dropColumn("refresh", "id"); + await queryRunner.createPrimaryKey("refresh", ["token", "userId"]); + } + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.dropPrimaryKey("refresh"); + await queryRunner.addColumn( + "refresh", + new TableColumn({ + name: "id", + type: "int", + isPrimary: true, + isNullable: false, + isGenerated: true, + generationStrategy: "increment", + }) + ); + } } diff --git a/src/migrations/1724579024939-invite.ts b/src/migrations/1724579024939-invite.ts index 220e37b..b8f9a9c 100644 --- a/src/migrations/1724579024939-invite.ts +++ b/src/migrations/1724579024939-invite.ts @@ -1,18 +1,73 @@ -import { MigrationInterface, QueryRunner } from "typeorm"; +import { MigrationInterface, QueryRunner, Table, TableColumn } from "typeorm"; export class Invite1724579024939 implements MigrationInterface { - name = 'Invite1724579024939' + name = "Invite1724579024939"; - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(`CREATE TABLE \`invite\` (\`mail\` varchar(255) NOT NULL, \`token\` varchar(255) NOT NULL, \`username\` varchar(255) NOT NULL, \`firstname\` varchar(255) NOT NULL, \`lastname\` varchar(255) NOT NULL, \`secret\` varchar(255) NOT NULL, PRIMARY KEY (\`mail\`)) ENGINE=InnoDB`); - await queryRunner.query(`ALTER TABLE \`user\` ADD \`firstname\` varchar(255) NOT NULL`); - await queryRunner.query(`ALTER TABLE \`user\` ADD \`lastname\` varchar(255) NOT NULL`); - } + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.createTable( + new Table({ + name: "invite", + columns: [ + { + name: "mail", + type: "varchar", + length: "255", + isPrimary: true, + isNullable: false, + }, + { + name: "token", + type: "varchar", + length: "255", + isNullable: false, + }, + { + name: "username", + type: "varchar", + length: "255", + isNullable: false, + }, + { + name: "firstname", + type: "varchar", + length: "255", + isNullable: false, + }, + { + name: "lastname", + type: "varchar", + length: "255", + isNullable: false, + }, + { + name: "secret", + type: "varchar", + length: "255", + isNullable: false, + }, + ], + }), + true + ); - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE \`user\` DROP COLUMN \`lastname\``); - await queryRunner.query(`ALTER TABLE \`user\` DROP COLUMN \`firstname\``); - await queryRunner.query(`DROP TABLE \`invite\``); - } + await queryRunner.addColumns("user", [ + new TableColumn({ + name: "firstname", + type: "varchar", + length: "255", + isNullable: false, + }), + new TableColumn({ + name: "lastname", + type: "varchar", + length: "255", + isNullable: false, + }), + ]); + } + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.dropColumns("user", ["lastname", "firstname"]); + await queryRunner.dropTable("invite"); + } }