为什么设置中的顺序这么难?

Why is the order in the Setup so hard?

我基于库中包含的 node-orm2 示例项目构建了一个 node express 应用程序。我有一个讨厌的 'bug',我的 return 值看起来乱七八糟。

一个"User"只有一个"Permission":User.hasOne('permission', db.models.permission, {required: true, autoFetch: true}); "Company" 有一个所有者,但这是相反的,所以 "User" 有多个 "Companies"。 Company.hasOne('user', db.models.user, { required: true, reverse: 'companies', autoFetch: true});

第一次设置:

function setup(db, cb) {
 require('./company')(orm, db);
 require('./user')(orm, db);
 require('./permission')(orm, db);

 return cb(null, db);
}

搞砸了结果:

{
  "id": 1,
  "username": "l.vanbuiten",
  "name": "Lex",
  "surname": "van Buiten",
  "age": "1992-05-13T22:00:00.000Z",
  "createdAt": "2015-12-21T23:25:04.000Z",
  "permission": {
    "id": 1,
    "username": "l.vanbuiten",
    "name": "Lex",
    "surname": "van Buiten",
    "age": "1992-05-13T22:00:00.000Z",
    "createdAt": "2015-12-21T23:25:04.000Z",
    "permission_id": 1
  },
  "companies": []
}

交换这些设置行后,一切如我所愿:

应该如何:

function setup(db, cb) {
 require('./permission')(orm, db);
 require('./user')(orm, db);
 require('./company')(orm, db);

 return cb(null, db);
}

想要的结果

{
  "id": 1,
  "username": "l.vanbuiten",
  "name": "Lex",
  "surname": "van Buiten",
  "age": "1992-05-13T22:00:00.000Z",
  "createdAt": "2015-12-21T23:25:04.000Z",
  "permission": {
    "id": 1,
    "name": "permission name"
  },
  "companies": [
    {
      "id": 1,
      "name": "company name",
      "description": "company description",
      "createdAt": "2015-12-21T23:25:25.000Z"
    }
  ]
}

一个示例模型:

var moment = require('moment');

module.exports = function (orm, db) {
var Company = db.define('company', {
    id:             {type: 'serial', key: true},
    name:           {type: 'text', required: true, unique: true},
    description:    {type: 'text', big: true},
    createdAt:  {type: 'date', required: true, time: true}
},
{
    cache: false,
    hooks: {
        beforeValidation: function () {
            this.createdAt = new Date();
        }
    },
    validations: {
        name: [
            orm.enforce.unique("Company already exist"),
            orm.enforce.ranges.length(1, undefined, "Company name cannot be empty"),
            orm.enforce.ranges.length(undefined, 50, "Company name is to long")
        ]
    },
    methods: {
        serialize: function () {
            return {
                id: this.id,
                name: this.name,
                description: this.description,
                createdAt: this.createdAt
            }
        }
    }
});
Company.hasOne('user', db.models.user, { required: true, reverse: 'companies', autoFetch: true});
};

怎么顺序相差这么大?

(免责声明:我没有很多 node-orm2 经验)

在我看来,您设置关联的方式导致了您的问题。

在您的第一个设置中,您定义模型和关联的顺序如下:

  • 创建 Company 模型
  • 通过 db.models.user 创建 CompanyUser 之间的关联(这还不存在!)
  • 创建 User 模型
  • 等等

相反,我认为您必须等到所有模型都已定义后才能创建关联:

function setup(db, cb) {
  // Create models.
  var Permission = require('./permission')(orm, db);
  var User       = require('./user')(orm, db);
  var Company    = require('./company')(orm, db);

  // Create associations.
  Company.hasOne('user', User, { required: true, reverse: 'companies', autoFetch: true});
  ...

  // Return.
  return cb(null, db);
}

(这还假定每个所需模型文件中的导出函数 returns 模型,例如 company.js 末尾的 return Company)。