* Deletes a file with the given id * * @param id - The id of the file doc
(id: ObjectId, options?: { timeoutMS: number })
| 160 | * @param id - The id of the file doc |
| 161 | */ |
| 162 | async delete(id: ObjectId, options?: { timeoutMS: number }): Promise<void> { |
| 163 | const { timeoutMS } = resolveOptions(this.s.db, options); |
| 164 | let timeoutContext: CSOTTimeoutContext | undefined = undefined; |
| 165 | |
| 166 | if (timeoutMS) { |
| 167 | timeoutContext = new CSOTTimeoutContext({ |
| 168 | timeoutMS, |
| 169 | serverSelectionTimeoutMS: this.s.db.client.s.options.serverSelectionTimeoutMS |
| 170 | }); |
| 171 | } |
| 172 | |
| 173 | const { deletedCount } = await this.s._filesCollection.deleteOne( |
| 174 | { _id: id }, |
| 175 | { timeoutMS: timeoutContext?.remainingTimeMS } |
| 176 | ); |
| 177 | |
| 178 | const remainingTimeMS = timeoutContext?.remainingTimeMS; |
| 179 | if (remainingTimeMS != null && remainingTimeMS <= 0) |
| 180 | throw new MongoOperationTimeoutError(`Timed out after ${timeoutMS}ms`); |
| 181 | // Delete orphaned chunks before returning FileNotFound |
| 182 | await this.s._chunksCollection.deleteMany({ files_id: id }, { timeoutMS: remainingTimeMS }); |
| 183 | |
| 184 | if (deletedCount === 0) { |
| 185 | // TODO(NODE-3483): Replace with more appropriate error |
| 186 | // Consider creating new error MongoGridFSFileNotFoundError |
| 187 | throw new MongoRuntimeError(`File not found for id ${id}`); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /** Convenience wrapper around find on the files collection */ |
| 192 | find(filter: Filter<GridFSFile> = {}, options: FindOptions = {}): FindCursor<GridFSFile> { |