Thin MongoDB ODM built for Standard Schema
mongodb
zod
deno
1import type { z } from "@zod/zod";
2import type { Document, IndexDescription, ObjectId } from "mongodb";
3
4/**
5 * Type alias for Zod schema objects
6 */
7export type Schema = z.ZodObject<z.ZodRawShape>;
8
9/**
10 * Infer the TypeScript type from a Zod schema, including MongoDB Document
11 */
12export type Infer<T extends Schema> = z.infer<T> & Document;
13
14/**
15 * Infer the model type from a Zod schema, including MongoDB Document and ObjectId
16 */
17export type InferModel<T extends Schema> = Infer<T> & {
18 _id?: ObjectId;
19};
20
21/**
22 * Infer the input type for a Zod schema (handles defaults)
23 */
24export type Input<T extends Schema> = z.input<T>;
25
26/**
27 * Type for indexes that can be passed to the Model constructor
28 */
29export type Indexes = IndexDescription[];
30
31/**
32 * Complete definition of a model, including schema and indexes
33 *
34 * @example
35 * ```ts
36 * const userDef: ModelDef<typeof userSchema> = {
37 * schema: userSchema,
38 * indexes: [
39 * { key: { email: 1 }, unique: true },
40 * { key: { name: 1 } }
41 * ]
42 * };
43 * ```
44 */
45export type ModelDef<T extends Schema> = {
46 schema: T;
47 indexes?: Indexes;
48};