Attempts to integrate the mongoose-fuzzy-searching library#670
Attempts to integrate the mongoose-fuzzy-searching library#670SigmasonicX wants to merge 2 commits intomainfrom
Conversation
…zzy-searching working
| declare module 'mongoose-fuzzy-searching' { | ||
| import { Document, DocumentQuery, HookAsyncCallback, HookSyncCallback, Model, Schema } from 'mongoose' | ||
|
|
||
| export type FuzzyFieldStringOptions<T> = (keyof T & string)[]; |
There was a problem hiding this comment.
This basically means "the only fields allowed on this type are the fields already defined on T, as strings".
|
|
||
| export type FuzzyFieldStringOptions<T> = (keyof T & string)[]; | ||
|
|
||
| export interface FuzzyFieldOptions<T> { |
There was a problem hiding this comment.
The docs and defaults from this come from the package's docs.
| ): DocumentQuery<T[], T, QueryHelpers> | ||
| } | ||
|
|
||
| export default function registerFuzzySearch<T>(schema: Schema<T>, options: MongooseFuzzyOptions<T>): void |
There was a problem hiding this comment.
this needs to be defined as a default export because of how the original JS package exposes its registration function (i.e. as an unnamed function in the module.exports dictionary)
| ContentSchema.plugin(MongooseFuzzySearching, { fields: ['title'] }); | ||
| export const Content = mongoose.model('Content', ContentSchema) as MongooseFuzzyModel<ContentDocument>; |
There was a problem hiding this comment.
I moved all of this setup into /content/schemas/index.ts for consistency.
| preSave: async function (next: HookNextFunction) { | ||
| this.set('title', sanitizeHtml(this.title, sanitizeOptions)); | ||
| this.set('body', sanitizeHtml(this.body, sanitizeOptions)); | ||
|
|
||
| // this will only trigger if any creation or editing functions has modified the `desc` field, | ||
| // otherwise we'll leave it alone | ||
| if (this.isModified('desc')) { | ||
| this.set('desc', sanitizeHtml(this.desc, sanitizeOptions)); | ||
| } | ||
|
|
||
| return next(); |
There was a problem hiding this comment.
Per the mongoose-fuzzy-searching docs, it overrides any pre()- hooks you have defined on a schema, so they need to be defined within the plugin's middlewares config field. For that reason, I also define it as the first plugin in the chain, to ensure those hooks run as early as possible.
| private fuzzySearchableContent: MongooseFuzzyModel<ContentDocument>; | ||
| constructor( | ||
| @InjectModel('Content') private readonly content: PaginateModel<ContentDocument>, | ||
| @InjectModel('Content') private readonly fuzzySearchableContent: MongooseFuzzyModel<ContentDocument>, |
There was a problem hiding this comment.
I was kind of surprised this worked! But it did. Nice and easy.
| await ContentGroupStore.determineContentFilter(paginateQuery, filter); | ||
| return await this.fuzzySearchableContent.fuzzySearch(query); | ||
| // return await this.content.paginate(paginateQuery, paginateOptions); | ||
| const fuzzySearchedContent = await this.fuzzySearchableContent.fuzzySearch(query); |
There was a problem hiding this comment.
So we're not actually doing anything with this variable. fuzzySearch() returns an array of ContentDocuments, but this function expects to return a PaginateResult<ContentDocument>, and those are two very different things. This is where a big part of the complexity of making mongoose-paginate and mongoose-fuzzy-searching play nicely together is going to happen.
| @@ -1,19 +0,0 @@ | |||
| declare module 'mongoose-fuzzy-search' { | |||
There was a problem hiding this comment.
Moved this file to adhere more to standard TypeScript convention.
| "types": ["node"], | ||
| "target": "es6" | ||
| "target": "es6", | ||
| "typeRoots": ["../../../node_modules/@types", "src/customTypes"] |
There was a problem hiding this comment.
Now that we have some custom types, we need to tell the compiler where to find them. Since we do that, we have to tell it where to find all of them, so the node_modules types go in here too.
We could import the .d.ts files manually, but who wants to do that everywhere?
Addresses ticket #
Description
Changes
Areas Affected
.
.
.