Skip to content
Alpha · on npm

KrepisA typed data layer for MongoDB

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.

users.ts
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'

Two packages ​

Use the analyser on its own, the data layer on its own, or both.

krepis

The data layer

Typed collections over the official MongoDB driver, for new code or for moving off Mongoose one collection at a time.

  • Schemas, typed queries and updates, include, aggregation, transactions
  • fromMongoose to share an existing Mongoose connection
  • @sameernair/krepis/connections for per-tenant pools and connection checks
Data layer guide →

krepis-kit

The 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.

  • Express middleware and an Apollo Server plugin
  • krepis analyze, with --fail for CI
  • krepis drill to rehearse connection incidents
Analyser guide →

A first look ​

Short examples. The guide covers each part properly.

ts
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 });
ts
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()] })
bash
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 CI

Where it stands ​

Krepis is new. This is an honest summary of where it is.

Built and tested

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.

Not yet

Used in production. APIs may change before v0.1.

Scope

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.

Released under the MIT License. An independent project, not affiliated with MongoDB, Inc.