【问题标题】:Model with multiple Edges using Sails.js and OrientDB使用 Sails.js 和 OrientDB 的多边模型
【发布时间】:2015-01-28 00:31:06
【问题描述】:

我是 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
    }
  }
};

我希望每辆载具能够有多种颜色。例如

  • VehicleA -> 红色
  • VehicleA -> 蓝色
  • VehicleA -> 黄色

文档显示了如何设置一对一关系,但我无法找出正确的方法来进行一对多或多对多。我研究过使用这个适配器: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',      
    },
  }
};

然后在我的模型中存储这些边的数组?有一个更好的方法吗?提前致谢。

【问题讨论】:

    标签: node.js sails.js graph-databases orientdb waterline


    【解决方案1】:

    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

    添加以下属性,以便vehicle_color 了解车辆:

    cars: {
      collection: 'Vehicle',
      via: 'color'
    }
    

    鉴于您将使用多对多关联,您不需要为边缘创建模型,水线将为您创建它。

    应该可以,否则请告诉我。

    更新: waterline-orientdb 已更名为sails-orientdb。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多