backpack icon indicating copy to clipboard operation
backpack copied to clipboard

Can use fs.readFile ?

Open tkow opened this issue 6 years ago • 7 comments

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?

tkow avatar Jan 10 '18 08:01 tkow

Same here, how to do it the right way?

gamelaster avatar Feb 03 '18 23:02 gamelaster

same here

trevorwang avatar Apr 28 '18 08:04 trevorwang

same here

0261 avatar May 18 '18 13:05 0261

You can use a plugin like https://github.com/quadric/babel-plugin-inline-import to swap fs.read for a require statement.

cliedeman avatar May 18 '18 14:05 cliedeman

@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.

tkow avatar May 19 '18 12:05 tkow

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;

jamespegg avatar Oct 28 '18 14:10 jamespegg

Instead of using __dirname you can use node-app-root-path

5achinJani avatar May 02 '19 13:05 5achinJani