[英]Model with multiple Edges using Sails.js and OrientDB
我是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
}
}
};
我希望每辆车都能有多种颜色。 例如
该文档显示了如何建立一对一关系,但是我无法弄清楚进行一对多或多对多的正确方法。 我研究了使用此适配器的方法: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属性替换为:
color: {
collection: 'Vehicle_Color',
via: 'cars',
dominant: true
}
添加以下属性,以便vehicle_color了解车辆:
cars: {
collection: 'Vehicle',
via: 'color'
}
由于您将使用多对多关联,因此不需要为边缘创建模型,水线会为您创建模型。
应该这样做,否则让我知道。
更新: waterline-orientdb已重命名为sails-orientdb。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.