create database structure

This commit is contained in:
Anika Raemer 2025-09-27 17:39:13 +02:00
parent e5b5d7e67d
commit ad6ee64565
15 changed files with 483 additions and 14 deletions

View file

@ -0,0 +1,83 @@
import { MigrationInterface, QueryRunner, TableUnique } from "typeorm";
export class RenameUserColumns1758958856261 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
// drop constraint
await queryRunner.dropUniqueConstraint("user", "UQ_user_userName");
// rename columns
await queryRunner.renameColumn(
"user",
"userName",
"user_name"
)
await queryRunner.renameColumn(
"user",
"firstName",
"first_name"
)
await queryRunner.renameColumn(
"user",
"lastName",
"last_name"
)
await queryRunner.renameColumn(
"user",
"createdAt",
"create_date"
)
await queryRunner.renameColumn(
"user",
"updatedAt",
"update_date"
)
// Add a unique constraint on user_name
await queryRunner.createUniqueConstraint(
"user",
new TableUnique({
columnNames: ["user_name"],
name: "UQ_user_user_name",
})
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
// drop constraint
await queryRunner.dropUniqueConstraint("user", "UQ_user_user_name");
// rename columns
await queryRunner.renameColumn(
"user",
"user_name",
"userName",
)
await queryRunner.renameColumn(
"user",
"first_name",
"firstName"
)
await queryRunner.renameColumn(
"user",
"last_name",
"lastName"
)
await queryRunner.renameColumn(
"user",
"create_date",
"createdAt"
)
await queryRunner.renameColumn(
"user",
"update_date",
"updatedAt"
)
// Add a unique constraint on userName
await queryRunner.createUniqueConstraint(
"user",
new TableUnique({
columnNames: ["userName"],
name: "UQ_user_userName",
})
);
}
}

View file

@ -0,0 +1,48 @@
import { MigrationInterface, QueryRunner, Table } from "typeorm";
export class CreateRecipeTable1758959405239 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
// Create table
await queryRunner.createTable(
new Table({
name: "recipe",
columns: [
{
name: "id",
type: "uuid",
isPrimary: true,
isGenerated: true,
generationStrategy: "uuid",
},
{
name: "amount",
type: "varchar",
isNullable: true,
},
{
name: "amount_description",
type: "varchar",
isNullable: true,
},
{
name: "create_date",
type: "timestamp",
default: "now()",
},
{
name: "update_date",
type: "timestamp",
default: "now()",
},
],
}),
true
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
// Drop the table
await queryRunner.dropTable("recipe");
}
}

View file

@ -0,0 +1,79 @@
import {
MigrationInterface,
QueryRunner,
Table,
TableForeignKey,
} from "typeorm";
export class CreateRecipeIngredientGroupTable1758959437946
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
// Create table
await queryRunner.createTable(
new Table({
name: "recipe_ingredient_group",
columns: [
{
name: "id",
type: "uuid",
isPrimary: true,
isGenerated: true,
generationStrategy: "uuid",
},
{
name: "create_date",
type: "timestamp",
default: "now()",
},
{
name: "update_date",
type: "timestamp",
default: "now()",
},
{
name: "title",
type: "varchar",
isNullable: false,
},
{
name: "sortOrder",
type: "int",
isNullable: false,
},
{
name: "recipe_id", // foreign key column
type: "uuid",
isNullable: false,
},
],
}),
true
);
// Add foreign key to recipe table
await queryRunner.createForeignKey(
"recipe_ingredient_group",
new TableForeignKey({
columnNames: ["recipe_id"],
referencedTableName: "recipe",
referencedColumnNames: ["id"],
onDelete: "CASCADE", // delete ingredient groups if recipe is deleted
})
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
// Drop foreign key first
const table = await queryRunner.getTable("recipe_ingredient_group");
const foreignKey = table?.foreignKeys.find(
(fk) => fk.columnNames.indexOf("recipe_id") !== -1
);
if (foreignKey) {
await queryRunner.dropForeignKey("recipe_ingredient_group", foreignKey);
}
// Drop table
await queryRunner.dropTable("recipe_ingredient_group");
}
}

View file

@ -0,0 +1,79 @@
import {
MigrationInterface,
QueryRunner,
Table,
TableForeignKey,
} from "typeorm";
export class CreateRecipeIngredientTable1758959442589
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
// Create table
await queryRunner.createTable(
new Table({
name: "recipe_ingredient",
columns: [
{
name: "id",
type: "uuid",
isPrimary: true,
isGenerated: true,
generationStrategy: "uuid",
},
{
name: "create_date",
type: "timestamp",
default: "now()",
},
{
name: "update_date",
type: "timestamp",
default: "now()",
},
{
name: "title",
type: "varchar",
isNullable: false,
},
{
name: "sortOrder",
type: "int",
isNullable: false,
},
{
name: "recipe_ingredient_group_id", // foreign key column
type: "uuid",
isNullable: false,
},
],
}),
true
);
// Add foreign key to recipe table
await queryRunner.createForeignKey(
"recipe_ingredient",
new TableForeignKey({
columnNames: ["recipe_ingredient_group_id"],
referencedTableName: "recipe_ingredient_group",
referencedColumnNames: ["id"],
onDelete: "CASCADE", // delete ingredient if ingredient_group is deleted
})
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
// Drop foreign key first
const table = await queryRunner.getTable("recipe_ingredient");
const foreignKey = table?.foreignKeys.find(
(fk) => fk.columnNames.indexOf("recipe_ingredient_group_id") !== -1
);
if (foreignKey) {
await queryRunner.dropForeignKey("recipe_ingredient", foreignKey);
}
// Drop table
await queryRunner.dropTable("recipe_ingredient");
}
}

View file

@ -0,0 +1,79 @@
import {
MigrationInterface,
QueryRunner,
Table,
TableForeignKey,
} from "typeorm";
export class CreateRecipeInstructionStepTable1758959460127
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
// Create table
await queryRunner.createTable(
new Table({
name: "recipe_instruction_step",
columns: [
{
name: "id",
type: "uuid",
isPrimary: true,
isGenerated: true,
generationStrategy: "uuid",
},
{
name: "create_date",
type: "timestamp",
default: "now()",
},
{
name: "update_date",
type: "timestamp",
default: "now()",
},
{
name: "text",
type: "varchar",
isNullable: false,
},
{
name: "sortOrder",
type: "int",
isNullable: false,
},
{
name: "recipe_id", // foreign key column
type: "uuid",
isNullable: false,
},
],
}),
true
);
// Add foreign key to recipe table
await queryRunner.createForeignKey(
"recipe_instruction_step",
new TableForeignKey({
columnNames: ["recipe_id"],
referencedTableName: "recipe",
referencedColumnNames: ["id"],
onDelete: "CASCADE", // delete instruction steps if recipe is deleted
})
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
// Drop foreign key first
const table = await queryRunner.getTable("recipe_instruction_step");
const foreignKey = table?.foreignKeys.find(
(fk) => fk.columnNames.indexOf("recipe_id") !== -1
);
if (foreignKey) {
await queryRunner.dropForeignKey("recipe_instruction_step", foreignKey);
}
// Drop table
await queryRunner.dropTable("recipe_instruction_step");
}
}