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

ohh*_*hho 22 activerecord ruby-on-rails node.js

我正在考虑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
Run Code Online (Sandbox Code Playgroud)

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

latest_10_topics_from_friends = current_user.friend_topics.limit(10)
Run Code Online (Sandbox Code Playgroud)

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

Jua*_*nda 13

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

https://github.com/balderdashy/waterline


Har*_*tty 11

更新的答案

使用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"
Run Code Online (Sandbox Code Playgroud)

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

current_user.friend_topics().limit(10)
Run Code Online (Sandbox Code Playgroud)

  • 对于截至2015年阅读此内容的任何人来说,Tower现在根据他们的文档没有维护,没有新的提交1 - 2年.继续看...... (3认同)
  • Sequelize 是 Node 对 orm 的回答,但与 ActiveRecord 相比,它是垃圾。Activerecord 很简单,sequelize 很冗长。 (3认同)

Jav*_*avo 5

如果您使用的是MySql,可以尝试使用Sequelize.js.很难达到ActiveRecord提供的功能数量,但是,我一直在使用Sequelize,它是Node.js的一个不错的解决方案.

其他DB还有其他几种ORM解决方案,您可以在这里查看http://search.npmjs.org/