-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
basic_schema.test-d.ts
67 lines (58 loc) · 2.88 KB
/
basic_schema.test-d.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
import { expectAssignable, expectNotAssignable, expectNotType, expectType } from 'tsd';
import { Document, ObjectId } from '../../src/bson';
import { Collection } from '../../src/collection';
import { Db } from '../../src/db';
import { MongoClient } from '../../src/mongo_client';
import type { InferIdType } from '../../src/mongo_types';
const db = new Db(new MongoClient(''), '');
type ACounter = { a: number };
type ACounterWithId = { a: number; _id: ObjectId };
////////////////////////////////////////////////////////////////////////////////////////////////////
// Can define Schema without _id
expectType<Collection<ACounter>>(new Collection<ACounter>(db, ''));
// Or with one
expectType<Collection<ACounterWithId>>(new Collection<ACounterWithId>(db, ''));
////////////////////////////////////////////////////////////////////////////////////////////////////
// Simple Schema that does not define an _id
// With _id
type InsertOneArgOf<S extends Document> = Parameters<Collection<S>['insertOne']>[0];
expectAssignable<InsertOneArgOf<ACounter>>({ _id: new ObjectId(), a: 3 });
// Without _id
expectAssignable<InsertOneArgOf<ACounter>>({ a: 3 });
// Does not permit extra keys
expectNotType<InsertOneArgOf<ACounter>>({ a: 2, b: 34 });
////////////////////////////////////////////////////////////////////////////////////////////////////
// Simple Schema that does define an _id
// With _id
expectAssignable<InsertOneArgOf<ACounterWithId>>({ _id: new ObjectId(), a: 3 });
// Without _id
expectNotAssignable<InsertOneArgOf<ACounterWithId>>({ a: 3 });
// Does not permit extra keys
expectNotType<InsertOneArgOf<ACounterWithId>>({ a: 2, b: 34 });
////////////////////////////////////////////////////////////////////////////////////////////////////
// CustomType _id Schema (behavior change)
// _id that is a custom type must be generated client side, so it is required
class MyId {
uuid!: number;
}
interface CustomIdType {
a: number;
_id: MyId;
}
type customIdCollection = Collection<CustomIdType>;
type insertOneArg = Parameters<customIdCollection['insertOne']>[0];
// inferring the _id type is straight forward for a schema like this
type IdType = InferIdType<CustomIdType>;
expectType<IdType>(new MyId());
// _id is a required field since it isn't an ObjectId
expectAssignable<insertOneArg>({ a: 2, _id: new MyId() });
expectNotType<insertOneArg>({ a: 2 });
////////////////////////////////////////////////////////////////////////////////////////////////////
// InferIdType -
// eslint-disable-next-line @typescript-eslint/ban-types
type Empty = {};
const a: never = 0 as never;
const oid = new ObjectId();
expectType<InferIdType<Empty>>(oid); // Empty schema gets the implicit _id
expectType<InferIdType<{ _id: Empty }>>(a); // Empty object as an oid resolves to never, while this is a valid _id, it is likely undesirable
expectType<InferIdType<{ _id: { a: number } }>>({ a: 3 }); // embedded documents are permitted as _id fields