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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@bobosneefdev/zongodb

v5.2.3

Published

Standard schemas, typed MongoDB collections, and database-enforced validation

Readme

ZongoDB

Standard schemas → typed MongoDB collections with database-enforced validation.

ZongoDB compiles document schemas to MongoDB validators, installs them and declared indexes, then returns native Collection<T> instances. It does not wrap writes, execute schema transformations, or own your connection.

Install

bun add @bobosneefdev/zongodb mongodb
# Choose your schema library; for example:
bun add zod

ESM only. Requires Node 20.19+, MongoDB driver 7, and TypeScript 5.4+ for type declarations. Schema-library adapters use optional peer dependencies and do not affect the core import.

Usage

import { createZongo } from "@bobosneefdev/zongodb";
import { MongoClient } from "mongodb";
import { z } from "zod";

const client = new MongoClient(process.env.MONGODB_URI!);
await client.connect();

try {
  const database = await createZongo({
    db: client.db("app"),
    collections: {
      users: {
        schema: z.strictObject({
          name: z.string().min(1),
          age: z.number().int().min(0),
        }),
        indexes: [{ key: { name: 1 }, unique: true }],
      },
    },
  });

  await database.collections.users.insertOne({ name: "Ada", age: 36 });
  const user = await database.collections.users.findOne({ name: "Ada" });
  // user: { _id: ObjectId; name: string; age: number } | null
} finally {
  await client.close();
}

The factory resolves after validators and indexes are installed. Failures reject with ZongoInitializationError, which includes collection, operation, cause, and completed operations. Successful results expose db, collections, and the compiled validators.

Sessions, transactions, aggregation, update pipelines, bulk writes, and driver options remain native MongoDB APIs. Driver validation bypass options and database privileges retain their normal meaning.

Schema libraries

The default input implements Standard JSON Schema. ZongoDB requests jsonSchema.output({ target: "draft-07" }) and infers the stored output type. A validation-only Standard Schema needs a converter.

// Zod: native support
const users = { schema: z.object({ name: z.string() }) };

// Valibot: use its official wrapper
import * as v from "valibot";
import { toStandardJsonSchema } from "@valibot/to-json-schema";
const usersV = { schema: toStandardJsonSchema(v.object({ name: v.string() })) };

// ArkType: native support
import { type } from "arktype";
const usersA = { schema: type({ name: "string" }) };

Tests exercise Zod 4.6, Valibot 1.5 with its JSON Schema package 1.7, and ArkType 2.2. Other conforming implementations can work when they emit the supported dialect. No vendor detection occurs in core.

Schemas describe stored documents. Parse/transform/default your input before inserting it. ZongoDB cannot reproduce arbitrary refinements, nor discover constraints an upstream converter has omitted. An output schema that cannot be represented in JSON Schema needs an explicit converter.

Optional standard-json bridge

Install standard-json and any converter dependencies required by your schema library. Its options are vendor-specific; the example below is for Zod.

import { toJsonSchema } from "@standard-community/standard-json";
import { compileCollections } from "@bobosneefdev/zongodb";

const validators = await compileCollections({
  users: {
    schema: z.object({ name: z.string() }),
    toMongoSchema: (schema) => toJsonSchema(schema, { target: "draft-7", io: "output" }),
  },
});

The toMongoSchema(schema) hook accepts schema objects, promises, and promise-like results. Its result must describe the stored output type using JSON Schema plus MongoDB's bsonType extension. The compiler validates the result at runtime. The old toJSONSchema name remains as a deprecated compatibility alias.

BSON and custom IDs

Omit _id for the driver's default ObjectId behavior. Declare _id: z.string() for an application-supplied string ID. MongoDB's restrictions on ID values still apply. If a custom ID schema rejects ObjectIds, supply an ID on every insert, including when the schema marks it optional.

For BSON-specific values, use a packaged adapter instead of duplicating the schema. The Zod adapter maps z.date(), z.instanceof(ObjectId), and Binary/Uint8Array/Buffer instance schemas. The Effect v4 adapter maps the output side of Schema.Date, date codecs, Schema.Uint8Array, and its exported ObjectIdSchema and BinarySchema declarations.

