@@ -27,55 +27,62 @@ export interface ClientBulkWriteOptions extends CommandOperationOptions {
27
27
28
28
/** @public */
29
29
export interface ClientWriteModel {
30
- /** The namespace for the write. */
30
+ /**
31
+ * The namespace for the write.
32
+ *
33
+ * A namespace is a combination of the database name and the name of the collection: `<database-name>.<collection>`.
34
+ * All documents belong to a namespace.
35
+ *
36
+ * @see https://www.mongodb.com/docs/manual/reference/limits/#std-label-faq-dev-namespace
37
+ */
31
38
namespace : string ;
32
39
}
33
40
34
41
/** @public */
35
- export interface ClientInsertOneModel extends ClientWriteModel {
42
+ export interface ClientInsertOneModel < TSchema > extends ClientWriteModel {
36
43
name : 'insertOne' ;
37
44
/** The document to insert. */
38
- document : OptionalId < Document > ;
45
+ document : OptionalId < TSchema > ;
39
46
}
40
47
41
48
/** @public */
42
- export interface ClientDeleteOneModel extends ClientWriteModel {
49
+ export interface ClientDeleteOneModel < TSchema > extends ClientWriteModel {
43
50
name : 'deleteOne' ;
44
51
/**
45
52
* The filter used to determine if a document should be deleted.
46
53
* For a deleteOne operation, the first match is removed.
47
54
*/
48
- filter : Filter < Document > ;
55
+ filter : Filter < TSchema > ;
49
56
/** Specifies a collation. */
50
57
collation ?: CollationOptions ;
51
58
/** The index to use. If specified, then the query system will only consider plans using the hinted index. */
52
59
hint ?: Hint ;
53
60
}
54
61
55
62
/** @public */
56
- export interface ClientDeleteManyModel extends ClientWriteModel {
63
+ export interface ClientDeleteManyModel < TSchema > extends ClientWriteModel {
57
64
name : 'deleteMany' ;
58
65
/**
59
66
* The filter used to determine if a document should be deleted.
60
67
* For a deleteMany operation, all matches are removed.
61
68
*/
62
- filter : Filter < Document > ;
69
+ filter : Filter < TSchema > ;
63
70
/** Specifies a collation. */
64
71
collation ?: CollationOptions ;
65
72
/** The index to use. If specified, then the query system will only consider plans using the hinted index. */
66
73
hint ?: Hint ;
67
74
}
68
75
69
76
/** @public */
70
- export interface ClientReplaceOneModel extends ClientWriteModel {
77
+ export interface ClientReplaceOneModel < TSchema > extends ClientWriteModel {
71
78
name : 'replaceOne' ;
72
79
/**
73
80
* The filter used to determine if a document should be replaced.
74
81
* For a replaceOne operation, the first match is replaced.
75
82
*/
76
- filter : Filter < Document > ;
83
+ filter : Filter < TSchema > ;
77
84
/** The document with which to replace the matched document. */
78
- replacement : WithoutId < Document > ;
85
+ replacement : WithoutId < TSchema > ;
79
86
/** Specifies a collation. */
80
87
collation ?: CollationOptions ;
81
88
/** The index to use. If specified, then the query system will only consider plans using the hinted index. */
@@ -85,19 +92,19 @@ export interface ClientReplaceOneModel extends ClientWriteModel {
85
92
}
86
93
87
94
/** @public */
88
- export interface ClientUpdateOneModel extends ClientWriteModel {
95
+ export interface ClientUpdateOneModel < TSchema > extends ClientWriteModel {
89
96
name : 'updateOne' ;
90
97
/**
91
98
* The filter used to determine if a document should be updated.
92
99
* For an updateOne operation, the first match is updated.
93
100
*/
94
- filter : Filter < Document > ;
101
+ filter : Filter < TSchema > ;
95
102
/**
96
103
* The modifications to apply. The value can be either:
97
104
* UpdateFilter<Document> - A document that contains update operator expressions,
98
105
* Document[] - an aggregation pipeline.
99
106
*/
100
- update : UpdateFilter < Document > | Document [ ] ;
107
+ update : UpdateFilter < TSchema > | Document [ ] ;
101
108
/** A set of filters specifying to which array elements an update should apply. */
102
109
arrayFilters ?: Document [ ] ;
103
110
/** Specifies a collation. */
@@ -109,19 +116,19 @@ export interface ClientUpdateOneModel extends ClientWriteModel {
109
116
}
110
117
111
118
/** @public */
112
- export interface ClientUpdateManyModel extends ClientWriteModel {
119
+ export interface ClientUpdateManyModel < TSchema > extends ClientWriteModel {
113
120
name : 'updateMany' ;
114
121
/**
115
122
* The filter used to determine if a document should be updated.
116
123
* For an updateMany operation, all matches are updated.
117
124
*/
118
- filter : Filter < Document > ;
125
+ filter : Filter < TSchema > ;
119
126
/**
120
127
* The modifications to apply. The value can be either:
121
128
* UpdateFilter<Document> - A document that contains update operator expressions,
122
129
* Document[] - an aggregation pipeline.
123
130
*/
124
- update : UpdateFilter < Document > | Document [ ] ;
131
+ update : UpdateFilter < TSchema > | Document [ ] ;
125
132
/** A set of filters specifying to which array elements an update should apply. */
126
133
arrayFilters ?: Document [ ] ;
127
134
/** Specifies a collation. */
@@ -137,48 +144,81 @@ export interface ClientUpdateManyModel extends ClientWriteModel {
137
144
* to MongoClient#bulkWrite.
138
145
* @public
139
146
*/
140
- export type AnyClientBulkWriteModel =
141
- | ClientInsertOneModel
142
- | ClientReplaceOneModel
143
- | ClientUpdateOneModel
144
- | ClientUpdateManyModel
145
- | ClientDeleteOneModel
146
- | ClientDeleteManyModel ;
147
+ export type AnyClientBulkWriteModel < TSchema extends Document > =
148
+ | ClientInsertOneModel < TSchema >
149
+ | ClientReplaceOneModel < TSchema >
150
+ | ClientUpdateOneModel < TSchema >
151
+ | ClientUpdateManyModel < TSchema >
152
+ | ClientDeleteOneModel < TSchema >
153
+ | ClientDeleteManyModel < TSchema > ;
154
+
155
+ /**
156
+ * A mapping of namespace strings to collections schemas.
157
+ * @public
158
+ *
159
+ * @example
160
+ * ```ts
161
+ * type MongoDBSchemas = {
162
+ * 'db.books': Book;
163
+ * 'db.authors': Author;
164
+ * }
165
+ *
166
+ * const model: ClientBulkWriteModel<MongoDBSchemas> = {
167
+ * namespace: 'db.books'
168
+ * name: 'insertOne',
169
+ * document: { title: 'Practical MongoDB Aggregations', authorName: 3 } // error `authorName` cannot be number
170
+ * };
171
+ * ```
172
+ *
173
+ * The type of the `namespace` field narrows other parts of the BulkWriteModel to use the correct schema for type assertions.
174
+ *
175
+ */
176
+ export type ClientBulkWriteModel <
177
+ SchemaMap extends Record < string , Document > = Record < string , Document >
178
+ > = {
179
+ [ Namespace in keyof SchemaMap ] : AnyClientBulkWriteModel < SchemaMap [ Namespace ] > & {
180
+ namespace : Namespace ;
181
+ } ;
182
+ } [ keyof SchemaMap ] ;
147
183
148
184
/** @public */
149
185
export interface ClientBulkWriteResult {
186
+ /**
187
+ * Whether the bulk write was acknowledged.
188
+ */
189
+ readonly acknowledged : boolean ;
150
190
/**
151
191
* The total number of documents inserted across all insert operations.
152
192
*/
153
- insertedCount : number ;
193
+ readonly insertedCount : number ;
154
194
/**
155
195
* The total number of documents upserted across all update operations.
156
196
*/
157
- upsertedCount : number ;
197
+ readonly upsertedCount : number ;
158
198
/**
159
199
* The total number of documents matched across all update operations.
160
200
*/
161
- matchedCount : number ;
201
+ readonly matchedCount : number ;
162
202
/**
163
203
* The total number of documents modified across all update operations.
164
204
*/
165
- modifiedCount : number ;
205
+ readonly modifiedCount : number ;
166
206
/**
167
207
* The total number of documents deleted across all delete operations.
168
208
*/
169
- deletedCount : number ;
209
+ readonly deletedCount : number ;
170
210
/**
171
211
* The results of each individual insert operation that was successfully performed.
172
212
*/
173
- insertResults ?: Map < number , ClientInsertOneResult > ;
213
+ readonly insertResults ?: ReadonlyMap < number , ClientInsertOneResult > ;
174
214
/**
175
215
* The results of each individual update operation that was successfully performed.
176
216
*/
177
- updateResults ?: Map < number , ClientUpdateResult > ;
217
+ readonly updateResults ?: ReadonlyMap < number , ClientUpdateResult > ;
178
218
/**
179
219
* The results of each individual delete operation that was successfully performed.
180
220
*/
181
- deleteResults ?: Map < number , ClientDeleteResult > ;
221
+ readonly deleteResults ?: ReadonlyMap < number , ClientDeleteResult > ;
182
222
}
183
223
184
224
/** @public */
0 commit comments