Typed against your schema
Filters, updates, projections and aggregation stages are checked at compile time, written in MongoDB's own syntax.
For Node.js and TypeScript. Safe defaults over the official driver, and an analyser that looks for N+1 queries in the app you already have.
import { createDal, schema, t } from '@sameernair/krepis';const dal = createDal(client, { dbName: 'app' });const Users = dal.collection('users', schema({ email: t.string().lowercase(), passwordHash: t.string().hidden(), plan: t.enum(['free', 'pro']),}));const user = await Users.findOne({ plan: 'pro' });// passwordHash is left out unless selectedawait Users.findOne({ plan: 'gold' });// type error: not 'free' | 'pro'Use the analyser on its own, the data layer on its own, or both.
The data layer
Typed collections over the official MongoDB driver, for new code or for moving off Mongoose one collection at a time.
include, aggregation, transactionsfromMongoose to share an existing Mongoose connection@sameernair/krepis/connections for per-tenant pools and connection checksThe analyser and CLI
Attach it to the client your app already has. It records query shapes per request, and the CLI reports N+1 patterns from them.
krepis analyze, with --fail for CIkrepis drill to rehearse connection incidentsShort examples. The guide covers each part properly.
import { MongoClient } from 'mongodb';
import { createDal, schema, t, op } from '@sameernair/krepis';
const client = new MongoClient(process.env.MONGODB_URI!);
const dal = createDal(client, { dbName: 'shop' });
const Users = dal.collection('users', schema({
email: t.string().lowercase().trim(),
passwordHash: t.string().hidden(), // left out of reads unless selected
plan: t.enum(['free', 'pro']), // checked on every write
}));
// Plain MongoDB filter syntax, typed from the schema.
const pro = await Users.find({ plan: op.in(['pro']) }).limit(50).toArray();
// { "$ne": null } arriving from a request body throws KrepisInputError
// instead of matching every document.
await Users.findOne({ email: req.body.email });import { observe } from '@sameernair/krepis-kit';
// Attach before client.connect(). With Mongoose: mongoose.connection.getClient().
const observer = observe(client, { capture: true });
app.use(observer.middleware()); // Express: one unit of work per request
// or: new ApolloServer({ plugins: [observer.apolloPlugin()] })npx krepis init # adds .krepis/ to .gitignore
npm test # run your app or tests with capture on
npx krepis analyze # N+1 patterns, with call sites and a suggested fix
npx krepis analyze --fail # exit 1 when one is found, for CIKrepis is new. This is an honest summary of where it is.
The data layer, @sameernair/krepis/connections, and the analyser, CLI and drills. Tested against drivers 4.17 to 7 and Mongoose 6 to 9. Published to npm as alpha releases: see Install.
Used in production. APIs may change before v0.1.
Krepis works at the database layer. It does not replace validation, authentication or access control in your app. The analyser's default finding is N+1; index and hazard checks are opt-in. See Rules.
κρηπίς (krepis) is the stepped stone platform a Greek temple stands on. The aim is the same: a plain, solid base for the data code above it.
Feedback and issues are welcome on GitHub.