|
| 1 | +import { expectAssignable, expectError, expectNotAssignable, expectType } from 'tsd'; |
| 2 | + |
| 3 | +import { |
| 4 | + type ClientBulkWriteModel, |
| 5 | + type ClientDeleteManyModel, |
| 6 | + type ClientDeleteOneModel, |
| 7 | + type ClientInsertOneModel, |
| 8 | + type ClientReplaceOneModel, |
| 9 | + type ClientUpdateManyModel, |
| 10 | + type ClientUpdateOneModel, |
| 11 | + type Document, |
| 12 | + type Filter, |
| 13 | + type MongoClient, |
| 14 | + type OptionalId, |
| 15 | + type UpdateFilter, |
| 16 | + type UUID, |
| 17 | + type WithoutId |
| 18 | +} from '../mongodb'; |
| 19 | + |
| 20 | +declare const client: MongoClient; |
| 21 | +type Book = { title: string; released: Date }; |
| 22 | +type Author = { name: string; published: number }; |
| 23 | +type Store = { _id: UUID }; |
| 24 | + |
| 25 | +// Baseline check that schema modifies the following fields for each type. |
| 26 | +declare const clientInsertOneModel: ClientInsertOneModel<Book>; |
| 27 | +expectType<OptionalId<Book>>(clientInsertOneModel.document); |
| 28 | + |
| 29 | +declare const clientReplaceOneModel: ClientReplaceOneModel<Book>; |
| 30 | +expectType<Filter<Book>>(clientReplaceOneModel.filter); |
| 31 | +expectType<WithoutId<Book>>(clientReplaceOneModel.replacement); |
| 32 | + |
| 33 | +declare const clientUpdateOneModel: ClientUpdateOneModel<Book>; |
| 34 | +expectType<Filter<Book>>(clientUpdateOneModel.filter); |
| 35 | +expectType<UpdateFilter<Book> | Document[]>(clientUpdateOneModel.update); |
| 36 | + |
| 37 | +declare const clientUpdateManyModel: ClientUpdateManyModel<Book>; |
| 38 | +expectType<Filter<Book>>(clientUpdateManyModel.filter); |
| 39 | +expectType<UpdateFilter<Book> | Document[]>(clientUpdateManyModel.update); |
| 40 | + |
| 41 | +declare const clientDeleteOneModel: ClientDeleteOneModel<Book>; |
| 42 | +expectType<Filter<Book>>(clientDeleteOneModel.filter); |
| 43 | + |
| 44 | +declare const clientDeleteManyModel: ClientDeleteManyModel<Book>; |
| 45 | +expectType<Filter<Book>>(clientDeleteManyModel.filter); |
| 46 | + |
| 47 | +client.bulkWrite([]); // empty should always work |
| 48 | + |
| 49 | +// No schemas - all correct use |
| 50 | +client.bulkWrite([ |
| 51 | + { |
| 52 | + namespace: 'db.authors', |
| 53 | + name: 'insertOne', |
| 54 | + document: { name: 'bob', published: 2 } |
| 55 | + }, |
| 56 | + { |
| 57 | + namespace: 'db.authors', |
| 58 | + name: 'replaceOne', |
| 59 | + filter: { name: 'bob' }, |
| 60 | + replacement: { name: 'ann', published: 2 } |
| 61 | + }, |
| 62 | + { |
| 63 | + namespace: 'db.authors', |
| 64 | + name: 'updateOne', |
| 65 | + filter: { name: 'bob', published: 2 }, |
| 66 | + update: {} |
| 67 | + }, |
| 68 | + { |
| 69 | + namespace: 'db.authors', |
| 70 | + name: 'updateMany', |
| 71 | + filter: { name: 'bob', published: 2 }, |
| 72 | + update: {} |
| 73 | + }, |
| 74 | + { namespace: 'db.authors', name: 'deleteOne', filter: {} }, |
| 75 | + { namespace: 'db.authors', name: 'deleteMany', filter: {} } |
| 76 | +]); |
| 77 | + |
| 78 | +// No schemas - random namespaces, no type checking |
| 79 | +client.bulkWrite([ |
| 80 | + { |
| 81 | + namespace: 'db.whatever', |
| 82 | + name: 'insertOne', |
| 83 | + document: { name: 'bob', randomKey: 2 } |
| 84 | + }, |
| 85 | + { |
| 86 | + namespace: 'db.change', |
| 87 | + name: 'replaceOne', |
| 88 | + filter: { name: 'bob' }, |
| 89 | + replacement: { name: 2, published: 2 } |
| 90 | + }, |
| 91 | + { |
| 92 | + namespace: 'db.it', |
| 93 | + name: 'updateOne', |
| 94 | + filter: { name: 'bob', published: new Date() }, |
| 95 | + update: {} |
| 96 | + }, |
| 97 | + { |
| 98 | + namespace: 'db.up', |
| 99 | + name: 'updateMany', |
| 100 | + filter: { name: 'bob', published: 2 }, |
| 101 | + update: {} |
| 102 | + }, |
| 103 | + { namespace: 'db.random', name: 'deleteOne', filter: {} }, |
| 104 | + { namespace: 'db.namespace', name: 'deleteMany', filter: {} } |
| 105 | +]); |
| 106 | + |
| 107 | +// Operation names are still type checked when there is no schema |
| 108 | +expectError<ClientBulkWriteModel>({ |
| 109 | + namespace: 'db.author', |
| 110 | + name: 'insertLots', // Not an operation we support |
| 111 | + document: { name: 'bob', published: 2 } |
| 112 | +}); |
| 113 | + |
| 114 | +type MongoDBSchemas = { |
| 115 | + 'db.books': Book; |
| 116 | + 'db.authors': Author; |
| 117 | + 'db.stores': Store; |
| 118 | +}; |
| 119 | + |
| 120 | +expectError<ClientBulkWriteModel<MongoDBSchemas>>({ |
| 121 | + namespace: 'db.author', // Unknown namespace! a typo! |
| 122 | + name: 'insertOne', |
| 123 | + document: { name: 'bob', published: 2 } |
| 124 | +}); |
| 125 | + |
| 126 | +expectError<ClientBulkWriteModel<MongoDBSchemas>>({ |
| 127 | + namespace: 'db.authors', |
| 128 | + name: 'insertOne', |
| 129 | + document: { name: 'bob', published: '' } // Incorrect type for known field |
| 130 | +}); |
| 131 | + |
| 132 | +expectError<ClientBulkWriteModel<MongoDBSchemas>>({ |
| 133 | + namespace: 'db.authors', |
| 134 | + name: 'insertOne', |
| 135 | + document: { name: 'bob', publish: 2 } // unknown field! typo! |
| 136 | +}); |
| 137 | + |
| 138 | +// Defined schemas - all correct use |
| 139 | +client.bulkWrite<MongoDBSchemas>([ |
| 140 | + { |
| 141 | + namespace: 'db.authors', |
| 142 | + name: 'insertOne', |
| 143 | + document: { name: 'bob', published: 2 } |
| 144 | + }, |
| 145 | + { |
| 146 | + namespace: 'db.authors', |
| 147 | + name: 'replaceOne', |
| 148 | + filter: { name: 'bob' }, |
| 149 | + replacement: { name: 'ann', published: 2 } |
| 150 | + }, |
| 151 | + { |
| 152 | + namespace: 'db.authors', |
| 153 | + name: 'updateOne', |
| 154 | + filter: { name: 'bob', published: 2 }, |
| 155 | + update: {} |
| 156 | + }, |
| 157 | + { |
| 158 | + namespace: 'db.authors', |
| 159 | + name: 'updateMany', |
| 160 | + filter: { name: 'bob', published: 2 }, |
| 161 | + update: {} |
| 162 | + }, |
| 163 | + { namespace: 'db.authors', name: 'deleteOne', filter: {} }, |
| 164 | + { namespace: 'db.authors', name: 'deleteMany', filter: {} } |
| 165 | +]); |
| 166 | + |
| 167 | +// Defined schemas - incorrect use |
| 168 | +expectError( |
| 169 | + client.bulkWrite<MongoDBSchemas>([ |
| 170 | + { |
| 171 | + namespace: 'db.authors', |
| 172 | + name: 'insertOne', |
| 173 | + document: { name: 'bob', published: '' } // wrong type |
| 174 | + } |
| 175 | + ]) |
| 176 | +); |
| 177 | + |
| 178 | +expectError( |
| 179 | + client.bulkWrite<MongoDBSchemas>([ |
| 180 | + { |
| 181 | + namespace: 'db.authors', |
| 182 | + name: 'replaceOne', |
| 183 | + filter: { name: 'bob' }, |
| 184 | + replacement: { name: 'ann', publish: 2 } // key typo |
| 185 | + } |
| 186 | + ]) |
| 187 | +); |
| 188 | + |
| 189 | +expectError( |
| 190 | + client.bulkWrite<MongoDBSchemas>([ |
| 191 | + { |
| 192 | + namespace: 'db.blah', // unknown namespace |
| 193 | + name: 'updateOne', |
| 194 | + filter: { name: 'bob', published: 2 }, |
| 195 | + update: {} |
| 196 | + } |
| 197 | + ]) |
| 198 | +); |
| 199 | + |
| 200 | +expectError( |
| 201 | + client.bulkWrite<MongoDBSchemas>([ |
| 202 | + { |
| 203 | + namespace: 'db.authors', |
| 204 | + name: 'updateManyy', // unknown operation |
| 205 | + filter: { name: 'bob', published: 2 }, |
| 206 | + update: {} |
| 207 | + } |
| 208 | + ]) |
| 209 | +); |
| 210 | + |
| 211 | +type MongoDBSchemasWithCalculations = { |
| 212 | + // River Books uses star ratings |
| 213 | + [key: `river-books.${string}`]: Book & { fiveStarRatings: number }; |
| 214 | + // Ocean literature uses thumbs up for ratings |
| 215 | + [key: `ocean-literature.${string}`]: Book & { thumbsUp: number }; |
| 216 | +}; |
| 217 | + |
| 218 | +// correct use |
| 219 | +client.bulkWrite<MongoDBSchemasWithCalculations>([ |
| 220 | + { |
| 221 | + namespace: 'river-books.store0', |
| 222 | + name: 'insertOne', |
| 223 | + document: { title: 'abc', released: new Date(), fiveStarRatings: 10 } |
| 224 | + }, |
| 225 | + { |
| 226 | + namespace: 'ocean-literature.store0', |
| 227 | + name: 'insertOne', |
| 228 | + document: { title: 'abc', released: new Date(), thumbsUp: 10 } |
| 229 | + } |
| 230 | +]); |
| 231 | + |
| 232 | +// prevented from changing each store's rating system! |
| 233 | +expectError( |
| 234 | + client.bulkWrite<MongoDBSchemasWithCalculations>([ |
| 235 | + { |
| 236 | + namespace: 'river-books.store0', |
| 237 | + name: 'insertOne', |
| 238 | + document: { title: 'abc', released: new Date(), thumbsUp: 10 } |
| 239 | + }, |
| 240 | + { |
| 241 | + namespace: 'ocean-literature.store0', |
| 242 | + name: 'insertOne', |
| 243 | + document: { title: 'abc', released: new Date(), fiveStarRatings: 10 } |
| 244 | + } |
| 245 | + ]) |
| 246 | +); |
| 247 | + |
| 248 | +// Example partial use case: |
| 249 | +// I want to make sure I don't mess up any namespaces but I don't want to define schemas: |
| 250 | + |
| 251 | +type MongoDBNamespaces = 'db.books' | 'db.authors' | 'db.stores'; |
| 252 | + |
| 253 | +client.bulkWrite<{ [K in MongoDBNamespaces]: Document }>([ |
| 254 | + { |
| 255 | + namespace: 'db.books', |
| 256 | + name: 'insertOne', |
| 257 | + document: { title: 'abc', released: 32n, blah_blah: 10 } // wrong type for released does not error |
| 258 | + }, |
| 259 | + { |
| 260 | + namespace: 'db.authors', |
| 261 | + name: 'insertOne', |
| 262 | + document: { title: 'abc', released: 'yesterday', fiveStarRatings: 10 } |
| 263 | + } |
| 264 | +]); |
| 265 | + |
| 266 | +expectError( |
| 267 | + client.bulkWrite<{ [K in MongoDBNamespaces]: Document }>([ |
| 268 | + { |
| 269 | + namespace: 'db.wrongNS', |
| 270 | + name: 'insertOne', |
| 271 | + document: { title: 'abc', released: new Date(), thumbsUp: 10 } |
| 272 | + } |
| 273 | + ]) |
| 274 | +); |
0 commit comments