React

 

 

 

 Next.js 서버사이드렌더링

 

버전이 다르기 때문에 소스가 강좌와 다를 수 있다.

버전

next:  13.0.4

antd:  5.0.1

 

소스 : https://github.dev/braverokmc79/node-bird-sns

 

제로초 소스 : https://github.com/ZeroCho/react-nodebird

 

 

 

 

 

 

 

 

74.  sequelize   최신 문법  사용

 

강의 :

https://www.inflearn.com/course/%EB%85%B8%EB%93%9C%EB%B2%84%EB%93%9C-%EB%A6%AC%EC%95%A1%ED%8A%B8-%EB%A6%AC%EB%89%B4%EC%96%BC/unit/49018?tab=curriculum

 

 

시퀄라이즈 사용법

https://sequelize.org/docs/v6/

 

 

 

백엔드

models/comment.js

//기존
// module.exports = (sequelize, DataTypes) => {
//     const Comment = sequelize.define('Comment', {
//         //id: {}, id 가 기본적으로 들어있다.
//         content: {
//             type: DataTypes.TEXT,
//             allowNull: false
//         },
//         //UserId:1
//         //PostId:3
//     }, {
//         charset: 'utf8mb4',
//         collate: 'utf8mb4_unicode_ci' //이모티콘 저장
//     });

//     Comment.associate = (db) => {
//         db.Comment.belongsTo(db.User);
//         db.Comment.belongsTo(db.Post);
//     }
//     return Comment;
// }
//==>업데이트 
const DataTypes = require('sequelize');
const { Model } = DataTypes;

module.exports = class Comment extends Model {
    static init(sequelize) {
        return super.init({
            // id가 기본적으로 들어있다.
            content: {
                type: DataTypes.TEXT,
                allowNull: false,
            },
            // UserId: 1
            // PostId: 3
        }, {
            modelName: 'Comment',
            tableName: 'comments',
            charset: 'utf8mb4',
            collate: 'utf8mb4_general_ci', // 이모티콘 저장
            sequelize,
        });
    }

    static associate(db) {
        db.Comment.belongsTo(db.User);
        db.Comment.belongsTo(db.Post);
    }
};

 

models/hashtag.js

// module.exports = (sequelize, DataTypes) => {
//     const Hashtag = sequelize.define('Hashtag', {
//         //id: {}, id 가 기본적으로 들어있다.
//         name: {
//             type: DataTypes.STRING(50),
//             allowNull: false
//         },
//     }, {
//         charset: 'utf8mb4',
//         collate: 'utf8mb4_unicode_ci' //이모티콘 저장
//     });

//     Hashtag.associate = (db) => {
//         db.Hashtag.belongsToMany(db.Post, { through: 'PostHashtag' });
//     }
//     return Hashtag;
// }
// 업데이트 =>

const DataTypes = require('sequelize');
const { Model } = DataTypes;

module.exports = class Hashtag extends Model {
    static init(sequelize) {
        return super.init({
            // id가 기본적으로 들어있다.
            name: {
                type: DataTypes.STRING(20),
                allowNull: false,
            },
        }, {
            modelName: 'Hashtag',
            tableName: 'hashtags',
            charset: 'utf8mb4',
            collate: 'utf8mb4_general_ci', // 이모티콘 저장
            sequelize,
        });
    }
    static associate(db) {
        db.Hashtag.belongsToMany(db.Post, { through: 'PostHashtag' });
    }
};










 

 

 

models/image.js

// module.exports = (sequelize, DataTypes) => {
//     const Image = sequelize.define('Image', {
//         //id: {}, id 가 기본적으로 들어있다.
//         src: {
//             type: DataTypes.STRING(200),
//             allowNull: false
//         },
//     }, {
//         charset: 'utf8mb4',
//         collate: 'utf8mb4_unicode_ci' //이모티콘 저장
//     });


//     Image.associate = (db) => {
//         db.Image.belongsTo(db.Post);
//     }
//     return Image;
// }

const DataTypes = require('sequelize');
const { Model } = DataTypes;

module.exports = class Image extends Model {
    static init(sequelize) {
        return super.init({
            //id가 기본적으로 들어있다.
            src: {
                type: DataTypes.STRING(200),
                allowNull: false,
            }
        }, {
            modelName: 'Image',
            tableName: 'images',
            charset: 'utf8mb4',
            collate: 'utf8mb4_general_ci', // 이모티콘 저장
            sequelize,

        })
    }

    static associate(db) {
        db.Image.belongsTo(db.Post);
    }
}

 

 

 

 

models/post.js

// module.exports = (sequelize, DataTypes) => {
//     const Post = sequelize.define('Post', { //MYSQL  에는  POST 테이블 생성
//         //id: {}, id 가 기본적으로 들어있다.
//         content: {
//             type: DataTypes.TEXT,
//             allowNull: false,
//         },

//         //RetweetId
//     }, {
//         charset: 'utf8mb4',
//         collate: 'utf8mb4_unicode_ci' //이모티콘 저장
//     });

//     Post.associate = (db) => {
//         db.Post.belongsTo(db.User); //post.addUser post.getUser , post.setUser
//         db.Post.belongsToMany(db.Hashtag, { through: 'PostHashtag' }); //post.addHashtags
//         db.Post.hasMany(db.Comment); //post.addComments, post.getComments
//         db.Post.hasMany(db.Image); //post.addImages . post.getImages
//         db.Post.belongsToMany(db.User, { through: 'Like', as: 'Likers' }); //post.addLikers , post.removeLikers
//         db.Post.belongsTo(db.Post, { as: "Retweet" }); //post.addRetweet
//     }
//     return Post;
// }



const DataTypes = require('sequelize');
const { Model } = DataTypes;

