@rubixstudios/payload-typesense 1.6.12 → 1.7.0
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/README.md +76 -120
- package/dist/components/HeadlessSearchInput.d.ts.map +1 -1
- package/dist/components/HeadlessSearchInput.js +174 -142
- package/dist/components/ThemeProvider.d.ts +3 -3
- package/dist/components/ThemeProvider.d.ts.map +1 -1
- package/dist/components/ThemeProvider.js +6 -5
- package/dist/components/icons/clockIcon.d.ts +2 -0
- package/dist/components/icons/clockIcon.d.ts.map +1 -0
- package/dist/components/icons/clockIcon.js +16 -0
- package/dist/components/icons/collectionIcon.d.ts +2 -0
- package/dist/components/icons/collectionIcon.d.ts.map +1 -0
- package/dist/components/icons/collectionIcon.js +16 -0
- package/dist/components/icons/errorIcon.d.ts +2 -0
- package/dist/components/icons/errorIcon.d.ts.map +1 -0
- package/dist/components/icons/errorIcon.js +16 -0
- package/dist/components/icons/index.d.ts +6 -0
- package/dist/components/icons/index.d.ts.map +1 -0
- package/dist/components/icons/index.js +5 -0
- package/dist/components/icons/noResultsIcon.d.ts +6 -0
- package/dist/components/icons/noResultsIcon.d.ts.map +1 -0
- package/dist/components/icons/noResultsIcon.js +19 -0
- package/dist/components/icons/searchIcon.d.ts +6 -0
- package/dist/components/icons/searchIcon.d.ts.map +1 -0
- package/dist/components/icons/searchIcon.js +16 -0
- package/dist/components/index.d.ts +2 -1
- package/dist/components/index.d.ts.map +1 -1
- package/dist/components/index.js +2 -1
- package/dist/components/render/Header.d.ts +3 -3
- package/dist/components/render/Header.d.ts.map +1 -1
- package/dist/components/render/Header.js +29 -26
- package/dist/components/render/NoResults.d.ts +3 -3
- package/dist/components/render/NoResults.d.ts.map +1 -1
- package/dist/components/render/NoResults.js +34 -37
- package/dist/components/render/ResultError.d.ts +3 -3
- package/dist/components/render/ResultError.d.ts.map +1 -1
- package/dist/components/render/ResultError.js +27 -26
- package/dist/components/render/Results.d.ts +4 -4
- package/dist/components/render/Results.d.ts.map +1 -1
- package/dist/components/render/Results.js +140 -136
- package/dist/components/themes/hooks.d.ts +2 -40
- package/dist/components/themes/hooks.d.ts.map +1 -1
- package/dist/components/themes/hooks.js +33 -82
- package/dist/components/themes/themes.d.ts.map +1 -1
- package/dist/components/themes/themes.js +16 -20
- package/dist/components/themes/types.d.ts.map +1 -1
- package/dist/components/themes/utils.d.ts +2 -14
- package/dist/components/themes/utils.d.ts.map +1 -1
- package/dist/components/themes/utils.js +24 -37
- package/dist/endpoints/handler/createAdvancedSearch.d.ts +1 -1
- package/dist/endpoints/handler/createAdvancedSearch.d.ts.map +1 -1
- package/dist/endpoints/handler/createAdvancedSearch.js +72 -8
- package/dist/endpoints/handler/createCollections.d.ts +1 -1
- package/dist/endpoints/handler/createCollections.d.ts.map +1 -1
- package/dist/endpoints/handler/createCollections.js +8 -8
- package/dist/endpoints/handler/createSearch.d.ts +1 -1
- package/dist/endpoints/handler/createSearch.d.ts.map +1 -1
- package/dist/endpoints/handler/createSearch.js +45 -44
- package/dist/endpoints/handler/createSuggest.d.ts +1 -1
- package/dist/endpoints/handler/createSuggest.d.ts.map +1 -1
- package/dist/endpoints/handler/createSuggest.js +26 -11
- package/dist/endpoints/health.d.ts +2 -2
- package/dist/endpoints/health.d.ts.map +1 -1
- package/dist/endpoints/health.js +58 -52
- package/dist/endpoints/search.d.ts +2 -9
- package/dist/endpoints/search.d.ts.map +1 -1
- package/dist/endpoints/search.js +17 -16
- package/dist/{utils → hooks}/useDebounce.d.ts.map +1 -1
- package/dist/{utils → hooks}/useDebounce.js +2 -2
- package/dist/{utils → hooks}/useSearch.d.ts.map +1 -1
- package/dist/hooks/useSearch.js +93 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +38 -32
- package/dist/lib/cache.d.ts.map +1 -1
- package/dist/lib/cache.js +54 -25
- package/dist/lib/client.d.ts +1 -1
- package/dist/lib/client.d.ts.map +1 -1
- package/dist/lib/client.js +35 -31
- package/dist/lib/headlessSearch.d.ts +24 -20
- package/dist/lib/headlessSearch.d.ts.map +1 -1
- package/dist/lib/hooks.d.ts +3 -11
- package/dist/lib/hooks.d.ts.map +1 -1
- package/dist/lib/hooks.js +12 -45
- package/dist/lib/initialization.d.ts +1 -1
- package/dist/lib/initialization.d.ts.map +1 -1
- package/dist/lib/initialization.js +51 -49
- package/dist/lib/schema-mapper.d.ts +3 -6
- package/dist/lib/schema-mapper.d.ts.map +1 -1
- package/dist/lib/schema-mapper.js +82 -84
- package/dist/lib/validation.d.ts +26 -34
- package/dist/lib/validation.d.ts.map +1 -1
- package/dist/lib/validation.js +48 -58
- package/dist/types.d.ts +154 -127
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -1
- package/dist/utils/buildError.d.ts +1 -1
- package/dist/utils/buildError.d.ts.map +1 -1
- package/dist/utils/buildError.js +2 -2
- package/dist/utils/ensureCollection.d.ts +2 -2
- package/dist/utils/ensureCollection.d.ts.map +1 -1
- package/dist/utils/ensureCollection.js +103 -23
- package/dist/utils/extractText.d.ts.map +1 -1
- package/dist/utils/extractText.js +11 -8
- package/dist/utils/getCacheStats.d.ts.map +1 -1
- package/dist/utils/getCacheStats.js +1 -8
- package/dist/utils/getCollectionInfo.d.ts.map +1 -1
- package/dist/utils/getCollectionInfo.js +1 -1
- package/dist/utils/{getAllCollections.d.ts → getCollections.d.ts} +2 -2
- package/dist/utils/getCollections.d.ts.map +1 -0
- package/dist/utils/getCollections.js +140 -0
- package/dist/utils/keyboard.d.ts +4 -4
- package/dist/utils/keyboard.d.ts.map +1 -1
- package/dist/utils/keyboard.js +20 -22
- package/dist/utils/sortResults.d.ts +3 -0
- package/dist/utils/sortResults.d.ts.map +1 -0
- package/dist/utils/sortResults.js +73 -0
- package/dist/utils/testConnection.d.ts.map +1 -1
- package/dist/utils/vectorSearch.d.ts +3 -2
- package/dist/utils/vectorSearch.d.ts.map +1 -1
- package/dist/utils/vectorSearch.js +9 -7
- package/package.json +14 -14
- package/dist/utils/getAllCollections.d.ts.map +0 -1
- package/dist/utils/getAllCollections.js +0 -103
- package/dist/utils/useSearch.js +0 -66
- /package/dist/{utils → hooks}/useDebounce.d.ts +0 -0
- /package/dist/{utils → hooks}/useSearch.d.ts +0 -0
package/dist/lib/cache.js
CHANGED
|
@@ -9,22 +9,34 @@ export class SearchCache {
|
|
|
9
9
|
constructor(options = {}){
|
|
10
10
|
this.defaultTTL = options.ttl ?? 5 * 60 * 1000;
|
|
11
11
|
this.maxSize = options.maxSize ?? 1000;
|
|
12
|
+
if (!Number.isFinite(this.defaultTTL) || this.defaultTTL < 0) {
|
|
13
|
+
throw new RangeError('Cache TTL must be a non-negative number');
|
|
14
|
+
}
|
|
15
|
+
if (!Number.isSafeInteger(this.maxSize) || this.maxSize < 0) {
|
|
16
|
+
throw new RangeError('Cache maxSize must be a non-negative integer');
|
|
17
|
+
}
|
|
12
18
|
}
|
|
13
19
|
evict() {
|
|
20
|
+
if (this.cache.size < this.maxSize) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
14
23
|
this.cleanup();
|
|
15
|
-
if (this.cache.size < this.maxSize)
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
24
|
+
if (this.cache.size < this.maxSize) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const oldest = this.cache.keys().next();
|
|
28
|
+
if (!oldest.done) {
|
|
29
|
+
this.cache.delete(oldest.value);
|
|
19
30
|
}
|
|
20
31
|
}
|
|
21
32
|
generateKey(query, collection, params) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
33
|
+
return `${this.normalize(collection)}:${JSON.stringify([
|
|
34
|
+
query,
|
|
35
|
+
this.serialize(params)
|
|
36
|
+
])}`;
|
|
25
37
|
}
|
|
26
38
|
isExpired(entry) {
|
|
27
|
-
return Date.now() - entry.timestamp
|
|
39
|
+
return Date.now() - entry.timestamp >= entry.ttl;
|
|
28
40
|
}
|
|
29
41
|
lookup(query, collection, params) {
|
|
30
42
|
const key = this.generateKey(query, collection, params);
|
|
@@ -43,9 +55,15 @@ export class SearchCache {
|
|
|
43
55
|
return collection ?? 'universal';
|
|
44
56
|
}
|
|
45
57
|
serialize(params) {
|
|
46
|
-
if (!params || Object.keys(params).length === 0)
|
|
47
|
-
|
|
48
|
-
|
|
58
|
+
if (!params || Object.keys(params).length === 0) {
|
|
59
|
+
return '';
|
|
60
|
+
}
|
|
61
|
+
return JSON.stringify(params, (_key, value)=>{
|
|
62
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
63
|
+
return Object.fromEntries(Object.entries(value).sort(([a], [b])=>a.localeCompare(b)));
|
|
64
|
+
}
|
|
65
|
+
return value;
|
|
66
|
+
});
|
|
49
67
|
}
|
|
50
68
|
cleanup() {
|
|
51
69
|
for (const [key, entry] of this.cache.entries()){
|
|
@@ -55,13 +73,19 @@ export class SearchCache {
|
|
|
55
73
|
}
|
|
56
74
|
}
|
|
57
75
|
clear(pattern) {
|
|
58
|
-
if (
|
|
76
|
+
if (pattern === undefined) {
|
|
59
77
|
this.cache.clear();
|
|
60
78
|
return;
|
|
61
79
|
}
|
|
62
|
-
const matcher = typeof pattern === 'string' ? (key)=>key.includes(pattern) : (key)=>pattern.test(key);
|
|
63
80
|
for (const key of this.cache.keys()){
|
|
64
|
-
|
|
81
|
+
let matches;
|
|
82
|
+
if (typeof pattern === 'string') {
|
|
83
|
+
matches = key.includes(pattern);
|
|
84
|
+
} else {
|
|
85
|
+
pattern.lastIndex = 0;
|
|
86
|
+
matches = pattern.test(key);
|
|
87
|
+
}
|
|
88
|
+
if (matches) {
|
|
65
89
|
this.cache.delete(key);
|
|
66
90
|
}
|
|
67
91
|
}
|
|
@@ -76,6 +100,7 @@ export class SearchCache {
|
|
|
76
100
|
return entry.data;
|
|
77
101
|
}
|
|
78
102
|
getStats() {
|
|
103
|
+
this.cleanup();
|
|
79
104
|
const total = this.stats.hits + this.stats.misses;
|
|
80
105
|
return {
|
|
81
106
|
hitRate: total === 0 ? 0 : this.stats.hits / total,
|
|
@@ -84,23 +109,27 @@ export class SearchCache {
|
|
|
84
109
|
};
|
|
85
110
|
}
|
|
86
111
|
has(query, collection, params) {
|
|
87
|
-
|
|
88
|
-
const entry = this.cache.get(key);
|
|
89
|
-
if (!entry || this.isExpired(entry)) {
|
|
90
|
-
if (entry) {
|
|
91
|
-
this.cache.delete(key);
|
|
92
|
-
}
|
|
93
|
-
return false;
|
|
94
|
-
}
|
|
95
|
-
return true;
|
|
112
|
+
return this.lookup(query, collection, params) !== null;
|
|
96
113
|
}
|
|
97
114
|
set(query, data, collection, params, ttl) {
|
|
115
|
+
const duration = ttl ?? this.defaultTTL;
|
|
116
|
+
if (!Number.isFinite(duration) || duration < 0) {
|
|
117
|
+
throw new RangeError('Cache TTL must be a non-negative number');
|
|
118
|
+
}
|
|
98
119
|
const key = this.generateKey(query, collection, params);
|
|
99
|
-
this.
|
|
120
|
+
if (this.maxSize === 0 || duration === 0) {
|
|
121
|
+
this.cache.delete(key);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (this.cache.has(key)) {
|
|
125
|
+
this.cache.delete(key);
|
|
126
|
+
} else {
|
|
127
|
+
this.evict();
|
|
128
|
+
}
|
|
100
129
|
this.cache.set(key, {
|
|
101
130
|
data,
|
|
102
131
|
timestamp: Date.now(),
|
|
103
|
-
ttl:
|
|
132
|
+
ttl: duration
|
|
104
133
|
});
|
|
105
134
|
}
|
|
106
135
|
}
|
package/dist/lib/client.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import Typesense from 'typesense';
|
|
2
2
|
import { type TypesenseConfig } from '../types.js';
|
|
3
|
-
export declare const createClient: (
|
|
3
|
+
export declare const createClient: (config: TypesenseConfig['typesense']) => Typesense.Client;
|
|
4
4
|
//# sourceMappingURL=client.d.ts.map
|
package/dist/lib/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/lib/client.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,WAAW,CAAA;AAEjC,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/lib/client.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,WAAW,CAAA;AAEjC,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,aAAa,CAAA;AAyDlD,eAAO,MAAM,YAAY,WAAY,eAAe,CAAC,WAAW,CAAC,KAAG,SAAS,CAAC,MAa7E,CAAA"}
|
package/dist/lib/client.js
CHANGED
|
@@ -1,52 +1,56 @@
|
|
|
1
1
|
import Typesense from 'typesense';
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
const isOffline = ()=>{
|
|
2
|
+
const error = new Error('Typesense offline');
|
|
3
|
+
const offline = ()=>{
|
|
5
4
|
const evt = process.env.npm_lifecycle_event || '';
|
|
6
5
|
if (evt.includes('build') || evt === 'tsc') return true;
|
|
7
|
-
if (process.argv.some((
|
|
6
|
+
if (process.argv.some((arg)=>/(?:^|:|\/)(?:build|tsc)(?::|$)/i.test(arg))) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
8
9
|
if (process.env.CI === 'true') return true;
|
|
9
10
|
return false;
|
|
10
11
|
};
|
|
11
|
-
const
|
|
12
|
+
const offlineClient = ()=>{
|
|
13
|
+
const reject = ()=>Promise.reject(error);
|
|
14
|
+
const documents = ()=>({
|
|
15
|
+
delete: reject,
|
|
16
|
+
import: reject,
|
|
17
|
+
search: reject,
|
|
18
|
+
upsert: reject
|
|
19
|
+
});
|
|
20
|
+
const collections = Object.assign(()=>({
|
|
21
|
+
documents,
|
|
22
|
+
retrieve: reject,
|
|
23
|
+
update: reject
|
|
24
|
+
}), {
|
|
25
|
+
create: reject,
|
|
26
|
+
retrieve: reject
|
|
27
|
+
});
|
|
12
28
|
return {
|
|
13
29
|
analytics: {
|
|
14
|
-
retrieve:
|
|
15
|
-
return Promise.reject(offline);
|
|
16
|
-
}
|
|
17
|
-
},
|
|
18
|
-
collections: ()=>{
|
|
19
|
-
return {
|
|
20
|
-
retrieve: ()=>{
|
|
21
|
-
return Promise.reject(offline);
|
|
22
|
-
}
|
|
23
|
-
};
|
|
30
|
+
retrieve: reject
|
|
24
31
|
},
|
|
32
|
+
collections,
|
|
25
33
|
health: {
|
|
26
|
-
retrieve:
|
|
27
|
-
|
|
28
|
-
|
|
34
|
+
retrieve: reject
|
|
35
|
+
},
|
|
36
|
+
multiSearch: {
|
|
37
|
+
perform: reject
|
|
29
38
|
},
|
|
30
39
|
operations: {
|
|
31
|
-
get:
|
|
32
|
-
return Promise.reject(offline);
|
|
33
|
-
}
|
|
40
|
+
get: reject
|
|
34
41
|
}
|
|
35
42
|
};
|
|
36
43
|
};
|
|
37
|
-
export const createClient = (
|
|
38
|
-
if (
|
|
39
|
-
|
|
40
|
-
client = createOfflineClient();
|
|
41
|
-
return client;
|
|
44
|
+
export const createClient = (config)=>{
|
|
45
|
+
if (offline()) {
|
|
46
|
+
return offlineClient();
|
|
42
47
|
}
|
|
43
|
-
|
|
44
|
-
apiKey:
|
|
45
|
-
connectionTimeoutSeconds:
|
|
46
|
-
nodes:
|
|
48
|
+
return new Typesense.Client({
|
|
49
|
+
apiKey: config.apiKey,
|
|
50
|
+
connectionTimeoutSeconds: config.connectionTimeoutSeconds ?? 2,
|
|
51
|
+
nodes: config.nodes.map((node)=>({
|
|
47
52
|
...node,
|
|
48
53
|
port: typeof node.port === 'string' ? parseInt(node.port, 10) : node.port
|
|
49
54
|
}))
|
|
50
55
|
});
|
|
51
|
-
return client;
|
|
52
56
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type ChangeEvent, type FocusEvent, type KeyboardEvent, type ReactNode, type RefObject } from 'react';
|
|
2
2
|
import { type ThemeConfig } from '../components/themes/types.js';
|
|
3
3
|
import { type BaseSearchInputProps, type SearchResult } from '../types.js';
|
|
4
4
|
export interface HeadlessSearchInputProps<T = Record<string, unknown>> extends BaseSearchInputProps<T> {
|
|
@@ -7,71 +7,75 @@ export interface HeadlessSearchInputProps<T = Record<string, unknown>> extends B
|
|
|
7
7
|
*/
|
|
8
8
|
collections?: string[];
|
|
9
9
|
/**
|
|
10
|
-
* Enable suggestions
|
|
10
|
+
* Enable search suggestions
|
|
11
11
|
*/
|
|
12
12
|
enableSuggestions?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Callback when a search result is hovered
|
|
15
|
+
*/
|
|
16
|
+
onResultHover?: (result: SearchResult<T>) => void;
|
|
13
17
|
/**
|
|
14
18
|
* Number of results to show per page
|
|
15
19
|
*/
|
|
16
20
|
perPage?: number;
|
|
17
21
|
/**
|
|
18
|
-
* Show
|
|
22
|
+
* Show dates in search results
|
|
19
23
|
*/
|
|
20
24
|
renderDate?: boolean;
|
|
21
25
|
/**
|
|
22
26
|
* Custom render function for error state
|
|
23
27
|
*/
|
|
24
|
-
renderError?: (error: string) =>
|
|
28
|
+
renderError?: (error: string) => ReactNode;
|
|
25
29
|
/**
|
|
26
|
-
* Custom input element
|
|
30
|
+
* Custom input element for complete rendering control
|
|
27
31
|
*/
|
|
28
32
|
renderInput?: (props: {
|
|
29
33
|
className: string;
|
|
30
|
-
onBlur: (
|
|
31
|
-
onChange: (
|
|
34
|
+
onBlur: (event: FocusEvent<HTMLInputElement>) => void;
|
|
35
|
+
onChange: (event: ChangeEvent<HTMLInputElement>) => void;
|
|
32
36
|
onFocus: () => void;
|
|
33
|
-
onKeyDown: (
|
|
37
|
+
onKeyDown: (event: KeyboardEvent<HTMLInputElement>) => void;
|
|
34
38
|
placeholder: string;
|
|
35
|
-
ref:
|
|
39
|
+
ref: RefObject<HTMLInputElement | null>;
|
|
36
40
|
value: string;
|
|
37
|
-
}) =>
|
|
41
|
+
}) => ReactNode;
|
|
38
42
|
/**
|
|
39
43
|
* Custom render function for loading state
|
|
40
44
|
*/
|
|
41
|
-
renderLoading?: () =>
|
|
45
|
+
renderLoading?: () => ReactNode;
|
|
42
46
|
/**
|
|
43
47
|
* Custom render function for no results
|
|
44
48
|
*/
|
|
45
|
-
renderNoResults?: (query: string) =>
|
|
49
|
+
renderNoResults?: (query: string) => ReactNode;
|
|
46
50
|
/**
|
|
47
|
-
* Custom render function for results
|
|
51
|
+
* Custom render function for individual search results
|
|
48
52
|
*/
|
|
49
53
|
renderResult?: (result: SearchResult<T>, index: number, handlers: {
|
|
50
54
|
onClick: (result: SearchResult<T>) => void;
|
|
51
55
|
onHover?: (result: SearchResult<T>) => void;
|
|
52
|
-
}) =>
|
|
56
|
+
}) => ReactNode;
|
|
53
57
|
/**
|
|
54
58
|
* Custom render function for results header
|
|
55
59
|
*/
|
|
56
|
-
renderResultsHeader?: (found: number) =>
|
|
60
|
+
renderResultsHeader?: (found: number) => ReactNode;
|
|
57
61
|
/**
|
|
58
62
|
* Custom CSS class for individual result items
|
|
59
63
|
*/
|
|
60
64
|
resultItemClassName?: string;
|
|
61
65
|
/**
|
|
62
|
-
* Custom CSS class for the results
|
|
66
|
+
* Custom CSS class for the results content
|
|
63
67
|
*/
|
|
64
68
|
resultsClassName?: string;
|
|
65
69
|
/**
|
|
66
|
-
* Custom CSS class for results container
|
|
70
|
+
* Custom CSS class for the outer results container
|
|
67
71
|
*/
|
|
68
72
|
resultsContainerClassName?: string;
|
|
69
73
|
/**
|
|
70
|
-
* Show loading
|
|
74
|
+
* Show loading indicator during searches
|
|
71
75
|
*/
|
|
72
76
|
showLoading?: boolean;
|
|
73
77
|
/**
|
|
74
|
-
* Show
|
|
78
|
+
* Show the total number of search results
|
|
75
79
|
*/
|
|
76
80
|
showResultCount?: boolean;
|
|
77
81
|
/**
|
|
@@ -79,7 +83,7 @@ export interface HeadlessSearchInputProps<T = Record<string, unknown>> extends B
|
|
|
79
83
|
*/
|
|
80
84
|
theme?: string | ThemeConfig;
|
|
81
85
|
/**
|
|
82
|
-
* Enable vector search
|
|
86
|
+
* Enable vector search instead of text search
|
|
83
87
|
*/
|
|
84
88
|
vector?: boolean;
|
|
85
89
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"headlessSearch.d.ts","sourceRoot":"","sources":["../../src/lib/headlessSearch.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"headlessSearch.d.ts","sourceRoot":"","sources":["../../src/lib/headlessSearch.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,UAAU,EACf,KAAK,aAAa,EAClB,KAAK,SAAS,EACd,KAAK,SAAS,EACf,MAAM,OAAO,CAAA;AAEd,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,+BAA+B,CAAA;AAChE,OAAO,EAAE,KAAK,oBAAoB,EAAE,KAAK,YAAY,EAAE,MAAM,aAAa,CAAA;AAE1E,MAAM,WAAW,wBAAwB,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CACnE,SAAQ,oBAAoB,CAAC,CAAC,CAAC;IAC/B;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,EAAE,CAAA;IAEtB;;OAEG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAA;IAE3B;;OAEG;IACH,aAAa,CAAC,EAAE,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,KAAK,IAAI,CAAA;IAEjD;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;IAEhB;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;IAEpB;;OAEG;IACH,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,SAAS,CAAA;IAE1C;;OAEG;IACH,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE;QACpB,SAAS,EAAE,MAAM,CAAA;QACjB,MAAM,EAAE,CAAC,KAAK,EAAE,UAAU,CAAC,gBAAgB,CAAC,KAAK,IAAI,CAAA;QACrD,QAAQ,EAAE,CAAC,KAAK,EAAE,WAAW,CAAC,gBAAgB,CAAC,KAAK,IAAI,CAAA;QACxD,OAAO,EAAE,MAAM,IAAI,CAAA;QACnB,SAAS,EAAE,CAAC,KAAK,EAAE,aAAa,CAAC,gBAAgB,CAAC,KAAK,IAAI,CAAA;QAC3D,WAAW,EAAE,MAAM,CAAA;QACnB,GAAG,EAAE,SAAS,CAAC,gBAAgB,GAAG,IAAI,CAAC,CAAA;QACvC,KAAK,EAAE,MAAM,CAAA;KACd,KAAK,SAAS,CAAA;IAEf;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,SAAS,CAAA;IAE/B;;OAEG;IACH,eAAe,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,SAAS,CAAA;IAE9C;;OAEG;IACH,YAAY,CAAC,EAAE,CACb,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,EACvB,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE;QACR,OAAO,EAAE,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,KAAK,IAAI,CAAA;QAC1C,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,KAAK,IAAI,CAAA;KAC5C,KACE,SAAS,CAAA;IAEd;;OAEG;IACH,mBAAmB,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,SAAS,CAAA;IAElD;;OAEG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAA;IAE5B;;OAEG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAA;IAEzB;;OAEG;IACH,yBAAyB,CAAC,EAAE,MAAM,CAAA;IAElC;;OAEG;IACH,WAAW,CAAC,EAAE,OAAO,CAAA;IAErB;;OAEG;IACH,eAAe,CAAC,EAAE,OAAO,CAAA;IAEzB;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,GAAG,WAAW,CAAA;IAE5B;;OAEG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB"}
|
package/dist/lib/hooks.d.ts
CHANGED
|
@@ -1,13 +1,5 @@
|
|
|
1
|
-
import { type CollectionAfterChangeHook, type CollectionAfterDeleteHook } from 'payload';
|
|
2
1
|
import type Typesense from 'typesense';
|
|
3
|
-
import { type BaseDocument, type TypesenseConfig } from '../types.js';
|
|
4
|
-
export declare const
|
|
5
|
-
|
|
6
|
-
afterDelete?: Record<string, CollectionAfterDeleteHook[]>;
|
|
7
|
-
}) => {
|
|
8
|
-
afterChange?: Record<string, CollectionAfterChangeHook[]>;
|
|
9
|
-
afterDelete?: Record<string, CollectionAfterDeleteHook[]>;
|
|
10
|
-
};
|
|
11
|
-
export declare const syncDocumentToTypesense: (typesenseClient: Typesense.Client, collectionSlug: string, doc: BaseDocument, _operation: "create" | "update", config: NonNullable<TypesenseConfig["collections"]>[string] | undefined, vector?: NonNullable<TypesenseConfig["vectorSearch"]>) => Promise<void>;
|
|
12
|
-
export declare const deleteDocumentFromTypesense: (typesenseClient: Typesense.Client, collectionSlug: string, docId: string) => Promise<void>;
|
|
2
|
+
import { type BaseDocument, type TypesenseCollectionConfig, type TypesenseConfig } from '../types.js';
|
|
3
|
+
export declare const syncDoc: (client: Typesense.Client, slug: string, doc: BaseDocument, config: TypesenseCollectionConfig, vector?: NonNullable<TypesenseConfig['vectorSearch']>) => Promise<void>;
|
|
4
|
+
export declare const deleteDoc: (client: Typesense.Client, slug: string, id: string) => Promise<void>;
|
|
13
5
|
//# sourceMappingURL=hooks.d.ts.map
|
package/dist/lib/hooks.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/lib/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/lib/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,WAAW,CAAA;AAEtC,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,yBAAyB,EAC9B,KAAK,eAAe,EACrB,MAAM,aAAa,CAAA;AAIpB,eAAO,MAAM,OAAO,WACV,SAAS,CAAC,MAAM,QAClB,MAAM,OACP,YAAY,UACT,yBAAyB,WACxB,WAAW,CAAC,eAAe,CAAC,cAAc,CAAC,CAAC,KACpD,OAAO,CAAC,IAAI,CAcd,CAAA;AAED,eAAO,MAAM,SAAS,WACZ,SAAS,CAAC,MAAM,QAClB,MAAM,MACR,MAAM,KACT,OAAO,CAAC,IAAI,CAMd,CAAA"}
|
package/dist/lib/hooks.js
CHANGED
|
@@ -1,54 +1,21 @@
|
|
|
1
1
|
import { ensureCollection } from '../utils/ensureCollection.js';
|
|
2
|
-
import {
|
|
3
|
-
export const
|
|
4
|
-
const hooks = {
|
|
5
|
-
...existingHooks
|
|
6
|
-
};
|
|
7
|
-
const vector = pluginOptions.vectorSearch;
|
|
8
|
-
for (const [collectionSlug, config] of Object.entries(pluginOptions.collections || {})){
|
|
9
|
-
if (!config?.enabled) {
|
|
10
|
-
continue;
|
|
11
|
-
}
|
|
12
|
-
const changeHook = async ({ doc, operation })=>{
|
|
13
|
-
await syncDocumentToTypesense(typesenseClient, collectionSlug, doc, operation, config, vector);
|
|
14
|
-
};
|
|
15
|
-
hooks.afterChange = {
|
|
16
|
-
...hooks.afterChange,
|
|
17
|
-
[collectionSlug]: [
|
|
18
|
-
...hooks.afterChange?.[collectionSlug] || [],
|
|
19
|
-
changeHook
|
|
20
|
-
]
|
|
21
|
-
};
|
|
22
|
-
const deleteHook = async ({ doc })=>{
|
|
23
|
-
if (typeof doc?.id === 'string' || typeof doc?.id === 'number') {
|
|
24
|
-
await deleteDocumentFromTypesense(typesenseClient, collectionSlug, String(doc?.id));
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
hooks.afterDelete = {
|
|
28
|
-
...hooks.afterDelete,
|
|
29
|
-
[collectionSlug]: [
|
|
30
|
-
...hooks.afterDelete?.[collectionSlug] || [],
|
|
31
|
-
deleteHook
|
|
32
|
-
]
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
return hooks;
|
|
36
|
-
};
|
|
37
|
-
export const syncDocumentToTypesense = async (typesenseClient, collectionSlug, doc, _operation, config, vector)=>{
|
|
2
|
+
import { mapCollection, mapDoc } from './schema-mapper.js';
|
|
3
|
+
export const syncDoc = async (client, slug, doc, config, vector)=>{
|
|
38
4
|
try {
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
5
|
+
const document = mapDoc(doc, slug, config);
|
|
6
|
+
if (!document) return;
|
|
7
|
+
const schema = mapCollection(slug, config, vector);
|
|
8
|
+
const { available } = await ensureCollection(client, slug, schema, vector);
|
|
9
|
+
if (!available) return;
|
|
10
|
+
await client.collections(slug).documents().upsert(document);
|
|
44
11
|
} catch {
|
|
45
|
-
//
|
|
12
|
+
// Avoid interrupting Payload document saves
|
|
46
13
|
}
|
|
47
14
|
};
|
|
48
|
-
export const
|
|
15
|
+
export const deleteDoc = async (client, slug, id)=>{
|
|
49
16
|
try {
|
|
50
|
-
await
|
|
17
|
+
await client.collections(slug).documents(id).delete();
|
|
51
18
|
} catch {
|
|
52
|
-
//
|
|
19
|
+
// Avoid interrupting Payload document deletions
|
|
53
20
|
}
|
|
54
21
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Payload } from 'payload';
|
|
2
2
|
import type Typesense from 'typesense';
|
|
3
3
|
import { type TypesenseConfig } from '../types.js';
|
|
4
|
-
export declare const initializeTypesense: (payload: Payload,
|
|
4
|
+
export declare const initializeTypesense: (payload: Payload, client: Typesense.Client, options: TypesenseConfig) => Promise<void>;
|
|
5
5
|
//# sourceMappingURL=initialization.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initialization.d.ts","sourceRoot":"","sources":["../../src/lib/initialization.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,SAAS,CAAA;AACtC,OAAO,KAAK,SAAS,MAAM,WAAW,CAAA;AAEtC,OAAO,
|
|
1
|
+
{"version":3,"file":"initialization.d.ts","sourceRoot":"","sources":["../../src/lib/initialization.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,SAAS,CAAA;AACtC,OAAO,KAAK,SAAS,MAAM,WAAW,CAAA;AAEtC,OAAO,EAIL,KAAK,eAAe,EACrB,MAAM,aAAa,CAAA;AAKpB,eAAO,MAAM,mBAAmB,YACrB,OAAO,UACR,SAAS,CAAC,MAAM,WACf,eAAe,KACvB,OAAO,CAAC,IAAI,CAYd,CAAA"}
|
|
@@ -1,79 +1,81 @@
|
|
|
1
1
|
import { ensureCollection } from '../utils/ensureCollection.js';
|
|
2
2
|
import { testConnection } from '../utils/testConnection.js';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const entries = Object.entries(pluginOptions.collections || {});
|
|
13
|
-
const vector = pluginOptions.vectorSearch;
|
|
14
|
-
for (const [slug, cfg] of entries){
|
|
15
|
-
if (cfg?.enabled) {
|
|
16
|
-
await initializeCollection(payload, typesenseClient, slug, cfg, vector);
|
|
17
|
-
}
|
|
3
|
+
import { mapCollection, mapDoc } from './schema-mapper.js';
|
|
4
|
+
export const initializeTypesense = async (payload, client, options)=>{
|
|
5
|
+
const connected = await testConnection(client);
|
|
6
|
+
if (!connected) return;
|
|
7
|
+
const vector = options.vectorSearch;
|
|
8
|
+
const autoSync = options.settings?.autoSync !== false;
|
|
9
|
+
for (const [slug, config] of Object.entries(options.collections || {})){
|
|
10
|
+
if (!config?.enabled) continue;
|
|
11
|
+
await initializeCollection(payload, client, slug, config, vector, autoSync);
|
|
18
12
|
}
|
|
19
13
|
};
|
|
20
|
-
const initializeCollection = async (payload,
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
await syncExistingDocuments(payload, typesenseClient, collectionSlug, config);
|
|
14
|
+
const initializeCollection = async (payload, client, slug, config, vector, autoSync = true)=>{
|
|
15
|
+
if (!payload.collections[slug]) return;
|
|
16
|
+
const schema = mapCollection(slug, config, vector);
|
|
17
|
+
const { available } = await ensureCollection(client, slug, schema, vector, true);
|
|
18
|
+
if (!available || !autoSync) return;
|
|
19
|
+
await syncExistingDocuments(payload, client, slug, config);
|
|
27
20
|
};
|
|
28
|
-
const syncExistingDocuments = async (payload,
|
|
21
|
+
const syncExistingDocuments = async (payload, client, slug, config)=>{
|
|
29
22
|
try {
|
|
30
|
-
const limit = config
|
|
23
|
+
const limit = config.syncLimit ?? 1000;
|
|
24
|
+
const batchSize = 100;
|
|
31
25
|
let page = 1;
|
|
32
26
|
let hasMore = true;
|
|
33
|
-
let totalPages;
|
|
34
27
|
while(hasMore){
|
|
35
|
-
const { docs, hasNextPage
|
|
36
|
-
collection:
|
|
28
|
+
const { docs, hasNextPage } = await payload.find({
|
|
29
|
+
collection: slug,
|
|
37
30
|
depth: 0,
|
|
38
31
|
limit,
|
|
39
32
|
page
|
|
40
33
|
});
|
|
41
|
-
if (
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (!docs.length) {
|
|
45
|
-
break;
|
|
46
|
-
}
|
|
47
|
-
const batchSize = 100;
|
|
48
|
-
for(let i = 0; i < docs.length; i += batchSize){
|
|
49
|
-
const batch = docs.slice(i, i + batchSize);
|
|
34
|
+
if (!docs.length) break;
|
|
35
|
+
for(let index = 0; index < docs.length; index += batchSize){
|
|
36
|
+
const batch = docs.slice(index, index + batchSize);
|
|
50
37
|
const mapped = batch.filter((doc)=>{
|
|
51
38
|
if (typeof doc !== 'object' || doc === null) return false;
|
|
52
39
|
if (!('id' in doc)) return false;
|
|
53
40
|
const id = doc.id;
|
|
54
41
|
return typeof id === 'string' || typeof id === 'number';
|
|
55
|
-
}).map((doc)=>
|
|
42
|
+
}).map((doc)=>mapDoc(doc, slug, config)).filter((doc)=>doc !== null);
|
|
43
|
+
if (!mapped.length) continue;
|
|
56
44
|
try {
|
|
57
|
-
await
|
|
45
|
+
await client.collections(slug).documents().import(mapped, {
|
|
58
46
|
action: 'upsert'
|
|
59
47
|
});
|
|
60
48
|
} catch (error) {
|
|
61
|
-
const
|
|
62
|
-
if (
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
49
|
+
const results = error.importResults;
|
|
50
|
+
if (!Array.isArray(results)) {
|
|
51
|
+
payload.logger.error({
|
|
52
|
+
err: error,
|
|
53
|
+
msg: `Failed to import Typesense documents for ${slug}`
|
|
54
|
+
});
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
for (const [index, result] of results.entries()){
|
|
58
|
+
if (result.success) continue;
|
|
59
|
+
const document = mapped[index];
|
|
60
|
+
if (!document) continue;
|
|
61
|
+
try {
|
|
62
|
+
await client.collections(slug).documents().upsert(document);
|
|
63
|
+
} catch (syncError) {
|
|
64
|
+
payload.logger.error({
|
|
65
|
+
err: syncError,
|
|
66
|
+
msg: `Failed to sync Typesense document in ${slug}`
|
|
67
|
+
});
|
|
69
68
|
}
|
|
70
69
|
}
|
|
71
70
|
}
|
|
72
71
|
}
|
|
73
|
-
hasMore = hasNextPage
|
|
72
|
+
hasMore = hasNextPage;
|
|
74
73
|
page++;
|
|
75
74
|
}
|
|
76
|
-
} catch
|
|
77
|
-
|
|
75
|
+
} catch (error) {
|
|
76
|
+
payload.logger.error({
|
|
77
|
+
err: error,
|
|
78
|
+
msg: `Failed to synchronize Typesense collection ${slug}`
|
|
79
|
+
});
|
|
78
80
|
}
|
|
79
81
|
};
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import { type BaseDocument, type
|
|
2
|
-
export declare const
|
|
3
|
-
|
|
4
|
-
fields: CollectionFieldSchema[];
|
|
5
|
-
};
|
|
6
|
-
export declare const mapToTypesense: (doc: BaseDocument, _collectionSlug: string, config: NonNullable<TypesenseConfig["collections"]>[string]) => Record<string, number | string> | null;
|
|
1
|
+
import { type BaseDocument, type CollectionSchema, type TypesenseCollectionConfig, type TypesenseConfig } from '../types.js';
|
|
2
|
+
export declare const mapCollection: (slug: string, config: TypesenseCollectionConfig, vector?: NonNullable<TypesenseConfig['vectorSearch']>) => CollectionSchema;
|
|
3
|
+
export declare const mapDoc: (doc: BaseDocument, _slug: string, config: TypesenseCollectionConfig) => Record<string, number | string> | null;
|
|
7
4
|
//# sourceMappingURL=schema-mapper.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema-mapper.d.ts","sourceRoot":"","sources":["../../src/lib/schema-mapper.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"schema-mapper.d.ts","sourceRoot":"","sources":["../../src/lib/schema-mapper.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,YAAY,EAEjB,KAAK,gBAAgB,EACrB,KAAK,yBAAyB,EAC9B,KAAK,eAAe,EACrB,MAAM,aAAa,CAAA;AAKpB,eAAO,MAAM,aAAa,SAClB,MAAM,UACJ,yBAAyB,WACxB,WAAW,CAAC,eAAe,CAAC,cAAc,CAAC,CAAC,KACpD,gBA2DF,CAAA;AAED,eAAO,MAAM,MAAM,QACZ,YAAY,SACV,MAAM,UACL,yBAAyB,KAChC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,GAAG,IAyCpC,CAAA"}
|