我是 Sails.js 和 OrientDB 的新手。我试图弄清楚如何使用 sails-orientDB 创建具有多个边缘的模型适配器。
我有一个存储基本车辆信息和车辆颜色的车辆模型:
module.exports = {
schema: true,
tableName: 'Vehicle',
attributes: {
id: {
type: 'string',
primaryKey: true,
columnName: '@rid'
},
make: {
type: 'string',
unique: false,
required: true
},
model: {
type: 'string',
unique: false,
required: true
},
year: {
type: 'int',
int: true,
unique: false,
required: true
},
color: {
collection: 'Vehicle_Color',
via: 'color',
edge: 'vehicleColor'
}
}
};
和车辆颜色模型:
module.exports = {
schema: true,
tableName: 'Vehicle_Color',
attributes: {
id: {
type: 'string',
primaryKey: true,
columnName: '@rid'
},
name: {
type: 'string',
unique: true,
required: true
},
hexCode: {
type: 'string',
hexColor: true
}
}
};
我希望每辆车都能有多种颜色。例如
- 车辆A -> 红色
- 车辆A -> 蓝色
- 车辆A -> 黄色
文档显示了如何建立一对一关系,但我无法找出进行一对多或多对多关系的正确方法。我研究过使用这个适配器:npmjs.com/package/waterline-orientdb,但这看起来否定了使用图形数据库的好处(github.com/appscot/waterline-orientdb/issues/29)。
我是否可以简单地创建一个边缘模型,例如:
module.exports = {
schema: true,
tableName: 'Vehicle_Color',
attributes: {
vehicleID: {
type: 'string',
},
colorID: {
type: 'string',
},
}
};
然后在我的模型中存储这些边的数组?有一个更好的方法吗?提前致谢。
最佳答案
Waterline-orientdb 刚刚更新( v0.10.40 ),现在它支持多对多关联中的边(除了多对多直通)。多对多关联更容易处理,所以我推荐它们。型号规范与waterline-docs中描述的相同.
回到您的具体案例。
车辆型号
您应该删除 edge 关键字,因为在处理嵌套关联时它可能会导致如下异常:
Error: Unknown rule: edge
at Object.matchRule (/Users/warchild/Development/node/waterline-orientdb/node_modules/waterline/node_modules/anchor/lib/match/matchRule.js:37:11)
您可以使用以下外部属性来重命名边缘(否则水线会将其命名为vehicleColor_color__Vehicle_Color_cars之类的名称):
joinTableNames: {
color: 'has_color' // I've used a different name to distinguish from Color Model
},
并将颜色属性替换为:
color: {
collection: 'Vehicle_Color',
via: 'cars',
dominant: true
}
车辆颜色
添加以下属性,以便vehicle_color了解车辆:
cars: {
collection: 'Vehicle',
via: 'color'
}
鉴于您将使用多对多关联,您不需要为边缘创建模型,waterline 将为您创建它。
应该可以了,否则请告诉我。
更新: Waterline-orientdb 已更名为 sails-orientdb。
关于node.js - 使用 Sails.js 和 OrientDB 进行多边建模,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28182657/