import { zodToMongoSchema } from "@bobosneefdev/zongodb/zod";

const events = {
  schema: z.strictObject({ created: z.date() }),
  toMongoSchema: zodToMongoSchema,
};

Effect schemas need their Standard Schema wrapper for collection typing:

import { Schema } from "effect";
import {
  BinarySchema,
  effectToMongoSchema,
  ObjectIdSchema,
} from "@bobosneefdev/zongodb/effect";

const events = {
  schema: Schema.toStandardSchemaV1(
    Schema.Struct({
      created: Schema.Date,
      owner: ObjectIdSchema,
      data: BinarySchema,
    }),
  ),
  toMongoSchema: effectToMongoSchema,
};

For other BSON values or schema libraries, return an explicit Mongo-compatible schema from toMongoSchema. Do not specify both type and bsonType on the same node.

Compiler contract

compileSchema(jsonSchema) is a pure, synchronous function returning { $jsonSchema }. compileCollections(definitions) runs native/custom converters and returns a map of these validators without touching a database.

import { compileCollections, compileSchema } from "@bobosneefdev/zongodb";

const validators = await compileCollections({ users });
const validator = compileSchema({
  type: "object",
  properties: { tags: { type: "array", items: { type: "string" }, uniqueItems: true } },
});

Supported source dialect: JSON Schema Draft 7, with optional bsonType and local $defs references. Conversion targets MongoDB's modified Draft 4 dialect.

| Feature | Behavior | | --- | --- | | Objects, properties, required, additional/pattern properties | Preserved; _id reserved in root branches | | Arrays, tuples, additional items, uniqueness | Preserved; empty tuple lists rejected (use maxItems: 0) | | Strings, patterns, lengths | Preserved; patterns must be compatible with MongoDB's regex engine | | Numbers, bounds, multiples | Zero bounds preserved; exclusive bounds translated | | Integers, including nullable integer unions | Numeric BSON value constrained to a multiple of 1 | | enum, const, allOf, anyOf, oneOf, not, dependencies | Translated without dropping constraints | | Boolean subschemas | Supported | | Local JSON Pointer $ref | Expanded; recursive/external refs rejected | | title, description | Preserved verbatim | | default, examples, $comment, read/write/deprecated annotations | Ignored; never executed | | format, propertyNames, conditionals, newer/unknown keywords | Rejected with the schema path |

Collection roots must establish an object type, directly or through supported compositions. _id is not added to nested objects. Root property-count and whole-object equality constraints apply to the stored document, including _id. Compilation is bounded to 10,000 expanded nodes and 100 nesting levels. No remote schemas are fetched.

Indexes and initialization

Indexes live next to their schema and accept MongoDB CreateIndexesOptions. key suggests inferred document paths, including numeric object keys (up to eight levels, excluding Date/BSON internals). Use rawKey for dynamic or advanced specifications, including ordered tuples or maps. Readonly tuples declared with as const are supported:

indexes: [
  { key: { name: 1 }, unique: true, name: "unique_name" },
  { rawKey: new Map([["name", 1], ["age", -1]]), name: "ordered_compound" },
]

Initialization is additive for indexes: existing index names are reused when MongoDB accepts the declared keys and options as equivalent, including text indexes and locale-specific collation defaults. Hidden-state differences fail explicitly. Same-key option conflicts fail explicitly; indexes are never deleted or automatically replaced. Existing collections receive collMod with strict/error validation. Existing data is not scanned, repaired, or migrated. All schemas compile first, but database changes across collections are not atomic; inspect completed after a failure. Manage destructive changes through your own migrations.

Development

bun install --frozen-lockfile
bun run lint
bun run check
bun run test

Tests require MongoDB at mongodb://127.0.0.1:27017, or MONGODB_URI. They create and drop a uniquely named zongo_v5_test_* database and do not touch application databases. The package smoke test installs a temporary packed consumer, requiring registry access, and checks Node ESM imports and TypeScript inference without Zod.

bun run format applies formatting/lint fixes; lint and check do not write files. bun run test:integration runs database checks; bun run test:package builds and verifies the packed package.

See the v5 migration guide for the intentional breaking changes.