简体   繁体   English

在 SailsJS 模型中嵌套对象

[英]Nesting objects in SailsJS models

how to put the full object workstation in another object userWorkstationSchedule ?如何将完整的对象workstation放在另一个对象userWorkstationSchedule

code:代码:

Workstation.js工作站.js

module.exports = {
    tableName: 'workstation',
    attributes: {
        name: { type: 'string', required: true, maxLength: 100, columnType: 'VARCHAR(100)' },

        company: { model: 'company', required: true },

        workDates: { collection: 'workstationSchedule', via: 'workstation'},
        reservedDates: {collection: 'userWorkstationSchedule', via: 'workstation'},

      }
}

reservedDates attribute receives data from the userWorkstationSchedule object UserWorkstationSchedule.js reservedDates属性从userWorkstationSchedule对象 UserWorkstationSchedule.js 接收数据

module.exports = {
    tableName: 'user_workstation_schedule',
    attributes: {
          presenceSchedules: { collection: 'presenceschedule', via: 'userWorkstationSchedule' },
          workstation: {collection: 'workstation', via: 'reservedDates'},
  }
}

You don't need to put "the full object", he is already in, and that's the power of Sails.js and of any ORM.你不需要放入“完整的对象”,他已经在里面了,这就是 Sails.js 和任何 ORM 的力量。

Data fecthing is made asynchronously and "lazy".数据处理是异步和“惰性的”。 (More info about LazyLoading ) (有关LazyLoading 的更多信息)

The ORM load only what is absolutely necessary and not all the linked associations. ORM 只加载绝对必要的内容,而不是所有链接的关联。

In Sails.js, when you need a particular data, you need to populate it via the .populate() method.在 Sails.js 中,当您需要特定数据时,您需要通过 .populate() 方法填充它。

var userWorkstationSchedule = await UserWorkstationSchedule.find().populate('workstation');

You can find more about populate in Sails.JS Documentation您可以在Sails.JS 文档中找到有关 populate 的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM