KnexJS Migration With Associated Seed Data

独自空忆成欢 提交于 2019-12-05 14:08:56

Knex.js's seed functionality does not provide any order of execution guarantees. Each seed should be written such that it can be executed in isolation - ie. your single file approach is correct.

If you want to break your individual seed files into submodules, then you might try the following:

// initial-data.js
var operatingSystems = require('./initial-data/operating-systems.js');
var servers = require('./initial-data/servers.js');
exports.seed = function(knex, Promise) {
  return operatingSystems.seed(knex, Promise)
  .then(function () {
    return servers.seed(knex, Promise);
  }).then(function() {
    // next ordered migration...
  });
}

I use the sql-fixtures module to handle FK relation dependencies in my seed file(s).

Imaginary implementation:

const dataSpec = {
  applications_servers: [{
    name: 'My ASP.Net thingie',
    application_id: 'applications:0',
    server_id: 'servers:0'
  }],

  servers: [{
    name: 'My Windows server',
    operating_system_id: 'operating_systems:0'
  }],

  operating_systems: [{
    name: 'Windows Server 2k10'
  }],

  applications: [{
    name: 'My fab web guestbook',
    description: '...'
  }]
}

but it might be superfluous if you declare your dependencies by means of the ORM.

Sometimes however, it is desirable to avoid coupling the DB seeds to your exact current Model implementation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!