Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is ActiveRecord equivalence in the node.js world?

I am considering to use node.js tools for a upcoming project, for learning and performance purpose. For example, some models in 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.js ecosystem?

like image 906
ohho Avatar asked Jul 09 '12 10:07

ohho


People also ask

What does ActiveRecord base mean?

ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.

Is ActiveRecord an ORM?

ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.

Can you use ActiveRecord without rails?

One of the primary aspects of ActiveRecord is that there is very little to no configuration needed. It follow convention over configuration. ActiveRecord is commonly used with the Ruby-on-Rails framework but you can use it with Sinatra or without any web framework if desired.


2 Answers

Waterline seems to be the thing that you're looking for. It was created by the same guys behind the Sails Project.

https://github.com/balderdashy/waterline

like image 59
Juanda Avatar answered Oct 02 '22 03:10

Juanda


Updated answer

Use sequelize


Old answer:

Look at the Tower project. 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) 
like image 23
Harish Shetty Avatar answered Oct 02 '22 03:10

Harish Shetty