deno.land / x / mongoose@6.7.5 / test / types / queries.test.ts

queries.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import { HydratedDocument, Schema, model, Document, Types, Query, Model, QueryWithHelpers, PopulatedDoc, FilterQuery, UpdateQuery, ApplyBasicQueryCasting, QuerySelector} from 'mongoose';import { ObjectId } from 'mongodb';import { expectError, expectType } from 'tsd';import { autoTypedModel } from './models.test';import { AutoTypedSchemaType } from './schema.test';
interface QueryHelpers { _byName(this: QueryWithHelpers<any, ITest, QueryHelpers>, name: string): QueryWithHelpers<Array<ITest>, ITest, QueryHelpers>; byName(this: QueryWithHelpers<any, ITest, QueryHelpers>, name: string): QueryWithHelpers<Array<ITest>, ITest, QueryHelpers>;}
const childSchema: Schema = new Schema({ name: String });const ChildModel = model<Child>('Child', childSchema);
const schema: Schema<ITest, Model<ITest, QueryHelpers>, {}, QueryHelpers> = new Schema({ name: { type: 'String' }, tags: [String], child: { type: 'ObjectId', ref: 'Child' }, docs: [{ _id: 'ObjectId', id: Number, tags: [String] }], endDate: Date});
schema.query._byName = function(name: string): QueryWithHelpers<any, ITest, QueryHelpers> { return this.find({ name });};
schema.query.byName = function(name: string): QueryWithHelpers<any, ITest, QueryHelpers> { expectError(this.notAQueryHelper()); return this._byName(name);};
interface Child { name: string;}interface ISubdoc extends Document { myId?: Types.ObjectId; id?: number; tags?: string[];}
interface ITest extends Document { name?: string; age?: number; parent?: Types.ObjectId; child?: PopulatedDoc<Child & Document<ObjectId>>, tags?: string[]; docs?: ISubdoc[]; endDate?: Date;}
const Test = model<ITest, Model<ITest, QueryHelpers>>('Test', schema);
Test.find({}, {}, { populate: { path: 'child', model: ChildModel, match: true } }).exec().then((res: Array<ITest>) => console.log(res));
Test.find().byName('test').byName('test2').orFail().exec().then(console.log);
Test.count({ name: /Test/ }).exec().then((res: number) => console.log(res));Test.findOne({ 'docs.id': 42 }).exec().then(console.log);
// ObjectId castingTest.find({ parent: new Types.ObjectId('0'.repeat(24)) });Test.find({ parent: '0'.repeat(24) });Test.find({ parent: { $in: ['0'.repeat(24)] } });
// OperatorsTest.find({ name: { $in: ['Test'] } }).exec().then((res: Array<ITest>) => console.log(res));Test.find({ tags: 'test' }).exec();Test.find({ tags: { $in: ['test'] } }).exec();
// Implicit `$in`Test.find({ name: ['Test1', 'Test2'] }).exec();
Test.find({ name: 'test' }, (err: Error | null, docs: ITest[]) => { console.log(!!err, docs[0].age);});
Test.findOne({ name: 'test' }, (err: Error | null, doc: ITest | null) => { if (doc != null) { console.log(!!err, doc.age); }});
Test.find({ name: { $gte: 'Test' } }, null, { collation: { locale: 'en-us' } }).exec(). then((res: Array<ITest>) => console.log(res[0].name));
Test.findOne().orFail(new Error('bar')).then((doc: ITest | null) => console.log('Found! ' + doc));
Test.distinct('name').exec().then((res: Array<any>) => console.log(res[0]));
Test.findOneAndUpdate({ name: 'test' }, { name: 'test2' }).exec().then((res: ITest | null) => console.log(res));Test.findOneAndUpdate({ name: 'test' }, { name: 'test2' }).then((res: ITest | null) => console.log(res));Test.findOneAndUpdate({ name: 'test' }, { $set: { name: 'test2' } }).then((res: ITest | null) => console.log(res));Test.findOneAndUpdate({ name: 'test' }, { $inc: { age: 2 } }).then((res: ITest | null) => console.log(res));Test.findOneAndUpdate({ name: 'test' }, { name: 'test3' }, { upsert: true, new: true }).then((res: ITest) => { res.name = 'test4';});Test.findOneAndUpdate({ name: 'test' }, { name: 'test3' }, { upsert: true, returnOriginal: false }).then((res: ITest) => { res.name = 'test4';});Test.findOneAndUpdate({ name: 'test' }, { name: 'test3' }, { rawResult: true }).then((res: any) => { console.log(res.ok);});Test.findOneAndUpdate({ name: 'test' }, { name: 'test3' }, { new: true, upsert: true, rawResult: true }).then((res: any) => { console.log(res.ok);});
Test.findOneAndReplace({ name: 'test' }, { _id: new Types.ObjectId(), name: 'test2' }).exec().then((res: ITest | null) => console.log(res));
Test.findOneAndUpdate({ name: 'test' }, { $addToSet: { tags: 'each' } });Test.findOneAndUpdate({ name: 'test' }, { $push: { tags: 'each' } });Test.findOneAndUpdate({ name: 'test' }, { $pull: { docs: { 'nested.id': 1 } } });
Test.findOneAndUpdate({ name: 'test', 'docs.id': 1 }, { $pull: { 'docs.$.tags': 'foo' } });
const update = Math.random() > 0.5 ? { $unset: { 'docs.0': 1 } } : { age: 55 };Test.findOneAndUpdate({ name: 'test' }, update);
Test.findOneAndUpdate({ name: 'test' }, { $currentDate: { endDate: true } });Test.findOneAndUpdate({ name: 'test' }, [{ $set: { endDate: true } }]);
Test.findByIdAndUpdate({ name: 'test' }, { name: 'test2' }, (err: any, doc) => console.log(doc));
Test.findOneAndUpdate({ name: 'test' }, { 'docs.0.myId': '0'.repeat(24) });
const query: Query<ITest | null, ITest> = Test.findOne();query instanceof Query;
// ChainingTest.findOne().where({ name: 'test' });Test.where().find({ name: 'test' });
// Projectionconst p0: Record<string, number> = Test.find().projection({ age: true, parent: 1, 'docs.id': 1});const p1: Record<string, number> = Test.find().projection('age docs.id');const p2: Record<string, number> | null = Test.find().projection();const p3: null = Test.find().projection(null);
// SortingTest.find().sort();Test.find().sort('-name');Test.find().sort({ name: -1 });Test.find().sort({ name: 'ascending' });Test.find().sort(undefined);Test.find().sort(null);Test.find().sort([['key', 'ascending']]);Test.find().sort([['key1', 'ascending'], ['key2', 'descending']]);expectError(Test.find().sort({ name: 2 }));expectError(Test.find().sort({ name: 'invalidSortOrder' }));expectError(Test.find().sort([['key', 'invalid']]));expectError(Test.find().sort([['key', false]]));expectError(Test.find().sort(['invalid']));
// Super generic queryfunction testGenericQuery(): void { interface CommonInterface<T> extends Document { something: string; content: T; }
async function findSomething<T>(model: Model<CommonInterface<T>>): Promise<CommonInterface<T>> { return model.findOne({ something: 'test' }).orFail().exec(); }}
function eachAsync(): void { Test.find().cursor().eachAsync((doc) => { expectType<(ITest & { _id: Types.ObjectId; })>(doc); }); Test.find().cursor().eachAsync((docs) => { expectType<(ITest & { _id: Types.ObjectId; })[]>(docs); }, { batchSize: 2 });}
async function gh10617(): Promise<void> { interface IDBModel extends Document { date: Date; // date created _tags: any[]; }
const schema = new Schema({ date: { type: Date, default: Date.now }, // date created _tags: [{ type: Schema.Types.ObjectId, ref: 'Tag' }] });
const DBModel: Model<IDBModel> = model<IDBModel>('Meep', schema); await DBModel.findOne({});}
function gh10757() { enum MyEnum { VALUE1, VALUE2, VALUE3 }
interface MyClass { status: MyEnum; }
type MyClassDocument = MyClass & Document;
const test: FilterQuery<MyClass> = { status: { $in: [MyEnum.VALUE1, MyEnum.VALUE2] } };}
function gh10857() { type MyUnion = 'VALUE1' | 'VALUE2'; interface MyClass { status: MyUnion; } type MyClassDocument = MyClass & Document; const test: FilterQuery<MyClass> = { status: { $in: ['VALUE1', 'VALUE2'] } };}
function gh10786() { interface User { phone?: string; name?: string }
const updateQuery: UpdateQuery<User> = { name: 'John' }; if (true) { updateQuery.phone = 'XXXX'; }}
async function gh11156(): Promise<void> { interface IUser { name: string; age: number; }
const schema = new Schema<IUser>({ name: String, age: Number });
const User: Model<IUser> = model<IUser>('User', schema);
expectType<{ name: string }>(await User.findOne<Pick<IUser, 'name'>>({}).orFail());}
async function gh11041(): Promise<void> { interface User { name: string; email: string; avatar?: string; }
// 2. Create a Schema corresponding to the document interface. const schema = new Schema<User>({ name: { type: String, required: true }, email: { type: String, required: true }, avatar: String });
// 3. Create a Model. const MyModel = model<User>('User', schema);
expectType<HydratedDocument<User> | null>(await MyModel.findOne({}).populate('someField').exec());}
async function gh11306(): Promise<void> { interface User { name: string; email: string; avatar?: string; }
// 2. Create a Schema corresponding to the document interface. const schema = new Schema<User>({ name: { type: String, required: true }, email: { type: String, required: true }, avatar: String });
// 3. Create a Model. const MyModel = model<User>('User', schema);
expectType<any[]>(await MyModel.distinct('name')); expectType<string[]>(await MyModel.distinct<string>('name'));}
async function gh11602(): Promise<void> { const updateResult = await Model.findOneAndUpdate(query, { $inc: { occurence: 1 } }, { upsert: true, returnDocument: 'after', rawResult: true }); expectError(updateResult.lastErrorObject?.modifiedCount); expectType<boolean | undefined>(updateResult.lastErrorObject?.updatedExisting); expectType<ObjectId | undefined>(updateResult.lastErrorObject?.upserted);}
function autoTypedQuery() { const AutoTypedModel = autoTypedModel(); const query = AutoTypedModel.find(); expectType<typeof query>(AutoTypedModel.find().byUserName(''));}
function gh11964() { type Condition<T> = ApplyBasicQueryCasting<T> | QuerySelector<ApplyBasicQueryCasting<T>>; // redefined here because it's not exported by mongoose
type WithId<T extends object> = T & { id: string };
class Repository<T extends object> { /* ... */
find(id: string) { const idCondition: Condition<WithId<T>>['id'] = id; // error :( const filter: FilterQuery<WithId<T>> = { id }; // error :(
/* ... */ } }}
function gh12091() { interface IUser{ friendsNames: string[]; } const userSchema = new Schema<IUser>({ friendsNames: [String] });
const update: UpdateQuery<IUser> = { $addToSet: { friendsNames: 'John Doe' } }; if (!update?.$addToSet) { return; } update.$addToSet.friendsNames = 'Jane Doe';}
function gh12142() { const schema = new Schema({ name: String, comments: [{ text: String }] });
const Test = model('Test', schema);
Test.updateOne( { _id: new Types.ObjectId() }, { $pull: { comments: new Types.ObjectId() } } );}
async function gh12342_manual() { interface Project { name?: string, stars?: number }
interface ProjectQueryHelpers { byName(name: string): QueryWithHelpers< HydratedDocument<Project>[], HydratedDocument<Project>, ProjectQueryHelpers > }
type ProjectModelType = Model<Project, ProjectQueryHelpers>;
const ProjectSchema = new Schema< Project, Model<Project, ProjectQueryHelpers>, {}, ProjectQueryHelpers >({ name: String, stars: Number });
ProjectSchema.query.byName = function byName( this: QueryWithHelpers<any, HydratedDocument<Project>, ProjectQueryHelpers>, name: string ) { return this.find({ name: name }); };
// 2nd param to `model()` is the Model class to return. const ProjectModel = model<Project, ProjectModelType>('Project', schema);
expectType<HydratedDocument<Project>[]>( await ProjectModel.findOne().where('stars').gt(1000).byName('mongoose') );}
async function gh12342_auto() { interface Project { name?: string, stars?: number }
const ProjectSchema = new Schema({ name: String, stars: Number }, { query: { byName(name: string) { return this.find({ name }); } } });
const ProjectModel = model('Project', ProjectSchema);
expectType<HydratedDocument<Project>[]>( await ProjectModel.findOne().where('stars').gt(1000).byName('mongoose') );}
mongoose

Version Info

Tagged at
a year ago