UNPKG

fastify-mongoose-rest

Version:

Rest API generator tools for fastify and mongoose

80 lines 2.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const helpers_1 = require("../helpers"); function Search(name, model, options) { let body = {}; let response = {}; if (options === null || options === void 0 ? void 0 : options.validationSchema) { body = { type: 'object', properties: { ...options.validationSchema, }, }; delete body.properties._id; response = helpers_1.createResponseSchema(options.validationSchema, 'array'); } return { method: 'POST', url: `/${name}/search`, schema: { summary: `Search ${name}`, tags: (options === null || options === void 0 ? void 0 : options.tags) || [], body: { type: 'object', properties: { query: { type: 'object', 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.body; const operation = model.find(query || {}); const operationCount = await model.find(query || {}).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 = Search; //# sourceMappingURL=search.js.map