@ucast/mongo
Version:
git@github.com:stalniy/ucast.git
1 lines • 9.75 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","names":["and","instructions"],"sources":["../../src/instructions.ts","../../src/MongoQueryParser.ts","../../src/index.ts"],"sourcesContent":["import {\n CompoundCondition,\n FieldCondition,\n NamedInstruction,\n CompoundInstruction,\n FieldInstruction,\n DocumentInstruction,\n Comparable,\n ITSELF,\n NULL_CONDITION,\n FieldParsingContext,\n optimizedCompoundCondition,\n ObjectQueryFieldParsingContext,\n ParsingContext,\n} from '@ucast/core';\nimport type { MongoQuery } from './types';\n\nexport const $and: CompoundInstruction<MongoQuery<any>[]> = {\n type: 'compound',\n validate: ensureIsNonEmptyArray,\n parse(instruction, queries, context) {\n const conditions = parseCompoundInstruction(instruction, queries, context);\n return optimizedCompoundCondition(instruction.name, conditions);\n }\n};\nexport const $or = $and;\nexport const $nor: CompoundInstruction<MongoQuery<any>[]> = {\n type: 'compound',\n validate: ensureIsNonEmptyArray,\n parse(instruction, queries, context) {\n const conditions = parseCompoundInstruction(instruction, queries, context);\n return new CompoundCondition(instruction.name, conditions);\n }\n};\n\nfunction parseCompoundInstruction(\n instruction: NamedInstruction,\n queries: MongoQuery<any>[],\n context: ParsingContext<{}>\n) {\n const conditions = new Array(queries.length);\n\n for (let i = 0; i < queries.length; i++) {\n const query = queries[i];\n ensureIsObjectAtIndex(instruction, query, i);\n conditions[i] = context.parse(query);\n }\n\n return conditions;\n}\n\nexport const $not: FieldInstruction<MongoQuery<any> | RegExp> = {\n type: 'field',\n validate(instruction, value) {\n const isValid = value && (value instanceof RegExp || value.constructor === Object);\n\n if (!isValid) {\n throw new Error(`\"${instruction.name}\" expects to receive either regular expression or object of field operators`);\n }\n },\n parse(instruction, value, context) {\n const condition = value instanceof RegExp\n ? new FieldCondition('regex' as typeof instruction.name, context.field, value)\n : context.parse(value, context);\n\n return new CompoundCondition(instruction.name, [condition]);\n },\n};\nexport const $elemMatch: FieldInstruction<MongoQuery<any>, ObjectQueryFieldParsingContext> = {\n type: 'field',\n validate(instruction, value) {\n if (!value || value.constructor !== Object) {\n throw new Error(`\"${instruction.name}\" expects to receive an object with nested query or field level operators`);\n }\n },\n parse(instruction, value, { parse, field, hasOperators }) {\n const condition = hasOperators(value) ? parse(value, { field: ITSELF }) : parse(value);\n return new FieldCondition(instruction.name, field, condition);\n }\n};\n\nexport const $size: FieldInstruction<number> = {\n type: 'field',\n validate: ensureIs('number')\n};\nexport const $in: FieldInstruction<unknown[]> = {\n type: 'field',\n validate: ensureIsArray,\n};\nexport const $nin = $in;\nexport const $all = $in;\nexport const $mod: FieldInstruction<[number, number]> = {\n type: 'field',\n validate(instruction, value) {\n if (!Array.isArray(value) || value.length !== 2) {\n throw new Error(`\"${instruction.name}\" expects an array with 2 numeric elements`);\n }\n }\n};\n\nexport const $exists: FieldInstruction<boolean> = {\n type: 'field',\n validate: ensureIs('boolean'),\n};\n\nexport const $gte: FieldInstruction<Comparable> = {\n type: 'field',\n validate: ensureIsComparable\n};\nexport const $gt = $gte;\nexport const $lt = $gt;\nexport const $lte = $gt;\n\nexport const $eq: FieldInstruction = {\n type: 'field',\n};\nexport const $ne = $eq;\n\nexport interface RegExpFieldContext extends FieldParsingContext {\n query: {\n $options?: string\n }\n}\n\nexport const $regex: FieldInstruction<string | RegExp, RegExpFieldContext> = {\n type: 'field',\n validate(instruction, value) {\n if (!(value instanceof RegExp) && typeof value !== 'string') {\n throw new Error(`\"${instruction.name}\" expects value to be a regular expression or a string that represents regular expression`);\n }\n },\n parse(instruction, rawValue, context) {\n const value = typeof rawValue === 'string'\n ? new RegExp(rawValue, context.query.$options || '')\n : rawValue;\n return new FieldCondition(instruction.name, context.field, value);\n }\n};\nexport const $options: FieldInstruction = {\n type: 'field',\n parse: () => NULL_CONDITION,\n};\n\nexport const $where: DocumentInstruction<() => boolean> = {\n type: 'document',\n validate: ensureIs('function'),\n};\n\nfunction ensureIsArray(instruction: NamedInstruction, value: unknown) {\n if (!Array.isArray(value)) {\n throw new Error(`\"${instruction.name}\" expects value to be an array`);\n }\n}\n\nfunction ensureIsNonEmptyArray(instruction: NamedInstruction, value: unknown[]) {\n ensureIsArray(instruction, value);\n\n if (!value.length) {\n throw new Error(`\"${instruction.name}\" expects to have at least one element in array`);\n }\n}\n\nfunction ensureIsComparable(instruction: NamedInstruction, value: string | number | Date) {\n const isComparable = typeof value === 'string' || typeof value === 'number' || value instanceof Date;\n\n if (!isComparable) {\n throw new Error(`\"${instruction.name}\" expects value to be comparable (i.e., string, number or date)`);\n }\n}\n\nfunction ensureIs(type: string) {\n return (instruction: NamedInstruction, value: unknown) => {\n if (typeof value !== type) {\n throw new Error(`\"${instruction.name}\" expects value to be a \"${type}\"`);\n }\n };\n}\n\nfunction ensureIsObjectAtIndex(instruction: NamedInstruction, value: unknown, index: number) {\n const item = value as { constructor?: unknown } | null;\n\n if (!item || item.constructor !== Object) {\n throw new Error(`\"${instruction.name}\" expects item at index ${index} to be an object`);\n }\n}\n","import {\n Condition,\n buildAnd as and,\n ParsingInstruction,\n ObjectQueryParser,\n FieldQueryOperators,\n} from '@ucast/core';\nimport { MongoQuery } from './types';\n\nexport interface ParseOptions {\n field: string\n}\n\nexport class MongoQueryParser extends ObjectQueryParser<MongoQuery<any>> {\n constructor(instructions: Record<string, ParsingInstruction>) {\n super(instructions, {\n defaultOperatorName: '$eq',\n operatorToConditionName: name => name.slice(1),\n });\n }\n\n parse<Q extends MongoQuery<any>, FQ extends FieldQueryOperators<Q> = FieldQueryOperators<Q>>(\n query: Q | FQ,\n options?: ParseOptions\n ): Condition {\n if (options && options.field) {\n return and(this.parseFieldOperators(options.field, query as FQ));\n }\n\n return super.parse(query);\n }\n}\n","import * as instructions from './instructions';\n\nexport const allParsingInstructions = instructions;\nexport * from './instructions';\nexport * from './MongoQueryParser';\nexport * from './types';\nexport { defaultInstructionParsers as defaultParsers } from '@ucast/core';\n"],"mappings":"ykBAiBA,MAAa,EAA+C,CAC1D,KAAM,WACN,SAAU,EACV,MAAM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