4

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?

1
  • I tried sequelize cli but listing me seed flag is missing Commented Mar 31, 2016 at 9:05

3 Answers 3

4

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, {});
        */
      }
    };
Sign up to request clarification or add additional context in comments.

4 Comments

if development:{seederStorage:'sequelize'} is not set in config.js either its fine
can I use bulkInsert() in a migration file?
Can you please add any example or documentations link of sequelize? Thanks in advance.
@maxwell yes you can
0
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');
  },
};

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

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

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.