Skip to content

Commit f759bf8

Browse files
committed
chore: move responses
1 parent eafde1d commit f759bf8

File tree

4 files changed

+39
-26
lines changed

4 files changed

+39
-26
lines changed

src/cmap/wire_protocol/responses.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -391,15 +391,3 @@ export class ClientBulkWriteCursorResponse extends CursorResponse {
391391
return this.get('writeConcernError', BSONType.object, false);
392392
}
393393
}
394-
395-
export class ProfilingLevelResponse extends MongoDBResponse {
396-
get was() {
397-
return this.get('was', BSONType.int, true);
398-
}
399-
}
400-
401-
export class DistinctResponse extends MongoDBResponse {
402-
get values() {
403-
return this.get('values', BSONType.array, true);
404-
}
405-
}

src/collection.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -853,27 +853,31 @@ export class Collection<TSchema extends Document = Document> {
853853
*/
854854
distinct<Key extends keyof WithId<TSchema>>(
855855
key: Key
856-
): Promise<Array<Flatten<WithId<TSchema>[Key]>>>;
856+
): Promise<Array<Flatten<WithId<TSchema>[Key]>> | Document>;
857857
distinct<Key extends keyof WithId<TSchema>>(
858858
key: Key,
859859
filter: Filter<TSchema>
860-
): Promise<Array<Flatten<WithId<TSchema>[Key]>>>;
860+
): Promise<Array<Flatten<WithId<TSchema>[Key]>> | Document>;
861861
distinct<Key extends keyof WithId<TSchema>>(
862862
key: Key,
863863
filter: Filter<TSchema>,
864864
options: DistinctOptions
865-
): Promise<Array<Flatten<WithId<TSchema>[Key]>>>;
865+
): Promise<Array<Flatten<WithId<TSchema>[Key]>> | Document>;
866866

867867
// Embedded documents overload
868-
distinct(key: string): Promise<any[]>;
869-
distinct(key: string, filter: Filter<TSchema>): Promise<any[]>;
870-
distinct(key: string, filter: Filter<TSchema>, options: DistinctOptions): Promise<any[]>;
868+
distinct(key: string): Promise<any[] | Document>;
869+
distinct(key: string, filter: Filter<TSchema>): Promise<any[] | Document>;
870+
distinct(
871+
key: string,
872+
filter: Filter<TSchema>,
873+
options: DistinctOptions
874+
): Promise<any[] | Document>;
871875

872876
async distinct<Key extends keyof WithId<TSchema>>(
873877
key: Key,
874878
filter: Filter<TSchema> = {},
875879
options: DistinctOptions = {}
876-
): Promise<any[]> {
880+
): Promise<any[] | Document> {
877881
return await executeOperation(
878882
this.client,
879883
new DistinctOperation(

src/operations/distinct.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { Document } from '../bson';
1+
import { BSONType, type Document, parseUtf8ValidationOption } from '../bson';
22
import { type Connection } from '../cmap/connection';
3-
import { DistinctResponse } from '../cmap/wire_protocol/responses';
3+
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
44
import type { Collection } from '../collection';
55
import { type CommandOperationOptions, ModernizedCommandOperation } from './command';
66
import { Aspect, defineAspects } from './operation';
@@ -21,11 +21,17 @@ export type DistinctOptions = CommandOperationOptions & {
2121
hint?: Document | string;
2222
};
2323

24+
class DistinctResponse extends MongoDBResponse {
25+
get values() {
26+
return this.get('values', BSONType.array, true);
27+
}
28+
}
29+
2430
/**
2531
* Return a list of distinct values for the given key across a collection.
2632
* @internal
2733
*/
28-
export class DistinctOperation extends ModernizedCommandOperation<any[]> {
34+
export class DistinctOperation extends ModernizedCommandOperation<any[] | Document> {
2935
override SERVER_COMMAND_RESPONSE_TYPE = DistinctResponse;
3036
override options: DistinctOptions;
3137
collection: Collection;
@@ -63,8 +69,17 @@ export class DistinctOperation extends ModernizedCommandOperation<any[]> {
6369
};
6470
}
6571

66-
override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): any[] {
67-
this.explain ? response.toObject() : response.values;
72+
override handleOk(
73+
response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
74+
): any[] | Document {
75+
if (this.explain) {
76+
return response.toObject(this.bsonOptions);
77+
}
78+
const values = response.values.toObject({
79+
...this.bsonOptions,
80+
validation: parseUtf8ValidationOption(this.bsonOptions)
81+
});
82+
return Object.entries(values).map(([_key, val]) => val);
6883
}
6984
}
7085

src/operations/profiling_level.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
import { type Document } from '../bson';
1+
import { BSONType, type Document } from '../bson';
22
import { type Connection } from '../cmap/connection';
3-
import { ProfilingLevelResponse } from '../cmap/wire_protocol/responses';
3+
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
44
import type { Db } from '../db';
55
import { MongoUnexpectedServerResponseError } from '../error';
66
import { type CommandOperationOptions, ModernizedCommandOperation } from './command';
77

88
/** @public */
99
export type ProfilingLevelOptions = CommandOperationOptions;
1010

11+
class ProfilingLevelResponse extends MongoDBResponse {
12+
get was() {
13+
return this.get('was', BSONType.int, true);
14+
}
15+
}
16+
1117
/** @internal */
1218
export class ProfilingLevelOperation extends ModernizedCommandOperation<string> {
1319
override SERVER_COMMAND_RESPONSE_TYPE = ProfilingLevelResponse;

0 commit comments

Comments
 (0)