node.js 世界中的 ActiveRecord 等价物是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11393438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is ActiveRecord equivalence in the node.js world?
提问by ohho
I am considering to use node.jstools for a upcoming project, for learning and performance purpose. For example, some models in Rails:
我正在考虑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
allows elegant query code like:
允许优雅的查询代码,如:
latest_10_topics_from_friends = current_user.friend_topics.limit(10)
and generates optimized SQLs. Is there something similar in node.jsecosystem?
并生成优化的 SQL。node.js生态系统中是否有类似的东西?
采纳答案by Harish Shetty
Updated answer
更新答案
Use sequelize
使用续集
Old answer:
旧答案:
Look at the Towerproject. You can define your models as follows:
看看塔项目。您可以按如下方式定义模型:
# 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"
Now you will be able to query your data as
现在您将能够查询您的数据
current_user.friend_topics().limit(10)
回答by Juanda
Waterline seems to be the thing that you're looking for. It was created by the same guys behind the Sails Project.
水线似乎是你正在寻找的东西。它是由 Sails 项目背后的同一个人创建的。
回答by Javo
If you are using MySql, you can try Sequelize.js. It's hard to reach the number of features that ActiveRecord offers, but nevertheless, I have been working with Sequelize and its a nice solution for Node.js
如果你使用的是 MySql,你可以试试 Sequelize.js。很难达到 ActiveRecord 提供的功能数量,但尽管如此,我一直在使用 Sequelize,它是 Node.js 的一个不错的解决方案
There are several other ORM solutions for other DB, you can check them here http://search.npmjs.org/
其他数据库还有其他几个 ORM 解决方案,您可以在这里查看它们http://search.npmjs.org/

