UNPKG

fastify-mongoose-rest

Version:

Rest API generator tools for fastify and mongoose

76 lines 2.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const helpers_1 = require("../helpers"); function List(name, model, options) { let response = {}; if (options === null || options === void 0 ? void 0 : options.validationSchema) { response = helpers_1.createResponseSchema(options.validationSchema, 'array'); } return { method: 'GET', url: `/${name}`, schema: { summary: `List ${name}`, tags: (options === null || options === void 0 ? void 0 : options.tags) || [], querystring: { type: 'object', properties: { query: { type: 'string', description: 'Mongoose find query', }, populate: { type: 'string', description: 'Population options of mongoose', }, projection: { type: 'string', description: 'Projection options of mongoose', }, sort: { type: 'string', description: 'Sort options of mongoose', }, skip: { type: 'number', description: 'Mongoose skip property', }, limit: { type: 'number', description: 'Mongoose limit property', }, }, }, response, }, handler: async (request, reply) => { const { query, populate, projection, sort, skip, limit } = request.query; let qs = {}; if (query) { qs = JSON.parse(query); } const operation = model.find(qs); const operationCount = await model.find(qs).countDocuments(); if (populate) { operation.populate(helpers_1.parseInput(populate)); } if (projection) { operation.projection(helpers_1.parseInput(projection)); } if (sort) { operation.sort(helpers_1.parseInput(sort)); } if (skip) { operation.skip(skip); } if (limit) { operation.limit(limit); } const resource = await operation.exec(); reply.header('X-Total-Count', operationCount); return reply.send(resource); }, }; } exports.default = List; //# sourceMappingURL=list.js.map