Sunday, 11 June 2017

sequelize after create exit

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

[{
    title: 'xyz1',
    url: 'https://www.example.com/xyz1'
}, {
    title: 'xyz2',
    url: 'https://www.example.com/xyz2'
}].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



via ARIF MAHMUD RANA

No comments:

Post a Comment