index.js 6.1 KB

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