@@ -32,50 +32,50 @@ export interface ClientWriteModel {
32
32
}
33
33
34
34
/** @public */
35
- export interface ClientInsertOneModel extends ClientWriteModel {
35
+ export interface ClientInsertOneModel < TSchema > extends ClientWriteModel {
36
36
name : 'insertOne' ;
37
37
/** The document to insert. */
38
- document : OptionalId < Document > ;
38
+ document : OptionalId < TSchema > ;
39
39
}
40
40
41
41
/** @public */
42
- export interface ClientDeleteOneModel extends ClientWriteModel {
42
+ export interface ClientDeleteOneModel < TSchema > extends ClientWriteModel {
43
43
name : 'deleteOne' ;
44
44
/**
45
45
* The filter used to determine if a document should be deleted.
46
46
* For a deleteOne operation, the first match is removed.
47
47
*/
48
- filter : Filter < Document > ;
48
+ filter : Filter < TSchema > ;
49
49
/** Specifies a collation. */
50
50
collation ?: CollationOptions ;
51
51
/** The index to use. If specified, then the query system will only consider plans using the hinted index. */
52
52
hint ?: Hint ;
53
53
}
54
54
55
55
/** @public */
56
- export interface ClientDeleteManyModel extends ClientWriteModel {
56
+ export interface ClientDeleteManyModel < TSchema > extends ClientWriteModel {
57
57
name : 'deleteMany' ;
58
58
/**
59
59
* The filter used to determine if a document should be deleted.
60
60
* For a deleteMany operation, all matches are removed.
61
61
*/
62
- filter : Filter < Document > ;
62
+ filter : Filter < TSchema > ;
63
63
/** Specifies a collation. */
64
64
collation ?: CollationOptions ;
65
65
/** The index to use. If specified, then the query system will only consider plans using the hinted index. */
66
66
hint ?: Hint ;
67
67
}
68
68
69
69
/** @public */
70
- export interface ClientReplaceOneModel extends ClientWriteModel {
70
+ export interface ClientReplaceOneModel < TSchema > extends ClientWriteModel {
71
71
name : 'replaceOne' ;
72
72
/**
73
73
* The filter used to determine if a document should be replaced.
74
74
* For a replaceOne operation, the first match is replaced.
75
75
*/
76
- filter : Filter < Document > ;
76
+ filter : Filter < TSchema > ;
77
77
/** The document with which to replace the matched document. */
78
- replacement : WithoutId < Document > ;
78
+ replacement : WithoutId < TSchema > ;
79
79
/** Specifies a collation. */
80
80
collation ?: CollationOptions ;
81
81
/** The index to use. If specified, then the query system will only consider plans using the hinted index. */
@@ -85,19 +85,19 @@ export interface ClientReplaceOneModel extends ClientWriteModel {
85
85
}
86
86
87
87
/** @public */
88
- export interface ClientUpdateOneModel extends ClientWriteModel {
88
+ export interface ClientUpdateOneModel < TSchema > extends ClientWriteModel {
89
89
name : 'updateOne' ;
90
90
/**
91
91
* The filter used to determine if a document should be updated.
92
92
* For an updateOne operation, the first match is updated.
93
93
*/
94
- filter : Filter < Document > ;
94
+ filter : Filter < TSchema > ;
95
95
/**
96
96
* The modifications to apply. The value can be either:
97
97
* UpdateFilter<Document> - A document that contains update operator expressions,
98
98
* Document[] - an aggregation pipeline.
99
99
*/
100
- update : UpdateFilter < Document > | Document [ ] ;
100
+ update : UpdateFilter < TSchema > | Document [ ] ;
101
101
/** A set of filters specifying to which array elements an update should apply. */
102
102
arrayFilters ?: Document [ ] ;
103
103
/** Specifies a collation. */
@@ -109,19 +109,19 @@ export interface ClientUpdateOneModel extends ClientWriteModel {
109
109
}
110
110
111
111
/** @public */
112
- export interface ClientUpdateManyModel extends ClientWriteModel {
112
+ export interface ClientUpdateManyModel < TSchema > extends ClientWriteModel {
113
113
name : 'updateMany' ;
114
114
/**
115
115
* The filter used to determine if a document should be updated.
116
116
* For an updateMany operation, all matches are updated.
117
117
*/
118
- filter : Filter < Document > ;
118
+ filter : Filter < TSchema > ;
119
119
/**
120
120
* The modifications to apply. The value can be either:
121
121
* UpdateFilter<Document> - A document that contains update operator expressions,
122
122
* Document[] - an aggregation pipeline.
123
123
*/
124
- update : UpdateFilter < Document > | Document [ ] ;
124
+ update : UpdateFilter < TSchema > | Document [ ] ;
125
125
/** A set of filters specifying to which array elements an update should apply. */
126
126
arrayFilters ?: Document [ ] ;
127
127
/** Specifies a collation. */
@@ -137,13 +137,42 @@ export interface ClientUpdateManyModel extends ClientWriteModel {
137
137
* to MongoClient#bulkWrite.
138
138
* @public
139
139
*/
140
- export type AnyClientBulkWriteModel =
141
- | ClientInsertOneModel
142
- | ClientReplaceOneModel
143
- | ClientUpdateOneModel
144
- | ClientUpdateManyModel
145
- | ClientDeleteOneModel
146
- | ClientDeleteManyModel ;
140
+ export type AnyClientBulkWriteModel < TSchema extends Document > =
141
+ | ClientInsertOneModel < TSchema >
142
+ | ClientReplaceOneModel < TSchema >
143
+ | ClientUpdateOneModel < TSchema >
144
+ | ClientUpdateManyModel < TSchema >
145
+ | ClientDeleteOneModel < TSchema >
146
+ | ClientDeleteManyModel < TSchema > ;
147
+
148
+ /**
149
+ * Take a Typescript type that maps namespaces to schema types.
150
+ * @public
151
+ *
152
+ * @example
153
+ * ```ts
154
+ * type MongoDBSchemas = {
155
+ * 'db.books': Book;
156
+ * 'db.authors': Author;
157
+ * }
158
+ *
159
+ * const model: ClientBulkWriteModel<MongoDBSchemas> = {
160
+ * namespace: 'db.books'
161
+ * name: 'insertOne',
162
+ * document: { title: 'Practical MongoDB Aggregations', authorName: 3 } // error `authorName` cannot be number
163
+ * };
164
+ * ```
165
+ *
166
+ * The type of the `namespace` field narrows other parts of the BulkWriteModel to use the correct schema for type assertions.
167
+ *
168
+ */
169
+ export type ClientBulkWriteModel <
170
+ SchemaMap extends Record < string , Document > = Record < string , Document >
171
+ > = {
172
+ [ Namespace in keyof SchemaMap ] : AnyClientBulkWriteModel < SchemaMap [ Namespace ] > & {
173
+ namespace : Namespace ;
174
+ } ;
175
+ } [ keyof SchemaMap ] ;
147
176
148
177
/** @public */
149
178
export interface ClientBulkWriteResult {
0 commit comments