Types
TypeScript type definitions exported by Monch.
Import
import type {
// Schema types
Schema,
Doc,
Input,
// Collection types
Collection,
CollectionConfig,
CollectionInstance,
MonchDocument,
// Query types
MonchFilter,
// Timestamp types
TimestampFields,
WithTimestamps,
WithTS,
// Method types
Methods,
Statics,
WithMethods,
// Hook types
Hooks,
// Index types
Index,
IndexOptions,
IndexDirection,
// Connection types
ConnectionConfig,
ConnectionResult,
DebugConfig,
OperationOptions,
// Serialization types
Serialized,
WithSerialize,
// Pagination types
PaginationResult,
PaginationOptions,
// Money types
MoneyValue,
MoneyOptions,
// Type extraction
ModelOf,
SerializedOf,
SchemaOf,
} from '@codician-team/monch';Schema Types
Schema
Schema definition type (record of Zod types):
type Schema = Record<string, ZodType>;Doc<S>
Document type inferred from schema:
type Doc<S extends Schema> = z.infer<z.ZodObject<S>>;Input<S>
Input type for insertions (respects defaults/optionals):
type Input<S extends Schema> = z.input<z.ZodObject<S>>;Collection Types
Collection<S, M, ST>
Full collection type with schema, methods, and statics:
type Collection<S, M, ST> = CollectionInstance<Doc<S>, M> & ST;CollectionInstance<T, M>
Collection instance with all methods:
interface CollectionInstance<T, M> {
find(filter?: MonchFilter<T>): Cursor<T, M>;
findOne(filter: MonchFilter<T>): Promise<MonchDocument<T, M> | null>;
findById(id: any): Promise<MonchDocument<T, M> | null>;
insertOne(doc: Input<T>, opts?: OperationOptions): Promise<MonchDocument<T, M>>;
// ... all other methods
}MonchDocument<T, M>
Document with instance methods and .serialize():
type MonchDocument<T, M> = T & M & WithSerialize<T>;CollectionConfig<S, M, ST>
Configuration for collection():
interface CollectionConfig<S, M, ST> {
name: string;
schema: S;
uri?: string;
client?: MongoClient;
db?: Db;
database?: string;
timestamps?: boolean;
indexes?: Index[];
createIndexes?: boolean;
hooks?: Hooks<Doc<S>>;
methods?: M;
statics?: ST;
}Query Types
MonchFilter<T>
Relaxed filter type for Zod compatibility:
type MonchFilter<T> = Filter<T> | Record<string, any>;Allows queries without strict type checking on nested paths and operators.
Timestamp Types
TimestampFields
interface TimestampFields {
createdAt: Date;
updatedAt: Date;
}WithTimestamps<T>
Add timestamp fields to a type:
type WithTimestamps<T> = T & TimestampFields;WithTS<T>
Alias for WithTimestamps:
type WithTS<T> = WithTimestamps<T>;Method Types
Methods<T>
Instance methods definition:
type Methods<T> = Record<string, (doc: T, ...args: any[]) => any>;Statics<T, M>
Static methods definition:
type Statics<T, M> = Record<string, (col: CollectionInstance<T, M>, ...args: any[]) => any>;WithMethods<T, M>
Document with methods attached:
type WithMethods<T, M> = T & { [K in keyof M]: M[K] extends (doc: T, ...args: infer A) => infer R ? (...args: A) => R : never };Hook Types
Hooks<T>
Lifecycle hooks definition:
interface Hooks<T> {
beforeValidate?: (doc: any) => any | Promise<any>;
afterValidate?: (doc: T) => T | Promise<T>;
beforeInsert?: (doc: T) => T | Promise<T>;
afterInsert?: (doc: T) => void | Promise<void>;
beforeUpdate?: (filter: any, update: any) => { filter: any; update: any } | Promise<{ filter: any; update: any }>;
afterUpdate?: (doc: T | null) => void | Promise<void>;
beforeDelete?: (filter: any) => any | Promise<any>;
afterDelete?: (count: number) => void | Promise<void>;
}Index Types
Index
interface Index {
key: Record<string, IndexDirection>;
unique?: boolean;
sparse?: boolean;
background?: boolean;
expireAfterSeconds?: number;
partialFilterExpression?: object;
name?: string;
collation?: object;
}IndexDirection
type IndexDirection = 1 | -1 | 'text' | '2dsphere' | '2d' | 'hashed';IndexOptions
interface IndexOptions {
unique?: boolean;
sparse?: boolean;
background?: boolean;
expireAfterSeconds?: number;
partialFilterExpression?: object;
name?: string;
collation?: object;
}Connection Types
ConnectionConfig
interface ConnectionConfig {
uri?: string;
client?: MongoClient;
db?: Db;
database?: string;
}ConnectionResult
interface ConnectionResult {
client: MongoClient;
db: Db;
}DebugConfig
interface DebugConfig {
enabled: boolean;
logger?: (operation: string, collection: string, ...args: any[]) => void;
}OperationOptions
interface OperationOptions {
session?: ClientSession;
}Serialization Types
Serialized<T>
Serialized version of a document:
type Serialized<T> = {
[K in keyof T]: T[K] extends ObjectId ? string
: T[K] extends Date ? string
: T[K] extends Long ? number | bigint
: T[K] extends Decimal128 ? string
: T[K] extends Binary ? string
: T[K] extends Timestamp ? { t: number; i: number }
: T[K] extends object ? Serialized<T[K]>
: T[K];
};WithSerialize<T>
Object with .serialize() method:
interface WithSerialize<T> {
serialize(): Serialized<T>;
}Pagination Types
PaginationResult<T>
interface PaginationResult<T> {
data: T[];
pagination: {
page: number;
limit: number;
total: number;
totalPages: number;
hasNext: boolean;
hasPrev: boolean;
};
}PaginationOptions
interface PaginationOptions {
page?: number;
limit?: number;
}Money Types
MoneyValue
interface MoneyValue {
amount: number;
currency: string;
formatted: string;
}MoneyOptions
interface MoneyOptions {
currency?: string;
decimals?: number;
}Type Extraction Helpers
ModelOf<C>
Extract document type from collection:
type ModelOf<C> = C extends Collection<infer S, any, any> ? Doc<S> : never;SerializedOf<C>
Extract serialized type from collection:
type SerializedOf<C> = C extends Collection<infer S, any, any> ? Serialized<Doc<S>> : never;SchemaOf<C>
Extract schema type from collection:
type SchemaOf<C> = C extends Collection<infer S, any, any> ? S : never;