1.0.2 • Published 4 years ago

mongo-filesystem v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

mongo-filesystem

Exports the MongoFS class that wraps a GridFSBucket instance.

Author: Frank Hellwig frank@hellwig.org


mongo-filesystem~MongoFS

This class implements a basic filesystem using the MongoDB GridFS interface. In GridFS, everything is a file so the concept of folders containing files must be artifically created by giving each file a pathname as the filename.

There are three basic concepts in this module:

  • folder: the absolute path to a folder containing files and subfolders
  • pathname: the absolute path to a file beginning with a forward slash
  • filename: a string representing the name of a file

In other words, folders and pathnames always absolute (beginning with a forward slash) and filenames do not contain any slashes. The methods in this class normalize all folder names and pathnames and replace backward slashes with forward slashes.

Kind: inner class of mongo-filesystem


new MongoFS(db, options)

Creates a new MongoFS instance by creating a GridFSBucket using the specified (optional) options.

Params

  • db object - A database handle.
  • options object - The options passed to the GridFSBucket constructor.

mongoFS.createFile(pathname, buf, metadata, contentType) ⇒ Promise

Creates a new file from the specified buffer.

Kind: instance method of MongoFS
Params

  • pathname string - The pathname of the file to create.
  • buf string | Buffer - The file data.
  • metadata object - Optional metadata.
  • contentType string - Optional content type.

mongoFS.deleteFile(pathname) ⇒ Promise

Deletes the file specified by the pathname.

Kind: instance method of MongoFS
Returns: Promise - Resolved with the number of files deleted (i.e., 1).
Params

  • pathname string - The pathname of the file to delete.

mongoFS.deleteFolder(folder) ⇒ Promise

Deletes all files and folders in the specified folder. Essentially, this method performs an rm -rf operation.

Kind: instance method of MongoFS
Returns: Promise - Resolved with the number of deleted files.
Params

  • folder string - The folder to delete.

mongoFS.findFiles(query) ⇒ Promise

Finds all files matching the specified query. The query is a standard MongoDB query. For example, to find all files where the metadata has a person with a last name of 'Smith', the resulting query is:

{ 'metadata.name.last': 'Smith' }

Kind: instance method of MongoFS
Returns: Promise - - Resolved with an array of descriptor objects.
Params

  • query object - The MongoDB query.

mongoFS.getFile(pathname) ⇒ Promise

Gets the specified file and its content.

Kind: instance method of MongoFS
Returns: Promise - Resolved with a descriptor that includes content.
Params

  • pathname string - The pathname of the file to get.

mongoFS.getFiles(folder, filenamesOnly) ⇒ Promise

Gets a list of the files in the specified folder. If the filenamesOnly flag is true, then an array of filenames is returned instead of an array of descriptor objects. The content is not included in the returned array.

Kind: instance method of MongoFS
Returns: Promise - Resolved with an array of descriptor objects or filenames.
Params

  • folder string - The folder of which to list the files.
  • filenamesOnly boolean - True to return only filenames.

mongoFS.getMetadata(pathname) ⇒ Promise

Gets the metadata of the file specified by the pathname.

Kind: instance method of MongoFS
Returns: Promise - - Resolved with the metadata of the file.
Params

  • pathname string - The pathname of the file to retrieve.

mongoFS.getSubfolders(folder) ⇒ Promise

Gets a list of direct subfolders contained within the specified folder.

Kind: instance method of MongoFS
Returns: Promise - Resolved with an array of subfolder names.
Params

  • folder string - The folder of which to list the subfolders.

mongoFS.moveFile(oldPathname, newFolder) ⇒ Promise

Moves a file specified by the oldPathname into the folder specified by the newFolder. The returned promise is rejected if the file does not exist.

Kind: instance method of MongoFS
Returns: Promise - Resolved with the number of files moved (always 1).
Params

  • oldPathname string - The pathname of the file to move.
  • newFolder string - The new absolute folder path.

mongoFS.renameFile(oldPathname, newFilename) ⇒ Promise

Renames a file specified by the oldPathname with the newFilename. The returned promise is rejected if the file does not exist, identifies more than one file, or the target file already exists.

Kind: instance method of MongoFS
Returns: Promise - Resolved with the number of files renamed (i.e., 1).
Params

  • oldPathname string - The absolute pathname of the file to rename.
  • newFilename string - The new filename.

mongoFS.renameFolder(oldFolder, newFolder) ⇒ Promise

Renames a folder by finding all files where the filename begins with the specified oldFolder and replacing that part of the filename with the prefix specified by the newFolder. The promise is rejected if the newFolder already exists.

Kind: instance method of MongoFS
Returns: Promise - Resolved with the number of files renamed.
Params

  • oldFolder string - The old absolute folder path.
  • newFolder string - The new absolute folder path.

mongoFS.updateMetadata(pathname, metadata) ⇒ Promise

Updates the metadata of the file specified by the pathname to the specified metadata. This replaces the current file metadata.

Kind: instance method of MongoFS
Returns: Promise - Resolved with the number of files updated (always 1).
Params

  • pathname string - The pathname of the file to update.
  • metadata object - The new metadata object.

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago