为什么安装程序中的订单如此之难?

时间:2015-12-22 00:09:14

标签: mysql node.js node-orm2

我根据包含在库中的node-orm2示例项目构建了一个node express应用程序。我有一个讨厌的'bug',我的返回值看起来很混乱。

“用户”只有一个“权限”:User.hasOne('permission', db.models.permission, {required: true, autoFetch: true}); 而“公司”有一个所有者,但这是相反的,所以“用户”有多个“公司”。 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});
};

订单差异如此之大?

1 个答案:

答案 0 :(得分:1)

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

在我看来,你建立联想的方式正在引发你的问题。

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

  • 创建Company模型
  • 通过Company创建Userdb.models.user之间的关联(目前还不存在!)
  • 创建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);
}

(这也假设每个所需模型文件中的导出函数返回模型,例如return Company末尾的company.js