https://dev.mysql.com/downloads/installer/
Choosing s Setup Type -> Custom

Selecct Products and Features -> server, workbench 추가





SELECT * FROM nodejs.users;
SELECT name, married FROM nodejs.users;
SELECT name, age FROM nodejs.users WHERE married = 1 AND age > 30l;
SELECT id, name FROM nodejs.users WHERE married = 0 OR age > 30;
SELECT id, name FROM nodejs.users ORDER BY age DESC;
SELECT id, name FROM nodejs.users ORDER BY age DESC LIMIT 1;
SELECT id, name FROM nodejs.users ORDER BY age DESC LIMIT 1 OFFSET 1;
UPDATE nodejs.users SET comment = '바꿀 내용' WHERE id = 2;
DELETE FROM nodejs.users WHERE id = 2;
$ npm i express morgan nunjucks sequelize sequelize-cli mysql2
$ npm i-D nodemon
$ npx sequelize init

알겠습니다. 사진 속 글자를 똑같이 적어 보겠습니다.

const Sequelize = require('sequelize');
const User = require('./user');
const Comment = require('./comment');
db.sequelize = sequelize;
db.User = User;
db.Comment = Comment;
User.init(sequelize);
Comment.init(sequelize);
User.associate(db);
Comment.associate(db);
module.exports = db;
db.User.hasOne(db.Info, { foreignKey: 'UserId', sourcekey: 'id' });
db.Info.belongsTo(db.User, { foreignkey: 'UserId', targetKey: 'id' });
db.Post.belongsToMany(db.Hashtag, { through: 'PostHashtag' });
db.Hashtag.belongsToMany(db.Post, { through: 'PostHashtag' });
const user = await User.fingOne({});
console.log(user.nick); // 사용자 닉네임
const user = await User.findOne({
include: [{
model: Comment,
}]
});
console.log(user.Comments); // 사용자 댓글
db.sequelize.models.PostHashtag
const user = await User.findOne({});
const comments = await user.getComments();
console.log(comments); // 사용자 댓글
// 관계를 설정할 때 as로 등록
db.User.hasMany(db.comment, { foreignkey: 'commenter', sourcekey: 'id', as: 'Answers' });
// 쿼리할 때는
const user = await User.findOne({});
const comments = await user.getAnswers();
console.log(comments); // 사용자 댓글
const user = await User.findOne({
include: [{
model: Comment,
where: {
id: 1,
},
attributes: ['id'],
}],
});
// 또는
const comments = await user.getComments({
where: {
id: 1,
},
attributes: ['id'],
});
const user = await User.findOne({});
const comment = await Comment.create();
await user.addComment(comment);
// 또는
await user.addComment(comment.id);
const user = await User.findOne({});
const comment1 = await Comment.create();
const comment2 = await Comment.create();
await user.addComment([comment1, comment2]);
const [result, metadata] = await sequelize.query("SELECT * from comments");
console.log(result);
UPDATE nodejs.users SET comment='48' WHERE id = 2;
User.update({
comment: '바꿀 내용',
}, {
where: { id: 2 },
});
DELETE FROM nodejs.users WHERE id = 2;
User.destroy({
where: { id: 2 },
});
const express = require('express');
const { User, Comment } = require('../models');
const router = express.Router();
router.post('/', async (req, res, next) => {
try {
const comment = await Comment.create({
commenter: req.body.id,
comment: req.body.comment,
});
res.status(201).json(comment);
} catch (err) {
console.error(err);
next(err);
}
});
router.patch('/:id', async (req, res, next) => {
try {
const result = await Comment.update({
comment: req.body.comment,
}, { where: { id: req.params.id } });
res.json(result);
} catch (err) {
console.error(err);
next(err);
}
});
router.delete('/:id', async (req, res, next) => {
try {
const result = await Comment.destroy({ where: { id: req.params.id } });
res.json(result);
} catch (err) {
console.error(err);
next(err);
}
});
module.exports = router;
잘보고 갑니다.~