Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 9a237a8

Browse files
raksivjyecusch
andauthored
Apply suggestions from code review
Co-authored-by: Jye Cusch <[email protected]>
1 parent 4074a84 commit 9a237a8

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

docs/guides/nodejs/nitric-and-pgsql.mdx

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@ preview:
3131
- sql-databases
3232
```
3333
34-
## Install PostgreSQL client
34+
## Add a PostgreSQL client
35+
36+
In this guide, we'll use the [node-postgres](https://github.com/brianc/node-postgres) library, but feel free to use another client of your choice and adjust the steps accordingly.
3537
3638
```bash
3739
npm install pg
3840
```
3941

4042
## Create a Nitric SQL Database and Connect to it
4143

42-
Establish a connection to a PostgreSQL database using Nitric’s `sql` feature and the `pg` library.
44+
Use Nitric’s `sql` feature to create a new SQL database, then use connection string and the `pg` library to connect to it.
4345

4446
```ts title: resources/db.ts
4547
import { sql } from '@nitric/sdk'
@@ -95,12 +97,10 @@ mainApi.get('/todos', async (ctx) => {
9597
})
9698

9799
// Insert a new todo
98-
mainApi.post('/todos/:id', async (ctx) => {
99-
const { id } = ctx.req.params
100+
mainApi.post('/todos/', async (ctx) => {
100101
const { text } = ctx.req.json()
101102
const client = await getClient()
102-
await client.query('INSERT INTO todos (id, text, done) VALUES ($1, $2, $3)', [
103-
parseInt(id),
103+
await client.query('INSERT INTO todos (text, done) VALUES ($1, $2)', [
104104
text,
105105
false,
106106
])
@@ -121,17 +121,10 @@ mainApi.patch('/todos/:id', async (ctx) => {
121121
mainApi.patch('/todos/:id/toggle', async (ctx) => {
122122
const { id } = ctx.req.params
123123
const client = await getClient()
124-
const todo = await client.query('SELECT done FROM todos WHERE id = $1', [
125-
parseInt(id),
126-
])
127-
128-
if (todo.rows.length > 0) {
129-
const newDone = !todo.rows[0].done
130-
await client.query('UPDATE todos SET done = $1 WHERE id = $2', [
131-
newDone,
132-
parseInt(id),
133-
])
134-
}
124+
125+
await client.query('UPDATE todos SET done = NOT done WHERE id = $1',
126+
[parseInt(id)]
127+
);
135128
})
136129

137130
// Delete a todo
@@ -156,7 +149,7 @@ Using the dashboard, apply your migrations from the databases tab to initialize
156149

157150
### Add some todos using the Nitric dashboard
158151

159-
Open the local dashboard at <a target="_blank" href="http://localhost:49152">localhost:49152</a>, then navigate to the `POST /todos/{id}` endpoint and fill in an `id` as the path param.
152+
Open the local dashboard at <a target="_blank" href="http://localhost:49152">localhost:49152</a>, then navigate to the `POST /todos` endpoint.
160153

161154
![Add id param for creating a todo](/docs/images/guides/nitric-and-pgsql/step-1.png)
162155

0 commit comments

Comments
 (0)