UNPKG

@tsed-plus/meilisearch-mongoose-indexer

Version:

Extends the Ts.ED framework with the functionality to synchronize Mongoose models with Meilisearch

92 lines (67 loc) 2.18 kB
# Ts.ED Plus Meilisearch Mongo(ose) Indexer > Support for [Meilisearch](https://www.meilisearch.com/) in your Ts.ED application. Extends the [Ts.ED framework](https://tsed.io) with the functionality to synchronize Mongoose models with Meilisearch ## Contents - [Prerequisite](#prerequisite) - [Getting Started](#getting-started) ## Prerequisite If you have not already, familiarize yourself with the [Ts.ED framework](https://tsed.io), [MongoDB](https://www.mongodb.com)/[Mongoose](https://mongoosejs.com) and [Meilisearch](https://www.meilisearch.com/). ## Getting Started Begin with the setup and configuration steps of [@tsed-plus/meilisearch](https://github.com/applogik/tsed-plus/tree/main/libs/meilisearch) and [@tsed/mongoose](https://tsed.io/tutorials/mongoose.html). ### Install Ts.ED Module ```sh # npm npm install @tsed-plus/meilisearch-mongoose-indexer # yarn yarn add @tsed-plus/meilisearch-mongoose-indexer ``` ### Usage 1. Have some class annotated with `@Model` (from `@tsed-plus/mongoose`), e.g. ```ts @Model({ collection: 'pokemons' }) export class Pokemon { @Required() @ObjectID('id') _id: ObjectID; @Required() number: number; @Required() name: string; @Required() img: string; @Required() @CollectionOf(String) type: string[]; @Required() @CollectionOf(Move) moves: Map<string, Move[]>; @Required() damages: Damages; @Property() @OnSerialize((v, ctx) => (ctx.meilisearch ? undefined : v)) misc?: any; } ``` 2. Implement a class based on `BaseMeiliMongoIndexer<T>` and add the `@MeiliMongoIndexer` decorator. ```ts @MeiliMongoIndexer({ model: Pokemon, }) export class MeilisearchTestModelIndexer extends BaseMeiliMongoIndexer<Pokemon> { transformDocument(input: Pokemon) { return serialize(input, { type: Pokemon, meilisearch: true }); } getCreateIndexOptions(): IndexOptions { return { primaryKey: 'id' }; } async getUpdateIndexAndSettingsOptions( index: Index<Pokemon> ): Promise<UpdateIndexAndSettingsOptions | undefined> { return { indexSettings: { synonyms: { enton: ['psyduck'] }, filterableAttributes: ['type'], }, }; } } ```