Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typescript definitions #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,36 @@ 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';

interface FastifyInstance {
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;