-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
130 lines (122 loc) · 3.78 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { MongoClient, MongoClientOptions } from 'mongodb';
interface MongoDbInterception {
/**
* true if you want to create a randomer field `__rdz`
*/
random?: boolean;
fulltext?: string[] | string;
/**
* true if you want to create `fulltext array field` and `randomizer field` in the background after data has been successfully written instead of intercepting the write operation.
*
* This basically listen for data changes in the background.
* @default false
*/
overhead?: boolean;
/**
* set this to true if you want to avoid write failing due to exceeding the 16MB document limit set by mongodb
*
* this silently stop adding searchable items to the array when this limit is reached
*
* only works when `overhead` is set to true
* @default
* false
*/
safeOverhead?: boolean;
}
interface InterceptedCollection {
/**
* @param collectionName the name of the collection you want to intercept
*/
[collectionName: string]: MongoDbInterception;
}
interface InterceptionMap {
/**
* @param dbName the name of the database you want to intercept
*/
[dbName: string]: InterceptedCollection;
}
interface MongoClientHackConfig extends InterceptionOption {
url?: string;
options?: MongoClientOptions;
}
interface InterceptionOption {
/**
* map out the collections you want to intercept
*/
map: InterceptionMap;
/**
* handle text tokenization
* @example
* "ทดสอบระบบตัดคำ" ----> "ทดสอบ ระบบ ตัด คำ"
* "南京市长江大桥" ----> "南京市 长江大桥"
*/
tokenizer?: (text: string) => string | Promise<string>;
}
/**
* initialize MongoClientHack Instance
*
* ```js
* import { MongoClientHack } from "mongodb-middleware-utils";
*
* // using MongoClientHack instance
* const mongoServer = new MongoClientHack({
* map: {
* 'my_database_name': { // name of the database you want to intercept
* 'my_collection_name': { // name of collection you want to intercept
* random: true,
* fulltext: ['name', 'des']
* }
* },
* 'another_database_name': {
* 'another_collection_name': {
* random: true,
* fulltext: ['name', 'des']
* }
* },
* // you can have as many map as needed
* ...otherMapping
* },
* url: 'mongodb://127.0.0.1:27017',
* options: {
* useUnifiedTopology: true,
* ...otherProps
* }
* });
```
*/
export class MongoClientHack extends MongoClient {
constructor(config: MongoClientHackConfig);
}
/**
* intercept MongoClient Instance
*
* ```js
* import { proxyClient } from "mongodb-middleware-utils";
*
* const mongoServer = new MongoClient('mongodb://127.0.0.1:27017');
*
* proxyClient({
* 'my_database_name': { // name of the database you want to intercept
* 'my_collection_name': { // name of collection you want to intercept
* random: true,
* fulltext: ['name', 'des']
* }
* },
* 'another_database_name': {
* 'another_collection_name': {
* random: true,
* fulltext: ['name', 'des']
* }
* },
* // you can have as many map as needed
* ...otherMapping
* })(mongoServer);
*
* // connect
* mongoServer.connect();
* ```
*/
export function proxyClient(config: InterceptionOption): (client: MongoClient) => void;
export function getFulltextArray(text: string): Promise<string[]>;
export const FULLTEXT_ARRAY_PREFIX: '__fta_';
export const RANDOMIZER_FIELD: '__rdz';