1

My Models

index.js

'use strict';

var fs        = require('fs');
var path      = require('path');
var Sequelize = require('sequelize');
var basename  = path.basename(module.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]);
} else {
  var sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
  .readdirSync(__dirname)
  .filter(function(file) {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(function(file) {
    var model = sequelize['import'](path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach(function(modelName) {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;

module.exports = db;

course.js

'use strict';
module.exports = function(sequelize, DataTypes) {
  var Course = sequelize.define('Course', {
    title: DataTypes.STRING,
    url: DataTypes.STRING,
    downloaded: DataTypes.BOOLEAN,
    date_published: DataTypes.DATEONLY
  }, {
    underscored: true,
    classMethods: {
      associate: function(models) {
        // associations can be defined here
      }
    }
  });
  return Course;
};

test.js

const Course = require('./models').Course
const courses = [
    {
        title: 'xyz1',
        url: 'https://www.example.com/xyz1'
    },
    {
        title: 'xyz2',
        url: 'https://www.example.com/xyz2'
    }
]


courses.forEach(item => Course.create(item).then(course => {}))

My Problem: Now when run node test.js it doesn't exit after code execution.

enter image description here

ARIF MAHMUD RANA
  • 5,026
  • 3
  • 31
  • 58

2 Answers2

2

When we are running a script from command line sequelize connection pool isn't closed that's why we need to call the sequelize.close()(http://docs.sequelizejs.com/manual/tutorial/upgrade-to-v4.html#general) to shutdown the pool on our finished jobs or run process.exit() which also will kill the pool.

In node untill all the work is done e.g event listening to something or any setInterval or like here any connection open(socket connection) execution will not be finished. That's why the execution wasn't finished here.

ARIF MAHMUD RANA
  • 5,026
  • 3
  • 31
  • 58
0

Try rewriting your code as -

const db = require('./models');
const Course = db.Course;

const courses = [
    {
        title: 'xyz1',
        url: 'https://www.example.com/xyz1'
    },
    {
        title: 'xyz2',
        url: 'https://www.example.com/xyz2'
    }
];

function _createCourses(courseList) {
   courseList.forEach(course => {
      Course.create(course).then(createdCourse => {
         console.log(createdCourse.get({
            plain: true
         }));
      });
   });
}

db.sequelize.sync()
  .then(() => {
     _createCourses(courses);
  });

The only difference here is we are calling sequalize.sync before creating the model objects. sync actually syncs all defined models in db. Check out the documentation for details - sequalize.sync

Munim
  • 2,626
  • 1
  • 19
  • 28