You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 20, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: docs/guides/nodejs/nitric-and-pgsql.mdx
+11-18Lines changed: 11 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,15 +31,17 @@ preview:
31
31
- sql-databases
32
32
```
33
33
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.
35
37
36
38
```bash
37
39
npm install pg
38
40
```
39
41
40
42
## Create a Nitric SQL Database and Connect to it
41
43
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.
const todo =awaitclient.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
-
awaitclient.query('UPDATE todos SET done = $1 WHERE id = $2', [
131
-
newDone,
132
-
parseInt(id),
133
-
])
134
-
}
124
+
125
+
awaitclient.query('UPDATE todos SET done = NOT done WHERE id = $1',
126
+
[parseInt(id)]
127
+
);
135
128
})
136
129
137
130
// Delete a todo
@@ -156,7 +149,7 @@ Using the dashboard, apply your migrations from the databases tab to initialize
156
149
157
150
### Add some todos using the Nitric dashboard
158
151
159
-
Open the local dashboard at <atarget="_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 <atarget="_blank"href="http://localhost:49152">localhost:49152</a>, then navigate to the `POST /todos` endpoint.
160
153
161
154

0 commit comments