-
Notifications
You must be signed in to change notification settings - Fork 0
/
orm-active-record-cursor.ts
167 lines (139 loc) · 4.13 KB
/
orm-active-record-cursor.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import {
CollationOptions, CountOptions, CursorFlag,
FindCursor,
ReadPreference, SortDirection
} from "mongodb";
import {OrmDocumentInstance} from "./orm-decorators";
import {StrictFilterQuery} from "./orm-strict-mongodb-types";
import { Document } from "bson";
export class ActiveRecordCursor<
ActiveRecordForModel extends { _document: OrmDocumentInstance, _isPersisted: boolean }
> {
constructor(
public activeRecordModelConstructor: new(...args: any[]) => ActiveRecordForModel,
public normalCursor: FindCursor<ActiveRecordForModel['_document']>
) {}
addCursorFlag(flag: CursorFlag, value: boolean) {
this.normalCursor.addCursorFlag(flag, value);
return this;
}
addQueryModifier(name: string, value: boolean) {
this.normalCursor.addQueryModifier(name, value);
return this;
}
batchSize(value: number) {
this.normalCursor.batchSize(value);
return this;
}
clone() {
return new ActiveRecordCursor<ActiveRecordForModel>(
this.activeRecordModelConstructor,
this.normalCursor.clone()
);
}
close() {
return this.normalCursor.close();
}
collation(value: CollationOptions) {
this.normalCursor.collation(value);
return this;
}
comment(value: string) {
this.normalCursor.comment(value);
return this;
}
count(options?: CountOptions): Promise<number> {
if (options) {
return this.normalCursor.count(options);
} else {
return this.normalCursor.count();
}
}
explain(): Promise<Document> {
return this.normalCursor.explain();
}
filter(filter: StrictFilterQuery<ActiveRecordForModel['_document']>) {
this.normalCursor.filter(filter);
return this;
}
forEach(iterator: (doc: ActiveRecordForModel) => boolean | void) {
this.normalCursor.forEach((doc: ActiveRecordForModel['_document']) => {
let activeRecord = new this.activeRecordModelConstructor();
activeRecord._document = doc;
activeRecord._isPersisted = true;
iterator(activeRecord);
});
}
hasNext(): Promise<boolean> {
return this.normalCursor.hasNext();
}
hint(hint: string | object) {
this.normalCursor.hint(hint);
return this;
}
isClosed() {
return this.normalCursor.closed;
}
limit(value: number) {
this.normalCursor.limit(value);
return this;
}
map(transform: (document: ActiveRecordForModel['_document']) => ActiveRecordForModel['_document']) {
this.normalCursor.map(transform);
return this;
}
max(max: object) {
this.normalCursor.max(max);
return this;
}
maxAwaitTimeMS(value: number) {
this.normalCursor.maxAwaitTimeMS(value);
return this;
}
maxTimeMS(value: number) {
this.normalCursor.maxTimeMS(value);
return this;
}
min(min: object) {
this.normalCursor.min(min);
return this;
}
async next(): Promise<ActiveRecordForModel | null> {
let result = await this.normalCursor.next();
if (!result) {
return null;
} else {
let activeRecord = new this.activeRecordModelConstructor();
activeRecord._document = result;
activeRecord._isPersisted = true;
return activeRecord;
}
}
project(value: { [K in keyof ActiveRecordForModel['_document']]?: 1 | 0 }) {
this.normalCursor.project(value);
return this;
}
rewind() {
this.normalCursor.rewind();
}
setReadPreference(readPreference: ReadPreference) {
this.normalCursor = this.normalCursor.withReadPreference(readPreference);
return this;
}
skip(value: number) {
this.normalCursor.skip(value);
return this;
}
sort<Keys extends keyof ActiveRecordForModel['_document']>(sorts: { [K in Keys]: SortDirection }) {
this.normalCursor.sort(sorts);
return this;
}
async toArray(): Promise<ActiveRecordForModel[]> {
return (await this.normalCursor.toArray()).map((doc) => {
let activeRecord = new this.activeRecordModelConstructor();
activeRecord._document = doc;
activeRecord._isPersisted = true;
return activeRecord;
});
}
}