什么是Node.js世界中的ActiveRecord等价?

我正在考虑为即将到来的项目使用node.js工具,用于学习和性能目的。 例如 ,Rails中的一些模型:

 class User has_many :topics has_many :friends has_many :friend_users, :through => :friends has_many :friend_topics, :through => :friend_users, :source => :topics end class Friend belongs_to :user belongs_to :friend_user, :class_name => "User", :foreign_key => :phone_no, :primary_key => :phone_no end class Topic belongs_to :user end 

允许优雅的查询代码,如:

 latest_10_topics_from_friends = current_user.friend_topics.limit(10) 

并生成优化的SQL。 在node.js生态系统中有类似的东西吗?

更新了答案

使用sequelize


老答案:

看看Tower项目。 你可以定义你的模型如下:

 # app/models/user.coffee class App.User extends Tower.Model @belongsTo "author", type: "User" @belongsTo "commentable", polymorphic: true @has_many "topics" @has_many "friends" @has_many "friend_users", through: "friends" @has_many "friend_topics", through: "friends_users", source: "topics" # app/models/friend.coffee class App.Friend extends Tower.Model @belongs_to "user" @belongs_to "friend_user", type: "User", foreign_key: "phone_no", primary_key: "phone_no" # app/models/topic.coffee class App.Topic extends Tower.Model @belongs_to "user" 

现在您将能够查询您的数据

 current_user.friend_topics().limit(10) 

水线似乎是你正在寻找的东西。 它是由Sails项目背后的同一个人创build的。

https://github.com/balderdashy/waterline

如果你正在使用MySql,你可以试试Sequelize.js。 很难达到ActiveRecord提供的function的数量,但是,我一直在使用Sequelize和它的一个很好的Node.js解决scheme

其他数据库还有其他几种ORM解决scheme,你可以在http://search.npmjs.org/查看。

mongoose可能是最接近的类比,虽然它是一个文件存储而不是关系型数据库,如rails / active record