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

@mikosoft/mongofast

v2.1.0

Published

Fast way to start with mongodb. Mongoose schema, model and functions ready to manage database, collection and document.

Downloads

15

Readme

@mikosoft/mongofast

Fast way to start with mongodb. Mongoose schema, model and functions ready to manage database, collection and document.

Mongofast is a helper to manage MongoDB database. It can connect/disconnect to the mongo server, create schema and model and save, list, update and delete documents. Also it can delete database and collection and list db collections.

Installation

$ npm install --save @mikosoft/mongofast

Example

/*** NodeJS script ***/
const Mongofast = require('@mikosoft/mongofast');

const main = async () => {
  // connection
  const mo_uri = 'mongodb://user:[email protected]:27017/db-name';
  const mongofast = new Mongofast();
  const Schema = mongofast.Schema;
  await mongofast.connect(mo_uri);

  const collectionName = 'mongo-test';

  // compile 'mongo-testMD' model
  const opts = {timestamps: {createdAt: 'created_at', updatedAt: false}};
  const moSchema = {
    user_id: { type: mongoose.Schema.Types.ObjectId, ref: 'usersMD' },
    company: { type: String, required: 'Field "company" is required' },
    year: Number,
  };
  await mongofast.compileModel(collectionName, moSchema, opts);

  // take a model
  mongofast.useModel(collectionName);

  // create new doc
  const doc = {
    user_id: '65bcc03db8f6dfe919bcc9d0',
    company: 'Apple Ltd',
    depth: 1971
  };
  await mongofast.save(doc)
    .then(docNew => {
      console.log(`New doc saved to ${collectionName} collection:`);
      console.log(docNew);
      mongofast.disconnect();
    });

  // disconnection
  await mongofast.disconnect();
};


main().catch(console.log);

Other examples are in /tests/ folder.

Generic Schema

The library is using generic (empty & not strict) mongoose schema but user can define any schema.

const collection = 'generic';

// options
const opts = {
  collection, // default collection
  _id: true, // disable _id
  id: false, // set virtual id property
  autoIndex: true, // auto-create indexes in mognodb collection on mongoose restart
  minimize: true, // remove empty objects
  // safe: true, // pass errors to callback
  strict: false, // values not defined in schema will not be saved in db
  validateBeforeSave: true, // validate doc before saving. prevent saving false docs
  timestamps: { // create timestamps for each doc 'created_at' & 'updated_at'
    createdAt: 'created_at',
    updatedAt: 'updated_at'
  },
  new: true,
  // skipVersioning: { myProp: true }, // prevent changing version __v when 'myProp' is updated
  // versionKey: '_myV', // replace __v with _myV (not needed in most cases)
};


// mongoose schema
const moSchema = new Schema({}, opts);

API

constructor()

async connect(mo_uri) :Promise

Connects to the mongoDB server via mongo URI. Used database is also defined. mongofast.connect('mongodb+srv://user:[email protected]/dbname?retryWrites=true&w=majority')

async disconnect() :Promise

Disconnect from already connected mongoDB server via mongo URI. mongofast.disconnect()

deleteDatabase() :Promise[boolean]

Remove whole database. mongofast.deleteDatabase()

showCollections() : Promise[array]

Lists all collections in the database. mongofast.showCollections()

deleteCollection(collectionName) :Promise[boolean]

Removes collection from the database. mongofast.deleteCollection('companies')

async compileModel(moSchema) :Promise[void]

Compiles mongoose model. Parameter must be valid mongoose schema. The model object is pushed into the "this.compiledModels" array. mongofast.compileModel(moSchema)

useModel(collectionName) :void

Takes model on which operations will be performed. It defines on which collection documents will be added, listed, updated, removed ... mongofast.useModel(collectionName)

save(doc) :Promise[object]

Adds document in collection. Mongoose save function is used. mongofast.save({x: 23, y: 'something'})

add(doc | docs) :Promise[object]

Adds document in collection. Parameter can be a single object or array of objects. Mongoose create function is used. mongofast.add({x: 23, y: 'something'}) mongofast.add([{x: 23, y: 'something'}, {x: 25, y: 'aaa'}]) - database called two times

insertMulti(docs, insOpts) :Promise[object]

Bulk insertions. While add() will send multiple db requests for every doc, this method will call database only once. Mongoose create function is used.

const insOpts = {
  ordered: false, // (true) if true, will fail fast on the first error encountered and don't execute the remaining writes
  rawResult: true, // (false) will return inserted docs
  lean: false, // (false) if true skip schema validation
  limit: null // (null) limits the number of documents being processed
};

mongofast.insertMulti([{x: 23, y: 'something'}, {x: 25, y: 'aaa'}], insOpts) // database called once

list(moQuery, limit, skip, sort, select) :Promise[object]

List and count documents from collection by using mongo search query criteria. Mongoose find function is used. mongofast.list({x: {$gt3: 3}}, 5, 0, '-x', '-_id')

listFast(moQuery, limit, skip, sort, select) :Promise[object]

List documents from collection by using mongo search query criteria. This is useful for big collections because list() method is using count and is much slower. Mongoose find function is used. mongofast.listFast({x: {$gt3: 3}}, 5, 0, '-x', '-_id')

getOne(moQuery, sort, select) :Promise[object]

Fetch one document from collection. Mongoose findOne function is used. mongofast.getOne({x: 3})

deleteOne(moQuery) :Promise[object]

Delete just one document. Mongoose findOneAndDelete function is used. mongofast.deleteOne({x: 3})

deleteMulti(moQuery) :Promise[object]

Delete multiple documents. Mongoose deleteMany function is used. mongofast.deleteMulti({x: {$lt: 5}})

editOne(moQuery, docNew, updOpts) :Promise[object]

Update one document. Mongoose findOneAndUpdate function is used.

const updOpts = {
  new: true, // return updated document as 'result'
  upsert: false, // whether to create the doc if it doesn't match (false)
  runValidators: false, // validators validate the update operation against the model's schema
  strict: false, // values not defined in schema will not be saved in db (default is defined in schema options, and can be overwritten here)
  // sort: {created_at: -1} // if multiple results are found, sets the sort order to choose which doc to update
}
mongofast.editOne({x: 3}, {x: 5, y: 'some thing'}, updOpts)

editMulti(moQuery, docNew, updOpts) :Promise[object]

Update multiple documents. Mongoose updateMany function is used. mongofast.editMulti({x: {$lt: 5}}, {y: 22})

countDocs(moQuery) :Promise[number]

Count documents according to the given mongo query. Returned value is a number. Mongoose countDocuments function is used. mongofast.count({x: {$gt: 10}})

License

The software licensed under MIT.