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
* Types: Add "pg" property to FastifyInstance
As recommended in the guide for creating TypeScript compatible Fastify plugins, properties that the Fastify instances are decorated with should also in the types get added to those instances: https://www.fastify.io/docs/latest/TypeScript/#creating-a-typescript-fastify-plugin
This change follows up on #63 by adding that.
* Document "name" option
* Enable use of non-docker postgres db in tests
* Fix tests when no error is thrown
* Throw if named and unnamed are combined
* Clarify that intention is to manually add type
As you can see there is no need to close the client, since is done internally. Promises and async await are supported as well.
149
149
150
+
### Name option
151
+
If you need to have multiple databases set up, then you can name each one of them by passing `name: 'foo'`. It will then be accessible as `fastify.pg.foo` instead of `fastify.pg`.
152
+
You can't use both named and an unnamed postgres conenction at once.
'SELECT id, username, hash, salt FROM users WHERE id=$1', [req.params.id],
165
+
functiononResult (err, result) {
166
+
reply.send(err || result)
167
+
}
168
+
)
169
+
})
170
+
171
+
fastify.listen(3000, err=> {
172
+
if (err) throw err
173
+
console.log(`server listening on ${fastify.server.address().port}`)
174
+
})
175
+
```
176
+
150
177
### Native option
151
178
If you want to gain the maximum performances you can install [pg-native](https://github.com/brianc/node-pg-native), and pass `native: true` to the plugin options.
152
179
*Note: it requires PostgreSQL client libraries & tools installed, see [instructions](https://github.com/brianc/node-pg-native#install).*
@@ -211,10 +238,24 @@ Install the compiler and typings for pg module:
211
238
npm install --save-dev typescript @types/pg
212
239
```
213
240
214
-
You can find examples in the [examples/typescript](./examples/typescript) directory.
241
+
Add the `pg` property to `FastifyInstance`. Like this if you use an unnamed instance:
242
+
243
+
```typescript
244
+
importtype { PostgresDb } from'fastify-postgres';
245
+
246
+
declaremodule'fastify' {
247
+
exportinterfaceFastifyInstance {
248
+
pg:PostgresDb;
249
+
}
250
+
}
251
+
```
252
+
253
+
More examples in the [examples/typescript](./examples/typescript) directory.
215
254
216
255
## Development and Testing
217
256
257
+
### Docker approach
258
+
218
259
First, start postgres with:
219
260
220
261
```
@@ -234,6 +275,18 @@ CREATE TABLE
234
275
$ npm test
235
276
```
236
277
278
+
### Custom Postgres approach
279
+
280
+
1. Set up a database of your choice oin a postgres server of your choice
281
+
2. Create the required table using
282
+
```sql
283
+
CREATETABLEusers(id serialPRIMARY KEY, username VARCHAR (50) NOT NULL);
284
+
```
285
+
3. Specify a connection string to it in a `DATABASE_TEST_URL` environment variable when you run the tests
286
+
```bash
287
+
DATABASE_TEST_URL="postgres://username:password@localhost/something_thats_a_test_database" npm test
0 commit comments