Class: GridStore

GridStore

new GridStore(db, id, filename, mode, options){GridStore}

Create a new GridStore instance

Modes

  • "r" - read only. This is the default mode.
  • "w" - write in truncate mode. Existing data will be overwriten.
Name Type Default Description
db Db

A database instance to interact with.

id object optional

optional unique id for this file

filename string optional

optional filename for this file, no unique constrain on the field

mode string

set the mode for this file.

options object null optional

Optional settings.

Name Type Default Description
w number | string null optional

The write concern.

wtimeout number null optional

The write concern timeout.

j boolean false optional

Specify a journal write concern.

fsync boolean false optional

Specify a file sync write concern.

root string null optional

Root collection to use. Defaults to {GridStore.DEFAULT_ROOT_COLLECTION}.

content_type string null optional

MIME type of the file. Defaults to {GridStore.DEFAULT_CONTENT_TYPE}.

chunk_size number 261120 optional

Size for the chunk. Defaults to {Chunk.DEFAULT_CHUNK_SIZE}.

metadata object null optional

Arbitrary data the user wants to store.

promiseLibrary object null optional

A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible

readPreference ReadPreference | string null optional

The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).

Properties:
Name Type Description
chunkSize number

Get the gridstore chunk size.

md5 number

The md5 checksum for this file.

chunkNumber number

The current chunk number the gridstore has materialized into memory

