node.js - 使用 Sails.js 和 OrientDB 进行多边建模

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

我是 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/

相关文章:

socket.io - 在 sails.js 中跟踪用户在线/离线状态

neo4j - 在 Neo4j Cypher 中通过 ID 删除关系的简单方法?

javascript - 将 XML 解析为 JSON UTF-8

node.js - Gulp-autoprefixer 抛出 ReferenceError : Promise is not defined

node.js - Bigtable 尝试确定行是否存在,模拟器因行不存在而挂起(在 row.exists() 上)

graph-databases - 用于匹配边缘属性的 ArangoDB AQL FILTER 图

gremlin - 在 gremlin 中的示例方法中传递动态值不起作用

javascript - PostgreSQL 错误 : connection terminated

node.js - Treeline 中的关系模型

node.js - sails.js(使用 node.js 和 express.js)的标题或应用名称在哪里?