npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongodb-filesystem

v1.1.8

Published

Implements a basic filesystem using the MongoDB GridFS interface

Downloads

8

Readme

mongodb-filesystem

Exports the MongoFS class that implements a filesystem using GridFS.

Example (Installation)

npm install --save mongodb-filesystem

Example (Usage)

const { MongoFS } = require('mongodb-filesystem');
const mongofs = new MongoFS(db);

MongoFS ⏏

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.

This approach has two consequences:

First, there is no concept of moving a file (or folder) into another folder. In Unix, mv myfile.txt mydir moves the file into the mydir folder if it is a directory. Otherwise, myfile.txt is renamed to mydir. In this module, the only option is renaming a file (or folder).

Second, the concept of an empty directory also does not exist. Folders are implicitly defined by one or more files having the same folder prefix. If no files have a specific folder prefix, then deleteFolder performs no action and getFiles and getFolders return an empty array.

Please note that all write operations are checked for conflicts. For example, when renaming '/my/dir/myfile.txt' to '/my/dir/yourfile.txt' and '/my/dir/yourfile.txt' aready exists, the promise is rejected. This comparison is case-insensitive, meaning that it would fail even if the existing file was named '/My/Dir/YourFile.txt'. This conflict test also applies to creating or renaming files to existing file or folder names and renaming folders to existing folder or file names.

Kind: Exported class


new MongoFS(db, [options], [modified])

Creates a new MongoFS instance by creating a GridFSBucket using the specified (optional) options. The modified function, if specified, is called for all actions that perform a mutation. The function is passed a single string argument identifying the mutation.

Params

  • db object - A database handle.
  • [options] object - The options passed to the GridFSBucket constructor.
  • [modified] function - A function called for all mutation actions.

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

Creates a new file from the specified buffer.

Kind: instance method of MongoFS
Returns: Promise - Resolved if successful.
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.createOrUpdateFile(pathname, buf, [metadata], [contentType]) ⇒ Promise

Creates a new file or updates an existing file from the specified buffer. On update, if the metadata is not specified, then the existing metadata is used.

Kind: instance method of MongoFS
Returns: Promise - Resolved with true if created, false if updated.
Params

  • pathname string - The pathname of the file to create or update.
  • 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. The promise is rejected if the file does not exist.

Kind: instance method of MongoFS
Returns: Promise - Resolved with the number of files deleted (always 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 files deleted.
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.

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

  • query object - The MongoDB query.

Example (Find files authored by Smith)

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

mongoFS.getFile(pathname) ⇒ Promise

Gets the specified file and its content. The returned promise is resolved with a descriptor object that has the following structure:

{
  filename: '/myfile.txt',
  pathname: '/myfolder/myfile.txt',
  metadata: { author: 'John Smith' },
  content: <Buffer 74 65 78 74>,
  contentType: 'text/plain',
  contentLength: 4,
  lastModified: '2020-06-05T12:53:03.128Z'
}

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.getFolders(parent) ⇒ Promise

Gets a list of folders contained within the specified parent folder.

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

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

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.isFile(pathname) ⇒ Promise

Determines if the pathname identifies an existing file.

Kind: instance method of MongoFS
Returns: Promise - - Resolved with a boolean.
Params

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

mongoFS.isFolder(pathname) ⇒ Promise

Determines if the pathname identifies an existing folder. Since the concept of folders is somewhat artificial, this method simply checks if there are any files having the folder prefix.

Kind: instance method of MongoFS
Returns: Promise - - Resolved with a boolean.
Params

  • pathname string - The pathname of the folder to check.

mongoFS.renameFile(oldPathname, newPathname) ⇒ Promise

Renames a file specified by the oldPathname with the newPathname. The promise is rejected if the file does not exist, a file by that name already exists, or the newPathname is actually a folder already containing other files.

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

  • oldPathname string - The absolute pathname of the file to rename.
  • newPathname string - The relative or absolute target pathname.

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 relative or absolute folder path.

mongoFS.updateFile(pathname, buf, [metadata], [contentType]) ⇒ Promise

Updates an existing file from the specified buffer. If the metadata is not specified, then the existing metadata is used.

Kind: instance method of MongoFS
Returns: Promise - Resolved if successful.
Params

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

mongoFS.updateMetadata(pathname, metadata) ⇒ Promise

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

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.