Deprecated
  • Use GridFSBucket API instead
    Returns:
    GridStore instance.

    Members

    staticGridStore.DEFAULT_CONTENT_TYPE

    Default file mime type

    staticGridStore.DEFAULT_ROOT_COLLECTION

    The collection to be used for holding the files and chunks collection.

    staticGridStore.IO_SEEK_CUR

    Seek mode where the given length is an offset to the current read/write head.

    staticGridStore.IO_SEEK_END

    Seek mode where the given length is an offset to the end of the file.

    staticGridStore.IO_SEEK_SET

    Seek mode where the given length is absolute.

    Methods

    staticGridStore.exist(db, name, rootCollection, options, callback){Promise}

    Checks if a file exists in the database.

    Name Type Default Description
    db Db

    the database to query.

    name string

    The name of the file to look for.

    rootCollection string optional

    The root collection that holds the files and chunks collection. Defaults to {GridStore.DEFAULT_ROOT_COLLECTION}.

    options object null optional

    Optional settings.

    Name Type Default Description
    readPreference ReadPreference | string null optional

    The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).

    promiseLibrary object null optional

    A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible

    session ClientSession optional

    optional session to use for this operation

    callback GridStore~resultCallback optional

    result from exists.

    Deprecated
    • Use GridFSBucket API instead
      Returns:
      Promise if no callback passed

      staticGridStore.list(db, rootCollection, options, callback){Promise}

      Gets the list of files stored in the GridFS.

      Name Type Default Description
      db Db

      the database to query.

      rootCollection string optional

      The root collection that holds the files and chunks collection. Defaults to {GridStore.DEFAULT_ROOT_COLLECTION}.

      options object null optional

      Optional settings.

      Name Type Default Description
      readPreference ReadPreference | string null optional

      The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).

      promiseLibrary object null optional

      A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible

      session ClientSession optional

      optional session to use for this operation

      callback GridStore~resultCallback optional

      result from exists.

      Deprecated
      • Use GridFSBucket API instead
        Returns:
        Promise if no callback passed

        staticGridStore.read(db, name, length, offset, options, callback){Promise}

        Reads the contents of a file.

        This method has the following signatures

        (db, name, callback)
        (db, name, length, callback)
        (db, name, length, offset, callback)
        (db, name, length, offset, options, callback)

        Name Type Default Description
        db Db

        the database to query.

        name string

        The name of the file.

        length number optional

        The size of data to read.

        offset number optional

        The offset from the head of the file of which to start reading from.

        options object null optional

        Optional settings.

        Name Type Default Description
        readPreference ReadPreference | string null optional

        The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).

        promiseLibrary object null optional

        A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible

        session ClientSession optional

        optional session to use for this operation

        callback GridStore~readCallback optional

        the command callback.

        Deprecated
        • Use GridFSBucket API instead
          Returns:
          Promise if no callback passed

          staticGridStore.readlines(db, name, separator, options, callback){Promise}

          Read the entire file as a list of strings splitting by the provided separator.

          Name Type Default Description
          db Db

          the database to query.

          name String | object

          the name of the file.

          separator string optional

          The character to be recognized as the newline separator.

          options object null optional

          Optional settings.

          Name Type Default Description
          readPreference ReadPreference | string null optional

          The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).

          promiseLibrary object null optional

          A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible

          session ClientSession optional

          optional session to use for this operation

          callback GridStore~readlinesCallback optional

          the command callback.

          Deprecated
          • Use GridFSBucket API instead
            Returns:
            Promise if no callback passed

            Deletes the chunks and metadata information of a file from GridFS.

            Name Type Default Description
            db Db

            The database to query.

            names string | array

            The name/names of the files to delete.

            options object null optional

            Optional settings.

            Name Type Default Description
            promiseLibrary object null optional

            A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible

            session ClientSession optional

            optional session to use for this operation

            callback GridStore~resultCallback optional

            the command callback.

            Deprecated
            • Use GridFSBucket API instead
              Returns:
              Promise if no callback passed

              chunkCollection(callback){Collection}

              Retrieve this file's chunks collection.

              Name Type Description
              callback GridStore~collectionCallback

              the command callback.

              Deprecated
              • Use GridFSBucket API instead

                close(options, callback){Promise}

                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+".

                Name Type Description
                options object optional

                Optional settings

                Name Type Description
                session ClientSession optional

                optional session to use for this operation

                callback GridStore~resultCallback optional

                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.

                Deprecated
                • Use GridFSBucket API instead
                  Returns:
                  Promise if no callback passed

                  Retrieves the file collection associated with this object.

                  Name Type Description
                  callback GridStore~collectionCallback

                  the command callback.

                  Deprecated
                  • Use GridFSBucket API instead

                    Handles the destroy part of a stream

                    Deprecated
                    • Use GridFSBucket API instead

                      Verify if the file is at EOF.

                      Deprecated
                      • Use GridFSBucket API instead
                        Returns:
                        if the read/write head is at the end of this file.

                        getc(callback){Promise}

                        Retrieves a single character from this file.

                        Name Type Description
                        callback GridStore~resultCallback optional

                        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.

                        Deprecated
                        • Use GridFSBucket API instead
                          Returns:
                          Promise if no callback passed

                          open(options, callback){Promise}

                          Opens the file from the database and initialize this object. Also creates a
                          new one if file does not exist.

                          Name Type Description
                          options object optional

                          Optional settings

                          Name Type Description
                          session ClientSession optional

                          optional session to use for this operation

                          callback GridStore~openCallback optional

                          this will be called after executing this method

                          Deprecated
                          • Use GridFSBucket API instead
                            Returns:
                            Promise if no callback passed

                            puts(string, options, callback){Promise}

                            Writes a string to the file with a newline character appended at the end if
                            the given string does not have one.

                            Name Type Description
                            string string

                            the string to write.

                            options object optional

                            Optional settings

                            Name Type Description
                            session ClientSession optional

                            optional session to use for this operation

                            callback GridStore~resultCallback optional

                            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.

                            Deprecated
                            • Use GridFSBucket API instead
                              Returns:
                              Promise if no callback passed

                              read(length, buffer, options, callback){Promise}

                              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)

                              Name Type Description
                              length number optional

                              the number of characters to read. Reads all the characters from the read/write head to the EOF if not specified.

                              buffer string | Buffer optional

                              a string to hold temporary data. This is used for storing the string data read so far when recursively calling this method.

                              options object optional

                              Optional settings

                              Name Type Description
                              session ClientSession optional

                              optional session to use for this operation

                              callback GridStore~readCallback optional

                              the command callback.

                              Deprecated
                              • Use GridFSBucket API instead
                                Returns:
                                Promise if no callback passed

                                readlines(separator, options, callback){Promise}

                                Read the entire file as a list of strings splitting by the provided separator.

                                Name Type Description
                                separator string optional

                                The character to be recognized as the newline separator.

                                options object optional

                                Optional settings

                                Name Type Description
                                session ClientSession optional

                                optional session to use for this operation

                                callback GridStore~readlinesCallback optional

                                the command callback.

                                Deprecated
                                • Use GridFSBucket API instead
                                  Returns:
                                  Promise if no callback passed

                                  rewind(options, callback){Promise}

                                  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.

                                  Name Type Description
                                  options object optional

                                  Optional settings

                                  Name Type Description
                                  session ClientSession optional

                                  optional session to use for this operation

                                  callback GridStore~resultCallback optional

                                  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.

                                  Deprecated
                                  • Use GridFSBucket API instead
                                    Returns:
                                    Promise if no callback passed

                                    seek(position, seekLocation, options, callback){Promise}

                                    Moves the read/write head to a new location.

                                    There are 3 signatures for this method

                                    Seek Location Modes

                                    • GridStore.IO_SEEK_SET, (default) set the position from the start of the file.
                                    • GridStore.IO_SEEK_CUR, set the position from the current position in the file.
                                    • GridStore.IO_SEEK_END, set the position from the end of the file.
                                    Name Type Description
                                    position number optional

                                    the position to seek to

                                    seekLocation number optional

                                    seek mode. Use one of the Seek Location modes.

                                    options object optional

                                    Optional settings

                                    Name Type Description
                                    session ClientSession optional

                                    optional session to use for this operation

                                    callback GridStore~gridStoreCallback optional

                                    the command callback.

                                    Deprecated
                                    • Use GridFSBucket API instead
                                      Returns:
                                      Promise if no callback passed

                                      Return a modified Readable stream including a possible transform method.

                                      Deprecated
                                      • Use GridFSBucket API instead

                                        tell(length, buffer, options, callback){Promise}

                                        Retrieves the position of the read/write head of this file.

                                        Name Type Description
                                        length number optional

                                        the number of characters to read. Reads all the characters from the read/write head to the EOF if not specified.

                                        buffer string | Buffer optional

                                        a string to hold temporary data. This is used for storing the string data read so far when recursively calling this method.

                                        options object optional

                                        Optional settings

                                        Name Type Description
                                        session ClientSession optional

                                        optional session to use for this operation

                                        callback GridStore~tellCallback optional

                                        the command callback.

                                        Deprecated
                                        • Use GridFSBucket API instead
                                          Returns:
                                          Promise if no callback passed

                                          Deletes all the chunks of this file in the database.

                                          Name Type Description
                                          options object optional

                                          Optional settings

                                          Name Type Description
                                          session ClientSession optional

                                          optional session to use for this operation

                                          callback GridStore~resultCallback optional

                                          the command callback.

                                          Deprecated
                                          • Use GridFSBucket API instead
                                            Returns:
                                            Promise if no callback passed

                                            write(data, close, options, callback){Promise}

                                            Writes some data. This method will work properly only if initialized with mode "w" or "w+".

                                            Name Type Description
                                            data string | Buffer

                                            the data to write.

                                            close boolean optional

                                            closes this file after writing if set to true.

                                            options object optional

                                            Optional settings

                                            Name Type Description
                                            session ClientSession optional

                                            optional session to use for this operation

                                            callback GridStore~resultCallback optional

                                            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.

                                            Deprecated
                                            • Use GridFSBucket API instead
                                              Returns:
                                              Promise if no callback passed

                                              writeFile(file, options, callback){Promise}

                                              Stores a file from the file system to the GridFS database.

                                              Name Type Description
                                              file string | Buffer | FileHandle

                                              the file to store.

                                              options object optional

                                              Optional settings

                                              Name Type Description
                                              session ClientSession optional

                                              optional session to use for this operation

                                              callback GridStore~resultCallback optional

                                              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.

                                              Deprecated
                                              • Use GridFSBucket API instead
                                                Returns:
                                                Promise if no callback passed

                                                Type Definitions

                                                collectionCallback(error, collection)

                                                The collection callback format.

                                                Name Type Description
                                                error MongoError

                                                An error instance representing the error during the execution.

                                                collection Collection

                                                The collection from the command execution.

                                                gridStoreCallback(error, gridStore)

                                                The tell callback format.

                                                Name Type Description
                                                error MongoError

                                                An error instance representing the error during the execution.

                                                gridStore GridStore

                                                The gridStore.

                                                openCallback(error, gridStore)

                                                The callback format for the Gridstore.open method

                                                Name Type Description
                                                error MongoError

                                                An error instance representing the error during the execution.

                                                gridStore GridStore

                                                The GridStore instance if the open method was successful.

                                                readCallback(error, data)

                                                The read callback format.

                                                Name Type Description
                                                error MongoError

                                                An error instance representing the error during the execution.

                                                data Buffer

                                                The data read from the GridStore object

                                                readlinesCallback(error, strings)

                                                The readlines callback format.

                                                Name Type Description
                                                error MongoError

                                                An error instance representing the error during the execution.

                                                strings Array.<string>

                                                The array of strings returned.

                                                resultCallback(options, error, result)

                                                The callback result format.

                                                Name Type Description
                                                options object optional

                                                Optional settings

                                                Name Type Description
                                                session ClientSession optional

                                                optional session to use for this operation

                                                error MongoError

                                                An error instance representing the error during the execution.

                                                result object

                                                The result from the callback.

                                                tellCallback(error, position)

                                                The tell callback format.

                                                Name Type Description
                                                error MongoError

                                                An error instance representing the error during the execution.

                                                position number

                                                The current read position in the GridStore.