Skip to content Skip to sidebar Skip to footer

Knex Seed: Insert Out Of Order From Promise

I'm trying to seed my knex data into my postgres database. However, I noticed that inserts from one Promise.all([...]) sometimes gets inserted into the previous or latter Promise.a

Solution 1:

You should use mapSeries from bluebird which insert data on order. As knex is using bluebird behind, you can use it as-

exports.seed = function (knex, Promise) {
  // Deletes ALL existing entries
  return knex('users').del()
    .then(function () {
      // Inserts seed entries
      const users = [{ username: 'u1', password: '1', access: 'regular' }];
      return Promise.mapSeries(users, (u) => {
        return knex('users')
          .insert(u);
      })
    });
};

Post a Comment for "Knex Seed: Insert Out Of Order From Promise"