Marpat
raw JSON → 3.0.5 verified Fri May 01 auth: no javascript
Marpat is a lightweight ES6 class-based ODM (Object Document Mapper) for MongoDB and NeDB, forked from Camo. It provides a storage-agnostic document collection backend with schema validation via Joi since v3.0.0. Current stable version is 3.0.5, with a release cadence of several months. Supports hooks, embedded documents, counting, and custom database drivers. Key differentiators: ES6 class syntax, support for NeDB (file or in-memory) and MongoDB, integrated Joi validation, and a client registry for custom backends. Requires Node >=8.
Common errors
error TypeError: marpat.Document is not a constructor ↓
cause Incorrect import: using default import instead of destructured require.
fix
Use const { Document } = require('marpat');
error Error: Cannot find module 'nedb' ↓
cause NeDB not installed while using nedb:// URI.
fix
Run npm install nedb --save
error ValidationError: "age" must be a number ↓
cause Schema with Joi validation but property value is wrong type.
fix
Ensure property values match the declared type (e.g., Number, String).
Warnings
breaking In version 3.0.0, Joi schema validation was added. Existing schemas may need updating to avoid validation errors. ↓
fix Ensure your schema properties comply with Joi validation rules. See migration docs.
deprecated The 'snyk' package was removed as a production dependency in v3.0.5; it was previously used for vulnerability scanning but is no longer bundled. ↓
fix Remove any reliance on snyk being part of marpat's dependencies.
gotcha Marpat does not support ESM imports (import syntax). Use CommonJS require. ↓
fix Use const { Document } = require('marpat'); instead of import.
gotcha The 'connect()' function returns a database instance, but it does not automatically set a global database; you must pass the instance or rely on the returned promise. ↓
fix Assign the returned database to a variable or use it within the async scope.
Install
npm install marpat yarn add marpat pnpm add marpat Imports
- Document wrong
import { Document } from 'marpat';correctconst { Document } = require('marpat'); - connect wrong
const connect = require('marpat').connect;correctconst { connect } = require('marpat'); - EmbeddedDocument wrong
const EmbeddedDocument = require('marpat').EmbeddedDocument;correctconst { EmbeddedDocument } = require('marpat');
Quickstart
const { connect, Document } = require('marpat');
class Animal extends Document {
constructor() {
super();
this.name = String;
this.species = String;
this.age = { type: Number, default: 0 };
}
}
async function main() {
const database = await connect('nedb://memory');
const cat = Animal.create({ name: 'Fluffy', species: 'Cat' });
await cat.save();
console.log('Saved cat:', cat);
const found = await Animal.findOne({ name: 'Fluffy' });
console.log('Found cat:', found);
await database.close();
}
main().catch(console.error);