UNPKG

@joktec/mongo

Version:

JokTec - Mongo Service

71 lines 2.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildQueryMethod = buildQueryMethod; const utils_1 = require("@joktec/utils"); const typegoose_1 = require("@typegoose/typegoose"); const lodash_1 = require("lodash"); const helpers_1 = require("../helpers"); function search(keyword) { if (!keyword) return this; return this.find({ $text: { $search: keyword } }); } function center(near) { if ((0, lodash_1.isNil)(near) || (0, lodash_1.isEmpty)(near)) return this; const qb = this.find(); const paths = Object.keys(near); if (!paths.length) return qb; if (paths.length === 1) { const { lat, lng, distance } = near[paths[0]]; qb.near(paths[0], { center: [lng, lat], maxDistance: (0, utils_1.toInt)(distance, 1000) }); } paths.map(path => { const { lat, lng, distance } = near[path]; const radius = (0, utils_1.toInt)(distance, 1000) / 6378.1; const area = { center: [lng, lat], radius, unique: true }; return qb.where(path).within().circle(area); }); return qb; } function destroyOne(filter, options) { const paths = this.model.schema.paths; const isParanoid = Object.values(paths).some(schema => !!schema.options.deletedAt); if (!isParanoid || options?.force) { return this.findOneAndDelete(filter, { ...helpers_1.DELETE_OPTIONS, ...options }); } const bodyUpdate = Object.values(paths).reduce((body, schema) => { if (schema.options.deletedAt) body[schema.options.deletedAt] = new Date(); return body; }, {}); return this.findOneAndUpdate(filter, { $set: bodyUpdate }, { ...helpers_1.PARANOID_OPTIONS, ...options }); } function destroyMany(filter, options) { const paths = this.model.schema.paths; const isParanoid = Object.values(paths).some(schema => !!schema.options.deletedAt); if (!isParanoid || options?.force) { return this.deleteMany(filter, { ...options, ...helpers_1.DELETE_OPTIONS }); } const bodyUpdate = Object.values(paths).reduce((body, schema) => { if (schema.options.deletedAt) body[schema.options.deletedAt] = new Date(); return body; }, {}); return this.updateMany(filter, { $set: bodyUpdate }, { ...helpers_1.PARANOID_OPTIONS, ...options }); } function restore(filter, options) { const paths = this.model.schema.paths; const bodyUpdate = Object.values(paths).reduce((body, schema) => { if (schema.options.deletedAt) body[schema.options.deletedAt] = null; return body; }, {}); return this.findOneAndUpdate(filter, { $set: bodyUpdate }, { ...helpers_1.RESTORE_OPTIONS, ...options }); } function buildQueryMethod() { const QueryMethods = [search, center, destroyOne, destroyMany, restore]; return QueryMethods.map(method => (0, typegoose_1.queryMethod)(method)); } //# sourceMappingURL=mongo.method.js.map