-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcreate.ts
More file actions
57 lines (48 loc) · 1.94 KB
/
create.ts
File metadata and controls
57 lines (48 loc) · 1.94 KB
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
import { type Document } from '../../bson';
import { type Connection } from '../../cmap/connection';
import { CreateSearchIndexesResponse } from '../../cmap/wire_protocol/responses';
import type { Collection } from '../../collection';
import type { ServerCommandOptions } from '../../sdam/server';
import type { ClientSession } from '../../sessions';
import { type TimeoutContext } from '../../timeout';
import { ModernizedOperation } from '../operation';
/**
* @public
*/
export interface SearchIndexDescription extends Document {
/** The name of the index. */
name?: string;
/** The index definition. */
definition: Document;
/** The type of the index. Currently `search` or `vectorSearch` are supported. */
type?: string;
}
/** @internal */
export class CreateSearchIndexesOperation extends ModernizedOperation<string[]> {
override SERVER_COMMAND_RESPONSE_TYPE = CreateSearchIndexesResponse;
private readonly collection: Collection;
private readonly descriptions: ReadonlyArray<SearchIndexDescription>;
constructor(collection: Collection, descriptions: ReadonlyArray<SearchIndexDescription>) {
super();
this.collection = collection;
this.descriptions = descriptions;
this.ns = collection.fullNamespace;
}
override get commandName() {
return 'createSearchIndexes' as const;
}
override buildCommand(_connection: Connection, _session?: ClientSession): Document {
const namespace = this.collection.fullNamespace;
return {
createSearchIndexes: namespace.collection,
indexes: this.descriptions
};
}
override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): string[] {
const indexesCreated = response.indexesCreated?.toObject();
return indexesCreated ? Object.entries(indexesCreated).map(([_key, val]) => val.name) : [];
}
override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions {
return { session: this.session, timeoutContext };
}
}