UNPKG

@feathersjs/mongodb

Version:

Feathers MongoDB service adapter

49 lines 1.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.keywordObjectId = void 0; exports.resolveObjectId = resolveObjectId; exports.resolveQueryObjectId = resolveQueryObjectId; const mongodb_1 = require("mongodb"); const toObjectId = (value) => new mongodb_1.ObjectId(value); async function resolveObjectId(value) { return toObjectId(value); } async function resolveQueryObjectId(value) { if (!value) { return undefined; } if (typeof value === 'string' || typeof value === 'number' || value instanceof mongodb_1.ObjectId) { return toObjectId(value); } const convertedObject = {}; if (Array.isArray(value.$in)) { convertedObject.$in = value.$in.map(toObjectId); } if (Array.isArray(value.$nin)) { convertedObject.$nin = value.$nin.map(toObjectId); } if (value.$ne !== undefined) { convertedObject.$ne = toObjectId(value.$ne); } return convertedObject; } exports.keywordObjectId = { keyword: 'objectid', type: 'string', modifying: true, compile(schemaVal) { if (!schemaVal) return () => true; return function (value, obj) { const { parentData, parentDataProperty } = obj; try { parentData[parentDataProperty] = new mongodb_1.ObjectId(value); return true; } catch (error) { throw new Error(`invalid objectid for property "${parentDataProperty}"`); } }; } }; //# sourceMappingURL=converters.js.map