Skip to content

Commit

Permalink
Added user animal type table
Browse files Browse the repository at this point in the history
  • Loading branch information
liya-zhu committed Oct 16, 2024
1 parent 487ef02 commit 83cb2c5
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { DataType } from "sequelize-typescript";

import { Migration } from "../umzug";

const TABLE_NAME = "user_animal_types";

export const up: Migration = async ({ context: sequelize }) => {
await sequelize.getQueryInterface().createTable(TABLE_NAME, {
user_id: {
type: DataType.INTEGER,
allowNull: false,
primaryKey: true,
references: {
model: "users",
key: "id"

Check failure on line 15 in backend/typescript/migrations/2024.10.04T00.23.02.create-user-animal-types-table.ts

View workflow job for this annotation

GitHub Actions / run-lint

Insert `,`
},
onUpdate: 'CASCADE',

Check failure on line 17 in backend/typescript/migrations/2024.10.04T00.23.02.create-user-animal-types-table.ts

View workflow job for this annotation

GitHub Actions / run-lint

Replace `'CASCADE'` with `"CASCADE"`
onDelete: 'CASCADE',

Check failure on line 18 in backend/typescript/migrations/2024.10.04T00.23.02.create-user-animal-types-table.ts

View workflow job for this annotation

GitHub Actions / run-lint

Replace `'CASCADE'` with `"CASCADE"`
},
animal_type_id: {
type: DataType.INTEGER,
allowNull: false,
primaryKey: true,
references: {
model: "animal_types",
key: "id"

Check failure on line 26 in backend/typescript/migrations/2024.10.04T00.23.02.create-user-animal-types-table.ts

View workflow job for this annotation

GitHub Actions / run-lint

Insert `,`
},
onUpdate: 'CASCADE',

Check failure on line 28 in backend/typescript/migrations/2024.10.04T00.23.02.create-user-animal-types-table.ts

View workflow job for this annotation

GitHub Actions / run-lint

Replace `'CASCADE'` with `"CASCADE"`
onDelete: 'CASCADE'

Check failure on line 29 in backend/typescript/migrations/2024.10.04T00.23.02.create-user-animal-types-table.ts

View workflow job for this annotation

GitHub Actions / run-lint

Replace `'CASCADE'` with `"CASCADE",`
}

Check failure on line 30 in backend/typescript/migrations/2024.10.04T00.23.02.create-user-animal-types-table.ts

View workflow job for this annotation

GitHub Actions / run-lint

Insert `,`
});
}

Check failure on line 32 in backend/typescript/migrations/2024.10.04T00.23.02.create-user-animal-types-table.ts

View workflow job for this annotation

GitHub Actions / run-lint

Insert `;`

export const down: Migration = async ({ context: sequelize }) => {
await sequelize.getQueryInterface().dropTable(TABLE_NAME);
};
7 changes: 6 additions & 1 deletion backend/typescript/models/animalType.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Column, Model, Table } from "sequelize-typescript";
import { Column, Model, Table, BelongsToMany } from "sequelize-typescript";
import UserAnimalType from "./userAnimalType.model";

Check failure on line 2 in backend/typescript/models/animalType.model.ts

View workflow job for this annotation

GitHub Actions / run-lint

Dependency cycle detected
import User from "./user.model";

Check failure on line 3 in backend/typescript/models/animalType.model.ts

View workflow job for this annotation

GitHub Actions / run-lint

Dependency cycle detected

@Table({ timestamps: false, tableName: "animal_types" })
export default class AnimalType extends Model {
@Column({})
animal_type_name!: string;

@BelongsToMany(() => User, () => UserAnimalType)
users!: User[];
}
8 changes: 7 additions & 1 deletion backend/typescript/models/user.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import {
Model,
Table,
AllowNull,
BelongsToMany,
} from "sequelize-typescript";
import { Role, UserStatus } from "../types";
import AnimalType from "./animalType.model";
import UserAnimalType from "./userAnimalType.model";

@Table({ tableName: "users" })
export default class User extends Model {
Expand Down Expand Up @@ -39,4 +42,7 @@ export default class User extends Model {

@Column({ type: DataType.ENUM("Active", "Inactive"), allowNull: false })
status!: UserStatus;
}

@BelongsToMany(() => AnimalType, () => UserAnimalType)
animalTypes!: AnimalType[];
}
27 changes: 27 additions & 0 deletions backend/typescript/models/userAnimalType.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {
Column,
DataType,
Model,
Table,
ForeignKey,
PrimaryKey
} from 'sequelize-typescript';
import User from './user.model';
import AnimalType from './animalType.model';

@Table({
tableName: 'User_AnimalTypes',
timestamps: true
})
export default class UserAnimalType extends Model<UserAnimalType> {
@ForeignKey(() => User)
@PrimaryKey
@Column({ type:DataType.INTEGER, allowNull: false })
user_id!: number;

@ForeignKey(() => AnimalType)
@PrimaryKey
@Column({ type: DataType.INTEGER, allowNull: false })
animal_type_id!: number;
}

0 comments on commit 83cb2c5

Please sign in to comment.