create caseSteps

This commit is contained in:
Takeshi Kimata
2024-03-02 15:41:43 +09:00
parent 22c951db07
commit 003e3e05fe
6 changed files with 22 additions and 29 deletions

View File

@@ -3,7 +3,7 @@
/** @type {import('sequelize-cli').Migration} */
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable("cases_steps", {
await queryInterface.createTable("caseSteps", {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
@@ -37,12 +37,12 @@ module.exports = {
},
});
await queryInterface.addIndex("cases_steps", ["caseId", "stepId"], {
await queryInterface.addIndex("caseSteps", ["caseId", "stepId"], {
unique: true,
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable("cases_steps");
await queryInterface.dropTable("caseSteps");
},
};

View File

@@ -1,5 +1,5 @@
function defineCase(sequelize, DataTypes) {
const Case = sequelize.define('Case', {
const Case = sequelize.define("Case", {
title: {
type: DataTypes.STRING,
allowNull: false,
@@ -40,19 +40,24 @@ function defineCase(sequelize, DataTypes) {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'folder',
key: 'id'
model: "folder",
key: "id",
},
onDelete: 'CASCADE'
}
onDelete: "CASCADE",
},
});
Case.associate = (models) => {
Case.belongsTo(models.Folder, { foreignKey: 'folderId', onDelete: 'CASCADE' });
Case.belongsToMany(models.Step, { through: 'Case_Step' });
Case.belongsTo(models.Folder, {
foreignKey: "folderId",
onDelete: "CASCADE",
});
Case.belongsToMany(models.Step, {
through: "caseSteps"
});
};
return Case;
}
module.exports = defineCase;
module.exports = defineCase;

View File

@@ -1,16 +0,0 @@
function defineCaseStep(sequelize, DataTypes) {
const CaseStep = sequelize.define("CaseStep", {
caseId: {
type: DataTypes.INTEGER,
allowNull: false,
},
stepId: {
type: DataTypes.INTEGER,
allowNull: false,
},
});
return CaseStep;
}
module.exports = defineCaseStep;

View File

@@ -11,7 +11,9 @@ function defineStep(sequelize, DataTypes) {
});
Step.associate = (models) => {
Step.belongsToMany(models.Case, { through: "Case_Step" });
Step.belongsToMany(models.Case, {
through: "caseSteps"
});
};
return Step;

View File

@@ -7,6 +7,8 @@ const { DataTypes } = require("sequelize");
module.exports = function (sequelize) {
const Case = defineCase(sequelize, DataTypes);
const Step = defineStep(sequelize, DataTypes);
Case.belongsToMany(Step, { through: 'caseSteps' });
Step.belongsToMany(Case, { through: 'caseSteps' });
router.get("/", async (req, res) => {
const { caseId, folderId } = req.query;

View File

@@ -125,7 +125,7 @@ module.exports = {
]);
// Add case-step join table
await queryInterface.bulkInsert("cases_steps", [
await queryInterface.bulkInsert("caseSteps", [
{
caseId: 1,
stepId: 1,