123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.GridFSBucket = void 0;
- const error_1 = require("../error");
- const mongo_types_1 = require("../mongo_types");
- const utils_1 = require("../utils");
- const write_concern_1 = require("../write_concern");
- const download_1 = require("./download");
- const upload_1 = require("./upload");
- const DEFAULT_GRIDFS_BUCKET_OPTIONS = {
- bucketName: 'fs',
- chunkSizeBytes: 255 * 1024
- };
- class GridFSBucket extends mongo_types_1.TypedEventEmitter {
- constructor(db, options) {
- super();
- this.setMaxListeners(0);
- const privateOptions = {
- ...DEFAULT_GRIDFS_BUCKET_OPTIONS,
- ...options,
- writeConcern: write_concern_1.WriteConcern.fromOptions(options)
- };
- this.s = {
- db,
- options: privateOptions,
- _chunksCollection: db.collection(privateOptions.bucketName + '.chunks'),
- _filesCollection: db.collection(privateOptions.bucketName + '.files'),
- checkedIndexes: false,
- calledOpenUploadStream: false
- };
- }
-
- openUploadStream(filename, options) {
- return new upload_1.GridFSBucketWriteStream(this, filename, options);
- }
-
- openUploadStreamWithId(id, filename, options) {
- return new upload_1.GridFSBucketWriteStream(this, filename, { ...options, id });
- }
-
- openDownloadStream(id, options) {
- return new download_1.GridFSBucketReadStream(this.s._chunksCollection, this.s._filesCollection, this.s.options.readPreference, { _id: id }, options);
- }
- delete(id, callback) {
- return (0, utils_1.maybePromise)(callback, callback => {
- return this.s._filesCollection.deleteOne({ _id: id }, (error, res) => {
- if (error) {
- return callback(error);
- }
- return this.s._chunksCollection.deleteMany({ files_id: id }, error => {
- if (error) {
- return callback(error);
- }
-
- if (!(res === null || res === void 0 ? void 0 : res.deletedCount)) {
-
-
- return callback(new error_1.MongoRuntimeError(`File not found for id ${id}`));
- }
- return callback();
- });
- });
- });
- }
-
- find(filter, options) {
- filter !== null && filter !== void 0 ? filter : (filter = {});
- options = options !== null && options !== void 0 ? options : {};
- return this.s._filesCollection.find(filter, options);
- }
-
- openDownloadStreamByName(filename, options) {
- let sort = { uploadDate: -1 };
- let skip = undefined;
- if (options && options.revision != null) {
- if (options.revision >= 0) {
- sort = { uploadDate: 1 };
- skip = options.revision;
- }
- else {
- skip = -options.revision - 1;
- }
- }
- return new download_1.GridFSBucketReadStream(this.s._chunksCollection, this.s._filesCollection, this.s.options.readPreference, { filename }, { ...options, sort, skip });
- }
- rename(id, filename, callback) {
- return (0, utils_1.maybePromise)(callback, callback => {
- const filter = { _id: id };
- const update = { $set: { filename } };
- return this.s._filesCollection.updateOne(filter, update, (error, res) => {
- if (error) {
- return callback(error);
- }
- if (!(res === null || res === void 0 ? void 0 : res.matchedCount)) {
- return callback(new error_1.MongoRuntimeError(`File with id ${id} not found`));
- }
- return callback();
- });
- });
- }
- drop(callback) {
- return (0, utils_1.maybePromise)(callback, callback => {
- return this.s._filesCollection.drop(error => {
- if (error) {
- return callback(error);
- }
- return this.s._chunksCollection.drop(error => {
- if (error) {
- return callback(error);
- }
- return callback();
- });
- });
- });
- }
-
- getLogger() {
- return this.s.db.s.logger;
- }
- }
- exports.GridFSBucket = GridFSBucket;
- GridFSBucket.INDEX = 'index';
|