Skip to content

Commit 02c9c62

Browse files
committed
wip: exclude 'vectorizeCollectionName' from arguments
1 parent b67016e commit 02c9c62

File tree

10 files changed

+136
-158
lines changed

10 files changed

+136
-158
lines changed

src/collections/configure/index.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import {
2+
ConfigureNonTextVectorizerOptions,
3+
ConfigureTextVectorizerOptions,
24
InvertedIndexConfigCreate,
35
InvertedIndexConfigUpdate,
46
MultiTenancyConfigCreate,
@@ -9,6 +11,8 @@ import {
911
ShardingConfigCreate,
1012
VectorConfigUpdate,
1113
VectorIndexType,
14+
Vectorizer,
15+
VectorizerConfigCreateType,
1216
VectorizerUpdateOptions,
1317
} from '../types/index.js';
1418

@@ -18,6 +22,7 @@ import { configure as configureVectorIndex, reconfigure as reconfigureVectorInde
1822
import { multiVectors, vectors } from './vectorizer.js';
1923

2024
import { parseWithDefault } from './parsing.js';
25+
import { RemoveConfiguration } from './types/util.js';
2126

2227
const dataType = {
2328
INT: 'int' as const,
@@ -64,7 +69,7 @@ const configure = {
6469
* @deprecated Use `vectors` instead.
6570
*/
6671
vectorizer: vectors,
67-
vectors,
72+
vectors: vectors as RemoveConfiguration<typeof vectors, 'vectorizeCollectionName'>,
6873
vectorIndex: configureVectorIndex,
6974
dataType,
7075
tokenization,
@@ -99,9 +104,9 @@ const configure = {
99104
bm25:
100105
options.bm25b || options.bm25k1
101106
? {
102-
b: options.bm25b,
103-
k1: options.bm25k1,
104-
}
107+
b: options.bm25b,
108+
k1: options.bm25k1,
109+
}
105110
: undefined,
106111
cleanupIntervalSeconds: options.cleanupIntervalSeconds,
107112
indexTimestamps: options.indexTimestamps,
@@ -110,10 +115,10 @@ const configure = {
110115
stopwords:
111116
options.stopwordsAdditions || options.stopwordsRemovals || options.stopwordsPreset
112117
? {
113-
preset: options.stopwordsPreset,
114-
additions: options.stopwordsAdditions,
115-
removals: options.stopwordsRemovals,
116-
}
118+
preset: options.stopwordsPreset,
119+
additions: options.stopwordsAdditions,
120+
removals: options.stopwordsRemovals,
121+
}
117122
: undefined,
118123
};
119124
},
@@ -131,10 +136,10 @@ const configure = {
131136
}): MultiTenancyConfigCreate => {
132137
return options
133138
? {
134-
autoTenantActivation: parseWithDefault(options.autoTenantActivation, false),
135-
autoTenantCreation: parseWithDefault(options.autoTenantCreation, false),
136-
enabled: parseWithDefault(options.enabled, true),
137-
}
139+
autoTenantActivation: parseWithDefault(options.autoTenantActivation, false),
140+
autoTenantCreation: parseWithDefault(options.autoTenantCreation, false),
141+
enabled: parseWithDefault(options.enabled, true),
142+
}
138143
: { autoTenantActivation: false, autoTenantCreation: false, enabled: true };
139144
},
140145
/**
@@ -209,18 +214,18 @@ const reconfigure = {
209214
bm25:
210215
options.bm25b || options.bm25k1
211216
? {
212-
b: options.bm25b,
213-
k1: options.bm25k1,
214-
}
217+
b: options.bm25b,
218+
k1: options.bm25k1,
219+
}
215220
: undefined,
216221
cleanupIntervalSeconds: options.cleanupIntervalSeconds,
217222
stopwords:
218223
options.stopwordsAdditions || options.stopwordsRemovals || options.stopwordsPreset
219224
? {
220-
preset: options.stopwordsPreset,
221-
additions: options.stopwordsAdditions,
222-
removals: options.stopwordsRemovals,
223-
}
225+
preset: options.stopwordsPreset,
226+
additions: options.stopwordsAdditions,
227+
removals: options.stopwordsRemovals,
228+
}
224229
: undefined,
225230
};
226231
},
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { VectorizerConfigCreateType } from "./vectorizer.js";
2+
3+
/** KeyofNonEmpty removes `{}` from a union type and returns keys of all constituent types. */
4+
type KeyofNonEmpty<T> = T extends object ? (keyof T extends never ? never : keyof T) : never;
5+
6+
7+
/**
8+
* ParameterUnion extracts the type of the first argument for every function in object `T`
9+
* then combines them in a union.
10+
*/
11+
type ParameterUnion<T> = {
12+
[K in keyof T]: T[K] extends (...args: any[]) => any ? Parameters<T[K]>[0] : never;
13+
}[keyof T];
14+
15+
16+
/**
17+
* RemoveConfiguration finds every function in object `T` and removes keys K from the type of it's first argument.
18+
*
19+
* This util assumes all of functions in `T` are either `{ opts: {...} }` or `{ opts?: {...} | undefined }` and is
20+
* very brittle as a result. In fact, its only purpose is to allow reusing all the code underpinning the now-deprecated
21+
* `configure.vectorizers` in `configure.vectors` while removing `vectorizeCollectionName` parameter from the latter.
22+
*
23+
* Usage (see: src/collections/configure/types/util.ts):
24+
* vectors: vectors as RemoveConfiguration<typeof vectors, 'vectorizeCollectionName'>
25+
*/
26+
export type RemoveConfiguration<T, K extends KeyofNonEmpty<ParameterUnion<T>>> =
27+
T extends object
28+
? {
29+
[F in keyof T]:
30+
31+
// Handle functions with optional `opts?` argument.
32+
T[F] extends (opts?: infer Arg) => infer Ret
33+
// ? (opts?: Arg) => Ret
34+
? (opts?: Omit<Arg, K>) => Ret
35+
36+
// Handle functions with required `opts` argument.
37+
: T[F] extends (opts: infer Arg) => infer Ret
38+
// ? (opts: Arg) => Ret
39+
? (opts: Omit<Arg, K>) => Ret
40+
41+
: T[F];
42+
} : never;

