Skip to content

Commit

Permalink
feat: added remaining utils: update, remove, getById, count
Browse files Browse the repository at this point in the history
  • Loading branch information
amandesai01 committed Jul 8, 2024
1 parent a2bf6b0 commit bb7c675
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/runtime/composables/useOramaSearch.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import {
insert as insertOrama,
insertMultiple as insertMultipleOrama,
search as searchOrama,
update as updateOrama,
updateMultiple as updateMultipleOrama,
remove as removeOrama,
removeMultiple as removeMultipleOrama,
getByID as getByIDOrama,
count as countOrama,
type PartialSchemaDeep,
type Results,
type SearchParams,
type AnyOrama,
type TypedDocument,
} from "@orama/orama";

import { wrapPromiseToRef } from "../utils";
import { ref, type Ref } from "#imports";
import useOramaInstance from "./useOramaInstance";

type DocumentID = string | number; // @orama/orama/dist/components/internal-document-id-store

export default function useOramaSearch<T extends AnyOrama>(id?: string) {
const oramaInstance = useOramaInstance<T>(id);

Expand Down Expand Up @@ -53,13 +65,82 @@ export default function useOramaSearch<T extends AnyOrama>(id?: string) {
);
};

const insertWrapped = (
doc: PartialSchemaDeep<any>,
language?: string,
skipHooks?: boolean
) =>
wrapPromiseToRef(insertOrama<T>(oramaInstance, doc, language, skipHooks));

const updateMultipleWrapped = (
ids: string[],
docs: PartialSchemaDeep<TypedDocument<T>>[],
batchSize?: number,
language?: string,
skipHooks?: boolean
) =>
wrapPromiseToRef(
updateMultipleOrama<T>(
oramaInstance,
ids,
docs,
batchSize,
language,
skipHooks
)
);

const updateWrapped = (
id: string,
doc: PartialSchemaDeep<TypedDocument<T>>,
language?: string,
skipHooks?: boolean
) =>
wrapPromiseToRef(
updateOrama<T>(oramaInstance, id, doc, language, skipHooks)
);

const removeMultipleWrapped = (
ids: DocumentID[],
batchSize?: number,
language?: string,
skipHooks?: boolean
) =>
wrapPromiseToRef(
removeMultipleOrama<T>(oramaInstance, ids, batchSize, language, skipHooks)
);

const removeWrapped = (
id: DocumentID,
language?: string,
skipHooks?: boolean
) => wrapPromiseToRef(removeOrama<T>(oramaInstance, id, language, skipHooks));

const getByIdWrapped = <ResultDocument extends TypedDocument<T>>(
id: string
) => wrapPromiseToRef(getByIDOrama<T, ResultDocument>(oramaInstance, id));

const countWrapped = () => wrapPromiseToRef(countOrama(oramaInstance));

const clearSearchResults = () => {
searchResults.value = null;
};

return {
search: searchWrapped,

insertMultiple: insertMultipleWrapped,
insert: insertWrapped,

updateMultiple: updateMultipleWrapped,
update: updateWrapped,

removeMultiple: removeMultipleWrapped,
remove: removeWrapped,

getById: getByIdWrapped,
count: countWrapped,

searchResults,
searchPending,
searchError,
Expand Down

0 comments on commit bb7c675

Please sign in to comment.