backpack
backpack copied to clipboard
Can use fs.readFile ?
I use backpack with sequelize . This code generate file like
var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var basename = path.basename(__filename);
var env = process.env.NODE_ENV || 'development';
var config = require(__dirname + '/../config/config.json')[env];
var db = {};
if (config.use_env_variable) {
var sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
var sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs.readdirSync(__dirname).filter(function(file) {
var noExtFileName = path.basename(__filename, '.js')
return (file.indexOf('.') !== 0) && !(file.match(noExtFileName))
}).forEach(function(file) {
if (file.slice(-3) !== '.js') return;
var model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});
and this code needs reading static files.
These code doesn't work after build because __dirname points relative path from build path which differ from pre-located .
This happens as well as other codes using reading files. Is there any idea?
Js dynamic import case , I resolve this situation by using require.context
like require.context(__dirname ,false,/\.\/(?!index).*\.js$/)
.
But, it's uneasy to manage codes. Is there any idea?
Same here, how to do it the right way?
same here
same here
You can use a plugin like https://github.com/quadric/babel-plugin-inline-import to swap fs.read for a require statement.
@cliedeman It'll work well introduced with babel-node even if we use sequelize-cli , thanks. And, I found that we can use other build tools like gulp or webpack-plugins. So, we choose how script compressed , ex) with back pack and gulp or set webpack plugin like url-loader for static file build.
I had the same issue, but it just needed a tweak with Sequelize to work. Where it's using sequelize['import']
to load the models, you want to use path.resolve
in order to pass the absolute path location to Sequelize:
const model = sequelize['import'](path.resolve(path.join(__dirname, file)));
I'd suggest this isn't a problem with this repository, just on oversight on the automatically generated code by the Sequelize CLI.
For reference, the full Sequelize models/index.js
looks like this;
'use strict';
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(__dirname + '/../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(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = sequelize['import'](path.resolve(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;
Instead of using __dirname
you can use node-app-root-path