Created
January 19, 2019 11:08
-
-
Save PavelPolyakov/89b9da3965c5fbdab9fb2052a1569a3b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Model } from "mongoose"; | |
import * as Mongoose from "mongoose"; | |
import { VehicleModel, Vehicle } from "./models/vehicle"; | |
import * as fp from "fastify-plugin"; | |
export interface Models { | |
Vehicle: Model<VehicleModel>; | |
} | |
export interface Db { | |
models: Models; | |
} | |
export default fp(async (fastify, opts: { uri: string }, next) => { | |
Mongoose.connection.on("connected", () => { | |
fastify.log.info({ actor: "MongoDB" }, "connected"); | |
}); | |
Mongoose.connection.on("disconnected", () => { | |
fastify.log.error({ actor: "MongoDB" }, "disconnected"); | |
}); | |
await Mongoose.connect( | |
opts.uri, | |
{ | |
useNewUrlParser: true, | |
keepAlive: 1 | |
} | |
); | |
const models: Models = { | |
Vehicle: Vehicle | |
}; | |
fastify.decorate("db", { models }); | |
next(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
refers to https://medium.com/car2godevs/fastify-with-typescript-production-ready-integration-2303318ecd9e