Registry / database / cry-db

cry-db

JSON →
library2.4.31jsnpmunverified

cry-db is a TypeScript-first MongoDB wrapper library, currently stable at version 2.4.31. Its versioning suggests active development, though a specific release cadence isn't published. It offers a high-level, opinionated API for common database operations, abstracting away direct MongoDB driver interactions. Key differentiators include built-in support for document revisions, soft-delete functionality, archiving, blocking, and auditing, which simplify complex data lifecycle management. The library also provides real-time publish events, enabling reactive applications. It offers two main interfaces: `Mongo` for flexible multi-collection operations and `Repo<T>` for type-safe, single-collection convenience. Connection details are automatically managed via `MONGO_URL` and `MONGO_DB` environment variables, streamlining setup. A central feature is its unique record lifecycle, where soft-deleted and archived records are filtered from standard queries by default, requiring explicit options to retrieve them.

npm install cry-db
INSTALL
IMPORT
SIG · CRY-DB
C
cry-db
databasejavascriptv2.4.31
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Repo
✓ import { Repo } from 'cry-db';
✗ const Repo = require('cry-db').Repo;
`Repo` is a named export, commonly used as a type-safe convenience class for single-collection operations.
Mongo
✓ import { Mongo } from 'cry-db';
✗ const Mongo = require('cry-db').Mongo;
`Mongo` is a named export, providing lower-level, multi-collection database operations.
ObjectId
✓ import { ObjectId } from 'cry-db';
✗ import { Types } from 'cry-db'; // For ObjectId
`cry-db` re-exports the `ObjectId` class from the underlying MongoDB driver for working with document IDs.

This quickstart demonstrates basic CRUD operations using both the `Repo` and `Mongo` classes, including an example of soft-delete and how to retrieve filtered records, and highlights the reliance on environment variables for MongoDB connection.

import { Repo, Mongo, ObjectId } from 'cry-db'; // Ensure MongoDB is running and accessible via MONGO_URL and MONGO_DB environment variables. // Example: process.env.MONGO_URL = 'mongodb://127.0.0.1:27017'; // Example: process.env.MONGO_DB = 'mytestdb'; async function runExample() { // Repo — single-collection convenience class with type safety const users = new Repo<{ _id?: ObjectId, name: string, age: number, active?: boolean }>('users', process.env.MONGO_DB || 'testdb'); // Mongo — multi-collection class for more granular control const mongo = new Mongo(process.env.MONGO_DB || 'testdb'); console.log('--- Repo operations (users collection) ---'); // Insert a new user let alice = await users.insert({ name: 'Alice', age: 30 }); console.log('Inserted Alice:', alice); // Find Alice by name let foundAlice = await users.findOne({ name: 'Alice' }); console.log('Found Alice:', foundAlice); // Update Alice's age await users.updateOne({ name: 'Alice' }, { $set: { age: 31 } }); let updatedAlice = await users.findOne({ name: 'Alice' }); console.log('Updated Alice:', updatedAlice); // Demonstrate soft-delete (opt-in) and retrieval await users.useSoftDelete(true); // Enable soft-delete for this Repo instance if (updatedAlice?._id) { await users.deleteOne({ _id: updatedAlice._id }); console.log('Soft-deleted Alice. Trying to find (should be null):', await users.findOne({ _id: updatedAlice._id })); console.log('Finding deleted Alice (with returnDeleted):', await users.findOne({ _id: updatedAlice._id }, { returnDeleted: true })); // Cleanup (hard delete to permanently remove) await users.hardDeleteOne(updatedAlice._id); } console.log('Count after cleanup:', await users.count({})); console.log('\n--- Mongo operations (products collection) ---'); // Mongo can operate on any collection without creating a dedicated Repo instance const productsCollection = 'products'; let product1 = await mongo.insert(productsCollection, { name: 'Laptop', price: 1200 }); console.log('Inserted Laptop:', product1); let foundProduct = await mongo.findOne(productsCollection, { name: 'Laptop' }); console.log('Found Laptop:', foundProduct); // Clean up if (product1?._id) { await mongo.hardDelete(productsCollection, { _id: product1._id }); } console.log('Hard-deleted Laptop.'); } runExample().catch(console.error);
Debug
Known issues
gotchaWhen soft-delete (`_deleted`) or archiving (`_archived`) features are enabled, most query methods automatically filter out records marked as deleted or archived by default. This can lead to seemingly missing documents.
fix
To retrieve soft-deleted records, pass `{ returnDeleted: true }` in `QueryOpts`. For archived records, use `{ returnArchived: true }`. To bypass all filters, use `findAll`.
affects: >=2.0.0
gotchaThe `deleteOne()` and `delete()` methods perform a *soft-delete* (setting the `_deleted` flag) if soft-delete is enabled for the `Repo` instance. They do not physically remove the document.
fix
To permanently remove a document from the database, use `hardDeleteOne()` or `hardDelete()`.
affects: >=2.0.0
gotchaConnection to MongoDB relies on `MONGO_URL` and `MONGO_DB` environment variables by default. If these are not set, the library will attempt to connect to `mongodb://127.0.0.1:27017` and use the default database name 'test'.
fix
Ensure `process.env.MONGO_URL` and `process.env.MONGO_DB` are correctly configured in your application environment or explicitly pass the database name to the `Repo` or `Mongo` constructor.
affects: >=2.0.0
gotchaWhen revisions are enabled (`useRevisions(true)`), every write operation (insert, update) automatically increments the `_rev` field and updates the `_ts` (timestamp) field on documents.
fix
Be aware of these automatic field additions and updates, especially when performing partial updates or expecting specific document shapes. Integrate `_rev` and `_ts` into your document interfaces (`T`) for type safety if using `Repo<T>`.
affects: >=2.0.0
Errors
Common errors & fixes
MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
The MongoDB server is not running or is not accessible at the configured `MONGO_URL` (often the default `mongodb://127.0.0.1:27017`).
fix
Ensure your MongoDB instance is running. Verify that `process.env.MONGO_URL` (or the explicit database name in the constructor) is correctly pointing to your MongoDB server address and port.
Query returns no documents, but I know they exist.
Documents might be marked as soft-deleted (`_deleted`) or archived (`_archived`), and standard queries automatically filter these out by default.
fix
When calling query methods like `find` or `findOne`, pass `{ returnDeleted: true }` and/or `{ returnArchived: true }` in the options object to include these records in the results. Alternatively, use `findAll` which bypasses these filters.
Upgrade
Version history
2.4.31latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
18 hits · last 30 days
node
14
OpenAI (training)
1
Resources
cry-db — npm install cry-db · libregistry