Skip to content
This repository was archived by the owner on Jan 14, 2023. It is now read-only.

Attempts to integrate the mongoose-fuzzy-searching library#670

Draft
SigmasonicX wants to merge 2 commits intomainfrom
sigmasonicx/implement_fuzzy_searching
Draft

Attempts to integrate the mongoose-fuzzy-searching library#670
SigmasonicX wants to merge 2 commits intomainfrom
sigmasonicx/implement_fuzzy_searching

Conversation

@SigmasonicX
Copy link
Contributor

  • Has errors

Addresses ticket #

Description

Changes

Areas Affected

  • Site Navigation
  • Home
  • Sign In/Out
  • Registration
  • Settings

.

  • Profile
  • My Stuff (main page)
  • Editor
  • Browse
  • Work Card

.

  • Work Page
  • Blog Page
  • Collections
  • Comments
  • Other Pages

.

  • Account Authentication
  • Dashboard
  • Mobile

declare module 'mongoose-fuzzy-searching' {
import { Document, DocumentQuery, HookAsyncCallback, HookSyncCallback, Model, Schema } from 'mongoose'

export type FuzzyFieldStringOptions<T> = (keyof T & string)[];
Copy link
Collaborator

@pingzing pingzing Sep 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Comment on lines -96 to -97
ContentSchema.plugin(MongooseFuzzySearching, { fields: ['title'] });
export const Content = mongoose.model('Content', ContentSchema) as MongooseFuzzyModel<ContentDocument>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved all of this setup into /content/schemas/index.ts for consistency.

Comment on lines +44 to +54
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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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' {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this file to adhere more to standard TypeScript convention.

"types": ["node"],
"target": "es6"
"target": "es6",
"typeRoots": ["../../../node_modules/@types", "src/customTypes"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants