* @param {MongoError} error An error instance representing the error during the execution.
* @param {object} result The result from the callback.
*/
/**
* Retrieves a single character from this file.
*
* @method
* @param {GridStore~resultCallback} [callback] this gets called after this method is executed. Passes null to the first parameter and the character read to the second or null to the second if the read/write head is at the end of the file.
* @return {Promise} returns Promise if no callback passed
* Writes a string to the file with a newline character appended at the end if
* the given string does not have one.
*
* @method
* @param {string} string the string to write.
* @param {GridStore~resultCallback} [callback] this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object.
* @return {Promise} returns Promise if no callback passed
* Writes some data. This method will work properly only if initialized with mode "w" or "w+".
*
* @method
* @param {(string|Buffer)} data the data to write.
* @param {boolean} [close] closes this file after writing if set to true.
* @param {GridStore~resultCallback} [callback] this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object.
* @return {Promise} returns Promise if no callback passed
* @deprecated Use GridFSBucket API instead
*/
GridStore.prototype.write = function write(data, close, callback) {
* Stores a file from the file system to the GridFS database.
*
* @method
* @param {(string|Buffer|FileHandle)} file the file to store.
* @param {GridStore~resultCallback} [callback] this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object.
* @return {Promise} returns Promise if no callback passed
* @deprecated Use GridFSBucket API instead
*/
GridStore.prototype.writeFile = function (file, callback) {
* Saves this file to the database. This will overwrite the old entry if it
* already exists. This will work properly only if mode was initialized to
* "w" or "w+".
*
* @method
* @param {GridStore~resultCallback} [callback] this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object.
* @return {Promise} returns Promise if no callback passed
* Deletes all the chunks of this file in the database if mode was set to "w" or
* "w+" and resets the read/write head to the initial position.
*
* @method
* @param {GridStore~resultCallback} [callback] this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object.
* @return {Promise} returns Promise if no callback passed
* @param {MongoError} error An error instance representing the error during the execution.
* @param {Buffer} data The data read from the GridStore object
*/
/**
* Retrieves the contents of this file and advances the read/write head. Works with Buffers only.
*
* There are 3 signatures for this method:
*
* (callback)
* (length, callback)
* (length, buffer, callback)
*
* @method
* @param {number} [length] the number of characters to read. Reads all the characters from the read/write head to the EOF if not specified.
* @param {(string|Buffer)} [buffer] a string to hold temporary data. This is used for storing the string data read so far when recursively calling this method.
* @param {GridStore~readCallback} [callback] the command callback.
* @return {Promise} returns Promise if no callback passed
* @param {MongoError} error An error instance representing the error during the execution.
* @param {number} position The current read position in the GridStore.
*/
/**
* Retrieves the position of the read/write head of this file.
*
* @method
* @param {number} [length] the number of characters to read. Reads all the characters from the read/write head to the EOF if not specified.
* @param {(string|Buffer)} [buffer] a string to hold temporary data. This is used for storing the string data read so far when recursively calling this method.
* @param {GridStore~tellCallback} [callback] the command callback.
* @return {Promise} returns Promise if no callback passed
var txtId = self.fileId._bsontype == "ObjectID" ? self.fileId.toHexString() : self.fileId;
return error(MongoError.create({message: f("file with id %s not opened for writing", (self.referenceBy == REFERENCE_BY_ID ? txtId : self.filename)), driver:true}), self);
}
// Process the mode of the object
if(self.mode == "r") {
nthChunk(self, 0, options, function(err, chunk) {
if(err) return error(err);
self.currentChunk = chunk;
self.position = 0;
callback(null, self);
});
} else if(self.mode == "w" && doc) {
// Delete any existing chunks
deleteChunks(self, options, function(err) {
if(err) return error(err);
self.currentChunk = new Chunk(self, {'n':0}, self.writeConcern);
var writeBuffer = function(self, buffer, close, callback) {
if(typeof close === "function") { callback = close; close = null; }
var finalClose = typeof close == 'boolean' ? close : false;
if(self.mode != "w") {
callback(MongoError.create({message: f("file with id %s not opened for writing", (self.referenceBy == REFERENCE_BY_ID ? self.referenceBy : self.filename)), driver:true}), null);
* Seek mode where the given length is an offset to the current read/write head.
*
* @classconstant IO_SEEK_CUR
**/
GridStore.IO_SEEK_CUR = 1;
/**
* Seek mode where the given length is an offset to the end of the file.
*
* @classconstant IO_SEEK_END
**/
GridStore.IO_SEEK_END = 2;
/**
* Checks if a file exists in the database.
*
* @method
* @static
* @param {Db} db the database to query.
* @param {string} name The name of the file to look for.
* @param {string} [rootCollection] The root collection that holds the files and chunks collection. Defaults to **{GridStore.DEFAULT_ROOT_COLLECTION}**.
* @param {string} [rootCollection] The root collection that holds the files and chunks collection. Defaults to **{GridStore.DEFAULT_ROOT_COLLECTION}**.
// Ensure we don't have an invalid combination of write concerns
if(finalOptions.w < 1
&& (finalOptions.journal == true || finalOptions.j == true || finalOptions.fsync == true)) throw MongoError.create({message: "No acknowledgement using w < 1 cannot be combined with journal:true or fsync:true", driver:true});
// Return the options
return finalOptions;
}
/**
* Create a new GridStoreStream instance (INTERNAL TYPE, do not instantiate directly)
*
* @class
* @extends external:Duplex
* @return {GridStoreStream} a GridStoreStream instance.
* The read() method pulls some data out of the internal buffer and returns it. If there is no data available, then it will return null.
* @function external:Duplex#read
* @param {number} size Optional argument to specify how much data to read.
* @return {(String | Buffer | null)}
*/
/**
* Call this function to cause the stream to return strings of the specified encoding instead of Buffer objects.
* @function external:Duplex#setEncoding
* @param {string} encoding The encoding to use.
* @return {null}
*/
/**
* This method will cause the readable stream to resume emitting data events.
* @function external:Duplex#resume
* @return {null}
*/
/**
* This method will cause a stream in flowing-mode to stop emitting data events. Any data that becomes available will remain in the internal buffer.
* @function external:Duplex#pause
* @return {null}
*/
/**
* This method pulls all the data out of a readable stream, and writes it to the supplied destination, automatically managing the flow so that the destination is not overwhelmed by a fast readable stream.
* @function external:Duplex#pipe
* @param {Writable} destination The destination for writing data
* @param {object} [options] Pipe options
* @return {null}
*/
/**
* This method will remove the hooks set up for a previous pipe() call.
* @function external:Duplex#unpipe
* @param {Writable} [destination] The destination for writing data
* @return {null}
*/
/**
* This is useful in certain cases where a stream is being consumed by a parser, which needs to "un-consume" some data that it has optimistically pulled out of the source, so that the stream can be passed on to some other party.
* @function external:Duplex#unshift
* @param {(Buffer|string)} chunk Chunk of data to unshift onto the read queue.
* @return {null}
*/
/**
* Versions of Node prior to v0.10 had streams that did not implement the entire Streams API as it is today. (See "Compatibility" below for more information.)
* @function external:Duplex#wrap
* @param {Stream} stream An "old style" readable stream.
* @return {null}
*/
/**
* This method writes some data to the underlying system, and calls the supplied callback once the data has been fully handled.
* @function external:Duplex#write
* @param {(string|Buffer)} chunk The data to write
* @param {string} encoding The encoding, if chunk is a String
* @param {function} callback Callback for when this chunk of data is flushed
* @return {boolean}
*/
/**
* Call this method when no more data will be written to the stream. If supplied, the callback is attached as a listener on the finish event.
* @function external:Duplex#end
* @param {(string|Buffer)} chunk The data to write
* @param {string} encoding The encoding, if chunk is a String
* @param {function} callback Callback for when this chunk of data is flushed
* @return {null}
*/
/**
* GridStoreStream stream data event, fired for each document in the cursor.