【问题标题】:What would be a translation of Ruby On Rails to Sequelize (or other ORMs) in Node.js? [closed]Ruby On Rails 到 Node.js 中的 Sequelize(或其他 ORM)的翻译是什么? [关闭]
【发布时间】:2015-08-06 03:11:29
【问题描述】:

如果我从 Ruby on Rails 切换到 Sequelize,我会使用哪些等效代码。

如果我想使用同构 JavaScript,我可能会想为 Node.js 使用 ORM...

在我花太多时间在一个与另一个上之前,有什么建议吗?

我查看了 Sequelize、ORM NPM、Active Record Npm,它们看起来都不错,但我不确定哪个效果最好。如果有人可以向我展示一些对他们的节点等效 ORM 的翻译,我将能够快速实现它并了解很多关于该 ORM 的工作原理。谢谢。

如果有人可以提供一个简短指南,说明我如何将这个 Ruby on Rails 代码转换为 Sequelize JS 或 Bookshelf JS 的等效位,我可以决定哪个看起来最方便。

  def show
    @pet = Pet.find_by(id: params[:id])
    if @pet
      render json: @pet
    else
      render status: 400, nothing: true
    end
  end
  def update
    @pet = Pet.find_by(id: params[:id])
    if @pet.update(pet_params)
      render json: @pet
    else
      render status: 400, nothing: true
    end
  end
private
  def pet_params
    params.require(:pet).permit(:name, :species, :author_id)
  end

这会让我的生活更轻松,谢谢。

【问题讨论】:

    标签: javascript ruby-on-rails node.js postgresql orm


    【解决方案1】:

    我相信 sequelize 拥有最好的文档,正如您可能在其他答案中看到的那样。

    app.get("Projects/:id", function (res,req){
    var particularProj=Project.findById(params.id).then(function(project) {
      // project will be an instance of Project and stores the content of the table entry with id 123. if such an entry is not defined you will get null
    
          res.render("show.ejs", {proj: particularProj}
       });
    });
    

    类似于 Ruby on Rails Active Record 中的以下内容:

    def show
        proj=Project.find(params[:id]);
        @proj=proj
    end
    

    【讨论】:

    • 谢谢,我想我可以编辑我的问题以更紧密地匹配它,因为这正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    相关资源
    最近更新 更多