module.exports = class Post extends Model {
    static init(sequelize) {
        return super.init({
            // id가 기본적으로 들어있다.
            content: {
                type: DataTypes.TEXT,
                allowNull: false,
            },
            // RetweetId
        }, {
            modelName: 'Post',
            tableName: 'posts',
            charset: 'utf8mb4',
            collate: 'utf8mb4_general_ci', // 이모티콘 저장
            sequelize,
        });
    }
    static associate(db) {
        db.Post.belongsTo(db.User); // post.addUser, post.getUser, post.setUser
        db.Post.belongsToMany(db.Hashtag, { through: 'PostHashtag' }); // post.addHashtags
        db.Post.hasMany(db.Report);
        db.Post.hasMany(db.Comment); // post.addComments, post.getComments
        db.Post.hasMany(db.Image); // post.addImages, post.getImages
        db.Post.belongsToMany(db.User, { through: 'Like', as: 'Likers' }) // post.addLikers, post.removeLikers
        db.Post.belongsTo(db.Post, { as: 'Retweet' }); // post.addRetweet
    }
};

 

 

models/report.js

 

const DataTypes = require('sequelize');
const { Model } = DataTypes;

module.exports = class Report extends Model {
    static init(sequelize) {
        return super.init({
            // id가 기본적으로 들어있다.
            content: {
                type: DataTypes.TEXT,
                allowNull: false,
            },
        }, {
            modelName: 'Report',
            tableName: 'reports',
            charset: 'utf8mb4',
            collate: 'utf8mb4_general_ci', // 이모티콘 저장
            sequelize,
        });
    }
    static associate(db) {
        db.Report.belongsTo(db.User);
        db.Report.belongsTo(db.Post);
    }
};

 

 

 

 

models/user.js

// module.exports = (sequelize, DataTypes) => {
//     const User = sequelize.define('User', { //MYSQL  에는  users 테이블 생성
//         //id: {}, id 가 기본적으로 들어있다.
//         email: {
//             type: DataTypes.STRING(30),//STRING,TEXT,BOOLEAN,INTEGER,FLOAT,DATETIME
//             allowNull: false, //필수,
//             unique: true, //고유한 값
//         },
//         nickname: {
//             type: DataTypes.STRING(30),
//             allowNull: false, //필수
//         },
//         password: {
//             type: DataTypes.STRING(100),
//             allowNull: false,//필수
//         }
//         //PostId:1,2,5,10,15
//         //CommentId
//     }, {
//         charset: 'utf8mb4',
//         collate: 'utf8mb4_unicode_ci'
//     });

//     User.associate = (db) => {
//         db.User.hasMany(db.Post);
//         db.User.hasMany(db.Comment);
//         db.User.belongsToMany(db.Post, { through: 'Like', as: 'Liked' });
//         db.User.belongsToMany(db.User, { through: 'Follow', as: 'Followers', foreignKey: "FollowingId" });
//         db.User.belongsToMany(db.User, { through: 'Follow', as: 'Followings', foreignKey: "FollowerId" });
//     };

//     return User;
// }


const DataTypes = require('sequelize');
const { Model } = DataTypes;

module.exports = class User extends Model {
    static init(sequelize) {
        return super.init({
            // id가 기본적으로 들어있다.
            email: {
                type: DataTypes.STRING(30), // STRING, TEXT, BOOLEAN, INTEGER, FLOAT, DATETIME
                allowNull: false, // 필수
                unique: true, // 고유한 값
            },
            nickname: {
                type: DataTypes.STRING(30),
                allowNull: false, // 필수
            },
            password: {
                type: DataTypes.STRING(100),
                allowNull: false, // 필수
            },
        }, {
            modelName: 'User',
            tableName: 'users',
            charset: 'utf8mb4',
            collate: 'utf8mb4_unicode_ci', // 한글 저장
            sequelize,
        });
    }
    static associate(db) {
        db.User.hasMany(db.Post);
        db.User.hasMany(db.Report);
        db.User.hasMany(db.Comment);
        db.User.belongsToMany(db.Post, { through: 'Like', as: 'Liked' })
        db.User.belongsToMany(db.User, { through: 'Follow', as: 'Followers', foreignKey: 'FollowingId' });
        db.User.belongsToMany(db.User, { through: 'Follow', as: 'Followings', foreignKey: 'FollowerId' });
    }
};

 

 

 

 

 

 

 

 

 

 

 

models/index.js

const Sequelize = require('sequelize');
const comment = require('./comment');
const hashtag = require('./hashtag');
const image = require('./image');
const post = require('./post');
const user = require('./user');
const report = require('./report');


//const Sequelize = require("sequelize");
const env = process.env.NODE_ENV || 'development';
const config = require('../config/config')[env];
const db = {}

const sequelize = new Sequelize(config.database, config.username, config.password, config);

// db.Comment = require('./comment')(sequelize, Sequelize);
// db.Hashtag = require('./hashtag')(sequelize, Sequelize);
// db.Image = require('./image')(sequelize, Sequelize);
// db.Post = require('./post')(sequelize, Sequelize);
// db.User = require('./user')(sequelize, Sequelize);

db.Comment = comment;
db.Hashtag = hashtag;
db.Image = image;
db.Post = post;
db.User = user;
db.Report = report;

Object.keys(db).forEach(modelName => {
  db[modelName].init(sequelize);
})


Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

about author

PHRASE

Level 60  라이트

잠을 발명한 자에게 하나님의 축복이 있을 지어다. 그것은 모든 사람의 생각을 뒤덮는 외투요, 모든 굶주림을 치료해 주는 음식이요, 목동과 왕, 무학자와 현자를 평등하게 하는 저울추이다. -세르반테스

댓글 ( 4)

댓글 남기기

작성