Skip to content

Only simple crud #4614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ export class Collection<TSchema extends Document = Document> {
filter,
replacement,
resolveOptions(this, options)
)
) as TODO_NODE_3286
);
}

Expand Down Expand Up @@ -489,7 +489,11 @@ export class Collection<TSchema extends Document = Document> {
): Promise<DeleteResult> {
return await executeOperation(
this.client,
new DeleteManyOperation(this as TODO_NODE_3286, filter, resolveOptions(this, options))
new DeleteManyOperation(
this as TODO_NODE_3286,
filter,
resolveOptions(this, options)
) as TODO_NODE_3286
);
}

Expand Down
45 changes: 25 additions & 20 deletions src/operations/count.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type Connection } from '..';
import type { Document } from '../bson';
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
import type { Collection } from '../collection';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { type TimeoutContext } from '../timeout';
import type { MongoDBNamespace } from '../utils';
import { CommandOperation, type CommandOperationOptions } from './command';
import { type CommandOperationOptions, ModernizedCommandOperation } from './command';
import { Aspect, defineAspects } from './operation';

/** @public */
Expand All @@ -21,8 +21,15 @@ export interface CountOptions extends CommandOperationOptions {
hint?: string | Document;
}

class CountResponse extends MongoDBResponse {
get n(): number {
return this.getNumber('n') ?? 0;
}
}

/** @internal */
export class CountOperation extends CommandOperation<number> {
export class CountOperation extends ModernizedCommandOperation<number> {
override SERVER_COMMAND_RESPONSE_TYPE = CountResponse;
override options: CountOptions;
collectionName?: string;
query: Document;
Expand All @@ -39,35 +46,33 @@ export class CountOperation extends CommandOperation<number> {
return 'count' as const;
}

override async execute(
server: Server,
session: ClientSession | undefined,
timeoutContext: TimeoutContext
): Promise<number> {
const options = this.options;
override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document {
const cmd: Document = {
count: this.collectionName,
query: this.query
};

if (typeof options.limit === 'number') {
cmd.limit = options.limit;
if (typeof this.options.limit === 'number') {
cmd.limit = this.options.limit;
}

if (typeof options.skip === 'number') {
cmd.skip = options.skip;
if (typeof this.options.skip === 'number') {
cmd.skip = this.options.skip;
}

if (options.hint != null) {
cmd.hint = options.hint;
if (this.options.hint != null) {
cmd.hint = this.options.hint;
}

if (typeof options.maxTimeMS === 'number') {
cmd.maxTimeMS = options.maxTimeMS;
if (typeof this.options.maxTimeMS === 'number') {
cmd.maxTimeMS = this.options.maxTimeMS;
}

const result = await super.executeCommand(server, session, cmd, timeoutContext);
return result ? result.n : 0;
return cmd;
}

override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): number {
return response.n;
}
}

Expand Down
58 changes: 37 additions & 21 deletions src/operations/delete.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { Document } from '../bson';
import { type Connection } from '../cmap/connection';
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
import type { Collection } from '../collection';
import { MongoCompatibilityError, MongoServerError } from '../error';
import { type TODO_NODE_3286 } from '../mongo_types';
Expand All @@ -7,7 +9,11 @@ import type { ClientSession } from '../sessions';
import { type TimeoutContext } from '../timeout';
import { type MongoDBNamespace } from '../utils';
import { type WriteConcernOptions } from '../write_concern';
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
import {
type CollationOptions,
type CommandOperationOptions,
ModernizedCommandOperation
} from './command';
import { Aspect, defineAspects, type Hint } from './operation';

/** @public */
Expand Down Expand Up @@ -43,7 +49,8 @@ export interface DeleteStatement {
}

/** @internal */
export class DeleteOperation extends CommandOperation<DeleteResult> {
export class DeleteOperation extends ModernizedCommandOperation<Document> {
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
override options: DeleteOptions;
statements: DeleteStatement[];

Expand All @@ -66,12 +73,9 @@ export class DeleteOperation extends CommandOperation<DeleteResult> {
return this.statements.every(op => (op.limit != null ? op.limit > 0 : true));
}

override async execute(
server: Server,
session: ClientSession | undefined,
timeoutContext: TimeoutContext
): Promise<DeleteResult> {
const options = this.options ?? {};
override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document {
const options = this.options;

const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
const command: Document = {
delete: this.ns.collection,
Expand All @@ -97,13 +101,7 @@ export class DeleteOperation extends CommandOperation<DeleteResult> {
}
}

const res: TODO_NODE_3286 = await super.executeCommand(
server,
session,
command,
timeoutContext
);
return res;
return command;
}
}

Expand All @@ -127,19 +125,37 @@ export class DeleteOneOperation extends DeleteOperation {
deletedCount: res.n
};
}

override handleOk(
response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
): DeleteResult {
const res = super.handleOk(response);

// @ts-expect-error Explain commands have broken TS
if (this.explain) return res;

if (res.code) throw new MongoServerError(res);
if (res.writeErrors) throw new MongoServerError(res.writeErrors[0]);

return {
acknowledged: this.writeConcern?.w !== 0,
deletedCount: res.n
};
}
}
export class DeleteManyOperation extends DeleteOperation {
constructor(collection: Collection, filter: Document, options: DeleteOptions) {
super(collection.s.namespace, [makeDeleteStatement(filter, options)], options);
}

override async execute(
server: Server,
session: ClientSession | undefined,
timeoutContext: TimeoutContext
): Promise<DeleteResult> {
const res: TODO_NODE_3286 = await super.execute(server, session, timeoutContext);
override handleOk(
response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
): DeleteResult {
const res = super.handleOk(response);

// @ts-expect-error Explain commands have broken TS
if (this.explain) return res;

if (res.code) throw new MongoServerError(res);
if (res.writeErrors) throw new MongoServerError(res.writeErrors[0]);

Expand Down
27 changes: 16 additions & 11 deletions src/operations/estimated_document_count.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { type Connection } from '..';
import type { Document } from '../bson';
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
import type { Collection } from '../collection';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { type TimeoutContext } from '../timeout';
import { CommandOperation, type CommandOperationOptions } from './command';
import { type CommandOperationOptions, ModernizedCommandOperation } from './command';
import { Aspect, defineAspects } from './operation';

/** @public */
Expand All @@ -16,8 +16,15 @@ export interface EstimatedDocumentCountOptions extends CommandOperationOptions {
maxTimeMS?: number;
}

class EstimatedDocumentCountResponse extends MongoDBResponse {
get n(): number {
return this.getNumber('n') ?? 0;
}
}

/** @internal */
export class EstimatedDocumentCountOperation extends CommandOperation<number> {
export class EstimatedDocumentCountOperation extends ModernizedCommandOperation<number> {
override SERVER_COMMAND_RESPONSE_TYPE = EstimatedDocumentCountResponse;
override options: EstimatedDocumentCountOptions;
collectionName: string;

Expand All @@ -31,11 +38,7 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
return 'count' as const;
}

override async execute(
server: Server,
session: ClientSession | undefined,
timeoutContext: TimeoutContext
): Promise<number> {
override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document {
const cmd: Document = { count: this.collectionName };

if (typeof this.options.maxTimeMS === 'number') {
Expand All @@ -48,9 +51,11 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
cmd.comment = this.options.comment;
}

const response = await super.executeCommand(server, session, cmd, timeoutContext);
return cmd;
}

return response?.n || 0;
override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): number {
return response.n;
}
}

Expand Down
Loading