Open tastekim opened 1 year ago
// model/index.js
import fs from 'fs';
import path from 'path';
import Sequelize from 'sequelize';
import process from 'process';
const basename = path.basename(new URL(import.meta.url).pathname);
const env = process.env.NODE_ENV || 'development';
const config = import('../config/config.js')[env];
const db = {};
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs
.readdirSync(path.dirname(new URL(import.meta.url).pathname))
.filter(file => {
return (
file.indexOf('.') !== 0 &&
file !== basename &&
file.slice(-3) === '.js' &&
file.indexOf('.test.js') === -1
);
})
.forEach(file => {
const model = import(path.join(path.dirname(new URL(import.meta.url).pathname), file)).default;
db[model.name] = model(sequelize, Sequelize.DataTypes);
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
sequelize.sync({ force: true });
// force true ์ ๋๋น ๋ ๋ ค๋ฒ๋ฆฌ๊ณ ์๋ก๋ง๋ญ๋๋ค! ์ฒซ์คํ ํ ๊ผญ false๋ก ๋ฐ๊ฟ์ฃผ๊ธฐ!
export { sequelize, Sequelize };
export default db;
// config/config.js
import 'dotenv/config';
const config = {
development: {
username: process.env.RDS_USERNAME,
password: process.env.RDS_PASSWORD,
database: process.env.RDS_DBNAME,
host: process.env.RDS_HOST,
dialect: 'mysql',
},
test: {
username: 'root',
password: null,
database: 'database_test',
host: '127.0.0.1',
dialect: 'mysql',
},
production: {
username: 'root',
password: null,
database: 'database_production',
host: '127.0.0.1',
dialect: 'mysql',
},
};
export default config;
npx sequelize-cli db:migrate:status --config "config/config.js" --env "development"
Sequelize CLI [Node: 16.16.0, CLI: 6.6.0, ORM: 6.28.0]
Loaded configuration file "config/config.js".
ERROR: Dialect needs to be explicitly supplied as of v4.0.0
sequelize init
ํ ๋ชจ๋ธ ํ์ผ ์์ฑ ํ์ migration ํ์ผ์ ๋ง๋ค์ด์ ์๋ํ๋ฉด require๋ฅผ ์ฌ์ฉํ ์ ์๋ค๋ ์๋ฌ๊ฐ ๋จผ์ ๋ฌ๋ค. package.json ์ "type": "module"
๋ก ์ค์ ํ๊ธฐ ๋๋ฌธumzug
๋ผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋๋ฐ ์ด๊ฑด sequelize์ ์๋งค ํจํค์ง์ด๊ธฐ๋ ํ๋ฐ ๊ธฐ๋ณธ์ ์ผ๋ก commonjs ๋ฅผ ์๊ตฌํจ.cjs
ํ์ผ ํฌ๋งท์ ์ฐพ์.cjs
๋ก ๋ฐ๊พผ ํ npx sequelize db:migrate
์๋
์๋ฌ ๋ด์ฉ