01
Mongo documents enter through Zod
Every repository read is parsed before application code sees it, so broken storage shape fails at the boundary.
A small persistence boundary for teams that want MongoDB to feel like Zod, without teaching every schema about storage history.
pnpm add @mp-lb/zogZog keeps the repository API close to MongoDB while giving each model a typed, parsed boundary.
const userModel = createModel("users", userSchema, {
primaryKey: "id",
collectionName: "users",
legacyCollectionNames: ["user_accounts"],
indexes: [uniqueIndex({ email: 1 })],
});
const db = defineDb([userModel] as const, {
mongoClient,
databaseName: "app",
});
const user = await db.users.findById("user_1");Every repository read is parsed before application code sees it, so broken storage shape fails at the boundary.
Keep your model in terms of id while Zog stores the same value canonically as Mongo _id.
Declare, diff, and sync Mongo indexes from the same place you define collection shape.
Collection renames, old primary keys, and storage normalization stay outside your current schema.