@samveys/mongodb

Typed document ODM with injection-safe queries and transactions

Version
1.0.3Nov 5, 2026
Install
pnpm add @samveys/mongodb

This list was read from version 1.0.2; run pnpm sync to refresh it.

Import
import { createMongoManager, defineDocument, mongo, field } from '@samveys/mongodb'

Function (3)

createMongoManager Function

No documentation comment.

function createMongoManager(config: MongoConfig): MongoManager;

Exported from packages/mongodb/src/manager.ts

defineDocument Function

Declares a typed document collection; bind it to a manager with `manager.model(definition)`.

function defineDocument<S extends Schema>(name: string, schema: S): ModelDefinition<S>;

Exported from packages/mongodb/src/index.ts

mongo Function

The `@samveys/mongodb` plugin: binds `'mongo'` (the {@link MongoManager}); closes the client on `app:terminating`.

function mongo(config: MongoConfig): import("@samveys/core").Plugin<MongoConfig>;

Exported from packages/mongodb/src/plugin.ts

Value (1)

field Value

Field builders, mirroring the ORM's `col.*` naming.

const field: {
    string: () => Field<string>;

Exported from packages/mongodb/src/fields.ts

Interface (10)

Field Interface

A typed field builder. `T` is the value type, `O` marks optional fields and `A` marks fields filled automatically (so they are not required on create).

interface Field<T, O extends boolean = false, A extends boolean = false> {
    readonly meta: FieldMeta;
    /** Phantom type carrier; never set at runtime. */
    readonly _type?: T;
    readonly _optional?: O;
    readonly _auto?: A;
    optional(): Field<T, true, A>;
    unique(): Field<T, O, A>;
    autoCreate(): Field<T, O, true>;
    autoUpdate(): Field<T, O, true>;
}

Exported from packages/mongodb/src/contracts.ts

FieldMeta Interface

No documentation comment.

interface FieldMeta {
    readonly kind: FieldKind;
    readonly optional: boolean;
    readonly unique: boolean;
    readonly autoCreate: boolean;
    readonly autoUpdate: boolean;
    readonly item?: FieldMeta;
}

Exported from packages/mongodb/src/contracts.ts

Model Interface

No documentation comment.

interface Model<S extends Schema> {
    readonly name: string;
    readonly schema: S;
    create(data: CreateInput<S>, options?: ModelOptions): Promise<DocumentOf<S>>;
    find(id: string, options?: ModelOptions): Promise<DocumentOf<S> | null>;
    findOrFail(id: string, options?: ModelOptions): Promise<DocumentOf<S>>;
    query(options?: ModelOptions): QueryBuilder<S>;
    update(id: string, data: UpdateInput<S>, options?: ModelOptions): Promise<DocumentOf<S>>;
    delete(id: string, options?: ModelOptions): Promise<boolean>;
    count(options?: ModelOptions): Promise<number>;
  …
Show the whole declaration (13 lines)
interface Model<S extends Schema> {
    readonly name: string;
    readonly schema: S;
    create(data: CreateInput<S>, options?: ModelOptions): Promise<DocumentOf<S>>;
    find(id: string, options?: ModelOptions): Promise<DocumentOf<S> | null>;
    findOrFail(id: string, options?: ModelOptions): Promise<DocumentOf<S>>;
    query(options?: ModelOptions): QueryBuilder<S>;
    update(id: string, data: UpdateInput<S>, options?: ModelOptions): Promise<DocumentOf<S>>;
    delete(id: string, options?: ModelOptions): Promise<boolean>;
    count(options?: ModelOptions): Promise<number>;
    /** Creates declared unique indexes; safe to call repeatedly. */
    syncIndexes(): Promise<string[]>;
}

Exported from packages/mongodb/src/contracts.ts

ModelDefinition Interface

No documentation comment.

interface ModelDefinition<S extends Schema> {
    readonly name: string;
    readonly schema: S;
    bind(manager: MongoManager): Model<S>;
}

Exported from packages/mongodb/src/contracts.ts

ModelOptions Interface

No documentation comment.

interface ModelOptions {
    readonly session?: MongoSession | undefined;
}

Exported from packages/mongodb/src/contracts.ts

MongoConfig Interface

No documentation comment.

interface MongoConfig {
    readonly url: string;
    readonly database: string;
    readonly serverSelectionTimeoutMs?: number;
}

Exported from packages/mongodb/src/contracts.ts

MongoManager Interface

No documentation comment.

interface MongoManager {
    connect(): Promise<void>;
    close(): Promise<void>;
    model<S extends Schema>(definition: ModelDefinition<S>): Model<S>;
    transaction<T>(callback: (session: MongoSession) => Promise<T>): Promise<T>;

Exported from packages/mongodb/src/contracts.ts

MongoSession Interface

Opaque handle passed to `transaction()` callbacks.

interface MongoSession {
    readonly handle: unknown;
}

Exported from packages/mongodb/src/contracts.ts

Page Interface

No documentation comment.

interface Page<T> {
    readonly data: readonly T[];
    readonly total: number;
    readonly page: number;
    readonly perPage: number;
    readonly lastPage: number;
}

Exported from packages/mongodb/src/contracts.ts

QueryBuilder Interface

No documentation comment.

interface QueryBuilder<S extends Schema> {
    where<K extends keyof S & string>(field: K, value: Matchable<ValueOf<S[K]>>): QueryBuilder<S>;
    where<K extends keyof S & string>(field: K, operator: WhereOperator, value: Matchable<ValueOf<S[K]>> | readonly Matchable<ValueOf<S[K]>>[]): QueryBuilder<S>;
    orderBy(field: (keyof S & string) | 'id', direction?: OrderDirection): QueryBuilder<S>;
    limit(count: number): QueryBuilder<S>;
    offset(count: number): QueryBuilder<S>;
    withSession(session: MongoSession): QueryBuilder<S>;
    get(): Promise<DocumentOf<S>[]>;
    first(): Promise<DocumentOf<S> | null>;
    count(): Promise<number>;
  …
Show the whole declaration (14 lines)
interface QueryBuilder<S extends Schema> {
    where<K extends keyof S & string>(field: K, value: Matchable<ValueOf<S[K]>>): QueryBuilder<S>;
    where<K extends keyof S & string>(field: K, operator: WhereOperator, value: Matchable<ValueOf<S[K]>> | readonly Matchable<ValueOf<S[K]>>[]): QueryBuilder<S>;
    orderBy(field: (keyof S & string) | 'id', direction?: OrderDirection): QueryBuilder<S>;
    limit(count: number): QueryBuilder<S>;
    offset(count: number): QueryBuilder<S>;
    withSession(session: MongoSession): QueryBuilder<S>;
    get(): Promise<DocumentOf<S>[]>;
    first(): Promise<DocumentOf<S> | null>;
    count(): Promise<number>;
    paginate(page?: number, perPage?: number): Promise<Page<DocumentOf<S>>>;
    update(data: UpdateInput<S>): Promise<number>;
    delete(): Promise<number>;
}

Exported from packages/mongodb/src/contracts.ts

Type (8)

AnyField Type

No documentation comment.

type AnyField = Field<any, boolean, boolean>;

Exported from packages/mongodb/src/contracts.ts

CreateInput Type

Input accepted by `create`: auto and optional fields may be omitted.

type CreateInput<S extends Schema> = Flatten<{
    [K in Exclude<keyof S, OptionalKeys<S> | AutoKeys<S>>]: ValueOf<S[K]>;
} & {
    [K in Exclude<OptionalKeys<S> | AutoKeys<S>, never> & keyof S]?: ValueOf<S[K]> | undefined;
}>;

Exported from packages/mongodb/src/contracts.ts

DocumentOf Type

The stored document as returned by the API (`id` is the ObjectId hex string).

type DocumentOf<S extends Schema> = Flatten<{
    id: string;
} & {
    [K in Exclude<keyof S, OptionalKeys<S>>]: ValueOf<S[K]>;
} & {
    [K in OptionalKeys<S>]?: ValueOf<S[K]> | undefined;
}>;

Exported from packages/mongodb/src/contracts.ts

FieldKind Type

No documentation comment.

type FieldKind = 'string' | 'int' | 'number' | 'boolean' | 'date' | 'array';

Exported from packages/mongodb/src/contracts.ts

OrderDirection Type

No documentation comment.

type OrderDirection = 'asc' | 'desc';

Exported from packages/mongodb/src/contracts.ts

Schema Type

No documentation comment.

type Schema = Record<string, AnyField>;

Exported from packages/mongodb/src/contracts.ts

UpdateInput Type

Input accepted by `update`: every field optional.

type UpdateInput<S extends Schema> = {
    [K in keyof S]?: ValueOf<S[K]> | null;
};

Exported from packages/mongodb/src/contracts.ts

WhereOperator Type

No documentation comment.

type WhereOperator = '=' | '!=' | '>' | '>=' | '<' | '<=' | 'in' | 'not in';

Exported from packages/mongodb/src/contracts.ts