Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can use fs.readFile ? #102

Open
tkow opened this issue Jan 10, 2018 · 7 comments
Open

Can use fs.readFile ? #102

tkow opened this issue Jan 10, 2018 · 7 comments

Comments

@tkow
Copy link

tkow commented Jan 10, 2018

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?

@gamelaster
Copy link

Same here, how to do it the right way?

@trevorwang
Copy link

same here

1 similar comment
@0261
Copy link

0261 commented May 18, 2018

same here

@cliedeman
Copy link

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

@tkow
Copy link
Author

tkow commented May 19, 2018

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

@jamespegg
Copy link

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;

@5achinJani
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants