Shrikant
Shrikant

Reputation: 538

How to bulk insert using sequelize and postgres

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?

Upvotes: 4

Views: 5845

Answers (3)

Hussain Ghazali
Hussain Ghazali

Reputation: 1

This how you can bulkInsert () using migrations for more reference see this repository https://github.com/hussainghazali/Migrations-with-value-nodeJS

Upvotes: 0

Hussain Ghazali
Hussain Ghazali

Reputation: 1

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface
      .createTable('tests', {
        id: { type: Sequelize.INTEGER, autoIncrement: false, primaryKey: true },
        name: { type: Sequelize.STRING },
        code: { type: Sequelize.STRING },
        imageUrl: { type: Sequelize.STRING },
        groupId: { type: Sequelize.INTEGER, allowNull: true },
        createdAt: { type: Sequelize.DATE, allowNull: true },
        updatedAt: { type: Sequelize.DATE, allowNull: true },
      })
      .then(() => {
        queryInterface.bulkInsert('tests', [
          {
            id: 1,
            name: 'Test One',
            code: 'TC',
            imageUrl: 'https://google.com',
            groupId: null,
            createdAt: new Date(),
            updatedAt: new Date(),
          },
          {
            id: 2,
            name: 'Test One',
            code: 'SCC',
            imageUrl: 'https://google.com',
            groupId: null,
            createdAt: new Date(),
            updatedAt: new Date(),
          },
          {
            id: 3,
            name: 'Test One',
            code: 'CM',
            imageUrl: 'https://google.com',
            groupId: null,
            createdAt: new Date(),
            updatedAt: new Date(),
          },
          {
            id: 4,
            name: 'Test One',
            code: 'UM',
            imageUrl: 'https://google.com',
            groupId: null,
            createdAt: new Date(),
            updatedAt: new Date(),
          },
          {
            id: 5,
            name: 'Test One',
            code: 'DDM',
            imageUrl: 'https://google.com',
            groupId: null,
            createdAt: new Date(),
            updatedAt: new Date(),
          },
          {
            id: 6,
            name: 'Test One',
            code: '',
            imageUrl: 'https://google.com',
            groupId: 3,
            createdAt: new Date(),
            updatedAt: new Date(),
          },
          {
            id: 7,
            name: 'Test One',
            code: '',
            imageUrl: 'https://google.com',
            groupId: 4,
            createdAt: new Date(),
            updatedAt: new Date(),
          },
        ]);
      });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('tests');
  },
};

Upvotes: 0

Shrikant
Shrikant

Reputation: 538

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, {});
        */
      }
    };

Upvotes: 4

Related Questions