@stacksjs/search-engine 0.58.48 → 0.58.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/search-engine",
3
3
  "type": "module",
4
- "version": "0.58.48",
4
+ "version": "0.58.49",
5
5
  "description": "The Stacks search engine integrations.",
6
6
  "author": "Chris Breuer",
7
7
  "license": "MIT",
@@ -45,7 +45,8 @@
45
45
  ],
46
46
  "files": [
47
47
  "README.md",
48
- "dist"
48
+ "dist",
49
+ "src"
49
50
  ],
50
51
  "scripts": {
51
52
  "build": "bun --bun build.ts",
@@ -0,0 +1 @@
1
+ export * as meilisearch from './meilisearch'
@@ -0,0 +1,124 @@
1
+ import process from 'node:process'
2
+ import { searchEngine } from '@stacksjs/config'
3
+ import { log } from '@stacksjs/logging'
4
+ import type { MeiliSearchOptions, SearchEngineDriver } from '@stacksjs/types'
5
+ import { ExitCode } from '@stacksjs/types'
6
+ import type { DocumentOptions, EnqueuedTask, Index, IndexOptions, IndexesResults, SearchResponse } from 'meilisearch'
7
+ import { MeiliSearch } from 'meilisearch'
8
+
9
+ function client(options?: MeiliSearchOptions) {
10
+ let host = searchEngine.meilisearch?.host
11
+ let apiKey = searchEngine.meilisearch?.apiKey
12
+
13
+ if (options?.host)
14
+ host = options.host
15
+
16
+ if (options?.apiKey)
17
+ apiKey = options.apiKey
18
+
19
+ if (!host) {
20
+ log.error('Please specify a search engine host.')
21
+ process.exit(ExitCode.FatalError)
22
+ }
23
+
24
+ return new MeiliSearch({ host, apiKey })
25
+ }
26
+
27
+ async function search(index: string, params: any): Promise<SearchResponse<Record<string, any>>> {
28
+ const offsetVal = ((params.page * params.perPage) - 20) || 0
29
+ const filter = convertToFilter(params.filter)
30
+ const sort = convertToMeilisearchSorting(params.sort)
31
+
32
+ return await client()
33
+ .index(index)
34
+ .search(params.query, { limit: params.perPage, filter, sort, offset: offsetVal })
35
+ }
36
+
37
+ async function addDocument(indexName: string, params: any): Promise<EnqueuedTask> {
38
+ return await client().index(indexName).addDocuments([params])
39
+ }
40
+
41
+ async function addDocuments(indexName: string, params: any[]): Promise<EnqueuedTask> {
42
+ return await client().index(indexName).addDocuments(params)
43
+ }
44
+
45
+ async function createIndex(name: string, options?: IndexOptions): Promise<EnqueuedTask> {
46
+ return await client().createIndex(name, options)
47
+ }
48
+
49
+ async function updateIndex(indexName: string, params: IndexOptions): Promise<EnqueuedTask> {
50
+ return await client().updateIndex(indexName, params)
51
+ }
52
+
53
+ async function updateDocument(indexName: string, params: DocumentOptions): Promise<EnqueuedTask> {
54
+ return await client().index(indexName).updateDocuments([params])
55
+ }
56
+
57
+ async function updateDocuments(indexName: string, params: DocumentOptions[]): Promise<EnqueuedTask> {
58
+ return await client().index(indexName).updateDocuments(params)
59
+ }
60
+
61
+ async function deleteDocument(indexName: string, id: number): Promise<EnqueuedTask> {
62
+ return await client().index(indexName).deleteDocument(id)
63
+ }
64
+
65
+ async function deleteDocuments(indexName: string, filters: string | string[]): Promise<EnqueuedTask> {
66
+ return await client().index(indexName).deleteDocuments({ filter: filters })
67
+ }
68
+
69
+ async function getDocument(indexName: string, id: number, fields: any): Promise<EnqueuedTask> {
70
+ return await client().index(indexName).getDocument(id, fields)
71
+ }
72
+
73
+ async function deleteIndex(indexName: string): Promise<EnqueuedTask> {
74
+ return await client().deleteIndex(indexName)
75
+ }
76
+
77
+ async function listAllIndexes(): Promise<IndexesResults<Index[]>> {
78
+ return await client().getIndexes()
79
+ }
80
+
81
+ function convertToFilter(jsonData: any): string[] {
82
+ const filters: string[] = []
83
+
84
+ for (const key in jsonData) {
85
+ if (Object.prototype.hasOwnProperty.call(jsonData, key)) {
86
+ const value = jsonData[key]
87
+ const filter = `${key}='${value}'`
88
+ filters.push(filter)
89
+ }
90
+ }
91
+
92
+ return filters
93
+ }
94
+
95
+ function convertToMeilisearchSorting(jsonData: any): string[] {
96
+ const filters: string[] = []
97
+
98
+ for (const key in jsonData) {
99
+ if (Object.prototype.hasOwnProperty.call(jsonData, key)) {
100
+ const value = jsonData[key]
101
+ const filter = `${key}='${value}'`
102
+ filters.push(filter)
103
+ }
104
+ }
105
+
106
+ return filters
107
+ }
108
+
109
+ export default {
110
+ client,
111
+ search,
112
+ createIndex,
113
+ deleteIndex,
114
+ updateIndex,
115
+ listAllIndexes,
116
+ addDocument,
117
+ addDocuments,
118
+ updateDocument,
119
+ listAllIndices: listAllIndexes,
120
+ updateDocuments,
121
+ deleteDocument,
122
+ deleteDocuments,
123
+ getDocument,
124
+ } satisfies SearchEngineDriver
@@ -0,0 +1,59 @@
1
+ import { searchEngine } from '@stacksjs/config'
2
+ import type { ApiResponse } from '@opensearch-project/opensearch'
3
+ import { Client } from '@opensearch-project/opensearch'
4
+ import type { SearchEngineDriver } from '@stacksjs/types'
5
+
6
+ const host = searchEngine.openSearch?.host
7
+ const protocol = searchEngine.openSearch?.protocol
8
+ const port = searchEngine.openSearch?.port
9
+ const auth = searchEngine.openSearch?.auth
10
+
11
+ const client = new Client({
12
+ node: `${protocol}://${auth}@${host}:${port}`,
13
+ })
14
+
15
+ async function search(index: string, params?: any) {
16
+ const response = await client.search({
17
+ index,
18
+ body: params.query,
19
+ })
20
+
21
+ return response
22
+ }
23
+
24
+ async function createIndex(indexName: string, settings?: any): Promise<ApiResponse<any, any>> {
25
+ return await client.indices.create({ index: indexName, body: settings })
26
+ }
27
+ // async function updateIndex(indexName: string, settings?: any): Promise<ApiResponse<any, any>> {
28
+ // return await client.indices.update({ index: indexName, body: settings })
29
+ // }
30
+
31
+ async function deleteIndex(indexName: string): Promise<ApiResponse<any, any>> {
32
+ return await client.indices.delete({ index: indexName })
33
+ }
34
+
35
+ async function addDocument(indexName: string, document: any): Promise<ApiResponse<any, any>> {
36
+ return await client.index({
37
+ id: document.id,
38
+ index: indexName,
39
+ body: document,
40
+ refresh: true,
41
+ })
42
+ }
43
+
44
+ async function deleteDocument(indexName: string, document: any): Promise<ApiResponse<any, any>> {
45
+ return await client.delete({
46
+ id: document.id,
47
+ index: indexName,
48
+ })
49
+ }
50
+
51
+ export default {
52
+ client,
53
+ search,
54
+ createIndex,
55
+ deleteIndex,
56
+ addDocument,
57
+ deleteDocument,
58
+ // ...other methods
59
+ } satisfies SearchEngineDriver
package/src/helpers.ts ADDED
@@ -0,0 +1,138 @@
1
+ /**
2
+ * This file is used to define the types/interfaces used in the project.
3
+ */
4
+
5
+ import type { Hits, SearchResponse } from 'meilisearch'
6
+
7
+ // export function isString(val: unknown): val is string {
8
+ // return typeof val === 'string'
9
+ // }
10
+
11
+ // this interface is primarily used to persist data to localStorage, in a unified way
12
+ export interface SearchEngineStore {
13
+ /**
14
+ * The search engine index.
15
+ */
16
+ index: string
17
+ /**
18
+ * The search engine host.
19
+ * @default 'http://127.0.0.1:7700'
20
+ */
21
+ source?: string
22
+ /**
23
+ * The search engine password/API key.
24
+ */
25
+ password?: string
26
+ /**
27
+ * The search query.
28
+ * @default 20
29
+ * @type {string}
30
+ * @default: ''
31
+ */
32
+ query?: string
33
+ /**
34
+ * The number of results to return.
35
+ * @default 20
36
+ * @type {number}
37
+ */
38
+ perPage?: number
39
+ /**
40
+ * The current page number
41
+ * @default 1
42
+ * @type {number}
43
+ */
44
+ currentPage?: number
45
+ /**
46
+ * The results object returned from the search engine.
47
+ */
48
+ results?: SearchResponse<Record<string, any>> // optional: the Meilisearch search response (defaults: {})
49
+ /**
50
+ * The hits object returned from the search engine.
51
+ */
52
+ hits?: Hits<Record<string, any>>
53
+
54
+ /**
55
+ * The search Filter Name
56
+ */
57
+ filterName?: string
58
+
59
+ /**
60
+ * The search filters
61
+ */
62
+ filters?: object
63
+
64
+ /**
65
+ * The next page value
66
+ */
67
+ goToNextPage?: number
68
+
69
+ /**
70
+ * Set to go to a specific page
71
+ */
72
+ goToPage?: number
73
+
74
+ /**
75
+ * Set to go to the previous page
76
+ */
77
+ goToPrevPage?: number
78
+
79
+ /**
80
+ * The last page number value
81
+ */
82
+ lastPageNumber?: number
83
+
84
+ /**
85
+ * The searched term to use for the search
86
+ */
87
+ search?: string
88
+
89
+ /**
90
+ * The search params filters to use for the search
91
+ */
92
+ searchFilters?: object
93
+
94
+ /**
95
+ * The search params to use for the search
96
+ */
97
+ searchParams?: object
98
+
99
+ /**
100
+ * Total hits value to use for the search
101
+ */
102
+ setTotalHits?: number
103
+
104
+ /**
105
+ * The sort value to use for the search
106
+ */
107
+ sort?: string
108
+
109
+ /**
110
+ * The sorts value to use for the search
111
+ */
112
+ sorts?: object
113
+
114
+ // table related config
115
+ // actionable?: string | boolean // optional: determines whether the table displays any "action items" (defaults: true)
116
+ // actions?: string | string[] // optional: the specific type of actions to be displayed/utilized in the table (defaults: 'Edit, Delete')
117
+ // columns: string[] // used as table heads/column titles
118
+ // selectable?: string | boolean // optional: determines whether the table displays the checkboxes (defaults: true)
119
+ // selectedRows?: number[] | string[] // optional: holds the selected rows (defaults: [])
120
+ // selectedAll?: boolean // optional: determines whether all the rows are selected (defaults: false)
121
+ // stickyHeader?: string | boolean // optional: determines whether the table displays the sticky header (defaults: false)
122
+ // stickyFooter?: string | boolean // optional: determines whether the table displays the sticky footer (defaults: false)
123
+ }
124
+
125
+ export function determineState(): SearchEngineStore {
126
+ const ls = localStorage.getItem('search-engine')
127
+
128
+ if (isString(ls))
129
+ return JSON.parse(ls) as SearchEngineStore
130
+
131
+ return { // default state
132
+ source: 'http://127.0.0.1:7700',
133
+ index: '',
134
+ perPage: 20,
135
+ currentPage: 1,
136
+ query: '',
137
+ }
138
+ }
package/src/index.ts ADDED
@@ -0,0 +1,69 @@
1
+ import { searchEngine } from '@stacksjs/config'
2
+ import type { UiEngine } from '@stacksjs/ui'
3
+ import { useStorage } from '@stacksjs/utils'
4
+
5
+ // import { client as meilisearch } from './drivers/meilisearch'
6
+ import { determineState } from './helpers'
7
+
8
+ // import type { Ref } from '@stacksjs/types'
9
+
10
+ const table = (useStorage('table', determineState()).value)
11
+ const totalHits = table.results?.estimatedTotalHits ?? 1
12
+
13
+ // state
14
+ const pages: UiEngine.Ref<number[]> = ref([])
15
+ export const totalPages = ref(0)
16
+ export const currentPage = computed(() => table.currentPage)
17
+ export const filterName = computed(() => table.filterName)
18
+
19
+ export const filters = computed(() => table.filters)
20
+ export const goToNextPage = computed(() => table.goToNextPage)
21
+ export const goToPage = computed(() => table.goToPage)
22
+ export const goToPrevPage = computed(() => table.goToPrevPage)
23
+ export const hits = computed(() => table.hits)
24
+ export const index = computed(() => table.index)
25
+ export const lastPageNumber = computed(() => table.lastPageNumber)
26
+ export const perPage = computed(() => table.perPage)
27
+ export const query = computed(() => table.query)
28
+ export const results = computed(() => table.results)
29
+ export const searchFilters = computed(() => table.searchFilters)
30
+ export const searchParams = computed(() => table.searchParams)
31
+ export const setTotalHits = computed(() => table.setTotalHits)
32
+ export const sort = computed(() => table.sort)
33
+ export const sorts = computed(() => table.sorts)
34
+
35
+ export function client() {
36
+ if (searchEngine.driver === 'meilisearch')
37
+ return meilisearch
38
+ }
39
+
40
+ export function useSearchEngine() {
41
+ return client()
42
+ }
43
+
44
+ export function calculatePagination() {
45
+ if (table.perPage)
46
+ totalPages.value = Math.ceil(totalHits / table.perPage)
47
+
48
+ const hitPages = [...Array(totalPages.value).keys()].map(i => i + 1)
49
+ const offset = 2
50
+ const currentPage = table.currentPage ?? 1
51
+ const lastPage = hitPages[hitPages.length - 1]
52
+
53
+ let from = currentPage - offset
54
+ if (from < 1)
55
+ from = 1
56
+
57
+ let to = from + offset * 2
58
+ if (to >= lastPage)
59
+ to = lastPage
60
+
61
+ const allPages = []
62
+ for (let page = from; page <= to; page++)
63
+ allPages.push(page)
64
+
65
+ pages.value = allPages
66
+ }
67
+
68
+ // it needs these exports
69
+ // currentPage, filterName, filters, goToNextPage, goToPage, goToPrevPage, hits, index, lastPageNumber, perPage, query, results, search, searchFilters, searchParams, setTotalHits, sort, sorts, totalPages