diff --git a/packages/orama/src/components/groups.ts b/packages/orama/src/components/groups.ts index 393dbea02..40ce8d66f 100644 --- a/packages/orama/src/components/groups.ts +++ b/packages/orama/src/components/groups.ts @@ -32,7 +32,7 @@ const ALLOWED_TYPES = ['string', 'number', 'boolean'] export async function getGroups>( orama: T, results: TokenScore[], - groupBy: GroupByParams, + groupBy: GroupByParams, ): Promise> { const properties = groupBy.properties const propertiesLength = properties.length diff --git a/packages/orama/src/methods/create.ts b/packages/orama/src/methods/create.ts index 17b29728c..a446bb797 100644 --- a/packages/orama/src/methods/create.ts +++ b/packages/orama/src/methods/create.ts @@ -1,13 +1,14 @@ import { formatElapsedTime, getDocumentIndexId, getDocumentProperties, validateSchema } from '../components/defaults.js' -import { createDocumentsStore } from '../components/documents-store.js' +import { DocumentsStore, createDocumentsStore } from '../components/documents-store.js' import { FUNCTION_COMPONENTS, OBJECT_COMPONENTS, SINGLE_OR_ARRAY_COMPONENTS } from '../components/hooks.js' -import { createIndex } from '../components/index.js' +import { Index, createIndex } from '../components/index.js' import { createInternalDocumentIDStore } from '../components/internal-document-id-store.js' -import { createSorter } from '../components/sorter.js' +import { Sorter, createSorter } from '../components/sorter.js' import { createTokenizer } from '../components/tokenizer/index.js' import { createError } from '../errors.js' import { AfterSearch, + AnySchema, ArrayCallbackComponents, Components, FunctionComponents, @@ -84,11 +85,11 @@ function validateComponents> ({ + const OramaSchema extends AnySchema, + TIndex = Index, + TDocumentStore = DocumentsStore, + TSorter = Sorter, + ResultOrama extends Orama = Orama> ({ schema, sort, language, diff --git a/packages/orama/src/methods/search.ts b/packages/orama/src/methods/search.ts index 2efd63bca..ba38e03ea 100644 --- a/packages/orama/src/methods/search.ts +++ b/packages/orama/src/methods/search.ts @@ -12,11 +12,13 @@ import { createError } from '../errors.js' import type { AnyOrama, BM25Params, + ComparisonOperator, CustomSorterFunctionItem, ElapsedTime, IndexMap, Result, Results, + SchemaValidKeys, SearchContext, SearchParams, SearchableValue, @@ -158,7 +160,8 @@ export async function search, boolean | string | string[] | ComparisonOperator>; + whereFiltersIDs = await orama.index.searchByWhereClause(context, index, whereParams) } const tokensLength = tokens.length diff --git a/packages/orama/src/methods/serialization.ts b/packages/orama/src/methods/serialization.ts index d81b8b902..48b299470 100644 --- a/packages/orama/src/methods/serialization.ts +++ b/packages/orama/src/methods/serialization.ts @@ -1,10 +1,12 @@ import { AnyOrama } from '../types.js' +import { Language } from '../index.js' export interface RawData { internalDocumentIDStore: unknown index: unknown docs: unknown sorting: unknown + language: Language } export async function load(orama: T, raw: RawData): Promise { @@ -12,6 +14,7 @@ export async function load(orama: T, raw: RawData): Promise< orama.data.index = await orama.index.load(orama.internalDocumentIDStore, raw.index) orama.data.docs = await orama.documentsStore.load(orama.internalDocumentIDStore, raw.docs) orama.data.sorting = await orama.sorter.load(orama.internalDocumentIDStore, raw.sorting) + orama.tokenizer.language = raw.language } export async function save(orama: T): Promise { @@ -20,5 +23,6 @@ export async function save(orama: T): Promise { index: await orama.index.save(orama.data.index), docs: await orama.documentsStore.save(orama.data.docs), sorting: await orama.sorter.save(orama.data.sorting), + language: orama.tokenizer.language, } } diff --git a/packages/orama/src/types.ts b/packages/orama/src/types.ts index 0f557273e..601a3ebc2 100644 --- a/packages/orama/src/types.ts +++ b/packages/orama/src/types.ts @@ -1,4 +1,7 @@ +import { DocumentsStore } from './components/documents-store.js' +import { Index } from './components/index.js' import { DocumentID, InternalDocumentID, InternalDocumentIDStore } from './components/internal-document-id-store.js' +import { Sorter } from './components/sorter.js' import { Language } from './components/tokenizer/languages.js' export type Nullable = T | null @@ -113,10 +116,15 @@ export type Reduce = { getInitialValue: (elementCount: number) => T } -export type GroupByParams = { - properties: string[] +// thanks to https://github.com/sindresorhus/type-fest/blob/main/source/literal-union.d.ts +export type LiteralUnion = LiteralType | (BaseType & Record) + +export type SchemaValidKeys = keyof T['schema'] extends string ? LiteralUnion : string; + +export type GroupByParams = { + properties: SchemaValidKeys[] maxResult?: number - reduce?: Reduce + reduce?: Reduce } export type ComparisonOperator = { @@ -144,7 +152,7 @@ export type SorterParams = { /** * The key of the document used to sort the result. */ - property: keyof T['schema'] extends string ? keyof T['schema'] : string; + property: SchemaValidKeys; /** * Whether to sort the result in ascending or descending order. */ @@ -269,7 +277,7 @@ export type SearchParams> * } * }) */ - groupBy?: GroupByParams + groupBy?: GroupByParams /** * Filter the search results. @@ -289,7 +297,7 @@ export type SearchParams> * } * }); */ - where?: Record + where?: Partial, boolean | string | string[] | ComparisonOperator>> /** * Threshold to use for refining the search results. @@ -805,7 +813,7 @@ export type PickInferGeneric = T extends AnyGeneric : never : never -export type Orama = FunctionComponents & +export type Orama = FunctionComponents & Internals, AnyGenericDocumentStore, AnyGenericSorter> & ArrayCallbackComponents & OramaID diff --git a/packages/orama/tests/search.test.ts b/packages/orama/tests/search.test.ts index bc5dbd938..533dfa4e2 100644 --- a/packages/orama/tests/search.test.ts +++ b/packages/orama/tests/search.test.ts @@ -1,6 +1,6 @@ import t from 'tap' import { stopwords as englishStopwords } from '@orama/stopwords/english' -import { Orama, create, getByID, insert, insertMultiple, search } from '../src/index.js' +import { create, getByID, insert, insertMultiple, search } from '../src/index.js' t.test('search method', t => { t.test('with term', async t => { @@ -584,7 +584,7 @@ t.test('search method', t => { t.end() }) -async function createSimpleDB(): Promise<[Orama, string, string, string, string]> { +async function createSimpleDB() { let i = 0 const db = await create({ schema: { @@ -641,5 +641,5 @@ async function createSimpleDB(): Promise<[Orama, string, string, string, string] }, }) - return [db, id1, id2, id3, id4] + return [db, id1, id2, id3, id4] as const } diff --git a/packages/orama/tests/serialization.test.ts b/packages/orama/tests/serialization.test.ts index 86b7167df..26e9ff8fb 100644 --- a/packages/orama/tests/serialization.test.ts +++ b/packages/orama/tests/serialization.test.ts @@ -1,14 +1,12 @@ import t from 'tap' -import { getInternalDocumentId } from '../src/components/internal-document-id-store.js' -import type { Document } from '../src/types.js' -import { Node as RadixNode } from '../src/trees/radix.js' -import { create, insert, load, Result, save, search } from '../src/index.js' -import { contains as trieContains } from '../src/trees/radix.js' -import { Index } from '../src/components/index.js' import { DocumentsStore } from '../src/components/documents-store.js' +import { Index } from '../src/components/index.js' +import { getInternalDocumentId } from '../src/components/internal-document-id-store.js' +import { create, insert, load, save, search } from '../src/index.js' +import { Node as RadixNode, contains as trieContains } from '../src/trees/radix.js' -function extractOriginalDoc(result: Result[]): Document[] { - return result.map(({ document }: Result) => document) +function extractOriginalDoc(result) { + return result.map(({ document }) => document) } t.test('Edge getters', t => { diff --git a/packages/orama/tests/sort.test.ts b/packages/orama/tests/sort.test.ts index b04510233..74cadf4a5 100644 --- a/packages/orama/tests/sort.test.ts +++ b/packages/orama/tests/sort.test.ts @@ -1,5 +1,5 @@ import t from 'tap' -import { CustomSorterFunctionItem, create, insert, insertMultiple, load, remove, save, search } from '../src/index.js' +import { create, insert, insertMultiple, load, remove, save, search } from '../src/index.js' t.test('search with sortBy', t => { t.test('on number', async t => { @@ -388,9 +388,6 @@ t.test('search with sortBy', t => { }) t.test('should allow custom function', async t => { - interface Doc { - string?: string - } const db = await create({ schema: { string: 'string', @@ -406,8 +403,8 @@ t.test('search with sortBy', t => { ]) const result = await search(db, { - sortBy: (a: CustomSorterFunctionItem, b: CustomSorterFunctionItem) => { - return ((a[2] as unknown as Doc).string || '').localeCompare((b[2] as unknown as Doc).string || '') + sortBy: (a, b) => { + return (a[2].string || '').localeCompare(b[2].string || '') }, }) diff --git a/packages/orama/tests/update.test.ts b/packages/orama/tests/update.test.ts index 432a17560..3de124ac5 100644 --- a/packages/orama/tests/update.test.ts +++ b/packages/orama/tests/update.test.ts @@ -1,5 +1,5 @@ import t from 'tap' -import { create, insert, getByID, update, updateMultiple, count } from '../src/index.js' +import { count, create, getByID, insert, update, updateMultiple } from '../src/index.js' t.test('update method', t => { t.plan(1)