Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bulk insert using sequelize and postgres

Tags:

sequelize.js

Here I tried to insert bulk data in my sequelize db but it creates problem. How to bulk insert using sequelize and postgres in angularjs?

like image 804
Shrikant Avatar asked Sep 02 '25 01:09

Shrikant


1 Answers

First of all create seed file using sequelize seed:create --name nameofseed it will create seeder folder in your root directory, add the seederStorage:'sequelize' in config.js of development:{seederStorage:'sequelize'} give the table name here is offer but in db it is offers and table fields in json objects. after creating seeder it needs to migrate and run respectively. here is command to run the seed: sequelize db:seed:all before it run the migrate command which is: sequelize db:migrate thats it.

 module.exports = {
      up: function(queryInterface, Sequelize) {
        queryInterface.bulkInsert('offers', [{
          ban: [1],
          sDate: '2016-03-31T08:00:10.354Z',
          eDate: '2016-03-31T08:00:10.354Z',
          isActive: true,
          reminder: false,
          sold: false,
          couponFor: 'Coupon Code',
          couponVal: 'Flat20%',
          createdAt: '2016-03-31T08:00:10.354Z',
          updatedAt: '2016-03-31T08:00:10.354Z'
        },
        {
          ban: [1, 2],
          sDate: '2016-03-31T08:00:10.354Z',
          eDate: '2016-03-31T08:00:10.354Z',
          isActive: true,
          reminder: false,
          sold: false,
          couponFor: 'Coupon Code',
          couponVal: 'Flat40%',
          createdAt: '2016-03-31T08:00:10.354Z',
          updatedAt: '2016-03-31T08:00:10.354Z'
        },
        {
          ban: [1, 2],
          sDate: '2016-03-31T08:00:10.354Z',
          eDate: '2016-03-31T08:00:10.354Z',
          isActive: true,
          reminder: false,
          sold: false,
          couponFor: 'Coupon Code',
          couponVal: 'Flat60%',
          createdAt: '2016-03-31T08:00:10.354Z',
          updatedAt: '2016-03-31T08:00:10.354Z'
        },
        {
          ban: [1],
          sDate: '2016-03-31T08:00:10.354Z',
          eDate: '2016-03-31T08:00:10.354Z',
          isActive: true,
          reminder: false,
          sold: false,
          couponFor: 'Coupon Code',
          couponVal: 'Flat100%',
          createdAt: '2016-03-31T08:00:10.354Z',
          updatedAt: '2016-03-31T08:00:10.354Z'
        }], {});
        /*
          Add altering commands here.
          Return a promise to correctly handle asynchronicity.

          Example:
          return queryInterface.bulkInsert('Person', [{
            name: 'John Doe',
            isBetaMember: false
          }], {});
        */
      },

      down: function(queryInterface, Sequelize) {
        /*
          Add reverting commands here.
          Return a promise to correctly handle asynchronicity.

          Example:
          return queryInterface.bulkDelete('Person', null, {});
        */
      }
    };
like image 154
Shrikant Avatar answered Sep 05 '25 15:09

Shrikant