index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.GridFSBucket = void 0;
  4. const error_1 = require("../error");
  5. const mongo_types_1 = require("../mongo_types");
  6. const write_concern_1 = require("../write_concern");
  7. const download_1 = require("./download");
  8. const upload_1 = require("./upload");
  9. const DEFAULT_GRIDFS_BUCKET_OPTIONS = {
  10. bucketName: 'fs',
  11. chunkSizeBytes: 255 * 1024
  12. };
  13. /**
  14. * Constructor for a streaming GridFS interface
  15. * @public
  16. */
  17. class GridFSBucket extends mongo_types_1.TypedEventEmitter {
  18. constructor(db, options) {
  19. super();
  20. this.setMaxListeners(0);
  21. const privateOptions = {
  22. ...DEFAULT_GRIDFS_BUCKET_OPTIONS,
  23. ...options,
  24. writeConcern: write_concern_1.WriteConcern.fromOptions(options)
  25. };
  26. this.s = {
  27. db,
  28. options: privateOptions,
  29. _chunksCollection: db.collection(privateOptions.bucketName + '.chunks'),
  30. _filesCollection: db.collection(privateOptions.bucketName + '.files'),
  31. checkedIndexes: false,
  32. calledOpenUploadStream: false
  33. };
  34. }
  35. /**
  36. * Returns a writable stream (GridFSBucketWriteStream) for writing
  37. * buffers to GridFS. The stream's 'id' property contains the resulting
  38. * file's id.
  39. *
  40. * @param filename - The value of the 'filename' key in the files doc
  41. * @param options - Optional settings.
  42. */
  43. openUploadStream(filename, options) {
  44. return new upload_1.GridFSBucketWriteStream(this, filename, options);
  45. }
  46. /**
  47. * Returns a writable stream (GridFSBucketWriteStream) for writing
  48. * buffers to GridFS for a custom file id. The stream's 'id' property contains the resulting
  49. * file's id.
  50. */
  51. openUploadStreamWithId(id, filename, options) {
  52. return new upload_1.GridFSBucketWriteStream(this, filename, { ...options, id });
  53. }
  54. /** Returns a readable stream (GridFSBucketReadStream) for streaming file data from GridFS. */
  55. openDownloadStream(id, options) {
  56. return new download_1.GridFSBucketReadStream(this.s._chunksCollection, this.s._filesCollection, this.s.options.readPreference, { _id: id }, options);
  57. }
  58. /**
  59. * Deletes a file with the given id
  60. *
  61. * @param id - The id of the file doc
  62. */
  63. async delete(id) {
  64. const { deletedCount } = await this.s._filesCollection.deleteOne({ _id: id });
  65. // Delete orphaned chunks before returning FileNotFound
  66. await this.s._chunksCollection.deleteMany({ files_id: id });
  67. if (deletedCount === 0) {
  68. // TODO(NODE-3483): Replace with more appropriate error
  69. // Consider creating new error MongoGridFSFileNotFoundError
  70. throw new error_1.MongoRuntimeError(`File not found for id ${id}`);
  71. }
  72. }
  73. /** Convenience wrapper around find on the files collection */
  74. find(filter = {}, options = {}) {
  75. return this.s._filesCollection.find(filter, options);
  76. }
  77. /**
  78. * Returns a readable stream (GridFSBucketReadStream) for streaming the
  79. * file with the given name from GridFS. If there are multiple files with
  80. * the same name, this will stream the most recent file with the given name
  81. * (as determined by the `uploadDate` field). You can set the `revision`
  82. * option to change this behavior.
  83. */
  84. openDownloadStreamByName(filename, options) {
  85. let sort = { uploadDate: -1 };
  86. let skip = undefined;
  87. if (options && options.revision != null) {
  88. if (options.revision >= 0) {
  89. sort = { uploadDate: 1 };
  90. skip = options.revision;
  91. }
  92. else {
  93. skip = -options.revision - 1;
  94. }
  95. }
  96. return new download_1.GridFSBucketReadStream(this.s._chunksCollection, this.s._filesCollection, this.s.options.readPreference, { filename }, { ...options, sort, skip });
  97. }
  98. /**
  99. * Renames the file with the given _id to the given string
  100. *
  101. * @param id - the id of the file to rename
  102. * @param filename - new name for the file
  103. */
  104. async rename(id, filename) {
  105. const filter = { _id: id };
  106. const update = { $set: { filename } };
  107. const { matchedCount } = await this.s._filesCollection.updateOne(filter, update);
  108. if (matchedCount === 0) {
  109. throw new error_1.MongoRuntimeError(`File with id ${id} not found`);
  110. }
  111. }
  112. /** Removes this bucket's files collection, followed by its chunks collection. */
  113. async drop() {
  114. await this.s._filesCollection.drop();
  115. await this.s._chunksCollection.drop();
  116. }
  117. }
  118. /**
  119. * When the first call to openUploadStream is made, the upload stream will
  120. * check to see if it needs to create the proper indexes on the chunks and
  121. * files collections. This event is fired either when 1) it determines that
  122. * no index creation is necessary, 2) when it successfully creates the
  123. * necessary indexes.
  124. * @event
  125. */
  126. GridFSBucket.INDEX = 'index';
  127. exports.GridFSBucket = GridFSBucket;
  128. //# sourceMappingURL=index.js.map