Open 9033 opened 3 years ago
const defThing = sequelize.define('defThing', { name: DataTypes.STRING }); const defLocation = sequelize.define('defLocation', { name: DataTypes.STRING }); const defRelation = sequelize.define('defRelation'); defThing.belongsToMany(defLocation, { through: defRelation }); defLocation.belongsToMany(defThing, { through: defRelation });
const old = false const done = false const notWork = false if(old){ const cupLamen = await defThing.create({ name:'컵라면' }) const mart = await defThing.create({ name:'마트' }) const nearStore = await defThing.create({ name:'편의점' }) await model.bulkCreate([ { defThingId:cupLamen.id, defLocationId:mart.id, }, { defThingId:cupLamen.id, defLocationId:nearStore.id, } ]) } // db table 생성 if(done){ await defRelation.sync() await defThing.sync() await defLocation.sync() } // 연결 하기 if(done){ // defLocation에 연결되어 있지 않은경우 연결 정보 생성. const triRice = await defThing.findOne({ where:{ name:'삼각김밥', }, }) const store = await defLocation.findOne({ where:{ name:'편의점', }, }) await triRice.addDefLocation(store) } // 연결 하기: findOrCreate if(done){ const quadRice = await defThing.findOrCreate({ where:{ name:'사각김밥', }, defaults:{ name:'사각김밥', }, }) const store = await defLocation.findOrCreate({ where:{ name:'김밥천국', }, defaults:{ name:'김밥천국', }, }) await quadRice[0].addDefLocation(store[0]) } // 연결 하기: findOrCreate 2 if(done){ const quadRice = await defThing.findOrCreate({ where:{ name:'사각김밥 2077', }, defaults:{ name:'사각김밥 2077', defLocations:[ { name:'김밥 극락', }, ] }, include: defLocation, }) } // 연결 끊기 if(done){ const triRice = await defThing.findOne({ where:{ name:'삼각김밥', }, }) const store = await defLocation.findOne({ where:{ name:'편의점', }, }) await triRice.removeDefLocation(store) } // 품목을 추가, 연걸 하기 if(done){ // defThing에 컵라면 추가. // defLocation에 마트 편의점 추가. // defRelation으로 연결. const cupLamen = await defThing.create({ name:'컵라면', defLocations:[ { name:'마트', }, { name:'편의점', } ] },{ include: defLocation, }) } // 품목 생성후 기존과 연결. if(done){ // 인간 사료 생성. 기존에 있던 상점에 전부 연결됨. const bulkCookie = await defThing.create({ name:'인간 사료', }) const stores = await defLocation.findAll() await bulkCookie.addDefLocation(stores) } // 여러 품목 생성후 기존과 연결. if(done){ const snacks = await defThing.bulkCreate([ { name:'미국 햄', }, { name:'독일 햄', } ]) const stores = await defLocation.findAll({ name:'마트', }) await Promise.all(snacks.map(snack=>snack.addDefLocation(stores))) // snacks.forEach 대신 사용. } // 여러 품목 생성 if(done){ const snacks = await defThing.bulkCreate([ { name:'미국 햄', defLocations:[ { name:'미국 마트', }, ] }, { name:'독일 햄', defLocations:[ { name:'독일 마트', }, ] } ],{ include: defLocation, }) } // 여러 품목을 여러 상점과 연결 if(done){ const snacks = await defThing.findAll({ where:{ name:{ [Sequelize.Op.like]:'%스낵', } }, }) const stores = await defLocation.findAll() await Promise.all(snacks.map(snack=>snack.addDefLocation(stores))) // snacks.forEach 대신 사용. } const r = await defLocation.findAll({ attributes:['name'], include:[ { attributes:['name'], model:defThing, } ], where:{ name:'김밥천국', }, order:[ ], // having:{}, // paranoid:false, // transaction, })