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?
ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.
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.
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.
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
Use sequelize
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With