|
| 1 | +# Node.js PostgreSQL CRUD example with Express Rest APIs |
| 2 | + |
| 3 | +Full Article with implementation: |
| 4 | +> [Node.js PostgreSQL CRUD example with Express Rest APIs](https://www.bezkoder.com/node-express-sequelize-postgresql/) |
| 5 | +
|
| 6 | +We will build Rest Apis that can create, retrieve, update, delete and find Tutorials by title. |
| 7 | + |
| 8 | +The following table shows overview of the Rest APIs that will be exported: |
| 9 | + |
| 10 | +- GET `api/tutorials` get all Tutorials |
| 11 | +- GET `api/tutorials/:id` get Tutorial by id |
| 12 | +- POST `api/tutorials` add new Tutorial |
| 13 | +- PUT `api/tutorials/:id` update Tutorial by id |
| 14 | +- DELETE `api/tutorials/:id` remove Tutorial by id |
| 15 | +- DELETE `api/tutorials` remove all Tutorials |
| 16 | +- GET `api/tutorials/published` find all published Tutorials |
| 17 | +- GET `api/tutorials?title=[kw]` find all Tutorials which title contains 'kw' |
| 18 | + |
| 19 | +Front-end that works well with this Back-end |
| 20 | +> [Axios Client](https://www.bezkoder.com/axios-request/) |
| 21 | +
|
| 22 | +> [Angular 8](https://www.bezkoder.com/angular-crud-app/) / [Angular 10](https://www.bezkoder.com/angular-10-crud-app/) / [Angular 11](https://www.bezkoder.com/angular-11-crud-app/) / [Angular 12](https://www.bezkoder.com/angular-12-crud-app/) / [Angular 13](https://www.bezkoder.com/angular-13-crud-example/) / [Angular 14](https://www.bezkoder.com/angular-14-crud-example/) / [Angular 15](https://www.bezkoder.com/angular-15-crud-example/) / [Angular 16](https://www.bezkoder.com/angular-16-crud-example/) |
| 23 | +
|
| 24 | +> [Vue 2 Client](https://www.bezkoder.com/vue-js-crud-app/) / [Vue 3 Client](https://www.bezkoder.com/vue-3-crud/) / [Vuetify Client](https://www.bezkoder.com/vuetify-data-table-example/) |
| 25 | +
|
| 26 | +> [React Client](https://www.bezkoder.com/react-crud-web-api/) / [React Redux Client](https://www.bezkoder.com/react-redux-crud-example/) |
| 27 | +
|
| 28 | +## Demo Video |
| 29 | +This is our Node.js PostgreSQL CRUD example using Express & Sequelize application demo, test Rest Apis with Postman. |
| 30 | + |
| 31 | +[](http://www.youtube.com/watch?v=x1pZHN_sjGk "Node.js PostgreSQL CRUD example Github") |
| 32 | + |
| 33 | +### Test the APIs |
| 34 | +Run our Node.js application with command: `node server.js`. |
| 35 | + |
| 36 | +Using Postman, we're gonna test all the Apis above. |
| 37 | + |
| 38 | +- Create a new Tutorial using `POST /tutorials` Api |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +After creating some new Tutorials, you can check PostgreSQL table: |
| 43 | +```testdb=# select * from tutorials; |
| 44 | + id | title | description | published | createdAt | updatedAt |
| 45 | +----+-------------+-------------------+-----------+----------------------------+---------------------------- |
| 46 | + 1 | Node Tut #1 | Tut#1 Description | f | 2020-01-29 10:42:57.121+07 | 2020-01-29 10:42:57.121+07 |
| 47 | + 2 | Node Tut #2 | Tut#2 Description | f | 2020-01-29 10:43:05.131+07 | 2020-01-29 10:43:05.131+07 |
| 48 | + 3 | Node Tut #3 | Tut#3 Description | f | 2020-01-29 10:43:48.028+07 | 2020-01-29 10:43:48.028+07 |
| 49 | + 4 | Js Tut #4 | Tut#4 Desc | f | 2020-01-29 10:45:40.016+07 | 2020-01-29 10:45:40.016+07 |
| 50 | + 5 | Js Tut #5 | Tut#5 Desc | f | 2020-01-29 10:45:44.289+07 | 2020-01-29 10:45:44.289+07 |
| 51 | +``` |
| 52 | + |
| 53 | +- Retrieve all Tutorials using `GET /tutorials` Api |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +- Retrieve a single Tutorial by id using `GET /tutorials/:id` Api |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +- Update a Tutorial using `PUT /tutorials/:id` Api |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +Check `tutorials` table after some rows were updated: |
| 66 | +```testdb=# select * from tutorials; |
| 67 | + id | title | description | published | createdAt | updatedAt |
| 68 | +----+----------------+-------------------+-----------+----------------------------+---------------------------- |
| 69 | + 1 | Node Tut #1 | Tut#1 Description | f | 2020-01-29 10:42:57.121+07 | 2020-01-29 10:42:57.121+07 |
| 70 | + 3 | Node Tut #3 | Tut#3 Description | f | 2020-01-29 10:43:48.028+07 | 2020-01-29 10:43:48.028+07 |
| 71 | + 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 |
| 72 | + 4 | Js Tut #4 | Tut#4 Desc | t | 2020-01-29 10:45:40.016+07 | 2020-01-29 10:54:17.468+07 |
| 73 | + 5 | Js Tut #5 | Tut#5 Desc | t | 2020-01-29 10:45:44.289+07 | 2020-01-29 10:54:20.544+07 |
| 74 | +``` |
| 75 | + |
| 76 | +- Find all Tutorials which title contains 'js': `GET /tutorials?title=js` |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +- Find all published Tutorials using `GET /tutorials/published` Api |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +- Delete a Tutorial using `DELETE /tutorials/:id` Api |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +Tutorial with id=4 was removed from `tutorials` table: |
| 89 | +```testdb=# select * from tutorials; |
| 90 | + id | title | description | published | createdAt | updatedAt |
| 91 | +----+----------------+-------------------+-----------+----------------------------+---------------------------- |
| 92 | + 1 | Node Tut #1 | Tut#1 Description | f | 2020-01-29 10:42:57.121+07 | 2020-01-29 10:42:57.121+07 |
| 93 | + 3 | Node Tut #3 | Tut#3 Description | f | 2020-01-29 10:43:48.028+07 | 2020-01-29 10:43:48.028+07 |
| 94 | + 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 |
| 95 | + 5 | Js Tut #5 | Tut#5 Desc | t | 2020-01-29 10:45:44.289+07 | 2020-01-29 10:54:20.544+07 |
| 96 | +``` |
| 97 | + |
| 98 | +- Delete all Tutorials using `DELETE /tutorials` Api |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +Now there are no rows in `tutorials` table: |
| 103 | +```testdb=# select * from tutorials; |
| 104 | + id | title | description | published | createdAt | updatedAt |
| 105 | +----+-------+-------------+-----------+-----------+----------- |
| 106 | +``` |
| 107 | + |
| 108 | +For more detail, please visit: |
| 109 | +> [Node.js PostgreSQL CRUD example with Express Rest APIs](https://www.bezkoder.com/node-express-sequelize-postgresql/) |
| 110 | +
|
| 111 | +> [Node.js Express Pagination with PostgreSQL example](https://www.bezkoder.com/node-js-pagination-postgresql/) |
| 112 | +
|
| 113 | +Security: |
| 114 | +> [Node.js JWT Authentication & Authorization with PostgreSQL example](https://www.bezkoder.com/node-js-jwt-authentication-postgresql/) |
| 115 | +
|
| 116 | +Associations: |
| 117 | +> [Sequelize Associations: One-to-Many Relationship example](https://www.bezkoder.com/sequelize-associate-one-to-many/) |
| 118 | +
|
| 119 | +> [Sequelize Associations: Many-to-Many Relationship example](https://www.bezkoder.com/sequelize-associate-many-to-many/) |
| 120 | +
|
| 121 | +Fullstack: |
| 122 | +> [Vue + Node.js + Express + PostgreSQL example](https://www.bezkoder.com/vue-node-express-postgresql/) |
| 123 | +
|
| 124 | +> [React + Node.js + Express + PostgreSQL example](https://www.bezkoder.com/react-node-express-postgresql/) |
| 125 | +
|
| 126 | +> [Angular 8 + Node.js + Express + PostgreSQL example](https://www.bezkoder.com/angular-node-express-postgresql/) |
| 127 | +
|
| 128 | +> [Angular 10 + Node.js + Express + PostgreSQL example](https://www.bezkoder.com/angular-10-node-express-postgresql/) |
| 129 | +
|
| 130 | +> [Angular 11 + Node.js + Express + PostgreSQL example](https://www.bezkoder.com/angular-11-node-js-express-postgresql/) |
| 131 | +
|
| 132 | +> [Angular 12 + Node.js + Express + PostgreSQL example](https://www.bezkoder.com/angular-12-node-js-express-postgresql/) |
| 133 | +
|
| 134 | +> [Angular 13 + Node.js + Express + PostgreSQL example](https://www.bezkoder.com/angular-13-node-js-express-postgresql/) |
| 135 | +
|
| 136 | +> [Angular 14 + Node.js + Express + PostgreSQL example](https://www.bezkoder.com/angular-14-node-js-express-postgresql/) |
| 137 | +
|
| 138 | +> [Angular 15 + Node.js + Express + PostgreSQL example](https://www.bezkoder.com/angular-15-node-js-express-postgresql/) |
| 139 | +
|
| 140 | +> [Angular 16 + Node.js + Express + PostgreSQL example](https://www.bezkoder.com/angular-16-node-js-express-postgresql/) |
| 141 | +
|
| 142 | +Integration (run back-end & front-end on same server/port): |
| 143 | +> [Integrate React with Node.js Restful Services](https://www.bezkoder.com/integrate-react-express-same-server-port/) |
| 144 | +
|
| 145 | +> [Integrate Angular with Node.js Restful Services](https://www.bezkoder.com/integrate-angular-12-node-js/) |
| 146 | +
|
| 147 | +> [Integrate Vue with Node.js Restful Services](https://www.bezkoder.com/serve-vue-app-express/) |
| 148 | +
|
| 149 | +## Project setup |
| 150 | +``` |
| 151 | +npm install |
| 152 | +``` |
| 153 | + |
| 154 | +### Run |
| 155 | +``` |
| 156 | +node server.js |
| 157 | +``` |
0 commit comments