Guest User

Untitled

a guest
Jan 20th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. import { Model } from "mongoose";
  2. import * as Mongoose from "mongoose";
  3. import { VehicleModel, Vehicle } from "./models/vehicle";
  4.  
  5. import * as fp from "fastify-plugin";
  6.  
  7. export interface Models {
  8. Vehicle: Model<VehicleModel>;
  9. }
  10.  
  11. export interface Db {
  12. models: Models;
  13. }
  14.  
  15. export default fp(async (fastify, opts: { uri: string }, next) => {
  16. Mongoose.connection.on("connected", () => {
  17. fastify.log.info({ actor: "MongoDB" }, "connected");
  18. });
  19.  
  20. Mongoose.connection.on("disconnected", () => {
  21. fastify.log.error({ actor: "MongoDB" }, "disconnected");
  22. });
  23.  
  24. await Mongoose.connect(
  25. opts.uri,
  26. {
  27. useNewUrlParser: true,
  28. keepAlive: 1
  29. }
  30. );
  31.  
  32. const models: Models = {
  33. Vehicle: Vehicle
  34. };
  35.  
  36. fastify.decorate("db", { models });
  37.  
  38. next();
  39. });
Add Comment
Please, Sign In to add comment