Skip to content

Commit

Permalink
feat: add type definitions
Browse files Browse the repository at this point in the history
Update README.md to show an example with Typescript

FIXES Techie-Qabila#8
  • Loading branch information
leosuncin committed Nov 11, 2021
1 parent d2ce4c9 commit 0ed0378
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
16 changes: 16 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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<FastifyMongooseOptions>;
export default fastifyMongoose;

0 comments on commit 0ed0378

Please sign in to comment.