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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:00:24  来源:igfitidea点击:

What is ActiveRecord equivalence in the node.js world?

ruby-on-railsnode.jsactiverecord

提问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 项目背后的同一个人创建的。

https://github.com/balderdashy/waterline

https://github.com/balderdashy/waterline

回答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/

回答by bhurlow

mongooseis probably the closest analogy though it's for a document store not a relational db like rails/active record

mongoose可能是最接近的类比,尽管它用于文档存储而不是像 rails/active record 这样的关系数据库