28

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

次のようなエレガントなクエリ コードを使用できます。

latest_10_topics_from_friends = current_user.friend_topics.limit(10)

最適化された SQL を生成します。node.js生態系に似たようなものはありますか?

4

4 に答える 4

14

ウォーターラインはあなたが探しているもののようです。これは、Sails Project の背後にいる同じ人によって作成されました。

https://github.com/balderdashy/waterline

于 2013-11-12T18:54:32.780 に答える
12

更新された回答

続編を使う


古い答え:

Towerプロジェクトを見てください。モデルを次のように定義できます。

# 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"

これで、次のようにデータをクエリできるようになります

current_user.friend_topics().limit(10)
于 2012-07-09T19:35:04.990 に答える
5

MySqlを使用している場合は、Sequelize.jsを試すことができます。ActiveRecordが提供する機能の数に到達するのは難しいですが、それでも、私はSequelizeとNode.jsの優れたソリューションを使用してきました。

他のDB用のORMソリューションは他にもいくつかあります。http://search.npmjs.org/で確認できます。

于 2012-07-09T11:00:36.543 に答える
0

mongooseはおそらく最も近いアナロジーですが、rails/active record のようなリレーショナル データベースではなく、ドキュメント ストア用です。

于 2014-02-26T21:52:33.597 に答える