Reputation: 337
Could you please help me to solve the problem? I use sqlite3 with sequelize npm package. After running migrations I don't see errors in console but I also don't see any database file. Also I can run migrations again and again, it doesn't look like correct behavior. Here is my /config/config.js file:
const path = require('path');
module.exports = {
test: {
username: 'root',
password: 'root',
database: path.join(__dirname, '..', 'database_test.sqlite'),
host: 'localhost',
dialect: 'sqlite',
logging: console.log,
operatorsAliases: false
}
};
Here is migrations/XXXXXXXXXXXXXX-create-appeal.js file:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('appeals', {
appealId: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID
},
name: {
allowNull: false,
type: Sequelize.STRING
},
description: {
type: Sequelize.STRING(511)
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: queryInterface => queryInterface.dropTable('appeals')
};
Here is models/index.js file
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(path.join(__dirname, '..', 'config', 'config.js'))[env];
const db = {};
let sequelize = new Sequelize(config.database, config.username, config.password, config);
fs
.readdirSync(__dirname)
.filter(file => (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js'))
.forEach(file => {
const model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
Here is models/appeal.js file:
'use strict';
module.exports = (sequelize, DataTypes) => {
const Appeal = sequelize.define('appeals', {
appealId: {
allowNull: false,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
type: DataTypes.UUID,
validate: {
isUUID: 4
},
get() {
return this.getDataValue('appealId').toLowerCase();
}
},
name: {
type: DataTypes.STRING,
allowNull: false
},
description: {
type: DataTypes.STRING(511)
}
}, {
tableName: 'appeals'
});
Appeal.associate = models => {
// associations can be defined here
};
return Appeal;
};
Another strange thing: if I put console log or error throwing in models/index.js, I'll see nothing, so that nodejs doesn't execute file.
Thanks.
Upvotes: 3
Views: 12524
Reputation: 337
I found a reason: it should be storage
instead of database
in config:
const path = require('path');
module.exports = {
test: {
username: 'root',
password: 'root',
storage: path.join(__dirname, '..', 'database_test.sqlite'),
host: 'localhost',
dialect: 'sqlite',
logging: console.log
}
};
Upvotes: 13
Reputation: 41
Try it:
./config.js
const path = require('path');
module.exports = {
development: {
username: 'root',
password: 'root',
database: path.join(__dirname, '..', 'database_test.sqlite'),
host: 'localhost',
dialect: 'sqlite',
logging: true,
operatorsAliases: false
}
};
.sequelizerc
const path = require('path');
module.exports = {
'models-path': path.resolve('./models'),
'seeders-path': path.resolve('./seeders'),
'migrations-path': path.resolve('./migrations'),
'config': path.resolve('./config', 'config.js')
};
Upvotes: 0