Skip to content

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,

  // 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):

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<S, M, ST>

Full collection type with schema, methods, and statics:

typescript
type Collection<S, M, ST> = CollectionInstance<Doc<S>, M> & ST;

CollectionInstance<T, M>

Collection instance with all methods:

typescript
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():

typescript
type MonchDocument<T, M> = T & M & WithSerialize<T>;

CollectionConfig<S, M, ST>

Configuration for collection():

typescript
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:

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>;

Method Types

Methods<T>

Instance methods definition:

typescript
type Methods<T> = Record<string, (doc: T, ...args: any[]) => any>;

Statics<T, M>

Static methods definition:

typescript
type Statics<T, M> = Record<string, (col: CollectionInstance<T, M>, ...args: any[]) => any>;

WithMethods<T, M>

Document with methods attached:

typescript
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:

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: number;
  currency: string;
  formatted: string;
}

MoneyOptions

typescript
interface MoneyOptions {
  currency?: string;
  decimals?: number;
}

Type Extraction Helpers

ModelOf<C>

Extract document type from collection:

typescript
type ModelOf<C> = C extends Collection<infer S, any, any> ? Doc<S> : never;

SerializedOf<C>

Extract serialized type from collection:

typescript
type SerializedOf<C> = C extends Collection<infer S, any, any> ? Serialized<Doc<S>> : never;

SchemaOf<C>

Extract schema type from collection:

typescript
type SchemaOf<C> = C extends Collection<infer S, any, any> ? S : never;

Released under the MIT License.