From 0ed0378ead62a8c73cb975eb21b238d9d1193bc0 Mon Sep 17 00:00:00 2001 From: Jaime Leonardo Suncin Cruz Date: Thu, 11 Nov 2021 11:32:12 -0600 Subject: [PATCH 1/2] feat: add type definitions Update README.md to show an example with Typescript FIXES #8 --- README.md | 28 ++++++++++++++++++++++++++++ index.d.ts | 16 ++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 index.d.ts diff --git a/README.md b/README.md index 91385d4..1bfcb84 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,34 @@ fastify.listen(3000, err => { }) ``` +**With Typescript** + +```typescript +import fastify from 'fastify' +import fastifyMongoose, { FastifyMongooseOptions } from 'fastify-mongoose' + +const options: FastifyMongooseOptions = { + uri: 'mongodb://localhost/test_db', + name: 'mongo' // Optional, the name to decorate fastify +} + +fastify.register(fastifyMongoose, ) + +fastify.listen(3000, err => { + if (err) throw err + console.log(`server listening on ${fastify.server.address().port}`) +}) + +declare module 'fastify' { + import { Connection, ObjectId } from 'mongoose'; + + mongo: { // needs to be the same name in options + db: Connection; + ObjectId: ObjectId; + } +} +``` + ##### Breaking changes `"version": "0.3.0` and above plugin have ability to open connection with multiple different databases, for achieving this functionality it have to stop using default mongoose connection. Onward please use db client returned by this plugin. diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..dfca357 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,16 @@ +import { FastifyPlugin } from 'fastify'; +import { ConnectOptions } from 'mongoose'; + +export interface FastifyMongooseOptions extends ConnectOptions { + /** + * Connection string to your MongoDB instance + */ + uri: string; + /** + * Name of the plugin, default: mongo + */ + name?: string; +} + +export const fastifyMongoose: FastifyPlugin; +export default fastifyMongoose; From 26010c8bcf1667a515fe257913392ac9d6b37da9 Mon Sep 17 00:00:00 2001 From: Jaime Leonardo Suncin Cruz Date: Thu, 11 Nov 2021 12:11:59 -0600 Subject: [PATCH 2/2] docs: fix error in the TS example --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1bfcb84..4ed3890 100644 --- a/README.md +++ b/README.md @@ -45,9 +45,11 @@ fastify.listen(3000, err => { declare module 'fastify' { import { Connection, ObjectId } from 'mongoose'; - mongo: { // needs to be the same name in options - db: Connection; - ObjectId: ObjectId; + interface FastifyInstance { + mongo: { // needs to be the same name in options + db: Connection; + ObjectId: ObjectId; + } } } ```