Knex Seed: insert out of order from promise

2019-07-25 16:32发布

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.all([..]) table.

exports.seed = function(knex, Promise) {
  return Promise.all([
    knex('users').del(),
    knex('songs').del(),
    ]).then(() => {
      return Promise.all([
        knex('users').insert({username: 'u1', password: '1', access: 'regular'}),
        knex('users').insert({username: 'u2', password: '1', access: 'regular'}),
        knex('users').insert({username: 'a', password: '1', access: 'admin'})
        ])
    }).then(() => {
        return knex('playlists').del().then(() => {
          return Promise.all([
            knex('playlists').insert({playlist_name: 'p1', user_id: 2, create_date: '2016-03-07 10:00:00'})
            .returning('playlist_id').then(function(ids) {
              return Promise.all([
                knex('songs').insert({song_name: 'Don\'t Blame Me', artist_name: 'Taylor Swift', album_name: 'Reputation', release_date: '2017-11-10', genre: 'Electricpop'}),      
                knex('songs').insert({song_name: 'Talking To The Moon', artist_name: 'Bruno Mars', album_name: 'It\'s Better If You Don\'t Understand', release_date: '2010-05-11', genre: 'Pop'}),
                knex('songs').insert({song_name: 'Catch a Grenade (The Hooligans Remix)', artist_name: 'Bruno Mars', album_name: 'The Grenade Session', release_date: '2011-02-08', genre: 'Pop'}),
                knex('songs').insert({song_name: 'Grenade', artist_name: 'Bruno Mars', album_name: 'The Grenade Session', release_date: '2011-02-08', genre: 'Pop'}),
                knex('songs').insert({song_name: 'Grenade (Acoustic)', artist_name: 'Bruno Mars', album_name: 'The Grenade Session', release_date: '2011-02-08', genre: 'Pop'}),
                knex('songs').insert({song_name: 'Grenade (Passion Pit Remix)', artist_name: 'Bruno Mars', album_name: 'The Grenade Session', release_date: '2011-02-08', genre: 'Pop'}),
              ])
            }),
            knex('playlists').insert({playlist_name: 'p2', user_id: 1, create_date: '2017-03-07 09:00:00'})
            .returning('playlist_id').then(function(ids) {
              return Promise.all([
                knex('songs').insert({song_name: 'Uptown', artist_name: 'Drake', album_name: 'So Far Gone', release_date: '2009-01-19', genre: 'Hip-hop'}),
                knex('songs').insert({song_name: 'I\'m Goin\' In', artist_name: 'Drake', album_name: 'So Far Gone', release_date: '2009-01-19', genre: 'Hip-hop'}),
                knex('songs').insert({song_name: 'Calm', artist_name: 'Drake', album_name: 'So Far Gone', release_date: '2009-01-19', genre: 'Hip-hop'}),
                knex('songs').insert({song_name: 'Fear', artist_name: 'Drake', album_name: 'So Far Gone', release_date: '2009-01-19', genre: 'Hip-hop'}),
                knex('songs').insert({song_name: 'God\'s Plan', artist_name: 'Drake', album_name: 'Scary Hours', release_date: '2018-01-19', genre: 'Hip-hop'}),
                knex('songs').insert({song_name: 'Diplomatic Immunity', artist_name: 'Drake', album_name: 'Scary Hours', release_date: '2018-01-19', genre: 'Hip-hop'}),
                knex('songs').insert({song_name: 'Housetatlantavegas', artist_name: 'Drake', album_name: 'So Far Gone', release_date: '2009-09-15', genre: 'Hip-hop'}),
                knex('songs').insert({song_name: 'Successful', artist_name: 'Drake', album_name: 'So Far Gone', release_date: '2009-01-19', genre: 'Hip-hop'}),
                knex('songs').insert({song_name: 'Best I Ever Had', artist_name: 'Drake', album_name: 'So Far Gone', release_date: '2009-01-19', genre: 'Hip-hop'}),
              ])
            }),
            knex('playlists').insert({playlist_name: 'p3', user_id: 1, create_date: '2018-01-05 08:00:00'})
            .returning('playlist_id').then(function(ids) {
              return Promise.all([
                knex('songs').insert({song_name: 'Look What You Made Me Do', artist_name: 'Taylor Swift', album_name: 'Reputation', release_date: '2017-11-10', genre: 'Electricpop'}),
                knex('songs').insert({song_name: 'So it Goes...', artist_name: 'Taylor Swift', album_name: 'Reputation', release_date: '2017-11-10', genre: 'Electricpop'}),
                knex('songs').insert({song_name: 'Gorgeous', artist_name: 'Taylor Swift', album_name: 'Reputation', release_date: '2017-11-10', genre: 'Electricpop'}),
                knex('songs').insert({song_name: 'Getaway Car', artist_name: 'Taylor Swift', album_name: 'Reputation', release_date: '2017-11-10', genre: 'Electricpop'}),
                knex('songs').insert({song_name: 'King of My Heart', artist_name: 'Taylor Swift', album_name: 'Reputation', release_date: '2017-11-10', genre: 'Electricpop'}),
                knex('songs').insert({song_name: 'Dancing With Our Hands Tied', artist_name: 'Taylor Swift', album_name: 'Reputation', release_date: '2017-11-10', genre: 'Electricpop'}),
                knex('songs').insert({song_name: 'Delicate', artist_name: 'Taylor Swift', album_name: 'Reputation', release_date: '2017-11-10', genre: 'Electricpop'}),
                knex('songs').insert({song_name: '...Ready for it?', artist_name: 'Taylor Swift', album_name: 'Reputation', release_date: '2017-11-10', genre: 'Electricpop'}),
              ])
            })
          ])
      });
    }).then(() => {
      return knex('playlists_songs').del().then(() => {
        return Promise.all([
          knex('playlists_songs').insert({playlist_id: 1, song_id:1}),
          knex('playlists_songs').insert({playlist_id: 1, song_id:2}),
          knex('playlists_songs').insert({playlist_id: 1, song_id:3}),
          knex('playlists_songs').insert({playlist_id: 1, song_id:4}),
          knex('playlists_songs').insert({playlist_id: 1, song_id:5}),
          knex('playlists_songs').insert({playlist_id: 1, song_id:6}),
          knex('playlists_songs').insert({playlist_id: 1, song_id:7}),
          knex('playlists_songs').insert({playlist_id: 1, song_id:8}),
          knex('playlists_songs').insert({playlist_id: 1, song_id:9}),
          knex('playlists_songs').insert({playlist_id: 1, song_id:10}),
          knex('playlists_songs').insert({playlist_id: 2, song_id:11}),
          knex('playlists_songs').insert({playlist_id: 2, song_id:12}),
          knex('playlists_songs').insert({playlist_id: 2, song_id:13}),
          knex('playlists_songs').insert({playlist_id: 2, song_id:14}),
          knex('playlists_songs').insert({playlist_id: 2, song_id:15}),
          knex('playlists_songs').insert({playlist_id: 2, song_id:16}),
          knex('playlists_songs').insert({playlist_id: 2, song_id:17}),
          knex('playlists_songs').insert({playlist_id: 3, song_id:18}),
          knex('playlists_songs').insert({playlist_id: 3, song_id:19}),
          knex('playlists_songs').insert({playlist_id: 3, song_id:20}),
          knex('playlists_songs').insert({playlist_id: 3, song_id:21}),
          knex('playlists_songs').insert({playlist_id: 3, song_id:22}),
          knex('playlists_songs').insert({playlist_id: 3, song_id:23}),
        ])
      });
    })
};

When running knex seed:run, songs that should go into p2 ends up into p1.

What should I do to make the seeds run in order? I tried chaining the playlist one after another with then but the same problem still occurs (possible that I chained incorrectly).

All suggestions would be appreciated.

Thanks!

1条回答
Animai°情兽
2楼-- · 2019-07-25 16:58

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);
      })
    });
};
查看更多
登录 后发表回答