Skip to content

Commit

Permalink
Merge pull request #40 from uwblueprint/F24/julia/add-user-animal-typ…
Browse files Browse the repository at this point in the history
…e-table

Added user animal type table
  • Loading branch information
liya-zhu authored Nov 6, 2024
2 parents 0d18e06 + 83d222f commit da373cc
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
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",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
animal_type_id: {
type: DataType.INTEGER,
allowNull: false,
primaryKey: true,
references: {
model: "animal_types",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
});
};

export const down: Migration = async ({ context: sequelize }) => {
await sequelize.getQueryInterface().dropTable(TABLE_NAME);
};
12 changes: 11 additions & 1 deletion backend/typescript/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import * as path from "path";
import { Sequelize } from "sequelize-typescript";
import User from "./user.model";
import AnimalType from "./animalType.model";
import UserAnimalType from "./userAnimalType.model";
import defineRelationships from "./modelRelationships";

const DATABASE_URL =
process.env.NODE_ENV === "production"
Expand All @@ -8,6 +12,12 @@ const DATABASE_URL =
: `postgres://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASSWORD}@${process.env.DB_HOST}:5432/${process.env.POSTGRES_DB_DEV}`;

/* eslint-disable-next-line import/prefer-default-export */
export const sequelize = new Sequelize(DATABASE_URL, {
const sequelize = new Sequelize(DATABASE_URL, {
models: [path.join(__dirname, "/*.model.ts")],
});

sequelize.addModels([User, AnimalType, UserAnimalType]);

defineRelationships();

export { sequelize, User, AnimalType, UserAnimalType };
7 changes: 7 additions & 0 deletions backend/typescript/models/modelRelationships.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import User from "./user.model";
import AnimalType from "./animalType.model";
import UserAnimalType from "./userAnimalType.model";

export default function defineRelationships(): void {
User.belongsToMany(AnimalType, { through: UserAnimalType });
}
26 changes: 26 additions & 0 deletions backend/typescript/models/userAnimalType.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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 {
@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 da373cc

Please sign in to comment.