Skip to content

Commit

Permalink
Merge branch 'main' into feat/typing
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Aug 27, 2023
2 parents 1f3e69b + fc6e863 commit a345414
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 35 deletions.
2 changes: 1 addition & 1 deletion packages/orama/src/components/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const ALLOWED_TYPES = ['string', 'number', 'boolean']
export async function getGroups<T extends AnyOrama, ResultDocument = TypedDocument<T>>(
orama: T,
results: TokenScore[],
groupBy: GroupByParams<ResultDocument>,
groupBy: GroupByParams<T, ResultDocument>,
): Promise<GroupResult<ResultDocument>> {
const properties = groupBy.properties
const propertiesLength = properties.length
Expand Down
17 changes: 9 additions & 8 deletions packages/orama/src/methods/create.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -84,11 +85,11 @@ function validateComponents<OramaSchema, TIndex, TDocumentStore, TSorter, TOrama
}

export async function create<
const OramaSchema,
TIndex,
TDocumentStore,
TSorter,
ResultOrama extends Orama<OramaSchema, TIndex, TDocumentStore, TSorter>> ({
const OramaSchema extends AnySchema,
TIndex = Index,
TDocumentStore = DocumentsStore,
TSorter = Sorter,
ResultOrama extends Orama<OramaSchema, TIndex, TDocumentStore, TSorter> = Orama<OramaSchema, TIndex, TDocumentStore, TSorter>> ({
schema,
sort,
language,
Expand Down
5 changes: 4 additions & 1 deletion packages/orama/src/methods/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import { createError } from '../errors.js'
import type {
AnyOrama,
BM25Params,
ComparisonOperator,
CustomSorterFunctionItem,
ElapsedTime,
IndexMap,
Result,
Results,
SchemaValidKeys,
SearchContext,
SearchParams,
SearchableValue,
Expand Down Expand Up @@ -158,7 +160,8 @@ export async function search<T extends AnyOrama, ResultDocument = TypedDocument<
let whereFiltersIDs: InternalDocumentID[] = []

if (hasFilters) {
whereFiltersIDs = await orama.index.searchByWhereClause(context, index, params.where!)
const whereParams = params.where as unknown as Record<SchemaValidKeys<T>, boolean | string | string[] | ComparisonOperator>;
whereFiltersIDs = await orama.index.searchByWhereClause(context, index, whereParams)
}

const tokensLength = tokens.length
Expand Down
4 changes: 4 additions & 0 deletions packages/orama/src/methods/serialization.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
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<T extends AnyOrama>(orama: T, raw: RawData): Promise<void> {
orama.internalDocumentIDStore.load(orama, raw.internalDocumentIDStore)
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<T extends AnyOrama>(orama: T): Promise<RawData> {
Expand All @@ -20,5 +23,6 @@ export async function save<T extends AnyOrama>(orama: T): Promise<RawData> {
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,
}
}
22 changes: 15 additions & 7 deletions packages/orama/src/types.ts
Original file line number Diff line number Diff line change
@@ -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> = T | null
Expand Down Expand Up @@ -113,10 +116,15 @@ export type Reduce<T, R = AnyDocument> = {
getInitialValue: (elementCount: number) => T
}

export type GroupByParams<T> = {
properties: string[]
// thanks to https://github.com/sindresorhus/type-fest/blob/main/source/literal-union.d.ts
export type LiteralUnion<LiteralType, BaseType> = LiteralType | (BaseType & Record<never, never>)

export type SchemaValidKeys<T extends AnyOrama> = keyof T['schema'] extends string ? LiteralUnion<keyof T['schema'], string> : string;

export type GroupByParams<T extends AnyOrama, ResultDocument> = {
properties: SchemaValidKeys<T>[]
maxResult?: number
reduce?: Reduce<T>
reduce?: Reduce<ResultDocument>
}

export type ComparisonOperator = {
Expand Down Expand Up @@ -144,7 +152,7 @@ export type SorterParams<T extends AnyOrama> = {
/**
* The key of the document used to sort the result.
*/
property: keyof T['schema'] extends string ? keyof T['schema'] : string;
property: SchemaValidKeys<T>;
/**
* Whether to sort the result in ascending or descending order.
*/
Expand Down Expand Up @@ -269,7 +277,7 @@ export type SearchParams<T extends AnyOrama, ResultDocument = TypedDocument<T>>
* }
* })
*/
groupBy?: GroupByParams<ResultDocument>
groupBy?: GroupByParams<T, ResultDocument>

/**
* Filter the search results.
Expand All @@ -289,7 +297,7 @@ export type SearchParams<T extends AnyOrama, ResultDocument = TypedDocument<T>>
* }
* });
*/
where?: Record<string, boolean | string | string[] | ComparisonOperator>
where?: Partial<Record<SchemaValidKeys<T>, boolean | string | string[] | ComparisonOperator>>

/**
* Threshold to use for refining the search results.
Expand Down Expand Up @@ -805,7 +813,7 @@ export type PickInferGeneric<T, Default> = T extends AnyGeneric<infer Generic>
: never
: never

export type Orama<TSchema, TIndex, TDocumentStore, TSorter> = FunctionComponents<TSchema> &
export type Orama<TSchema, TIndex = Index, TDocumentStore = DocumentsStore, TSorter = Sorter> = FunctionComponents<TSchema> &
Internals<TSchema, AnyGenericIndex<TIndex>, AnyGenericDocumentStore<TDocumentStore>, AnyGenericSorter<TSorter>> &
ArrayCallbackComponents<any> &
OramaID
Expand Down
6 changes: 3 additions & 3 deletions packages/orama/tests/search.test.ts
Original file line number Diff line number Diff line change
@@ -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 => {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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
}
14 changes: 6 additions & 8 deletions packages/orama/tests/serialization.test.ts
Original file line number Diff line number Diff line change
@@ -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 => {
Expand Down
9 changes: 3 additions & 6 deletions packages/orama/tests/sort.test.ts
Original file line number Diff line number Diff line change
@@ -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 => {
Expand Down Expand Up @@ -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',
Expand All @@ -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 || '')
},
})

Expand Down
2 changes: 1 addition & 1 deletion packages/orama/tests/update.test.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit a345414

Please sign in to comment.