使用 Waterline ORM / SailsJS 查询连接结果



>我有 2 个模型:

  • 操作
  • 米(不应该是复数,但现在就省略它;)

一米有很多操作。

我应该如何进行手术。名字知道 Meter.ID ?

这是我的 2 个模型:

操作:

module.exports = {
attributes: {
Id: {type: 'string'}, // operation_id
name: {type: 'string', required: true},
meters: {
collection: 'meters',
via: 'operation'
}
},
};

米:

module.exports = {
attributes: {
Id: {type: 'string', required: true, unique: true}, // meter_id
name: {type: 'string', required: true},
state: {type: 'number', required: true},
operation: {
model: 'operation',
}
},
};
// myApp/api/models/meter.js
// A meter may have many operations
module.exports = {
attributes: {
Id: {type: 'string', required: true, unique: true}, // meter_id
name: {type: 'string', required: true},
state: {type: 'number', required: true},
// Add a reference to operations
operations: {
collection: 'operations',
via: 'meter'
}
}
};

// myApp/api/models/operation.js
// A operation may only belong to a single meter
module.exports = {
attributes: {
Id: {type: 'string'}, // operation_id
name: {type: 'string', required: true},
// Add a reference to User
meter: {
model: 'meter'
}
}
};

var meters = await Meters.find(id: 123).populate('operations');
// The meters object would look something like the following
// [{
//   id: 123,
//   name: 'Foo',
//   state: '1',
//   meters: [{
//     id: 1,
//     name: 'mymeter',
//     user: 123
//   }]
// }]

希望这对你:)有所帮助

相关内容

  • 没有找到相关文章

最新更新