Skip to content

Commit 26c5f68

Browse files
stronger TS support at operatoins layer
1 parent b42d7e7 commit 26c5f68

File tree

4 files changed

+56
-65
lines changed

4 files changed

+56
-65
lines changed

src/collection.ts

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,7 @@ export class Collection<TSchema extends Document = Document> {
402402
): Promise<UpdateResult<TSchema>> {
403403
return await executeOperation(
404404
this.client,
405-
new UpdateOneOperation(
406-
this as TODO_NODE_3286,
407-
filter,
408-
update,
409-
resolveOptions(this, options)
410-
) as TODO_NODE_3286
405+
new UpdateOneOperation(this.s.namespace, filter, update, resolveOptions(this, options))
411406
);
412407
}
413408

@@ -425,12 +420,7 @@ export class Collection<TSchema extends Document = Document> {
425420
): Promise<UpdateResult<TSchema>> {
426421
return await executeOperation(
427422
this.client,
428-
new ReplaceOneOperation(
429-
this as TODO_NODE_3286,
430-
filter,
431-
replacement,
432-
resolveOptions(this, options)
433-
) as TODO_NODE_3286
423+
new ReplaceOneOperation(this.s.namespace, filter, replacement, resolveOptions(this, options))
434424
);
435425
}
436426

@@ -452,12 +442,7 @@ export class Collection<TSchema extends Document = Document> {
452442
): Promise<UpdateResult<TSchema>> {
453443
return await executeOperation(
454444
this.client,
455-
new UpdateManyOperation(
456-
this as TODO_NODE_3286,
457-
filter,
458-
update,
459-
resolveOptions(this, options)
460-
) as TODO_NODE_3286
445+
new UpdateManyOperation(this.s.namespace, filter, update, resolveOptions(this, options))
461446
);
462447
}
463448

@@ -473,11 +458,7 @@ export class Collection<TSchema extends Document = Document> {
473458
): Promise<DeleteResult> {
474459
return await executeOperation(
475460
this.client,
476-
new DeleteOneOperation(
477-
this as TODO_NODE_3286,
478-
filter,
479-
resolveOptions(this, options)
480-
) as TODO_NODE_3286
461+
new DeleteOneOperation(this.s.namespace, filter, resolveOptions(this, options))
481462
);
482463
}
483464

@@ -493,11 +474,7 @@ export class Collection<TSchema extends Document = Document> {
493474
): Promise<DeleteResult> {
494475
return await executeOperation(
495476
this.client,
496-
new DeleteManyOperation(
497-
this as TODO_NODE_3286,
498-
filter,
499-
resolveOptions(this, options)
500-
) as TODO_NODE_3286
477+
new DeleteManyOperation(this.s.namespace, filter, resolveOptions(this, options))
501478
);
502479
}
503480

@@ -521,7 +498,7 @@ export class Collection<TSchema extends Document = Document> {
521498
...options,
522499
readPreference: ReadPreference.PRIMARY
523500
})
524-
) as TODO_NODE_3286
501+
)
525502
);
526503
}
527504

@@ -589,7 +566,7 @@ export class Collection<TSchema extends Document = Document> {
589566
this.client,
590567
this.s.namespace,
591568
filter,
592-
resolveOptions(this as TODO_NODE_3286, options)
569+
resolveOptions(this, options)
593570
);
594571
}
595572

