Schema Builder
Build schemas with `M` and infer types from the same definition
The schema builder is the heart of Mongster. It gives you a typed way to describe collection shapes, validation rules, defaults, indexes, and timestamps.
import { , } from "mongster";
const = ({
: .().(1),
: .().(),
: .().(0).(),
}).();
type = M.<typeof >;
type = M.<typeof >;Top-level exports
M: the builder namespacedefineSchema(shape): equivalent toM.schema(shape)M.infer<typeof schema>: stored document typeM.inferInput<typeof schema>: create and insert input type
Builder methods
| Category | Methods |
|---|---|
| Primitives | string, number, boolean, date |
| BSON | objectId, decimal, binary |
| Composites | object, array, tuple, fixedArrayOf, union, oneOf, schema |
Common field chainers
- Validation:
min,max,enum,match,validate - Shape modifiers:
optional,nullable - Defaults:
default,defaultFn - Index metadata:
index,uniqueIndex,sparseIndex,partialIndex,hashedIndex,textIndex,ttl,expires
Schema-level helpers
withTimestamps()addIndex()pre()andpost()