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

mutent-mongodb

v1.0.0

Published

MongoDB adapter for Mutent

Downloads

173

Readme

mutent-mongodb

npm JavaScript Style Guide ci Coverage Status

Mutent's Adapter for MongoDB.

Features

  • Lost update/delete protection
  • Bulk update optimization: when possible, bulk updates are used
  • ESM and Common.js: both module systems are supported
  • TypeScript: native support (types declaration included)

API

new MongoAdapter(options)

Class constructor.

  • options: <Object>
    • [collection]: <Collection> The collection instance to use. This option has the top priority.
    • [collectionName]: <String> Collection's name. This option works along aside with db, dbName, and client options.
    • [db]: <Db> Database instance. You must specify also the collectionName option.
    • [dbName]: <String> Database's name. You must specify also collectionName, and client options.
    • [client]: <MongoClient> MongoDB client instance. You must specify also collectionName, and dbName options.
    • [filterQuery]: <Function> Filter query generation hook. Accepts the Entity instance and its Context, and returns the filter query that matches the requested document. By default match by _id field.
    • [diffDocuments]: <Function> | <Boolean>
    • [allowLostUpdates]: <Boolean> Set to true to allow (do not throw) lost updates and also flat the entity as orphaned (entity.meta.orphan = true).
    • [allowLostDeletes]: <Boolean> Set to true to allow (do not throw) lost deletes and also flat the entity as orphaned (entity.meta.orphan = true).
  • Returns: <MongoAdapter>

MongoAdapter::raw

Readonly property containing the requested MongoDB's Collection instance.

Unwrap options

All available options from the following MongoDB methods:

Errors

MongoDB related (MutentError instances) error codes.

MONGODB_LOST_UPDATE

Lost update detected (single document).

This error carries those extra info (error.info object):

  • dbName: Database's name.
  • collectionName: Collection's name.
  • filterQuery: Filter's query that has performed the lost update.
  • document: The original document retrieved from MongoDB previously.

MONGODB_LOST_DELETE

Lost delete detected (single document).

This error carries those extra info (error.info object):

  • dbName: Database's name.
  • collectionName: Collection's name.
  • filterQuery: Filter's query that has performed the lost update.
  • document: The original document retrieved from MongoDB previously.

MONGODB_LOST_UPDATES

Lost update detected during a bulk write.

This error carries those extra info (error.info object):

  • dbName: Database's name.
  • collectionName: Collection's name.
  • matchedCount: Number of documents matched by the bulk write.
  • upsertedCount: Number of documents upserted by the bulk write.
  • expectedUpdations: Expected number of insert/update ops.

MONGODB_LOST_DELETES

Lost delete detected during a bulk write.

This error carries those extra info (error.info object):

  • dbName: Database's name.
  • collectionName: Collection's name.
  • deletedCount: Number of deleted documents.
  • expectedDeletions: Expected number of delete ops.

Example

import { MongoClient } from 'mongodb'
import { Store } from 'mutent'
import MongoAdapter from 'mutent-mongodb'

const client = new MongoClient(process.env.MONGO_URL)

await client.connect()

const store = new Store({
  adapter: new MongoAdapter({
    client,
    dbName: 'my-database',
    collectionName: 'my-collection'
  })
})

// Access the raw Collection instance
const count = await store.raw.countDocuments()

const doc = await store
  .create({ my: 'document' })
  .unwrap()

console.log(`Created document ${doc._id}`)