From dff19ba4b2a84091a4453fcb95afc1dba9410c9e Mon Sep 17 00:00:00 2001 From: Yusuf Olokoba Date: Sat, 19 May 2018 18:17:19 -0400 Subject: [PATCH] Add support for search on Mongo adapter --- src/db-adapters/Mongoose/MongooseAdapter.ts | 15 ++++++++++++++- src/db-adapters/Mongoose/lib.ts | 8 ++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/db-adapters/Mongoose/MongooseAdapter.ts b/src/db-adapters/Mongoose/MongooseAdapter.ts index 270c3a4d..d9c586e9 100755 --- a/src/db-adapters/Mongoose/MongooseAdapter.ts +++ b/src/db-adapters/Mongoose/MongooseAdapter.ts @@ -1041,6 +1041,19 @@ export default class MongooseAdapter implements Adapter return args; } - } + }, + + 'search': { + isBinary: false, + legalIn: ["filter", "sort"], + finalizeArgs (operators, operator, args) { + if (args.length !== 1 || typeof args[0] !== "string") { + throw new SyntaxError( + `"search" expects exactly one argument: a query string` + ); + } + return args; + } + }, }; } diff --git a/src/db-adapters/Mongoose/lib.ts b/src/db-adapters/Mongoose/lib.ts index d82bac2e..2598dbde 100755 --- a/src/db-adapters/Mongoose/lib.ts +++ b/src/db-adapters/Mongoose/lib.ts @@ -122,6 +122,14 @@ export function toMongoCriteria(constraint: FieldExpression) { }); } + if (constraint.operator === "search") { + return { + $text: { + $search: constraint.args[0], + } + } + } + // Note: all the operators we support (as declared in the adapter) are either: // 1) the ones handled above (`and` + `or`); or 2) binary ones with a field // reference on the left-hand side. For the latter, this requirement has already