How to use @lykmapipo/mongoose-common - 10 common examples

To help you get started, we’ve selected a few @lykmapipo/mongoose-common examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github lykmapipo / mongoose-gridfs / lib / bucket.js View on Github external
function createBucket(optns = {}) {
  // ensure options
  let { connection } = optns;
  connection = (connection || mongoose.connection);
  const options =
    _.merge({}, DEFAULT_BUCKET_OPTIONS, _.omit(optns, 'connection'));

  // create GridFSBucket
  const db = connection.db;
  const bucket = new GridFSBucket(db, options);

  // return bucket
  return bucket;
}
github lykmapipo / mongoose-gridfs / lib / bucket.js View on Github external
* @author lally elias 
 * @license MIT
 * @since 1.0.0
 * @version 0.1.0
 * @instance
 * @example
 *
 * const bucket = createBucket();
 * const _id = new ObjectId();
 * const filename = 'filename.txt';
 * const writeStream = fs.createWriteStream(filename);
 * const readStream = bucket.createReadStream({_id, filename});
 * readStream.pipe(writeStream);
 *
 */
GridFSBucket.prototype.createReadStream = function createReadStream(optns) {
  // ensure options
  const options = _.merge({}, optns);
  const { _id, filename } = options;

  // ensure filename or _id
  if (_.isEmpty(filename) && !_id) {
    let error = Error('Missing filename or file id');
    error.status = 400;
    throw error;
  }

  // initalize file read stream
  let readstream;

  // open download stream by file id
  if (_id) {
github lykmapipo / mongoose-gridfs / lib / bucket.js View on Github external
* @version 0.1.0
 * @instance
 * @example
 *
 * // large file
 * const bucket = createBucket();
 * const filename = 'filename.txt';
 * const readStream = bucket.readFile({ filename });
 *
 * // small file
 * const bucket = createBucket();
 * const filename = 'filename.txt';
 * bucket.readFile({ filename }, (error, buffer) => { ... });
 *
 */
GridFSBucket.prototype.readFile = function readFile(optns, done) {
  // ensure options
  const _optns = _.merge({}, optns);

  //create file read stream
  const readstream = this.createReadStream(_optns);

  //pipe the whole stream into buffer if callback provided
  if (done && _.isFunction(done)) {
    return read(readstream, done);
  }

  //return stream
  else {
    return readstream;
  }
github lykmapipo / mongoose-gridfs / lib / bucket.js View on Github external
* @description find an existing file with given objectid
 * @param {ObjectId} _id valid objectid of the existing file
 * @param {Function} done a callback to invoke on success or error
 * @return {Object} existing file details
 * @author lally elias 
 * @license MIT
 * @since 0.1.0
 * @version 0.7.0
 * @instance
 * @example
 *
 * const bucket = createBucket();
 * bucket.findById(_id, (error, file) => { ... });
 *
 */
GridFSBucket.prototype.findById = function findById(_id, done) {
  return this.findOne({ _id }, done);
};


/* multer */

/**
 * @function _handleFile
 * @name _handleFile
 * @description write file to the bucket and return information on how to
 * access the file in the future
 * @param {Object} request injected request from multer
 * @param {Object} file injected file object from multer
 * @param {Function} done a callback to invoke on success or error
 * @author lally elias 
 * @see {@link https://github.com/expressjs/multer/blob/master/StorageEngine.md}
github lykmapipo / mongoose-gridfs / lib / bucket.js View on Github external
* @param {Object} optns valid find criteria
 * @param {Function} done a callback to invoke on success or error
 * @return {Object} existing file details
 * @author lally elias 
 * @license MIT
 * @since 0.1.0
 * @version 0.7.0
 * @instance
 * @example
 *
 * const bucket = createBucket();
 * bucket.findOne({ _id }, (error, file) => { ... });
 * bucket.findOne({ filename }, (error, file) => { ... });
 *
 */
GridFSBucket.prototype.findOne = function findOne(optns, done) {

  // ensure file find criteria
  const options = _.merge({}, optns);

  // find one existing file
  try {
    const cursor = this.find(options, { limit: 1 });
    if (!cursor) {
      const error = new Error('Collection not found');
      error.status = 400;
      return done(error);
    }
    return cursor.next(done);
  }
  // catch find errors
  catch (error) {
github lykmapipo / mongoose-gridfs / lib / bucket.js View on Github external
* @author lally elias 
 * @license MIT
 * @since 1.0.0
 * @version 0.1.0
 * @instance
 * @example
 *
 * const bucket = createBucket();
 * const _id = new ObjectId();
 * const filename = 'filename.txt';
 * const readStream = fs.createReadStream(filename);
 * const writeStream = bucket.createWriteStream({_id, filename});
 * readStream.pipe(writeStream);
 *
 */
GridFSBucket.prototype.createWriteStream = function createWriteStream(optns) {
  // ensure options
  const defaults = { _id: new ObjectId() };
  const options = _.merge({}, defaults, optns);
  const { _id, filename } = options;

  // ensure filename
  if (_.isEmpty(filename)) {
    let error = new Error('Missing filename');
    error.status = 400;
    throw error;
  }

  // open write stream
  const writeStream =
    this.openUploadStreamWithId(_id, filename, options);
github lykmapipo / mongoose-gridfs / lib / bucket.js View on Github external
* @example
 *
 * const express = require('express');
 * const multer  = require('multer');
 * const { createBucket } = require('mongoose-gridfs');
 * const app = express();
 * const storage = createBucket(); // createBucket(optns)
 * const upload = multer({ storage });
 *
 * app.post('/profile', upload.single('avatar'), (req, res, next) => {
 *   // req.file is the `avatar` file
 *   // req.body contains the text fields
 * });
 *
 */
GridFSBucket.prototype._removeFile = function _removeFile(request, file, done) {
  // remove file
  if (file._id) {
    return this.deleteFile(file._id, done);
  }
  // no operation
  else {
    return done(null, null);
  }
};


/* statics */

/**
 * @function createBucket
 * @name createBucket
github lykmapipo / mongoose-gridfs / lib / bucket.js View on Github external
* @description Remove an existing file and its chunks.
 * @param {ObjectId} _id The id of the file doc
 * @param {Function} done a callback to invoke on success or error
 * @author lally elias 
 * @license MIT
 * @since 1.0.0
 * @version 0.1.0
 * @instance
 * @example
 *
 * const bucket = createBucket();
 * bucket.deleteFile(_id, (error, results) => { ... });
 *
 */
GridFSBucket.prototype.deleteFile =
  GridFSBucket.prototype.unlink = function deleteFile(_id, done) {
    this.delete(_id, function afterDelete(error) {
      return done(error, _id);
    });
  };


/* finders */

/**
 * @function findOne
 * @name findOne
 * @description find an existing file using options provided
 * @param {Object} optns valid find criteria
 * @param {Function} done a callback to invoke on success or error
 * @return {Object} existing file details
 * @author lally elias 
github lykmapipo / mongoose-gridfs / lib / bucket.js View on Github external
* @example
 *
 * // large file
 * const bucket = createBucket();
 * const filename = 'filename.txt';
 * const readStream = fs.createReadStream(filename);
 * const writeStream = bucket.writeFile({ filename }, readStream);
 *
 * // small file
 * const bucket = createBucket();
 * const filename = 'filename.txt';
 * const readStream = fs.createReadStream(filename);
 * bucket.writeFile({ filename }, readStream, (error, file) => { ... });
 *
 */
GridFSBucket.prototype.writeFile = function writeFile(file, readstream, done) {
  // ensure file details
  const _file = _.merge({}, { _id: new ObjectId() }, file);

  // create file write stream
  const writestream = this.createWriteStream(_file);

  // stream file into mongodb gridfs bucket
  readstream.pipe(writestream);

  // work on the stream
  if (done && _.isFunction(done)) {

    // handle errors
    writestream.on('error', function onWriteFileError(error) {
      return done(error);
    });
github lykmapipo / mongoose-gridfs / lib / bucket.js View on Github external
* @alias unlink
 * @description Remove an existing file and its chunks.
 * @param {ObjectId} _id The id of the file doc
 * @param {Function} done a callback to invoke on success or error
 * @author lally elias 
 * @license MIT
 * @since 1.0.0
 * @version 0.1.0
 * @instance
 * @example
 *
 * const bucket = createBucket();
 * bucket.deleteFile(_id, (error, results) => { ... });
 *
 */
GridFSBucket.prototype.deleteFile =
  GridFSBucket.prototype.unlink = function deleteFile(_id, done) {
    this.delete(_id, function afterDelete(error) {
      return done(error, _id);
    });
  };


/* finders */

/**
 * @function findOne
 * @name findOne
 * @description find an existing file using options provided
 * @param {Object} optns valid find criteria
 * @param {Function} done a callback to invoke on success or error
 * @return {Object} existing file details