$ show plexinc/papr
Papr is a lightweight TypeScript library that provides type-safe schemas and JSON Schema validation for MongoDB collections. It wraps the official MongoDB Node.js driver, offering a familiar API (find, insertOne, updateOne, etc.) while automatically generating JSON Schema validators that are applied to MongoDB collections via `collMod` or `createCollection`. The library supports a wide range of BSON types, timestamps, defaults (static, dynamic, async), hooks (before/after), and custom filter types for enhanced type safety in queries and updates. It is designed as a thin, performant alternative to Mongoose, offloading validation to the MongoDB server. The codebase is well-structured with modular source files, extensive unit and type tests, benchmarks comparing performance against raw MongoDB driver and Mongoose, and comprehensive documentation. It is actively maintained with CI/CD pipelines, automated releases, and dependency management via Renovate.
The LLM identified 4 files worth highlighting for reuse:
A recursive TypeScript type that picks nested properties from an object type based on dot-notation paths. It handles arrays, primitives (Binary, Date, ObjectId), and nested objects up to a depth of 6. This is a standalone, reusable type utility that can be extracted into any TypeScript project needing deep property selection.
A TypeScript type that computes all possible tuple item combinations from a readonly tuple type, respecting required and optional positions. It uses recursive conditional types to split tuples and generate union types. This is a self-contained utility that can be reused in any TypeScript project dealing with tuple validation or variadic types.
Defines custom `PaprFilter` and `PaprUpdateFilter` types that provide strict type checking for MongoDB query and update operations, including dot-notation nested paths, array filters (`$`, `$[]`, `$[identifier]`), and all standard MongoDB operators. These types improve upon the official MongoDB driver's strict types by preventing invalid field access and type mismatches. They are reusable in any TypeScript project using MongoDB.
Provides a hook system for model methods (before/after) and a `logHook` creator that serializes method arguments for logging. The hook interface is generic and decoupled from the model implementation, making it reusable as a standalone middleware pattern for any async function wrapper.