Skip to content

Commit

Permalink
fix: typescript interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Eomm committed Sep 30, 2023
1 parent 60c018d commit 14ffaba
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 24 deletions.
33 changes: 17 additions & 16 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
import type { FastifyPluginCallback } from 'fastify'
import type { Document, Orama, ProvidedTypes, Results, SearchParams, create } from '@orama/orama'
import type { TypedDocument, insert, Orama, Results, SearchParams, create, AnyOrama, PartialSchemaDeep, Schema } from '@orama/orama'

interface OramaPersistence {
restore: () => Promise<ReturnType<typeof create> | null>
persist: (data: ReturnType<typeof create>) => Promise<any>
interface FastifyOramaPersistence<T = any, O = any> {
restore: () => Promise<Orama<T> | null>
persist: (data: Orama<T>) => Promise<O>
}

declare class PersistenceInMemory implements OramaPersistence {

declare class PersistenceInMemory<T = any, O = string | Buffer> implements FastifyOramaPersistence<T, O> {
constructor(options?: {
jsonIndex?: string,
})
restore: () => Promise<Promise<Orama<ProvidedTypes>> | null>
persist: (data: Promise<Orama<ProvidedTypes>>) => Promise<string>
restore: () => Promise<Orama<T> | null>
persist: (data: Orama<T>) => Promise<O>
}

declare class PersistenceInFile implements OramaPersistence {
declare class PersistenceInFile<T = any, O = string> implements FastifyOramaPersistence<T, O> {
constructor(options?: {
filePath?: string,
format?: string,
mustExistOnStart?: boolean
})
restore: () => Promise<Promise<Orama<ProvidedTypes>> | null>
persist: (data: Promise<Orama<ProvidedTypes>>) => Promise<string>
restore: () => Promise<Orama<T> | null>
persist: (data: Orama<T>) => Promise<O>
}

type OramaPluginOptions = {
persistence?: OramaPersistence
type FastifyOramaPluginOptions = {
persistence?: FastifyOramaPersistence
} & Partial<Parameters<typeof create>[0]>

declare const fastifyOrama: FastifyPluginCallback<OramaPluginOptions>
declare const fastifyOrama: FastifyPluginCallback<FastifyOramaPluginOptions>

declare module 'fastify' {
interface FastifyInstance {
orama: {
insert: (document: Document) => Promise<string>,
search: (params: SearchParams) => Promise<Results>,
getOrama<T>(): {
insert: (document: PartialSchemaDeep<TypedDocument<Orama<T>>>) => Promise<string>,
search: (params: SearchParams<Orama<Schema<T>>, T>) => Promise<Results<Schema<T>>>,
persist?: () => Promise<any>,
}
}
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ async function fastifyOrama (fastify, options) {
}

fastify.decorate('orama', oramaApi)
fastify.decorate('getOrama', function () {
return oramaApi
})
}

module.exports = fp(fastifyOrama, {
Expand Down
25 changes: 17 additions & 8 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { expectType } from 'tsd'

import Fastify from 'fastify'
import { Result } from '@orama/orama'
import { TypedDocument, Orama, Results, Schema, InternalTypedDocument } from '@orama/orama'

import { fastifyOrama, PersistenceInMemory, PersistenceInFile } from '.'

const app = Fastify()

const mySchema = {
quote: 'string',
author: 'string'
} as const
type MySchema = Schema<typeof mySchema>


app.register(fastifyOrama, {
schema: {
quote: 'string',
author: 'string'
},
schema: mySchema,
prefix: '/api/orama',
language: 'en'
})
Expand All @@ -37,12 +41,17 @@ app.register(fastifyOrama, {
})
})

app.orama.insert({ quote: 'Hello', author: 'World' })
const orama = app.getOrama<typeof mySchema>()
const id = await orama.insert({ quote: 'Hello', author: 'World' })
expectType<string>(id)

app.get('/hello', async () => {
const result = await app.orama.search({ term: 'hello' })

expectType<Result[]>(result.hits)
const orama = app.getOrama<typeof mySchema>()
const result = await orama.search({ term: 'hello' })

expectType<Results<InternalTypedDocument<MySchema>>>(result)
expectType<string>(result.hits[0].document.author)

return {
hello: result.hits
Expand Down
13 changes: 13 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,16 @@ it('Should throw when trying to register multiple instances without giving a nam
strictEqual(error.message, 'fastify-orama is already registered')
}
})

it('Expose a getOrama function', async () => {
const fastify = Fastify()

await fastify.register(fastifyOrama, {
schema: {
quote: 'string',
author: 'string'
}
})

strictEqual(fastify.orama, fastify.getOrama())
})

0 comments on commit 14ffaba

Please sign in to comment.