-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: review typescript signature (#164)
- Loading branch information
Showing
8 changed files
with
181 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { expectType } from 'tsd' | ||
|
||
import { InternalTypedDocument, Orama, PartialSchemaDeep, Results, Schema, SearchParams, TypedDocument } from '@orama/orama' | ||
import Fastify from 'fastify' | ||
import fp from 'fastify-plugin' | ||
|
||
import { PersistenceInFile, PersistenceInMemory, fastifyOrama } from '../..' | ||
|
||
const app = Fastify() | ||
|
||
const mySchema = { | ||
quote: 'string', | ||
author: 'string' | ||
} as const | ||
type MySchema = Schema<typeof mySchema> | ||
|
||
|
||
app.register(fastifyOrama, { | ||
schema: mySchema, | ||
prefix: '/api/orama', | ||
language: 'en' | ||
}) | ||
|
||
app.register(fastifyOrama, { | ||
persistence: new PersistenceInMemory() | ||
}) | ||
|
||
app.register(fastifyOrama, { | ||
persistence: new PersistenceInMemory({ | ||
jsonIndex: 'index.json' | ||
}) | ||
}) | ||
|
||
app.register(fastifyOrama, { | ||
persistence: new PersistenceInFile() | ||
}) | ||
app.register(fastifyOrama, { | ||
persistence: new PersistenceInFile({ | ||
filePath: 'index.json', | ||
format: 'json', | ||
mustExistOnStart: true | ||
}) | ||
}) | ||
|
||
const appWithOrama = app.withOrama<typeof mySchema>() | ||
const id = await appWithOrama.orama.insert({ quote: 'Hello', author: 'World' }) | ||
expectType<string>(id) | ||
|
||
appWithOrama.get('/hello', async () => { | ||
|
||
const {orama} = appWithOrama | ||
const result = await orama.search({ term: 'hello' }) | ||
|
||
expectType<Results<InternalTypedDocument<MySchema>>>(result) | ||
expectType<string>(result.hits[0].document.author) | ||
|
||
return { | ||
hello: result.hits | ||
} | ||
}) | ||
|
||
expectType<{ | ||
insert: (document: PartialSchemaDeep<TypedDocument<Orama<typeof mySchema>>>) => Promise<string>, | ||
search: (params: SearchParams<Orama<Schema<typeof mySchema>>, typeof mySchema>) => Promise<Results<Schema<typeof mySchema>>>, | ||
persist?: () => Promise<any>, | ||
}>(appWithOrama.orama) | ||
|
||
fp(function(fastify) { | ||
const fastifyWithOrama = fastify.withOrama<typeof mySchema>() | ||
|
||
expectType<{ | ||
insert: (document: PartialSchemaDeep<TypedDocument<Orama<typeof mySchema>>>) => Promise<string>, | ||
search: (params: SearchParams<Orama<Schema<typeof mySchema>>, typeof mySchema>) => Promise<Results<Schema<typeof mySchema>>>, | ||
persist?: () => Promise<any>, | ||
}>(fastifyWithOrama.orama) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import Fastify from 'fastify' | ||
|
||
import { PersistenceInMemory, fastifyOrama } from '../..' | ||
|
||
(async function () { | ||
const app = Fastify() | ||
|
||
const mySchema = { | ||
quote: 'string', | ||
author: 'string' | ||
} as const | ||
|
||
await app.register(fastifyOrama, { | ||
schema: mySchema, | ||
persistence: new PersistenceInMemory() | ||
}) | ||
|
||
const appWithOrama = app.withOrama<typeof mySchema>() | ||
const id = await appWithOrama.orama.insert({ quote: 'Hello', author: 'World' }) | ||
|
||
appWithOrama.get('/hello', async () => { | ||
|
||
const {orama} = appWithOrama | ||
const result = await orama.search({ term: 'hello' }) | ||
|
||
return { | ||
hello: result.hits | ||
} | ||
}) | ||
})(); |