Reputation: 51941
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?
Upvotes: 29
Views: 28574
Reputation: 64363
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)
Upvotes: 13
Reputation: 2059
mongoose is probably the closest analogy though it's for a document store not a relational db like rails/active record
Upvotes: 0
Reputation: 1658
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
Upvotes: 14
Reputation: 627
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
There are several other ORM solutions for other DB, you can check them here http://search.npmjs.org/
Upvotes: 4