|
5 | 5 | type Document,
|
6 | 6 | Long,
|
7 | 7 | parseToElementsToArray,
|
| 8 | + pluckBSONSerializeOptions, |
8 | 9 | type Timestamp
|
9 | 10 | } from '../../bson';
|
10 | 11 | import { MongoUnexpectedServerResponseError } from '../../error';
|
@@ -153,13 +154,7 @@ export class MongoDBResponse extends OnDemandDocument {
|
153 | 154 |
|
154 | 155 | public override toObject(options?: BSONSerializeOptions): Record<string, any> {
|
155 | 156 | const exactBSONOptions = {
|
156 |
| - useBigInt64: options?.useBigInt64, |
157 |
| - promoteLongs: options?.promoteLongs, |
158 |
| - promoteValues: options?.promoteValues, |
159 |
| - promoteBuffers: options?.promoteBuffers, |
160 |
| - bsonRegExp: options?.bsonRegExp, |
161 |
| - raw: options?.raw ?? false, |
162 |
| - fieldsAsRaw: options?.fieldsAsRaw ?? {}, |
| 157 | + ...pluckBSONSerializeOptions(options ?? {}), |
163 | 158 | validation: this.parseBsonSerializationOptions(options)
|
164 | 159 | };
|
165 | 160 | return super.toObject(exactBSONOptions);
|
@@ -188,33 +183,38 @@ export class CursorResponse extends MongoDBResponse {
|
188 | 183 | return value instanceof CursorResponse || value === CursorResponse.emptyGetMore;
|
189 | 184 | }
|
190 | 185 |
|
191 |
| - public id: Long; |
192 |
| - public ns: MongoDBNamespace | null = null; |
193 |
| - public batchSize = 0; |
194 |
| - |
195 |
| - private batch: OnDemandDocument; |
| 186 | + private _batch: OnDemandDocument | null = null; |
196 | 187 | private iterated = 0;
|
197 | 188 |
|
198 |
| - constructor(bytes: Uint8Array, offset?: number, isArray?: boolean) { |
199 |
| - super(bytes, offset, isArray); |
| 189 | + get cursor() { |
| 190 | + return this.get('cursor', BSONType.object, true); |
| 191 | + } |
200 | 192 |
|
201 |
| - const cursor = this.get('cursor', BSONType.object, true); |
| 193 | + get id(): Long { |
| 194 | + return Long.fromBigInt(this.cursor.get('id', BSONType.long, true)); |
| 195 | + } |
202 | 196 |
|
203 |
| - const id = cursor.get('id', BSONType.long, true); |
204 |
| - this.id = new Long(Number(id & 0xffff_ffffn), Number((id >> 32n) & 0xffff_ffffn)); |
| 197 | + get ns() { |
| 198 | + const namespace = this.cursor.get('ns', BSONType.string); |
| 199 | + if (namespace != null) return ns(namespace); |
| 200 | + return null; |
| 201 | + } |
205 | 202 |
|
206 |
| - const namespace = cursor.get('ns', BSONType.string); |
207 |
| - if (namespace != null) this.ns = ns(namespace); |
| 203 | + get length() { |
| 204 | + return Math.max(this.batchSize - this.iterated, 0); |
| 205 | + } |
208 | 206 |
|
209 |
| - if (cursor.has('firstBatch')) this.batch = cursor.get('firstBatch', BSONType.array, true); |
210 |
| - else if (cursor.has('nextBatch')) this.batch = cursor.get('nextBatch', BSONType.array, true); |
| 207 | + get batch() { |
| 208 | + if (this._batch != null) return this._batch; |
| 209 | + const cursor = this.cursor; |
| 210 | + if (cursor.has('firstBatch')) this._batch = cursor.get('firstBatch', BSONType.array, true); |
| 211 | + else if (cursor.has('nextBatch')) this._batch = cursor.get('nextBatch', BSONType.array, true); |
211 | 212 | else throw new MongoUnexpectedServerResponseError('Cursor document did not contain a batch');
|
212 |
| - |
213 |
| - this.batchSize = this.batch.size(); |
| 213 | + return this._batch; |
214 | 214 | }
|
215 | 215 |
|
216 |
| - get length() { |
217 |
| - return Math.max(this.batchSize - this.iterated, 0); |
| 216 | + get batchSize() { |
| 217 | + return this.batch?.size(); |
218 | 218 | }
|
219 | 219 |
|
220 | 220 | shift(options?: BSONSerializeOptions): any {
|
|
0 commit comments