@prairielearn/postgres 1.7.6 → 1.7.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +36 -5
- package/package.json +8 -8
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -79,7 +79,7 @@ console.log(result.rows);
|
|
|
79
79
|
|
|
80
80
|
The `queryAsync` function returns a [`pg.Result`](https://node-postgres.com/apis/result) object; see linked documentation for a list of additional properties that are available on that object.
|
|
81
81
|
|
|
82
|
-
There are
|
|
82
|
+
There are also utility methods that can make assertions about the results:
|
|
83
83
|
|
|
84
84
|
- `queryOneRowAsync`: Throws an error if the result doesn't have exactly one row.
|
|
85
85
|
- `queryZeroOrOneRowAsync`: Throws an error if the result has more than one row.
|
|
@@ -116,7 +116,7 @@ For increased safety and confidence, you can describe the shape of data you expe
|
|
|
116
116
|
|
|
117
117
|
```ts
|
|
118
118
|
import { z } from 'zod';
|
|
119
|
-
import { loadSqlEquiv,
|
|
119
|
+
import { loadSqlEquiv, queryRows, queryRow, queryOptionalRow } from '@prairielearn/postgres';
|
|
120
120
|
|
|
121
121
|
const sql = loadSqlEquiv(import.meta.url);
|
|
122
122
|
|
|
@@ -126,11 +126,40 @@ const User = z.object({
|
|
|
126
126
|
age: z.number(),
|
|
127
127
|
});
|
|
128
128
|
|
|
129
|
-
|
|
130
|
-
|
|
129
|
+
// Get all users. Returns an array of objects.
|
|
130
|
+
const users = await queryRows(sql.select_users, User);
|
|
131
|
+
|
|
132
|
+
// Get single user. Returns a single object.
|
|
133
|
+
const user = await queryRow(sql.select_user, { user_id: '1' }, User);
|
|
134
|
+
|
|
135
|
+
// Get a user that may not exist. Returns `null` if the user cannot be found.
|
|
136
|
+
const maybeUser = await queryOptionalRow(sql.select_user, { user_id: '1' }, User);
|
|
131
137
|
```
|
|
132
138
|
|
|
133
|
-
|
|
139
|
+
These functions have some behaviors that can make them more convenient to work with:
|
|
140
|
+
|
|
141
|
+
- Passing an object with parameters is optional.
|
|
142
|
+
|
|
143
|
+
- If the query returns a single column, that column is validated and returned directly. For example, consider the following query:
|
|
144
|
+
|
|
145
|
+
```sql
|
|
146
|
+
-- BLOCK select_user_names
|
|
147
|
+
SELECT
|
|
148
|
+
name
|
|
149
|
+
FROM
|
|
150
|
+
users;
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
If we then use that query with `queryRows`, the returned Promise resolves to an array of strings:
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
const userNames = await queryRows(sql.select_user_names, z.string());
|
|
157
|
+
|
|
158
|
+
// Prints something like `["Alice", "Bob"]`.
|
|
159
|
+
console.log(userNames);
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
There are also a number of legacy functions available, though these are discouraged in new code.
|
|
134
163
|
|
|
135
164
|
- `queryValidatedRows`
|
|
136
165
|
- `queryValidatedOneRow`
|
|
@@ -142,6 +171,8 @@ As with the non-validated query functions, there are several variants available:
|
|
|
142
171
|
- `callValidatedOneRow`
|
|
143
172
|
- `callValidatedZeroOrOneRow`
|
|
144
173
|
|
|
174
|
+
For details on the behavior of these functions, see the source code.
|
|
175
|
+
|
|
145
176
|
### Transactions
|
|
146
177
|
|
|
147
178
|
To use transactions, wrap your queries with the `runInTransactionAsync` function:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prairielearn/postgres",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.8",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -14,21 +14,21 @@
|
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"@prairielearn/tsconfig": "^0.0.0",
|
|
17
|
-
"@types/mocha": "^10.0.
|
|
18
|
-
"@types/multipipe": "^3.0.
|
|
19
|
-
"@types/node": "^18.
|
|
17
|
+
"@types/mocha": "^10.0.3",
|
|
18
|
+
"@types/multipipe": "^3.0.3",
|
|
19
|
+
"@types/node": "^18.18.7",
|
|
20
20
|
"mocha": "^10.2.0",
|
|
21
21
|
"ts-node": "^10.9.1",
|
|
22
22
|
"typescript": "^5.2.2"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@types/debug": "^4.1.
|
|
26
|
-
"@types/lodash": "^4.14.
|
|
27
|
-
"@types/pg-cursor": "^2.7.
|
|
25
|
+
"@types/debug": "^4.1.10",
|
|
26
|
+
"@types/lodash": "^4.14.200",
|
|
27
|
+
"@types/pg-cursor": "^2.7.1",
|
|
28
28
|
"multipipe": "^4.0.0",
|
|
29
29
|
"pg": "^8.11.3",
|
|
30
30
|
"pg-cursor": "^2.10.3",
|
|
31
31
|
"pg-pool": "^3.6.1",
|
|
32
|
-
"zod": "^3.22.
|
|
32
|
+
"zod": "^3.22.4"
|
|
33
33
|
}
|
|
34
34
|
}
|