-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
find.ts
55 lines (48 loc) 路 1.21 KB
/
find.ts
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
import { Document } from "../../../deps.ts";
import { CommandCursor, WireProtocol } from "../../protocol/mod.ts";
import { FindOptions } from "../../types.ts";
interface FindCursorContext {
dbName: string;
collectionName: string;
protocol: WireProtocol;
options: FindOptions;
filter?: Document;
}
export class FindCursor<T> extends CommandCursor<T> {
#context: FindCursorContext;
private async executor() {
const { protocol, filter, dbName, collectionName, options } = this.#context;
const { cursor } = await protocol.commandSingle(dbName, {
find: collectionName,
filter,
batchSize: 1,
noCursorTimeout: false,
...options,
});
return {
...cursor,
id: cursor.id.toString(),
};
}
constructor(context: FindCursorContext) {
super(context.protocol, () => this.executor());
this.#context = {
...context,
options: {
...context.options,
},
};
}
limit(limit: number): this {
this.#context.options.limit = limit;
return this;
}
skip(skip: number): this {
this.#context.options.skip = skip;
return this;
}
sort(sort: Document): this {
this.#context.options.sort = sort;
return this;
}
}