roles and permissions
This commit is contained in:
parent
d77c3ca1a5
commit
9808100d81
21 changed files with 389 additions and 59 deletions
123
src/migrations/1724771491085-role_permission.ts
Normal file
123
src/migrations/1724771491085-role_permission.ts
Normal file
|
@ -0,0 +1,123 @@
|
|||
import { MigrationInterface, QueryRunner, Table, TableForeignKey } from "typeorm";
|
||||
|
||||
export class RolePermission1724771491085 implements MigrationInterface {
|
||||
name = "RolePermission1724771491085";
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.createTable(
|
||||
new Table({
|
||||
name: "role",
|
||||
columns: [
|
||||
{
|
||||
name: "id",
|
||||
type: "int",
|
||||
isPrimary: true,
|
||||
isNullable: false,
|
||||
isGenerated: true,
|
||||
generationStrategy: "increment",
|
||||
},
|
||||
{
|
||||
name: "role",
|
||||
type: "varchar",
|
||||
length: "255",
|
||||
isNullable: false,
|
||||
},
|
||||
],
|
||||
}),
|
||||
true
|
||||
);
|
||||
|
||||
await queryRunner.createTable(
|
||||
new Table({
|
||||
name: "role_permission",
|
||||
columns: [
|
||||
{
|
||||
name: "permission",
|
||||
type: "varchar",
|
||||
length: "255",
|
||||
isPrimary: true,
|
||||
isNullable: false,
|
||||
},
|
||||
{
|
||||
name: "roleId",
|
||||
type: "int",
|
||||
isPrimary: true,
|
||||
isNullable: false,
|
||||
},
|
||||
],
|
||||
}),
|
||||
true
|
||||
);
|
||||
|
||||
await queryRunner.renameTable("permission", "user_permission");
|
||||
|
||||
await queryRunner.createTable(
|
||||
new Table({
|
||||
name: "user_roles",
|
||||
columns: [
|
||||
{
|
||||
name: "userId",
|
||||
type: "int",
|
||||
isPrimary: true,
|
||||
isNullable: false,
|
||||
},
|
||||
{
|
||||
name: "roleId",
|
||||
type: "int",
|
||||
isPrimary: true,
|
||||
isNullable: false,
|
||||
},
|
||||
],
|
||||
}),
|
||||
true
|
||||
);
|
||||
|
||||
await queryRunner.createForeignKey(
|
||||
"role_permission",
|
||||
new TableForeignKey({
|
||||
columnNames: ["roleId"],
|
||||
referencedColumnNames: ["id"],
|
||||
referencedTableName: "role",
|
||||
onDelete: "No Action",
|
||||
})
|
||||
);
|
||||
|
||||
await queryRunner.createForeignKey(
|
||||
"user_roles",
|
||||
new TableForeignKey({
|
||||
columnNames: ["userId"],
|
||||
referencedColumnNames: ["id"],
|
||||
referencedTableName: "user",
|
||||
onDelete: "No Action",
|
||||
})
|
||||
);
|
||||
|
||||
await queryRunner.createForeignKey(
|
||||
"user_roles",
|
||||
new TableForeignKey({
|
||||
columnNames: ["roleId"],
|
||||
referencedColumnNames: ["id"],
|
||||
referencedTableName: "role",
|
||||
onDelete: "No Action",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
const user_roles = await queryRunner.getTable("user_roles");
|
||||
const roles_foreignKey = user_roles.foreignKeys.find((fk) => fk.columnNames.indexOf("roleId") !== -1);
|
||||
const user_foreignKey = user_roles.foreignKeys.find((fk) => fk.columnNames.indexOf("userId") !== -1);
|
||||
await queryRunner.dropForeignKey("user_roles", roles_foreignKey);
|
||||
await queryRunner.dropForeignKey("user_roles", user_foreignKey);
|
||||
await queryRunner.dropTable("user_roles");
|
||||
|
||||
const role_permission = await queryRunner.getTable("role_permission");
|
||||
const permission_foreignKey = role_permission.foreignKeys.find((fk) => fk.columnNames.indexOf("roleId") !== -1);
|
||||
await queryRunner.dropForeignKey("role_permission", permission_foreignKey);
|
||||
await queryRunner.dropTable("role_permission");
|
||||
|
||||
await queryRunner.dropTable("role");
|
||||
|
||||
await queryRunner.renameTable("user_permission", "permission");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue