@@ -6,15 +6,18 @@ import * as ERRORS from "./errors";
6
6
import { tokenize } from "./tokenizer" ;
7
7
import { formatNanoseconds , getNanosecondsTime } from "./utils" ;
8
8
import { Language , SUPPORTED_LANGUAGES } from "./stemmer" ;
9
+ import type { ResolveSchema } from "./types" ;
9
10
10
11
export type PropertyType = "string" | "number" | "boolean" ;
11
12
12
13
export type PropertiesSchema = {
13
14
[ key : string ] : PropertyType | PropertiesSchema ;
14
15
} ;
15
16
16
- export type LyraProperties = {
17
- schema : PropertiesSchema ;
17
+ export type LyraProperties <
18
+ TSchema extends PropertiesSchema = PropertiesSchema
19
+ > = {
20
+ schema : TSchema ;
18
21
defaultLanguage ?: Language ;
19
22
} ;
20
23
@@ -43,18 +46,19 @@ type SearchResult = Promise<{
43
46
elapsed : string ;
44
47
} > ;
45
48
46
- export class Lyra {
49
+ export class Lyra < TSchema extends PropertiesSchema = PropertiesSchema > {
47
50
private defaultLanguage : Language = "english" ;
48
- private schema : PropertiesSchema ;
51
+ private schema : TSchema ;
49
52
private docs : LyraDocs = new Map ( ) ;
50
53
private index : LyraIndex = new Map ( ) ;
54
+
51
55
private queue : queueAsPromised < QueueDocParams > = fastq . promise (
52
56
this ,
53
57
this . _insert ,
54
58
1
55
59
) ;
56
60
57
- constructor ( properties : LyraProperties ) {
61
+ constructor ( properties : LyraProperties < TSchema > ) {
58
62
const defaultLanguage =
59
63
( properties ?. defaultLanguage ?. toLowerCase ( ) as Language ) ?? "english" ;
60
64
@@ -67,8 +71,8 @@ export class Lyra {
67
71
this . buildIndex ( properties . schema ) ;
68
72
}
69
73
70
- private buildIndex ( schema : PropertiesSchema , prefix = "" ) {
71
- for ( const prop in schema ) {
74
+ private buildIndex ( schema : TSchema , prefix = "" ) {
75
+ for ( const prop of Object . keys ( schema ) ) {
72
76
const propType = typeof prop ;
73
77
const isNested = typeof schema [ prop ] === "object" ;
74
78
@@ -77,7 +81,7 @@ export class Lyra {
77
81
const propName = `${ prefix } ${ prop } ` ;
78
82
79
83
if ( isNested ) {
80
- this . buildIndex ( schema [ prop ] as PropertiesSchema , `${ propName } .` ) ;
84
+ this . buildIndex ( schema [ prop ] as TSchema , `${ propName } .` ) ;
81
85
} else {
82
86
this . index . set ( propName , new Trie ( ) ) ;
83
87
}
@@ -207,7 +211,7 @@ export class Lyra {
207
211
}
208
212
209
213
public async insert (
210
- doc : object ,
214
+ doc : ResolveSchema < TSchema > ,
211
215
language : Language = this . defaultLanguage
212
216
) : Promise < { id : string } > {
213
217
const id = nanoid ( ) ;
0 commit comments