src/collections/configure/types/vectorizer.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,17 @@ export type VectorConfigUpdate<N extends string | undefined, I extends VectorInd
6464

6565
export type VectorizersConfigCreate<T, V> = V extends undefined
6666
?
67-
| VectorConfigCreate<PrimitiveKeys<T>, string | undefined, VectorIndexType, Vectorizer>
68-
| VectorConfigCreate<PrimitiveKeys<T>, string, VectorIndexType, Vectorizer>[]
67+
| VectorConfigCreate<PrimitiveKeys<T>, string | undefined, VectorIndexType, Vectorizer>
68+
| VectorConfigCreate<PrimitiveKeys<T>, string, VectorIndexType, Vectorizer>[]
6969
:
70-
| VectorConfigCreate<PrimitiveKeys<T>, (keyof V & string) | undefined, VectorIndexType, Vectorizer>
71-
| VectorConfigCreate<PrimitiveKeys<T>, keyof V & string, VectorIndexType, Vectorizer>[];
70+
| VectorConfigCreate<PrimitiveKeys<T>, (keyof V & string) | undefined, VectorIndexType, Vectorizer>
71+
| VectorConfigCreate<PrimitiveKeys<T>, keyof V & string, VectorIndexType, Vectorizer>[];
7272

7373
export type VectorizersConfigAdd<T> =
7474
| VectorConfigCreate<PrimitiveKeys<T>, string, VectorIndexType, Vectorizer>
7575
| VectorConfigCreate<PrimitiveKeys<T>, string, VectorIndexType, Vectorizer>[];
7676

77+
// TODO: remove vectorizeCollectionName config
7778
export type ConfigureNonTextVectorizerOptions<
7879
N extends string | undefined,
7980
I extends VectorIndexType,

0 commit comments

Comments
 (0)