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 * as fastify from "fastify"; | |
import * as http from "http"; | |
declare module "fastify" { | |
export interface FastifyInstance< | |
HttpServer = http.Server, | |
HttpRequest = http.IncomingMessage, | |
HttpResponse = http.ServerResponse | |
> { | |
blipp(): void; |
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
"dev": "ts-node --files ./src/index.ts" |
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
npm i mongoose @types/mongodb @types/mongoose |
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
version: '3' | |
services: | |
mongodb: | |
image: mongo:3.6.0 | |
ports: | |
- "27017:27017" | |
volumes: | |
- dbvolume:/data | |
environment: { | |
AUTH: "no" |
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 { Document, Schema, Model, model } from "mongoose"; | |
export interface VehicleDocument extends Document { | |
year: number; | |
name: string; | |
createdDate: Date; | |
} | |
export interface VehicleModel extends VehicleDocument {} |
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>; | |
} |
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 * as fastify from "fastify"; | |
import * as http from "http"; | |
import { Db } from "../modules/db"; | |
declare module "fastify" { | |
export interface FastifyInstance< | |
HttpServer = http.Server, | |
HttpRequest = http.IncomingMessage, | |
HttpResponse = http.ServerResponse | |
> { |
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 * as fp from "fastify-plugin"; | |
export default fp(async (server, opts, next) => { | |
server.get("/vehicles/:id", {}, async (request, reply) => { | |
try { | |
const _id = request.params.id; | |
const vehicle = await server.db.models.Vehicle.findOne({ | |
_id | |
}); |
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
// at the top | |
import vehiclesRoutes from "./modules/routes/vehicles"; | |
import db from "./modules/db"; | |
// after server was created | |
server.register(db, { uri: "mongodb://localhost:27017/vehicles" }); | |
server.register(vehiclesRoutes); |
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
npm i jest ts-jest @types/jest |