Types
TypeScript type definitions exported by Monch.
Import
typescript
import type {
// Schema types
Schema,
Doc,
Input,
// Collection types
Collection,
CollectionConfig,
CollectionInstance,
MonchDocument,
// Query types
MonchFilter,
// Timestamp types
TimestampFields,
WithTimestamps,
WithTS,
// 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):
typescript
type Schema = Record<string, ZodType>;Doc<S>
Document type inferred from schema:
typescript
type Doc<S extends Schema> = z.infer<z.ZodObject<S>>;Input<S>
Input type for insertions (respects defaults/optionals):
typescript
type Input<S extends Schema> = z.input<z.ZodObject<S>>;Collection Types
Collection<T, I, S>
Full collection type with type accessors:
typescript
type Collection<T, I, S> = CollectionInstance<T, I> & TypeAccessors<S>;CollectionInstance<T, I>
Collection instance with all query/mutation operations:
typescript
interface CollectionInstance<T, I> {
find(filter?: MonchFilter<T>): Cursor<T>;
findOne(filter: MonchFilter<T>): Promise<MonchDocument<T> | null>;
findById(id: any): Promise<MonchDocument<T> | null>;
insertOne(doc: I, opts?: OperationOptions): Promise<MonchDocument<T>>;
// ... all other operations
}MonchDocument<T>
Document with .serialize() and .toJSON():
typescript
type MonchDocument<T> = T & WithSerialize<T>;CollectionConfig<S, TS>
Configuration for collection():
typescript
interface CollectionConfig<S, TS> {
name: string;
schema: S;
uri?: string;
client?: MongoClient;
db?: Db;
database?: string;
timestamps?: boolean;
indexes?: Index[];
createIndexes?: boolean;
hooks?: Hooks<Doc<S>>;
}Query Types
MonchFilter<T>
Relaxed filter type for Zod compatibility:
typescript
type MonchFilter<T> = Filter<T> | Record<string, any>;Allows queries without strict type checking on nested paths and operators.
Timestamp Types
TimestampFields
typescript
interface TimestampFields {
createdAt: Date;
updatedAt: Date;
}WithTimestamps<T>
Add timestamp fields to a type:
typescript
type WithTimestamps<T> = T & TimestampFields;WithTS<T>
Alias for WithTimestamps:
typescript
type WithTS<T> = WithTimestamps<T>;Hook Types
Hooks<T>
Lifecycle hooks definition:
typescript
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
typescript
interface Index {
key: Record<string, IndexDirection>;
unique?: boolean;
sparse?: boolean;
background?: boolean;
expireAfterSeconds?: number;
partialFilterExpression?: object;
name?: string;
collation?: object;
}IndexDirection
typescript
type IndexDirection = 1 | -1 | 'text' | '2dsphere' | '2d' | 'hashed';IndexOptions
typescript
interface IndexOptions {
unique?: boolean;
sparse?: boolean;
background?: boolean;
expireAfterSeconds?: number;
partialFilterExpression?: object;
name?: string;
collation?: object;
}Connection Types
ConnectionConfig
typescript
interface ConnectionConfig {
uri?: string;
client?: MongoClient;
db?: Db;
database?: string;
}ConnectionResult
typescript
interface ConnectionResult {
client: MongoClient;
db: Db;
}DebugConfig
typescript
interface DebugConfig {
enabled: boolean;
logger?: (operation: string, collection: string, ...args: any[]) => void;
}OperationOptions
typescript
interface OperationOptions {
session?: ClientSession;
}Serialization Types
Serialized<T>
Serialized version of a document:
typescript
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:
typescript
interface WithSerialize<T> {
serialize(): Serialized<T>;
}Pagination Types
PaginationResult<T>
typescript
interface PaginationResult<T> {
data: T[];
pagination: {
page: number;
limit: number;
total: number;
totalPages: number;
hasNext: boolean;
hasPrev: boolean;
};
}PaginationOptions
typescript
interface PaginationOptions {
page?: number;
limit?: number;
}Money Types
MoneyValue
typescript
interface MoneyValue {
amount: Decimal128; // High-precision decimal
currency: string; // Currency code
}MoneyOptions
typescript
interface MoneyOptions {
currency?: string; // Currency code (default: 'USD')
min?: number; // Minimum amount
max?: number; // Maximum amount
allowNegative?: boolean; // Allow negative amounts (default: false)
}Type Extraction Helpers
ModelOf<C>
Extract document type from collection:
typescript
type ModelOf<C> = C extends Collection<any, any, infer S> ? Doc<S> : never;SerializedOf<C>
Extract serialized type from collection:
typescript
type SerializedOf<C> = C extends Collection<any, any, infer S> ? Serialized<Doc<S>> : never;SchemaOf<C>
Extract schema type from collection:
typescript
type SchemaOf<C> = C extends Collection<any, any, infer S> ? S : never;