forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregate.test.ts
138 lines (116 loc) · 3.96 KB
/
aggregate.test.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
import { Schema, model, Document, Expression, PipelineStage, Types, Model, Aggregate } from 'mongoose';
import { expectType } from 'tsd';
const schema: Schema = new Schema({ name: { type: 'String' } });
interface ITest extends Document {
name?: string;
}
const Test = model<ITest>('Test', schema);
const AnotherTest = model<ITest>('AnotherTest', schema);
Test.aggregate([{ $match: { name: 'foo' } }]).exec().then((res: any) => console.log(res));
Test.aggregate<ITest>([{ $match: { name: 'foo' } }]).exec().then((res: Array<ITest>) => console.log(res[0].name));
Test.aggregate<ITest>([{ $match: { name: 'foo' } }]).then((res: Array<ITest>) => console.log(res[0].name));
run().catch((err: Error) => console.log(err.stack));
async function run() {
const res: Array<ITest> = await Test.aggregate<ITest>([{ $match: { name: 'foo' } }]).exec();
console.log(res[0].name);
const res2: Array<ITest> = await Test.aggregate<ITest>([{ $match: { name: 'foo' } }]);
console.log(res2[0].name);
expectType<number | undefined>(Test.aggregate<ITest>([{ $match: { name: 'foo' } }]).options.maxTimeMS);
expectType<boolean | undefined>(Test.aggregate<ITest>([{ $match: { name: 'foo' } }]).options.allowDiskUse);
Test.aggregate<ITest>([{ $match: { name: 'foo' } }]).option({ maxTimeMS: 222 });
await Test.aggregate<ITest>([{ $match: { name: 'foo' } }]).cursor().eachAsync(async(res) => {
console.log(res);
});
for await (const obj of Test.aggregate<ITest>()) {
obj.name;
}
function eachAsync(): void {
Test.aggregate().cursor().eachAsync((doc) => {
expectType<any>(doc);
});
Test.aggregate().cursor().eachAsync((docs) => {
expectType<any[]>(docs);
}, { batchSize: 2 });
Test.aggregate().cursor<ITest>().eachAsync((doc) => {
expectType<ITest>(doc);
});
Test.aggregate().cursor<ITest>().eachAsync((docs) => {
expectType<ITest[]>(docs);
}, { batchSize: 2 });
}
// Aggregate.prototype.sort()
expectType<ITest[]>(await Test.aggregate<ITest>().sort('-name'));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: 1 }));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: -1 }));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: 'asc' }));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: 'ascending' }));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: 'desc' }));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: 'descending' }));
expectType<ITest[]>(await Test.aggregate<ITest>().sort({ name: { $meta: 'textScore' } }));
// Aggregate.prototype.model()
expectType<Model<any>>(Test.aggregate<ITest>().model());
expectType<Aggregate<ITest[]>>(Test.aggregate<ITest>().model(AnotherTest));
}
function gh12017_1() {
const a: Expression = { $subtract: [
{ $dayOfWeek: new Date() } as Expression.DayOfWeek, // errors
2
] };
}
function gh12017_2() {
const a: Expression.Reduce = {
$reduce: {
input: '$values',
initialValue: { depth: -1 }, // errors
in: {
depth: '$$this.depth' // errors
}
}
};
const b: Expression.Reduce = {
$reduce: {
input: '$values',
initialValue: 0,
in: { $add: ['$$value', '$$this'] }
}
};
const c: PipelineStage.Set = {
$set: {
child: {
foo: 'bar' // errors
},
friend: new Types.ObjectId()
}
};
const d: Expression.ToInt = { $toInt: 2.5 };
}
function gh12311() {
const densifyWithDates: PipelineStage.Densify = {
$densify: {
field: 'timestamp',
range: {
step: 1,
unit: 'hour',
bounds: [new Date('2022-01-01'), new Date('2022-12-31')]
}
}
};
const densifyWithNumbers: PipelineStage.Densify = {
$densify: {
field: 'age',
range: {
step: 1,
bounds: [30, 90]
}
}
};
const densifyWithFullBounds: PipelineStage.Densify = {
$densify: {
field: 'age',
range: {
step: 1,
bounds: 'full'
}
}
};
}