Skip to content

Commit

Permalink
Created model and migration files for behaviour and behaviour level t…
Browse files Browse the repository at this point in the history
…ables
  • Loading branch information
vips11 committed Oct 22, 2024
1 parent 9a43dbb commit cf42402
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { DataType } from "sequelize-typescript";

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

const TABLE_NAME = "behaviour_level_details";

export const up: Migration = async ({ context: sequelize }) => {
await sequelize.getQueryInterface().createTable(TABLE_NAME, {
id: {
type: DataType.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
behaviour_id: {
type: DataType.INTEGER,
allowNull: false,
},
level: {
type: DataType.INTEGER,
allowNull: false,
},
description: {
type: DataType.STRING,
allowNull: false,
},
training_instructions: {
type: DataType.STRING,
allowNull: false,
},
});

await sequelize.getQueryInterface().addConstraint(TABLE_NAME, {
fields: ["behaviour_id", "level"],
type: "unique",
name: "unique_behaviour_level",
});
};

export const down: Migration = async ({ context: sequelize }) => {
await sequelize.getQueryInterface().dropTable(TABLE_NAME);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { DataType } from "sequelize-typescript";
import { Migration } from "../umzug";

export const up: Migration = async ({ context: sequelize }) => {
await sequelize
.getQueryInterface()
.addColumn("behaviours", "parent_behaviour_id", {
type: DataType.INTEGER,
allowNull: true,
});
};

export const down: Migration = async ({ context: sequelize }) => {
await sequelize
.getQueryInterface()
.removeColumn("behaviours", "parent_behaviour_id");
};
14 changes: 12 additions & 2 deletions backend/typescript/models/behaviour.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { Column, Model, Table } from "sequelize-typescript";
import {
Column,
DataType,
ForeignKey,
Model,
Table,
} from "sequelize-typescript";

@Table({ timestamps: false, tableName: "behaviours" })
export default class Behaviour extends Model {
@Column
@Column({ type: DataType.STRING, allowNull: false })
behaviour_name!: string;

@ForeignKey(() => Behaviour)
@Column({ type: DataType.INTEGER })
parent_behaviour_id?: number | null;
}
28 changes: 28 additions & 0 deletions backend/typescript/models/behvaiourLevelDetails.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
Column,
Model,
Table,
ForeignKey,
DataType,
} from "sequelize-typescript";
import Behaviour from "./behaviour.model";
import { BehaviourLevel } from "../types";

@Table({ timestamps: false, tableName: "behvaiour_level_details" })
export default class BehaviourLevelDetails extends Model {
@Column({ type: DataType.INTEGER, allowNull: false })
id!: number;

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

@Column({ type: DataType.ENUM, allowNull: false })
level!: BehaviourLevel;

@Column({ type: DataType.STRING, allowNull: false })
description!: string;

@Column({ type: DataType.STRING, allowNull: false })
training_instructions!: string;
}
2 changes: 2 additions & 0 deletions backend/typescript/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export type AuthDTO = Token & UserDTO;

export type Letters = "A" | "B" | "C" | "D";

export type BehaviourLevel = 1 | 2 | 3 | 4;

const sexValues = ["M", "F"] as const;

export const sexEnum: Sex[] = [...sexValues];
Expand Down

0 comments on commit cf42402

Please sign in to comment.