Node.js + PostgreSQL:CRUD 示例
在本教程中,我将逐步向您展示使用 Express 和 Sequelize ORM 构建 Node.js + PostgreSQL CRUD 示例。
全文:https ://bezkoder.com/node-express-sequelize-postgresql/
Node.js PostgreSQL CRUD 示例概述
我们将构建可以按标题创建、检索、更新、删除和查找教程的 Rest Apis。
首先,我们先创建一个 Express Web 服务器。接下来,我们添加 PostgreSQL 数据库的配置,Tutorial
使用 Sequelize 创建模型,并编写控制器。然后,我们定义用于处理所有 CRUD 操作(包括自定义查找器)的路由。
下表显示了将要导出的 Rest API 的概览:
方法 | 网址 | 行动 |
---|---|---|
得到 | API/教程 | 获取所有教程 |
得到 | api/教程/:id | 获取教程id |
邮政 | API/教程 | 添加新教程 |
放 | api/教程/:id | 更新教程id |
删除 | api/教程/:id | 删除教程id |
删除 | API/教程 | 删除所有教程 |
得到 | api/教程/已发布 | 查找所有已发布的教程 |
得到 | api/教程?标题=[kw] | 查找标题包含以下内容的所有教程'kw' |
最后,我们将使用 Postman 测试 Rest Apis。
这是我们的项目结构:
演示视频
这是我们使用 Express 和 Sequelize 应用程序演示的 Node.js PostgreSQL CRUD 示例,使用 Postman 测试 Rest Apis。
测试 API
使用命令运行我们的 Node.js 应用程序:node server.js
。
使用 Postman,我们将测试上述所有 API。
POST /tutorials
使用Api创建新教程
创建一些新的教程后,您可以检查 PostgreSQL 表:
testdb=# select * from tutorials;
id | title | description | published | createdAt | updatedAt
----+-------------+-------------------+-----------+----------------------------+----------------------------
1 | Node Tut #1 | Tut#1 Description | f | 2020-01-29 10:42:57.121+07 | 2020-01-29 10:42:57.121+07
2 | Node Tut #2 | Tut#2 Description | f | 2020-01-29 10:43:05.131+07 | 2020-01-29 10:43:05.131+07
3 | Node Tut #3 | Tut#3 Description | f | 2020-01-29 10:43:48.028+07 | 2020-01-29 10:43:48.028+07
4 | Js Tut #4 | Tut#4 Desc | f | 2020-01-29 10:45:40.016+07 | 2020-01-29 10:45:40.016+07
5 | Js Tut #5 | Tut#5 Desc | f | 2020-01-29 10:45:44.289+07 | 2020-01-29 10:45:44.289+07
GET /tutorials
使用Api检索所有教程
GET /tutorials/:id
使用Api通过 ID 检索单个教程
PUT /tutorials/:id
使用Api更新教程
tutorials
更新某些行后检查表:
testdb=# select * from tutorials;
id | title | description | published | createdAt | updatedAt
----+----------------+-------------------+-----------+----------------------------+----------------------------
1 | Node Tut #1 | Tut#1 Description | f | 2020-01-29 10:42:57.121+07 | 2020-01-29 10:42:57.121+07
3 | Node Tut #3 | Tut#3 Description | f | 2020-01-29 10:43:48.028+07 | 2020-01-29 10:43:48.028+07
2 | Node Js Tut #2 | Tut#2 Description | t | 2020-01-29 10:43:05.131+07 | 2020-01-29 10:51:55.235+07
4 | Js Tut #4 | Tut#4 Desc | t | 2020-01-29 10:45:40.016+07 | 2020-01-29 10:54:17.468+07
5 | Js Tut #5 | Tut#5 Desc | t | 2020-01-29 10:45:44.289+07 | 2020-01-29 10:54:20.544+07
- 查找标题包含“js”的所有教程:
GET /tutorials?title=js
GET /tutorials/published
使用Api查找所有已发布的教程
DELETE /tutorials/:id
使用Api删除教程
id=4 的教程已从表中删除tutorials
:
testdb=# select * from tutorials;
id | title | description | published | createdAt | updatedAt
----+----------------+-------------------+-----------+----------------------------+----------------------------
1 | Node Tut #1 | Tut#1 Description | f | 2020-01-29 10:42:57.121+07 | 2020-01-29 10:42:57.121+07
3 | Node Tut #3 | Tut#3 Description | f | 2020-01-29 10:43:48.028+07 | 2020-01-29 10:43:48.028+07
2 | Node Js Tut #2 | Tut#2 Description | t | 2020-01-29 10:43:05.131+07 | 2020-01-29 10:51:55.235+07
5 | Js Tut #5 | Tut#5 Desc | t | 2020-01-29 10:45:44.289+07 | 2020-01-29 10:54:20.544+07
DELETE /tutorials
使用Api删除所有教程
现在表中没有行tutorials
:
testdb=# select * from tutorials;
id | title | description | published | createdAt | updatedAt
----+-------+-------------+-----------+-----------+-----------
有关更多详细信息、实现和 Github,请访问:
https://bezkoder.com/node-express-sequelize-postgresql/
进一步阅读
服务器端分页:
Node.js Express 分页与 PostgreSQL 示例
全栈:
- Vue.js + Node.js + Express + PostgreSQL 示例
- Angular 8 + Node.js Express + PostgreSQL 示例
- Angular 10 + Node.js Express + PostgreSQL 示例
- Angular 11 + Node.js Express + PostgreSQL 示例
- Angular 12 + Node.js Express + PostgreSQL 示例
- Angular 13 + Node.js Express + PostgreSQL 示例
- React + Node.js + Express + PostgreSQL 示例
安全性:
使用 PostgreSQL 进行 Node.js JWT 身份验证和授权的示例