|
1 | | -import { expect, test } from 'tstyche' |
| 1 | +import { describe, expect, test } from 'tstyche' |
2 | 2 |
|
3 | | -import { InternalTypedDocument, Orama, PartialSchemaDeep, Results, Schema, SearchParams, TypedDocument } from '@orama/orama' |
| 3 | +import type { InternalTypedDocument, Orama, PartialSchemaDeep, Results, Schema, SearchParams, TypedDocument } from '@orama/orama' |
4 | 4 | import Fastify from 'fastify' |
5 | 5 | import fp from 'fastify-plugin' |
6 | 6 |
|
7 | | -import { PersistenceInFile, PersistenceInMemory, fastifyOrama } from '../../index.js' |
8 | | - |
9 | | -const app = Fastify() |
| 7 | +import { OramaApi, PersistenceInFile, PersistenceInMemory, fastifyOrama } from '../../index.js' |
10 | 8 |
|
11 | 9 | const mySchema = { |
12 | 10 | quote: 'string', |
13 | 11 | author: 'string' |
14 | 12 | } as const |
| 13 | + |
15 | 14 | type MySchema = Schema<typeof mySchema> |
16 | 15 |
|
| 16 | +function baseSetup() { |
| 17 | + const app = Fastify() |
17 | 18 |
|
18 | | -app.register(fastifyOrama, { |
19 | | - schema: mySchema, |
20 | | - prefix: '/api/orama', |
21 | | - language: 'en' |
22 | | -}) |
| 19 | + app.register(fastifyOrama, { |
| 20 | + schema: mySchema, |
| 21 | + persistence: new PersistenceInMemory() |
| 22 | + }) |
23 | 23 |
|
24 | | -app.register(fastifyOrama, { |
25 | | - persistence: new PersistenceInMemory() |
26 | | -}) |
| 24 | + return app.withOrama<typeof mySchema>() |
| 25 | +} |
27 | 26 |
|
28 | | -app.register(fastifyOrama, { |
29 | | - persistence: new PersistenceInMemory({ |
30 | | - jsonIndex: 'index.json' |
31 | | - }) |
32 | | -}) |
| 27 | +function setupWithMorePersistenceOptions() { |
| 28 | + const app = baseSetup() |
33 | 29 |
|
34 | | -app.register(fastifyOrama, { |
35 | | - persistence: new PersistenceInFile() |
36 | | -}) |
37 | | -app.register(fastifyOrama, { |
38 | | - persistence: new PersistenceInFile({ |
39 | | - filePath: 'index.json', |
40 | | - format: 'json', |
41 | | - mustExistOnStart: true |
| 30 | + app.register(fastifyOrama, { |
| 31 | + schema: mySchema, |
| 32 | + prefix: '/api/orama', |
| 33 | + language: 'en' |
42 | 34 | }) |
43 | | -}) |
44 | 35 |
|
45 | | -const appWithOrama = app.withOrama<typeof mySchema>() |
| 36 | + app.register(fastifyOrama, { |
| 37 | + persistence: new PersistenceInMemory({ |
| 38 | + jsonIndex: 'index.json' |
| 39 | + }) |
| 40 | + }) |
46 | 41 |
|
47 | | -test('should enable the insertion of documents', () => { |
48 | | - appWithOrama.orama.insert({ quote: 'Hello', author: 'World' }).then(id => { |
49 | | - expect(id).type.toBe<string>() |
| 42 | + app.register(fastifyOrama, { |
| 43 | + persistence: new PersistenceInFile() |
| 44 | + }) |
| 45 | + app.register(fastifyOrama, { |
| 46 | + persistence: new PersistenceInFile({ |
| 47 | + filePath: 'index.json', |
| 48 | + format: 'json', |
| 49 | + mustExistOnStart: true |
| 50 | + }) |
50 | 51 | }) |
51 | | -}) |
52 | 52 |
|
53 | | -test('should enable the searching of documents', () => { |
54 | | - appWithOrama.get('/hello', async () => { |
| 53 | + return app.withOrama<typeof mySchema>() |
| 54 | +} |
55 | 55 |
|
56 | | - const {orama} = appWithOrama |
57 | | - const result = await orama.search({ term: 'hello' }) |
| 56 | +describe('Fastify Orama', () => { |
58 | 57 |
|
59 | | - expect(result).type.toBe<Results<InternalTypedDocument<MySchema>>>() |
60 | | - expect(result.hits[0].document.author).type.toBe<string>() |
| 58 | + test('should expose the Orama instance', async () => { |
| 59 | + await (async function () { |
| 60 | + const appWithOrama = baseSetup() |
| 61 | + const id = await appWithOrama.orama.insert({ quote: 'Hello', author: 'World' }) |
| 62 | + expect(id).type.toBe<string>() |
61 | 63 |
|
62 | | - return { |
63 | | - hello: result.hits |
64 | | - } |
| 64 | + appWithOrama.get('/hello', async () => { |
| 65 | + |
| 66 | + const {orama} = appWithOrama |
| 67 | + expect(orama).type.toBe<OramaApi<typeof mySchema>>() |
| 68 | + const result = await orama.search({ term: 'hello' }) |
| 69 | + |
| 70 | + return { |
| 71 | + hello: result.hits |
| 72 | + } |
| 73 | + }) |
| 74 | + })() |
| 75 | + }); |
| 76 | + |
| 77 | + test('should enable the insertion of documents', () => { |
| 78 | + const appWithOrama = setupWithMorePersistenceOptions() |
| 79 | + appWithOrama.orama.insert({ quote: 'Hello', author: 'World' }).then(id => { |
| 80 | + expect(id).type.toBe<string>() |
| 81 | + }) |
65 | 82 | }) |
66 | | -}) |
67 | 83 |
|
68 | | -test('should expose the Orama API', () => { |
69 | | - expect(appWithOrama.orama).type.toBe<{ |
70 | | - insert: (document: PartialSchemaDeep<TypedDocument<Orama<typeof mySchema>>>) => Promise<string>, |
71 | | - search: (params: SearchParams<Orama<Schema<typeof mySchema>>, typeof mySchema>) => Promise<Results<Schema<typeof mySchema>>>, |
72 | | - persist?: () => Promise<any>, |
73 | | - }>() |
74 | | -}) |
| 84 | + test('should enable the searching of documents', () => { |
| 85 | + const appWithOrama = setupWithMorePersistenceOptions() |
| 86 | + appWithOrama.get('/hello', async () => { |
75 | 87 |
|
| 88 | + const {orama} = appWithOrama |
| 89 | + const result = await orama.search({ term: 'hello' }) |
76 | 90 |
|
77 | | -test('should enable the withOrama method', () => { |
78 | | - fp(function(fastify) { |
79 | | - const fastifyWithOrama = fastify.withOrama<typeof mySchema>() |
| 91 | + expect(result).type.toBe<Results<InternalTypedDocument<MySchema>>>() |
| 92 | + expect(result.hits[0].document.author).type.toBe<string>() |
80 | 93 |
|
81 | | - expect(fastifyWithOrama.orama).type.toBe<{ |
| 94 | + return { |
| 95 | + hello: result.hits |
| 96 | + } |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + test('should expose the Orama API', () => { |
| 101 | + const appWithOrama = setupWithMorePersistenceOptions() |
| 102 | + expect(appWithOrama.orama).type.toBe<{ |
82 | 103 | insert: (document: PartialSchemaDeep<TypedDocument<Orama<typeof mySchema>>>) => Promise<string>, |
83 | 104 | search: (params: SearchParams<Orama<Schema<typeof mySchema>>, typeof mySchema>) => Promise<Results<Schema<typeof mySchema>>>, |
84 | 105 | persist?: () => Promise<any>, |
85 | 106 | }>() |
86 | 107 | }) |
| 108 | + |
| 109 | + |
| 110 | + test('should enable the withOrama method', () => { |
| 111 | + fp(function(fastify) { |
| 112 | + const fastifyWithOrama = fastify.withOrama<typeof mySchema>() |
| 113 | + |
| 114 | + expect(fastifyWithOrama.orama).type.toBe<{ |
| 115 | + insert: (document: PartialSchemaDeep<TypedDocument<Orama<typeof mySchema>>>) => Promise<string>, |
| 116 | + search: (params: SearchParams<Orama<Schema<typeof mySchema>>, typeof mySchema>) => Promise<Results<Schema<typeof mySchema>>>, |
| 117 | + persist?: () => Promise<any>, |
| 118 | + }>() |
| 119 | + }) |
| 120 | + }) |
87 | 121 | }) |
0 commit comments