윤준석

ADD: addKeyword function for add keyword to database

if user is not exist in database, create user
if keyword is not exist in database, create keyword
let database = module.exports = {};
const db = require("../models")
const User = db.user
const Keyword = db.keyword
const UserKeyword = db.userKeyword
database.addKeyword = async function(keyword, userId) {
const u = await User.findOrCreate({
where: {
userId: userId
}
})
const k = await Keyword.findOrCreate({
where: {
keyword: keyword
}
})
await UserKeyword.findOrCreate({
where: {
userId: u[0].id,
keywordId: k[0].id
}
})
}
......@@ -4,11 +4,13 @@ const setFlexMessage = require("./apis/setFlexMessage");
const fs = require("fs");
const { sequelize } = require('./models')
const database = require("./apis/database");
// Initialize DB connection
sequelize.sync({ force: false })
.then(() => {
console.log('database connection complete');
database.addKeyword("rtx3080", "phobyjun")
})
.catch((err) => {
console.log('database connection failed');
......
......@@ -36,8 +36,9 @@ db.Sequelize = Sequelize;
db.user = require("./user")(sequelize, Sequelize);
db.keyword = require("./keyword")(sequelize, Sequelize);
db.userKeyword = sequelize.define('user_keywords');
db.user.belongsToMany(db.keyword, {through: 'user_keyword'});
db.keyword.belongsToMany(db.user, {through: 'user_keyword'});
db.user.belongsToMany(db.keyword, {through: db.userKeyword});
db.keyword.belongsToMany(db.user, {through: db.userKeyword});
module.exports = db;
......
......@@ -7,7 +7,7 @@ module.exports = (sequelize, DataTypes) => {
primaryKey: true
},
userId: {
type: DataTypes.INTEGER,
type: DataTypes.STRING,
allowNull: false
}
})
......