src/operations/delete.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import type { Document } from '../bson';
22
import { type Connection } from '../cmap/connection';
33
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
4-
import type { Collection } from '../collection';
54
import { MongoCompatibilityError, MongoServerError } from '../error';
65
import type { ClientSession } from '../sessions';
7-
import { type MongoDBNamespace } from '../utils';
6+
import { type MongoDBCollectionNamespace, type MongoDBNamespace } from '../utils';
87
import { type WriteConcernOptions } from '../write_concern';
98
import {
109
type CollationOptions,
@@ -103,8 +102,8 @@ export class DeleteOperation extends ModernizedCommandOperation<Document> {
103102
}
104103

105104
export class DeleteOneOperation extends DeleteOperation {
106-
constructor(collection: Collection, filter: Document, options: DeleteOptions) {
107-
super(collection.s.namespace, [makeDeleteStatement(filter, { ...options, limit: 1 })], options);
105+
constructor(ns: MongoDBCollectionNamespace, filter: Document, options: DeleteOptions) {
106+
super(ns, [makeDeleteStatement(filter, { ...options, limit: 1 })], options);
108107
}
109108

110109
override handleOk(
@@ -125,8 +124,8 @@ export class DeleteOneOperation extends DeleteOperation {
125124
}
126125
}
127126
export class DeleteManyOperation extends DeleteOperation {
128-
constructor(collection: Collection, filter: Document, options: DeleteOptions) {
129-
super(collection.s.namespace, [makeDeleteStatement(filter, options)], options);
127+
constructor(ns: MongoDBCollectionNamespace, filter: Document, options: DeleteOptions) {
128+
super(ns, [makeDeleteStatement(filter, options)], options);
130129
}
131130

132131
override handleOk(

src/operations/execute_operation.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ const MMAPv1_RETRY_WRITES_ERROR_MESSAGE =
3434
'This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.';
3535

3636
type ResultTypeFromOperation<TOperation> =
37-
TOperation extends AbstractOperation<infer K> ? K : never;
37+
TOperation extends ModernizedOperation<infer _>
38+
? ReturnType<TOperation['handleOk']>
39+
: TOperation extends AbstractOperation<infer K>
40+
? K
41+
: never;
3842

3943
/**
4044
* Executes the given operation with provided arguments.
@@ -57,7 +61,7 @@ type ResultTypeFromOperation<TOperation> =
5761
* @param operation - The operation to execute
5862
*/
5963
export async function executeOperation<
60-
T extends AbstractOperation<TResult>,
64+
T extends AbstractOperation,
6165
TResult = ResultTypeFromOperation<T>
6266
>(client: MongoClient, operation: T, timeoutContext?: TimeoutContext | null): Promise<TResult> {
6367
if (!(operation instanceof AbstractOperation)) {
@@ -179,10 +183,7 @@ type RetryOptions = {
179183
*
180184
* @param operation - The operation to execute
181185
* */
182-
async function tryOperation<
183-
T extends AbstractOperation<TResult>,
184-
TResult = ResultTypeFromOperation<T>
185-
>(
186+
async function tryOperation<T extends AbstractOperation, TResult = ResultTypeFromOperation<T>>(
186187
operation: T,
187188
{ topology, timeoutContext, session, readPreference }: RetryOptions
188189
): Promise<TResult> {

src/operations/update.ts

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import type { Document } from '../bson';
22
import { type Connection } from '../cmap/connection';
33
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
4-
import type { Collection } from '../collection';
54
import { MongoCompatibilityError, MongoInvalidArgumentError, MongoServerError } from '../error';
65
import type { InferIdType } from '../mongo_types';
76
import type { ClientSession } from '../sessions';
87
import { formatSort, type Sort, type SortForCmd } from '../sort';
9-
import { hasAtomicOperators, type MongoDBNamespace } from '../utils';
8+
import {
9+
hasAtomicOperators,
10+
type MongoDBCollectionNamespace,
11+
type MongoDBNamespace
12+
} from '../utils';
1013
import {
1114
type CollationOptions,
1215
type CommandOperationOptions,
@@ -136,21 +139,27 @@ export class UpdateOperation extends ModernizedCommandOperation<Document> {
136139

137140
/** @internal */
138141
export class UpdateOneOperation extends UpdateOperation {
139-
constructor(collection: Collection, filter: Document, update: Document, options: UpdateOptions) {
140-
super(
141-
collection.s.namespace,
142-
[makeUpdateStatement(filter, update, { ...options, multi: false })],
143-
options
144-
);
142+
constructor(
143+
ns: MongoDBCollectionNamespace,
144+
filter: Document,
145+
update: Document,
146+
options: UpdateOptions
147+
) {
148+
super(ns, [makeUpdateStatement(filter, update, { ...options, multi: false })], options);
145149

146150
if (!hasAtomicOperators(update, options)) {
147151
throw new MongoInvalidArgumentError('Update document requires atomic operators');
148152
}
149153
}
150154

151-
override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): Document {
155+
override handleOk(
156+
response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
157+
): UpdateResult {
152158
const res = super.handleOk(response);
159+
160+
// @ts-expect-error Explain typing is broken
153161
if (this.explain != null) return res;
162+
154163
if (res.code) throw new MongoServerError(res);
155164
if (res.writeErrors) throw new MongoServerError(res.writeErrors[0]);
156165

@@ -167,20 +176,25 @@ export class UpdateOneOperation extends UpdateOperation {
167176

168177
/** @internal */
169178
export class UpdateManyOperation extends UpdateOperation {
170-
constructor(collection: Collection, filter: Document, update: Document, options: UpdateOptions) {
171-
super(
172-
collection.s.namespace,
173-
[makeUpdateStatement(filter, update, { ...options, multi: true })],
174-
options
175-
);
179+
constructor(
180+
ns: MongoDBCollectionNamespace,
181+
filter: Document,
182+
update: Document,
183+
options: UpdateOptions
184+
) {
185+
super(ns, [makeUpdateStatement(filter, update, { ...options, multi: true })], options);
176186

177187
if (!hasAtomicOperators(update, options)) {
178188
throw new MongoInvalidArgumentError('Update document requires atomic operators');
179189
}
180190
}
181191

182-
override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): Document {
192+
override handleOk(
193+
response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
194+
): UpdateResult {
183195
const res = super.handleOk(response);
196+
197+
// @ts-expect-error Explain typing is broken
184198
if (this.explain != null) return res;
185199
if (res.code) throw new MongoServerError(res);
186200
if (res.writeErrors) throw new MongoServerError(res.writeErrors[0]);
@@ -215,24 +229,24 @@ export interface ReplaceOptions extends CommandOperationOptions {
215229
/** @internal */
216230
export class ReplaceOneOperation extends UpdateOperation {
217231
constructor(
218-
collection: Collection,
232+
ns: MongoDBCollectionNamespace,
219233
filter: Document,
220234
replacement: Document,
221235
options: ReplaceOptions
222236
) {
223-
super(
224-
collection.s.namespace,
225-
[makeUpdateStatement(filter, replacement, { ...options, multi: false })],
226-
options
227-
);
237+
super(ns, [makeUpdateStatement(filter, replacement, { ...options, multi: false })], options);
228238

229239
if (hasAtomicOperators(replacement)) {
230240
throw new MongoInvalidArgumentError('Replacement document must not contain atomic operators');
231241
}
232242
}
233243

234-
override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): Document {
244+
override handleOk(
245+
response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
246+
): UpdateResult {
235247
const res = super.handleOk(response);
248+
249+
// @ts-expect-error Explain typing is broken
236250
if (this.explain != null) return res;
237251
if (res.code) throw new MongoServerError(res);
238252
if (res.writeErrors) throw new MongoServerError(res.writeErrors[0]);

0 commit comments

Comments
 (0)