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

@albert-team/mongol

v0.9.0

Published

Minimalistic MongoDB helper for Node.js.

Downloads

44

Readme

Mongol

Minimalistic MongoDB helper for Node.js.

FEATURES

INSTALLATION

Requirements

  • Node.js >= 10
  • MongoDB >= 3.6

Instructions

With yarn:

$ yarn add @albert-team/mongol

With npm:

$ npm i @albert-team/mongol

GET STARTED

The easy way:

const { Mongol } = require('@albert-team/mongol')

const main = async () => {
  const mongol = new Mongol('mongodb://localhost:27017/myproject', 'myproject')
  const coll = await mongol.promisifiedCollection('mycollection')

  // ...now you can use coll variable as a normal Collection object

  await mongol.disconnect()
}
main()

The hard way:

const { Mongol } = require('@albert-team/mongol')

const main = async () => {
  const mongol = new Mongol('mongodb://localhost:27017/myproject', 'myproject', {
    // you can pass any valid MongoClient options here
    client: { useNewUrlParser: true, useUnifiedTopology: true }
  })
  await mongol.connect()
  const db = mongol.database
  const coll = db.collection('mycollection')

  // ...now you can use coll variable as a normal Collection object

  await mongol.disconnect()
}
main()

USAGE

Auto-connect support

To access Db object, instead of manually calling Mongol.connect():

await mongol.connect()
const db = mongol.database

Just use Mongol.promisifiedDatabase and you are good to go:

const db = await mongol.promisifiedDatabase

To fetch an ExtendedCollection object, instead of manually calling Mongol.collection():

await mongol.connect()
const coll = mongol.collection('mycollection')

Just use Mongol.promisifiedCollection() and you are good to go:

const coll = await mongol.promisifiedCollection('mycollection')

Enhanced JSON Schema draft-4 support

Instead of throwing errors on schemas with omitted keywords ($ref, $schema, default, definitions, format and id), Mongol can help you ignore them quietly:

const usedSchema = await mongol.setSchema('mycollection', originalSchema, {
  ignoreType: true,
  ignoreUnsupportedKeywords: true
})

Database hook/trigger support

You can attach a hook to a collection either by using ExtendedCollection.attachHook():

const coll = await mongol.promisifiedCollection('mycollection')
// or
// const coll = mongol.collection('mycollection')
coll.attachHook({
  before: (context) => console.log(`Before ${context.operation}`),
  after: (context) => console.log(`After ${context.operation}`)
})
await coll.insertOne({ foo: 'bar' })
// Before insertOne
// After insertOne

Or using Mongol.attachDatabaseHook():

const coll = db.collection('mycollection')
mongol.attachDatabaseHook(coll, {
  before: (context) => console.log(`Before ${context.operation}`),
  after: (context) => console.log(`After ${context.operation}`)
})
await coll.insertOne({ foo: 'bar' })
// Before insertOne
// After insertOne

Notice:

  • Using ExtendedCollection.attachHook() is recommended, because it allows you to chain method calls as in the nested hooks example below.
  • Mongol.attachDatabaseHook() returns the original collection object but casted to ExtendedCollection anyway.

Nested hooks:

const coll = mongol
  .collection('mycollection')
  .attachHook({
    before: () => console.log('Inner before'),
    after: () => console.log('Inner after')
  })
  .attachHook({
    before: () => console.log('Outer before'),
    after: () => console.log('Outer after')
  })
await coll.insertOne({ foo: 'bar' })
// Outer before
// Inner before
// Inner after
// Outer after

Want "unhooked" version? Just create another collection object:

const another = mongol.collection('mycollection') // this has nothing to do with coll variable above
await another.insertOne({ foo: 'bar' })
//

Useful builtin hooks

Timestamp hook:

const { createTimestampHook } = require('@albert-team/mongol/builtins/hooks')

const coll = mongol
  .collection('mycollection')
  .attachHook(createTimestampHook({ namingConventions: ['unchanged', 'snakecase'] }))

Notice: Replacement document in replace operations (findOneAndReplace and replaceOne) is considered a new one, hence uses createdAt/created_at.

API DOCUMENTATION

Read more here.

CHANGELOG

Read more here.