Node.js中有没有像Rails中的scaffold一样的东西?

时间:2021-07-12 12:21:13

Is there any tool or framework in Node.js that allows you to create a table in the DB and generate RESTful APIs automatically like Rails?

Node.js中是否有任何工具或框架允许您在数据库中创建表并像Rails一样自动生成RESTful API?

Thanks a lot!

非常感谢!

2 个解决方案

#1


27  

I guess it depends on what you want:

我想这取决于你想要什么:

  • Rails style code generation (where it generates code for you that you can modify)
  • Rails样式代码生成(它为您生成可以修改的代码)
  • Django style semi magic where admin forms can be derived from the fields in your table/document schema.
  • 管理表单的Django样式半魔法可以从表/文档模式中的字段派生。
  • you mean generating an API for these fields in the database, ala what grape does for Ruby -- you do some configuration and translating data to JSON and transmitting it back over the wire is taken care of for you
  • 你的意思是为数据库中的这些字段生成API,ala葡萄为Ruby做了什么 - 你做了一些配置并将数据转换为JSON并通过网络传回它是为你做的
  • You have a new project and you're looking for a framework with one of these three characteristics
  • 您有一个新项目,并且您正在寻找具有这三个特征之一的框架

For the first three options there are a few related SO questions on this already:

对于前三个选项,已经有一些相关的SO问题:

There are a few awesome answers in this set of links, including:

这组链接中有一些很棒的答案,包括:

  • @jsalonen's answer gathering up some tools <-- mostly about generating admin UIs
  • @ jsalonen回答收集一些工具< - 主要是关于生成管理UI
  • @Dow's answer with some tools <-- he mentions RailwayJS (now CompoundJS), then points to some other SO questions/answers on this topic.
  • @ Dow用一些工具回答< - 他提到了RailwayJS(现在是CompoundJS),然后指出了关于这个主题的其他一些SO问题/答案。
  • @bergie's answer about a module that exposes JugglingORM models as RESTful API routes automatically
  • @ bergie关于将JugglingORM模型自动暴露为RESTful API路由的模块的答案
  • Or, as @Andbdrew mentioned in the comments, there's node-restify
  • 或者,正如@Andbdrew在评论中提到的那样,有节点解析
  • You could write your own generator - perhaps you could write a Cake task that would take a template file, execute some Javascript and output a view for your application. I started down this path recently myself. Certainly with the variety of technologies Node.js apps involve (different ORM choices, Javascript front ends, CSS layouts) this may make sense.
  • 您可以编写自己的生成器 - 也许您可以编写一个Cake任务,该任务将获取模板文件,执行一些Javascript并为您的应用程序输出视图。我最近自己开始走这条路。当然,随着Node.js应用程序涉及的各种技术(不同的ORM选择,Javascript前端,CSS布局),这可能是有意义的。

For the last option - an opinionated rapid web development Node.js framework that provides good API support, there are a few options:

对于最后一个选项 - 提供良好API支持的自以为是的快速Web开发Node.js框架,有几个选项:

  • @abject_error's answer, in this question, about Sails
  • @ abject_error在这个问题中回答了关于Sails的问题
  • Geddy <-- automatically provides .json versions of the data your controller specifies (depending on the request it uses this data to render HTML views or creates a JSON representation)
  • Geddy < - 自动提供控制器指定的数据的.json版本(取决于它使用此数据呈现HTML视图或创建JSON表示的请求)
  • There's a nice slide-show on How to quickly make REST APIs with CompoundJS, so CompoundJs may fit your tastes.
  • 关于如何使用CompoundJS快速制作REST API,有一个很好的幻灯片放映,因此CompoundJs可能符合您的口味。

I initially left these off as I assumed you may have an existing project, or didn't want to use an opinionated framework, but added them because why not.

我最初将这些关闭,因为我认为你可能有一个现有的项目,或者不想使用一个固定的框架,但添加它们,因为为什么不。

#2


15  

