Skip to content

Instantly share code, notes, and snippets.

@PavelPolyakov
Created January 19, 2019 11:08
Show Gist options
  • Save PavelPolyakov/89b9da3965c5fbdab9fb2052a1569a3b to your computer and use it in GitHub Desktop.
Save PavelPolyakov/89b9da3965c5fbdab9fb2052a1569a3b to your computer and use it in GitHub Desktop.
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();
});
@PavelPolyakov
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment