0

Is it okay if I make models from copy-pasting? For instance, I made my first model User through Sequelize's model:generate. And then for other models, I just copy-pasted everything from the User model to my new models.

When doing db sync:

db.sequelize.sync({ force : true}).then(() => {
  console.log("Drop and re-sync db.")
})

The new models won't create new tables. So I'm thinking copy-pasting for new models won't work. Is this right?

User model:

'use strict';
module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('User', {
    id: { type: DataTypes.BIGINT, allowNull: false, unique: true, primaryKey: true },
    fbid: { type: DataTypes.BIGINT, allowNull: false, unique: true },
    email: DataTypes.STRING,
    first_name: DataTypes.STRING,
    last_name: DataTypes.STRING,
    photo_url: DataTypes.STRING
  }, {});
  User.associate = function(models) {
    // associations can be defined here
  };
  return User;
};

And generated from copy-paste + modification of user model, I have: Country model

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Country = sequelize.define('Country', {
    country_name: DataTypes.STRING
  }, {});
  Country.associate = function(models) {
    // associations can be defined here
  };
  return Country;
};

Index.js generated by the cli in my models:

'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')[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.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;

Then after the generated models, after executing model:generate I transferred the generated User.js into a User folder. Then do the copy-paste, not sure if moving the files affect the registering of the models I created by copy-paste.

enter image description here

Glenn Posadas
  • 12,555
  • 6
  • 54
  • 95
  • Show your other model definitions – Anatoly May 06 '20 at 20:21
  • I added them. :) – Glenn Posadas May 06 '20 at 20:35
  • How do you register models? – Anatoly May 06 '20 at 20:53
  • I'm not sure. But like I said in the post, I generated my user model using ```model:generate``` command and then the other models are just copy-paste. I just wanna know if I really have to do this command everytime I need to make a new model. Currently, this won't work, and I had to do that command again. – Glenn Posadas May 06 '20 at 20:57
  • Can you check how models are registered? You also can check db.sequelize and look if it contains all registered models before calling 'sync' – Anatoly May 06 '20 at 21:38
  • Is there an index.js generated by sequelize CLI? Or are you registering each model manually in the server.js? – Kasey Chang May 07 '20 at 05:09
  • There's index.js auto-generated by the CLI, then I moved the auto-generated User.js into a new User folder. Then proceed with copy-paste of models. Thanks for the help! I posted more stuff in my question. – Glenn Posadas May 07 '20 at 05:17
  • I am reasonably certain that index.js (which auto-loads and registers the models for you) will NOT work if you moved your models into their own subdirectories. Study its code yourself. Why move them in the first place? – Kasey Chang May 07 '20 at 12:45
  • Thanks for still replying. I remember that I did try NOT to move those new files into new folders, but still no tables were generated. I moved them to subfolders because I know that's the right thing to do? Like what if you get a couple of more models? We do that in the platform I came from (iOS). Maybe I should read up on best practices in setting up node-express project files/folders hierarchies. – Glenn Posadas May 07 '20 at 13:42

1 Answers1

0

Based on the comments above, I realized that my index.js file (generated by Sequelize) wasn't looking for model files that ARE placed in subfolders. Instead, it ONLY looks for the model files that are in the root folder - same level of the index.js file.

So I posted this question: Javascript get all files in a folder that are inside another fold

I had to use the package glob to make the index.js file look for subfolders too.

So my index.js's content is now:

'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')[env];
const db = {};
const glob = require("glob")

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);
}

const files = glob.sync(__dirname + "/*/*.js")

files.forEach(file => {
  const model = sequelize['import'](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;

To answer the specific question, NO. I don't have to do the commands all the time. I can copy-paste-edit new models instead.

Glenn Posadas
  • 12,555
  • 6
  • 54
  • 95