SailsJS (https://github.com/balderdashy/sails) is exactly what you're looking for. It uses the Waterline module for simulating ActiveRecord. Just define a resource, and it will automatically create JSON endpoints for all RESTful actions, and also makes the endpoints compatible with Socket.io connections.

SailsJS(https://github.com/balderdashy/sails)正是您所需要的。它使用Waterline模块来模拟ActiveRecord。只需定义一个资源,它就会自动为所有RESTful操作创建JSON端点,并使端点与Socket.io连接兼容。

It's built on Express, so you can use any Connect middleware to extend it. AFAIK it's the best NodeJS framework for building API backends.

它基于Express构建,因此您可以使用任何Connect中间件来扩展它。 AFAIK是构建API后端的最佳NodeJS框架。

Defining a new resource is as simple as sails generate user. This will create the appropriate model and controller files, which you can then modify.

定义新资源就像sails生成用户一样简单。这将创建适当的模型和控制器文件,然后您可以修改它们。

Keep in mind Sails was built with schema-less datastores in mind, so it's primary support is for MongoDB, not MySQL. I'm not sure how well (if at all) it supports SQL databases.

请记住,Sails在构建时考虑了无架构数据存储,因此它主要支持MongoDB,而不是MySQL。我不确定它是否支持SQL数据库(如果有的话)。

#1


27  

I guess it depends on what you want:

我想这取决于你想要什么:

  • Rails style code generation (where it generates code for you that you can modify)
  • Rails样式代码生成(它为您生成可以修改的代码)
  • Django style semi magic where admin forms can be derived from the fields in your table/document schema.
  • 管理表单的Django样式半魔法可以从表/文档模式中的字段派生。
  • you mean generating an API for these fields in the database, ala what grape does for Ruby -- you do some configuration and translating data to JSON and transmitting it back over the wire is taken care of for you
  • 你的意思是为数据库中的这些字段生成API,ala葡萄为Ruby做了什么 - 你做了一些配置并将数据转换为JSON并通过网络传回它是为你做的
  • You have a new project and you're looking for a framework with one of these three characteristics
  • 您有一个新项目,并且您正在寻找具有这三个特征之一的框架

For the first three options there are a few related SO questions on this already:

对于前三个选项,已经有一些相关的SO问题:

There are a few awesome answers in this set of links, including:

这组链接中有一些很棒的答案,包括:

  • @jsalonen's answer gathering up some tools <-- mostly about generating admin UIs
  • @ jsalonen回答收集一些工具< - 主要是关于生成管理UI
  • @Dow's answer with some tools <-- he mentions RailwayJS (now CompoundJS), then points to some other SO questions/answers on this topic.
  • @ Dow用一些工具回答< - 他提到了RailwayJS(现在是CompoundJS),然后指出了关于这个主题的其他一些SO问题/答案。
  • @bergie's answer about a module that exposes JugglingORM models as RESTful API routes automatically
  • @ bergie关于将JugglingORM模型自动暴露为RESTful API路由的模块的答案
  • Or, as @Andbdrew mentioned in the comments, there's node-restify
  • 或者,正如@Andbdrew在评论中提到的那样,有节点解析
  • You could write your own generator - perhaps you could write a Cake task that would take a template file, execute some Javascript and output a view for your application. I started down this path recently myself. Certainly with the variety of technologies Node.js apps involve (different ORM choices, Javascript front ends, CSS layouts) this may make sense.
  • 您可以编写自己的生成器 - 也许您可以编写一个Cake任务,该任务将获取模板文件,执行一些Javascript并为您的应用程序输出视图。我最近自己开始走这条路。当然,随着Node.js应用程序涉及的各种技术(不同的ORM选择,Javascript前端,CSS布局),这可能是有意义的。

For the last option - an opinionated rapid web development Node.js framework that provides good API support, there are a few options:

对于最后一个选项 - 提供良好API支持的自以为是的快速Web开发Node.js框架,有几个选项:

  • @abject_error's answer, in this question, about Sails
  • @ abject_error在这个问题中回答了关于Sails的问题
  • Geddy <-- automatically provides .json versions of the data your controller specifies (depending on the request it uses this data to render HTML views or creates a JSON representation)
  • Geddy < - 自动提供控制器指定的数据的.json版本(取决于它使用此数据呈现HTML视图或创建JSON表示的请求)
  • There's a nice slide-show on How to quickly make REST APIs with CompoundJS, so CompoundJs may fit your tastes.
  • 关于如何使用CompoundJS快速制作REST API,有一个很好的幻灯片放映,因此CompoundJs可能符合您的口味。

I initially left these off as I assumed you may have an existing project, or didn't want to use an opinionated framework, but added them because why not.

我最初将这些关闭,因为我认为你可能有一个现有的项目,或者不想使用一个固定的框架,但添加它们,因为为什么不。

#2


15  

SailsJS (https://github.com/balderdashy/sails) is exactly what you're looking for. It uses the Waterline module for simulating ActiveRecord. Just define a resource, and it will automatically create JSON endpoints for all RESTful actions, and also makes the endpoints compatible with Socket.io connections.

SailsJS(https://github.com/balderdashy/sails)正是您所需要的。它使用Waterline模块来模拟ActiveRecord。只需定义一个资源,它就会自动为所有RESTful操作创建JSON端点,并使端点与Socket.io连接兼容。

It's built on Express, so you can use any Connect middleware to extend it. AFAIK it's the best NodeJS framework for building API backends.

它基于Express构建,因此您可以使用任何Connect中间件来扩展它。 AFAIK是构建API后端的最佳NodeJS框架。

Defining a new resource is as simple as sails generate user. This will create the appropriate model and controller files, which you can then modify.

定义新资源就像sails生成用户一样简单。这将创建适当的模型和控制器文件,然后您可以修改它们。

Keep in mind Sails was built with schema-less datastores in mind, so it's primary support is for MongoDB, not MySQL. I'm not sure how well (if at all) it supports SQL databases.

请记住,Sails在构建时考虑了无架构数据存储,因此它主要支持MongoDB,而不是MySQL。我不确定它是否支持SQL数据库(如果有的话)。