Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Firecms] Add support for typesense search #493

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dist
.env.test.local
.env.production.local

yarn.lock
npm-debug.log*
yarn-debug.log*
yarn-error.log*
Expand Down
4 changes: 3 additions & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-router": "^6.2.0",
"react-router-dom": "^6.2.0"
"react-router-dom": "^6.2.0",
"typesense": "1.5.3"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this dependency to peer dependencies?
So that only users who want to use it can pull it in.
Thanks!

},
"eslintConfig": {
"extends": [
Expand Down Expand Up @@ -120,6 +121,7 @@
"react-router-dom": "^6.9.0",
"tsd": "^0.28.1",
"typescript": "^5.0.2",
"typesense": "1.5.3",
"vite": "^4.2.1"
},
"files": [
Expand Down
2 changes: 1 addition & 1 deletion lib/src/firebase_app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ export { useInitializeAppCheck } from "./hooks/useInitializeAppCheck";
export * from "./types/auth";

export type { FirestoreTextSearchController } from "./types/text_search";
export { performAlgoliaTextSearch } from "./types/text_search";
export { performAlgoliaTextSearch, performTypesenseSearch } from "./types/text_search";
37 changes: 34 additions & 3 deletions lib/src/firebase_app/types/text_search.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { SearchIndex } from "algoliasearch";
import Collection from "typesense/lib/Typesense/Collection";
import { SearchParams } from "typesense/lib/Typesense/Documents";

/**
* Use this controller to return a list of ids from a search index, given a
Expand All @@ -10,7 +12,10 @@ import { SearchIndex } from "algoliasearch";
* @see performAlgoliaTextSearch
* @category Firebase
*/
export type FirestoreTextSearchController = (props: { path: string, searchString: string }) => Promise<readonly string[]> | undefined;
export type FirestoreTextSearchController = (props: {
path: string;
searchString: string;
}) => Promise<readonly string[]> | undefined;

/**
* Utility function to perform a text search in an algolia index,
Expand All @@ -19,8 +24,10 @@ export type FirestoreTextSearchController = (props: { path: string, searchString
* @param query
* @category Firebase
*/
export function performAlgoliaTextSearch(index: SearchIndex, query: string): Promise<readonly string[]> {

export function performAlgoliaTextSearch(
index: SearchIndex,
query: string
): Promise<readonly string[]> {
console.debug("Performing Algolia query", index, query);
return index
.search(query)
Expand All @@ -32,3 +39,27 @@ export function performAlgoliaTextSearch(index: SearchIndex, query: string): Pro
return [];
});
}

/**
* Utility function to perform a text search in an typesense index,
* returning the ids of the entities.
* @param index
* @param query
* @category Firebase
*/
export function performTypesenseSearch(
index: Collection<{}>,
searchParameters: SearchParams
): Promise<readonly string[]> {
return index
.documents()
.search(searchParameters)
.then(({ hits }) => {
if (hits === undefined) return [];
return hits.map((hit: any) => hit.document.id as string);
})
.catch((err: any) => {
console.log(err);
return [];
});
}