1
- import type { SearchEngineDriver } from '@stacksjs/types'
1
+ import { ExitCode , type MeilisearchOptions , type SearchEngineDriver } from '@stacksjs/types'
2
+ import { searchEngine } from '@stacksjs/config'
2
3
3
- // function client(options?: MeilisearchOptions) {
4
- // let host = searchEngine.meilisearch?.host
5
- // let apiKey = searchEngine.meilisearch?.apiKey
4
+ import { EnqueuedTask , Index , MeiliSearch , type DocumentOptions , type IndexesResults , type IndexOptions } from 'meilisearch'
5
+ import { log } from '@stacksjs/logging'
6
6
7
- // if (options?.host)
8
- // host = options.host
7
+ function client ( ) : MeiliSearch {
8
+ const host = searchEngine . meilisearch ?. host || 'http://127.0.0.1:7700'
9
+ let apiKey = searchEngine . meilisearch ?. apiKey || ''
9
10
10
- // if (options?.apiKey)
11
- // apiKey = options.apiKey
11
+ if ( ! host ) {
12
+ log . error ( 'Please specify a search engine host.' )
13
+ process . exit ( ExitCode . FatalError )
14
+ }
12
15
13
- // if (!host) {
14
- // log.error('Please specify a search engine host.')
15
- // process.exit(ExitCode.FatalError)
16
- // }
17
-
18
- // return new MeiliSearch({ host, apiKey })
19
- // }
16
+ return new MeiliSearch ( { host, apiKey } )
17
+ }
20
18
21
19
// async function search(index: string, params: any): Promise<SearchResponse<Record<string, any>>> {
22
20
// const offsetVal = ((params.page * params.perPage) - 20) || 0
@@ -28,49 +26,49 @@ import type { SearchEngineDriver } from '@stacksjs/types'
28
26
// .search(params.query, { limit: params.perPage, filter, sort, offset: offsetVal })
29
27
// }
30
28
31
- // async function addDocument(indexName: string, params: any): Promise<EnqueuedTask> {
32
- // return await client().index(indexName).addDocuments([params])
33
- // }
29
+ async function addDocument ( indexName : string , params : any ) : Promise < EnqueuedTask > {
30
+ return await client ( ) . index ( indexName ) . addDocuments ( [ params ] )
31
+ }
34
32
35
- // async function addDocuments(indexName: string, params: any[]): Promise<EnqueuedTask> {
36
- // return await client().index(indexName).addDocuments(params)
37
- // }
33
+ async function addDocuments ( indexName : string , params : any [ ] ) : Promise < EnqueuedTask > {
34
+ return await client ( ) . index ( indexName ) . addDocuments ( params )
35
+ }
38
36
39
- // async function createIndex(name: string, options?: IndexOptions): Promise<EnqueuedTask> {
40
- // return await client().createIndex(name, options)
41
- // }
37
+ async function createIndex ( name : string , options ?: IndexOptions ) : Promise < EnqueuedTask > {
38
+ return await client ( ) . createIndex ( name , options )
39
+ }
42
40
43
- // async function updateIndex(indexName: string, params: IndexOptions): Promise<EnqueuedTask> {
44
- // return await client().updateIndex(indexName, params)
45
- // }
41
+ async function updateIndex ( indexName : string , params : IndexOptions ) : Promise < EnqueuedTask > {
42
+ return await client ( ) . updateIndex ( indexName , params )
43
+ }
46
44
47
- // async function updateDocument(indexName: string, params: DocumentOptions): Promise<EnqueuedTask> {
48
- // return await client().index(indexName).updateDocuments([params])
49
- // }
45
+ async function updateDocument ( indexName : string , params : DocumentOptions ) : Promise < EnqueuedTask > {
46
+ return await client ( ) . index ( indexName ) . updateDocuments ( [ params ] )
47
+ }
50
48
51
- // async function updateDocuments(indexName: string, params: DocumentOptions[]): Promise<EnqueuedTask> {
52
- // return await client().index(indexName).updateDocuments(params)
53
- // }
49
+ async function updateDocuments ( indexName : string , params : DocumentOptions [ ] ) : Promise < EnqueuedTask > {
50
+ return await client ( ) . index ( indexName ) . updateDocuments ( params )
51
+ }
54
52
55
- // async function deleteDocument(indexName: string, id: number): Promise<EnqueuedTask> {
56
- // return await client().index(indexName).deleteDocument(id)
57
- // }
53
+ async function deleteDocument ( indexName : string , id : number ) : Promise < EnqueuedTask > {
54
+ return await client ( ) . index ( indexName ) . deleteDocument ( id )
55
+ }
58
56
59
- // async function deleteDocuments(indexName: string, filters: string | string[]): Promise<EnqueuedTask> {
60
- // return await client().index(indexName).deleteDocuments({ filter: filters })
61
- // }
57
+ async function deleteDocuments ( indexName : string , filters : string | string [ ] ) : Promise < EnqueuedTask > {
58
+ return await client ( ) . index ( indexName ) . deleteDocuments ( { filter : filters } )
59
+ }
62
60
63
- // async function getDocument(indexName: string, id: number, fields: any): Promise<EnqueuedTask> {
64
- // return await client().index(indexName).getDocument(id, fields)
65
- // }
61
+ async function getDocument ( indexName : string , id : number , fields : any ) : Promise < EnqueuedTask > {
62
+ return await client ( ) . index ( indexName ) . getDocument ( id , fields )
63
+ }
66
64
67
- // async function deleteIndex(indexName: string): Promise<EnqueuedTask> {
68
- // return await client().deleteIndex(indexName)
69
- // }
65
+ async function deleteIndex ( indexName : string ) : Promise < EnqueuedTask > {
66
+ return await client ( ) . deleteIndex ( indexName )
67
+ }
70
68
71
- // async function listAllIndexes(): Promise<IndexesResults<Index[]>> {
72
- // return await client().getIndexes()
73
- // }
69
+ async function listAllIndexes ( ) : Promise < IndexesResults < Index [ ] > > {
70
+ return await client ( ) . getIndexes ( )
71
+ }
74
72
75
73
// function convertToFilter(jsonData: any): string[] {
76
74
// const filters: string[] = []
@@ -103,16 +101,16 @@ import type { SearchEngineDriver } from '@stacksjs/types'
103
101
export default {
104
102
client,
105
103
// search,
106
- // createIndex,
107
- // deleteIndex,
108
- // updateIndex,
109
- // listAllIndexes,
110
- // addDocument,
111
- // addDocuments,
104
+ createIndex,
105
+ deleteIndex,
106
+ updateIndex,
107
+ listAllIndexes,
108
+ addDocument,
109
+ addDocuments,
112
110
// updateDocument,
113
- // listAllIndices: listAllIndexes,
114
- // updateDocuments,
115
- // deleteDocument,
116
- // deleteDocuments,
117
- // getDocument,
111
+ listAllIndices : listAllIndexes ,
112
+ updateDocuments,
113
+ deleteDocument,
114
+ deleteDocuments,
115
+ getDocument,
118
116
} satisfies SearchEngineDriver
0 commit comments