add meili search
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
import { MeiliConfig } from '@/modules/meilisearch/types';
|
||||
|
||||
export const MEILI_CONFIG = (): MeiliConfig => [
|
||||
{
|
||||
name: 'default',
|
||||
host: 'http://localhost:7700',
|
||||
apiKey: 'masterKey',
|
||||
},
|
||||
];
|
||||
@@ -0,0 +1,28 @@
|
||||
import { DynamicModule, Module } from '@nestjs/common';
|
||||
|
||||
import { MeiliService } from '@/modules/meilisearch/meili.service';
|
||||
import { MeiliConfig } from '@/modules/meilisearch/types';
|
||||
import { createMeiliOptions } from '@/modules/meilisearch/utils';
|
||||
|
||||
@Module({})
|
||||
export class MeiliModule {
|
||||
static forRoot(configRegister: () => MeiliConfig): DynamicModule {
|
||||
return {
|
||||
global: true,
|
||||
module: MeiliModule,
|
||||
providers: [
|
||||
{
|
||||
provide: MeiliService,
|
||||
useFactory: async () => {
|
||||
const service = new MeiliService(
|
||||
await createMeiliOptions(configRegister()),
|
||||
);
|
||||
await service.createClients();
|
||||
return service;
|
||||
},
|
||||
},
|
||||
],
|
||||
exports: [MeiliService],
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { isNil } from 'lodash';
|
||||
import { MeiliSearch } from 'meilisearch';
|
||||
|
||||
import { MeiliConfig } from '@/modules/meilisearch/types';
|
||||
|
||||
@Injectable()
|
||||
export class MeiliService {
|
||||
protected options: MeiliConfig;
|
||||
|
||||
protected clients: Map<string, MeiliSearch> = new Map();
|
||||
|
||||
constructor(options: MeiliConfig) {
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
getOptions() {
|
||||
return this.options;
|
||||
}
|
||||
|
||||
async createClients() {
|
||||
for (const option of this.options) {
|
||||
this.clients.set(option.name, new MeiliSearch(option));
|
||||
}
|
||||
}
|
||||
|
||||
getClient(name?: string): MeiliSearch {
|
||||
let key = 'default';
|
||||
if (!isNil(name)) {
|
||||
key = name;
|
||||
}
|
||||
if (!this.clients.has(key)) {
|
||||
throw new Error(`No client found for ${name}`);
|
||||
}
|
||||
return this.clients.get(key);
|
||||
}
|
||||
|
||||
getClients(): Map<string, MeiliSearch> {
|
||||
return this.clients;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { Config } from 'meilisearch';
|
||||
|
||||
export type MeiliConfig = MeiliOption[];
|
||||
|
||||
export type MeiliOption = Config & { name: string };
|
||||
@@ -0,0 +1,18 @@
|
||||
import { MeiliConfig } from '@/modules/meilisearch/types';
|
||||
|
||||
export const createMeiliOptions = async (config: MeiliConfig): Promise<MeiliConfig | undefined> => {
|
||||
if (config.length < 0) {
|
||||
return config;
|
||||
}
|
||||
let options: MeiliConfig = [...config];
|
||||
const names = options.map(({ name }) => name);
|
||||
if (!names.includes('default')) {
|
||||
options[0].name = 'default';
|
||||
} else if (names.filter((name) => name === 'default').length > 0) {
|
||||
options = options.reduce(
|
||||
(o, n) => (o.map(({ name }) => name).includes('default') ? o : [...o, n]),
|
||||
[],
|
||||
);
|
||||
}
|
||||
return options;
|
||||
};
|
||||
Reference in New Issue
Block a user