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 🙏

© 2025 – Pkg Stats / Ryan Hefner

mblaze.client

v3.4.0

Published

JavaScript library to connect to your express app's MongoDB driver.

Downloads

168

Readme

MBlaze - Client Library

JavaScript library to connect to your express app's MongoDB driver.

Before Getting Started

Get to know a little about the project.

Make sure you have the express middleware library installed and setup on your Express app using the steps here.

Setup and Usage

npm i --save mblaze.client
# or
yarn add mblaze.client
import DB from "mblaze.client";

const { BACKEND_MONGODB_ENDPOINT } = process.env;
const db = new DB(BACKEND_MONGODB_ENDPOINT); // This is your global mblaze instance

Querying Data

const queryResults = await db.collection("projects").get(); // Gets 100 documents from the collection by default.

queryResults.forEach((doc) =>
	console.log(doc.id, doc.exists, doc.ref, doc.data())
);
queryResults.empty; // boolean
queryResults.docs; // [FetchedDoc, FetchedDoc, ...]
Listing Based on Conditionals
await db.collection("projects").where("field", "==", value).get();

await db
	.collection("projects")
	.where("nestedField.furtherNesting.evenFurtherNesting", "!=", value)
	.get();

Comparison Operations supported:

  • ==
  • !=
  • >
  • <
  • >=
  • <=
  • in
  • not-in
  • array-contains
  • array-contains-any
When simple queries just don't cut it

MongoDB is great at querying data, so why be limited? This lets you run complex queries with even or conditions.

await db
	.collection("projects")
	.filter({
		$or: [
			{ textField: { $regex: pattern } },
			{ arrayField: contents },
			{ anotherField: moreContents },
		],
	})
	.get();
Referring to Single Docs
const doc = await db.collection("projects").doc(id).get(); // { exists: boolean, ref, data: () => data, id: string }

if (doc.exists) {
	const data = doc.data();
	const ref = doc.ref; // Will come in handy further
}

Or you can use the simpler method:

const ref = db.doc(`projects/${id}`); // { get, update, set, delete }
const doc = await ref.get(); // { exists: boolean, ref, data: () => data, id: string }
Limits and Offsets
await db.collection("projects"). ... .limit(1000).offset(500).get();
Sorting of results
await db.collection("projects"). ... .orderBy("fieldToSortBy", "asc" | "desc");

Setting/Creating Data

Once you have a doc ref, you can create a new entry for it.

const docRef = db.collection("projects").doc();
// or
const docRef = db.collection("projects").doc("your-own-id");

await docRef.set({ field: value }); // Overwrites existing document with the same id.
await docRef.set({ field: value }, { merge: true }); // Overwrites only the fields specified in case a doc with the id already exists.

Updating Data

await docRef.update({ field: updatedValue });

nonExistentDocRef.update({ field: updatedValue }).catch((err) => {
	console.log(err.message);
	console.log(err.statusCode);
});

Deleting Data

await docRef.delete();

nonExistentDocRef.delete().catch((err) => {
	console.log(err.message);
	console.log(err.statusCode);
});

Counting Data

const { count } = await db
	.collection("projects")
	.where(field, "==", value)
	.count();

const { count } = await db
	.collection("projects")
	.filters({ [field]: { $gte: value } })
	.count();

Adding custom headers to requests

You would obviously want some level of control and most of the times it will be based on cookies or tokens sent via headers.

At the time of initialization:

const db = new DB(BACKEND_MONGODB_ENDPOINT, {
	headers: {
		Authorization: token,
	},
});

// or

const db = new DB(BACKEND_MONGODB_ENDPOINT, ({
	operation,
	collectionName,
	id,
	filters,
	newData,
	limit,
	offset,
	merge,
	sortBy,
	sortOrder
}) => {
	// Compute your headers based on the information.
	return { headers: ... }
});

Transactions

As long as your backend MongoDB setup supports Transactions, you can run them using the db.runTransaction method.

Only write operations are supported for transactions. Read operations are not atomic/guaranteed because the first time the backend is informed of a transaction, it's when transaction.save is called.

db.runTransaction((transaction) => {
	transaction.update(docRef, { newField: "value" });
	transaction.delete(docToDeleteRef);
	transaction.set(docToCreateRef, { newDoc: "newValue" });

	transaction.save(); // Mandatory, otherwise the transaction is never registered.
});

Selecting Specific Fields

Currently selection of specific fields is only supported for list queries, will be adding it for single doc gets as well soon.

await db
	.collection("projects")
	.select({ [fieldToInclude]: 1, [fieldToExclude]: 0 })
	.get();

Field Values

MBlaze Client SDK supports Firestore-like Field Value operations.

import { FieldValue } from "mblaze.client";

db.collection("projects")
	.doc("project1")
	.update({
		timestamp: FieldValue.serverTimestamp(),
		arrayField: FieldValue.arrayUnion(15),
		arrayFieldToBeRemovedFrom: FieldValue.arrayRemove(5),
		nVisits: FieldValue.increment(10),
		fieldToDelete: FieldValue.delete(),
	});

Fallback URL

In case you have multiple services that can address your MongoDB cluster in the backend, you can specify a fallbackURL property for the client SDK to automatically fall back on in case the primary backend URL fails to provide a response. It could happen due to any number of reasons.

const db = new DB(BACKEND_MONGODB_ENDPOINT, {
	fallbackURL: BACKEND_FALLBACK_MONGODB_ENDPOINT,
});