create database structure
This commit is contained in:
parent
e5b5d7e67d
commit
ad6ee64565
15 changed files with 483 additions and 14 deletions
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue