Skip to content

@codesoul-co/hypha-memory / mongo-structured-store

模块用法

用于持久化并读取该边界的数据。Mongo structured store 模块公开 1 类、1 函数、7 接口、1 类型。

从包入口导入

ts
import {
  MongoStructuredStoreProvider,
  normalizeMongoStructuredStoreError,
} from '@codesoul-co/hypha-memory';

import type {
  MongoCollectionLike,
  MongoCursorLike,
  MongoDatabaseLike,
  MongoOperationOptionsLike,
  MongoSessionLike,
  MongoStructuredStoreHealth,
  MongoStructuredStoreProviderOptions,
  MongoTransactionMode,
} from '@codesoul-co/hypha-memory';

使用要点

  • 8 个类型/接口用于应用代码、Adapter 或测试中的静态契约;请使用 import type,运行时不应依赖它们。
  • 1 个类提供可实例化的运行时实现;构造参数与公开方法在各自条目中完整列出。
  • 1 个函数是该模块的直接操作入口;每个 overload 的必需/可选参数与返回类型均在下方列出。

公共导出

Symbol种类签名说明
MongoStructuredStoreProvidernew MongoStructuredStoreProvider(options: MongoStructuredStoreProviderOptions, session?: MongoSessionLike | undefined): MongoStructuredStoreProviderMongo-backed StructuredStoreProvider without leaking a Mongo SDK into Memory contracts.
normalizeMongoStructuredStoreError函数normalizeMongoStructuredStoreError(error: unknown, operation: string): import("./contracts").NormalizedMemoryErrorNormalize Mongo Structured Store Error 函数,提供 1 个公开调用签名;参数与返回类型见下表。
MongoCollectionLike接口interface MongoCollectionLikeMongo Collection Like 接口,共包含 6 个公开字段或方法。
MongoCursorLike接口interface MongoCursorLikeMongo Cursor Like 接口,共包含 3 个公开字段或方法。
MongoDatabaseLike接口interface MongoDatabaseLikeMongo Database Like 接口,共包含 3 个公开字段或方法。
MongoOperationOptionsLike接口interface MongoOperationOptionsLikeMongo Operation Options Like 接口,共包含 1 个公开字段或方法。
MongoSessionLike接口interface MongoSessionLikeMongo Session Like 接口,共包含 2 个公开字段或方法。
MongoStructuredStoreHealth接口interface MongoStructuredStoreHealthMongo Structured Store Health 接口,共包含 3 个公开字段或方法。
MongoStructuredStoreProviderOptions接口interface MongoStructuredStoreProviderOptionsMongo Structured Store Provider Options 接口,共包含 3 个公开字段或方法。
MongoTransactionMode类型type MongoTransactionMode = 'required' | 'preferred' | 'disabled'Mongo Transaction Mode 公共类型别名;完整类型表达式见声明。

MongoStructuredStoreProvider

Mongo-backed StructuredStoreProvider without leaking a Mongo SDK into Memory contracts.

  • 种类: 类
  • 导入: import { MongoStructuredStoreProvider } from '@codesoul-co/hypha-memory';
  • 源码模块: mongo-structured-store

声明

text
export declare class MongoStructuredStoreProvider implements StructuredStoreProvider {
    constructor(options: MongoStructuredStoreProviderOptions, session?: MongoSessionLike | undefined);
    get<T>(table: string, id: string): Promise<T | null>;
    insert<T extends {
            id: string;
        }>(table: string, record: T): Promise<void>;
    update<T>(table: string, id: string, patch: Partial<T>): Promise<void>;
    compareAndSet<T>(table: string, id: string, expected: Partial<T>, patch: Partial<T>): Promise<boolean>;
    delete(table: string, id: string): Promise<void>;
    query<T>(table: string, query: StructuredQuery): Promise<T[]>;
    transaction<T>(operation: (tx: StructuredStoreProvider) => Promise<T>): Promise<T>;
    supportsTransactions(): boolean;
    initialize(collections: readonly string[]): Promise<void>;
    health(): Promise<MongoStructuredStoreHealth>;
}

公开成员

成员种类签名说明
compareAndSet方法compareAndSet<T>(table: string, id: string, expected: Partial<T>, patch: Partial<T>): Promise<boolean>公开方法;参数与返回类型以签名列为准。
constructor构造函数(options: MongoStructuredStoreProviderOptions, session?: MongoSessionLike | undefined): MongoStructuredStoreProvider创建该类的实例。
delete方法delete(table: string, id: string): Promise<void>公开方法;参数与返回类型以签名列为准。
get方法get<T>(table: string, id: string): Promise<T | null>公开方法;参数与返回类型以签名列为准。
health方法health(): Promise<MongoStructuredStoreHealth>公开方法;参数与返回类型以签名列为准。
initialize方法initialize(collections: readonly string[]): Promise<void>公开方法;参数与返回类型以签名列为准。
insert方法insert<T extends { id: string; }>(table: string, record: T): Promise<void>公开方法;参数与返回类型以签名列为准。
query方法query<T>(table: string, query: StructuredQuery): Promise<T[]>公开方法;参数与返回类型以签名列为准。
supportsTransactions方法supportsTransactions(): boolean公开方法;参数与返回类型以签名列为准。
transaction方法transaction<T>(operation: (tx: StructuredStoreProvider) => Promise<T>): Promise<T>公开方法;参数与返回类型以签名列为准。
update方法update<T>(table: string, id: string, patch: Partial<T>): Promise<void>公开方法;参数与返回类型以签名列为准。

normalizeMongoStructuredStoreError

Normalize Mongo Structured Store Error 函数,提供 1 个公开调用签名;参数与返回类型见下表。

  • 种类: 函数
  • 导入: import { normalizeMongoStructuredStoreError } from '@codesoul-co/hypha-memory';
  • 源码模块: mongo-structured-store

声明

text
export declare function normalizeMongoStructuredStoreError(error: unknown, operation: string): import("./contracts").NormalizedMemoryError;

调用签名

text
normalizeMongoStructuredStoreError(error: unknown, operation: string): import("./contracts").NormalizedMemoryError

参数

参数类型必需说明
errorunknown必需参数;接受的值由类型列定义。
operationstring必需参数;接受的值由类型列定义。

返回值

  • 类型: NormalizedMemoryError
  • 说明: 返回值契约由上述类型定义。

MongoCollectionLike

Mongo Collection Like 接口,共包含 6 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { MongoCollectionLike } from '@codesoul-co/hypha-memory';
  • 源码模块: mongo-structured-store

声明

text
export interface MongoCollectionLike {
    findOne<T>(filter: Record<string, unknown>, options?: MongoOperationOptionsLike): Promise<T | null>;
    insertOne<T extends object>(document: T, options?: MongoOperationOptionsLike): Promise<unknown>;
    updateOne(filter: Record<string, unknown>, update: {
        $set: Record<string, unknown>;
    }, options?: MongoOperationOptionsLike): Promise<{
        matchedCount?: number;
    }>;
    deleteOne(filter: Record<string, unknown>, options?: MongoOperationOptionsLike): Promise<{
        deletedCount?: number;
    }>;
    find<T>(filter: Record<string, unknown>, options?: MongoOperationOptionsLike): MongoCursorLike<T>;
    createIndex?(keys: Record<string, 1 | -1>, options?: {
        unique?: boolean;
        name?: string;
    }): Promise<unknown>;
}

契约成员

成员种类签名说明
createIndex方法createIndex?(keys: Record<string, 1 | -1>, options?: { unique?: boolean; name?: string; }): Promise<unknown>公开方法;参数与返回类型以签名列为准。
deleteOne方法deleteOne(filter: Record<string, unknown>, options?: MongoOperationOptionsLike): Promise<{ deletedCount?: number; }>公开方法;参数与返回类型以签名列为准。
find方法find<T>(filter: Record<string, unknown>, options?: MongoOperationOptionsLike): MongoCursorLike<T>公开方法;参数与返回类型以签名列为准。
findOne方法findOne<T>(filter: Record<string, unknown>, options?: MongoOperationOptionsLike): Promise<T | null>公开方法;参数与返回类型以签名列为准。
insertOne方法insertOne<T extends object>(document: T, options?: MongoOperationOptionsLike): Promise<unknown>公开方法;参数与返回类型以签名列为准。
updateOne方法updateOne(filter: Record<string, unknown>, update: { $set: Record<string, unknown>; }, options?: MongoOperationOptionsLike): Promise<{ matchedCount?: number; }>公开方法;参数与返回类型以签名列为准。

MongoCursorLike

Mongo Cursor Like 接口,共包含 3 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { MongoCursorLike } from '@codesoul-co/hypha-memory';
  • 源码模块: mongo-structured-store

声明

text
export interface MongoCursorLike<T> {
    sort?(order: Record<string, 1 | -1>): MongoCursorLike<T>;
    limit?(limit: number): MongoCursorLike<T>;
    toArray(): Promise<T[]>;
}

契约成员

成员种类签名说明
limit方法limit?(limit: number): MongoCursorLike<T>公开方法;参数与返回类型以签名列为准。
sort方法sort?(order: Record<string, 1 | -1>): MongoCursorLike<T>公开方法;参数与返回类型以签名列为准。
toArray方法toArray(): Promise<T[]>公开方法;参数与返回类型以签名列为准。

MongoDatabaseLike

Mongo Database Like 接口,共包含 3 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { MongoDatabaseLike } from '@codesoul-co/hypha-memory';
  • 源码模块: mongo-structured-store

声明

text
export interface MongoDatabaseLike {
    collection(name: string): MongoCollectionLike;
    startSession?(): MongoSessionLike;
    command?(command: Record<string, unknown>): Promise<unknown>;
}

契约成员

成员种类签名说明
collection方法collection(name: string): MongoCollectionLike公开方法;参数与返回类型以签名列为准。
command方法command?(command: Record<string, unknown>): Promise<unknown>公开方法;参数与返回类型以签名列为准。
startSession方法startSession?(): MongoSessionLike公开方法;参数与返回类型以签名列为准。

MongoOperationOptionsLike

Mongo Operation Options Like 接口,共包含 1 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { MongoOperationOptionsLike } from '@codesoul-co/hypha-memory';
  • 源码模块: mongo-structured-store

声明

text
export interface MongoOperationOptionsLike {
    session?: MongoSessionLike;
}

契约成员

成员种类签名说明
session属性session?: MongoSessionLike公开属性;类型、只读和可选状态以签名列为准。

MongoSessionLike

Mongo Session Like 接口,共包含 2 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { MongoSessionLike } from '@codesoul-co/hypha-memory';
  • 源码模块: mongo-structured-store

声明

text
export interface MongoSessionLike {
    withTransaction<T>(operation: () => Promise<T>): Promise<T>;
    endSession(): Promise<void>;
}

契约成员

成员种类签名说明
endSession方法endSession(): Promise<void>公开方法;参数与返回类型以签名列为准。
withTransaction方法withTransaction<T>(operation: () => Promise<T>): Promise<T>公开方法;参数与返回类型以签名列为准。

MongoStructuredStoreHealth

Mongo Structured Store Health 接口,共包含 3 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { MongoStructuredStoreHealth } from '@codesoul-co/hypha-memory';
  • 源码模块: mongo-structured-store

声明

text
export interface MongoStructuredStoreHealth {
    status: 'healthy' | 'degraded' | 'unhealthy';
    transactions: boolean;
    message?: string;
}

契约成员

成员种类签名说明
message属性message?: string公开属性;类型、只读和可选状态以签名列为准。
status属性status: "healthy" | "degraded" | "unhealthy"公开属性;类型、只读和可选状态以签名列为准。
transactions属性transactions: boolean公开属性;类型、只读和可选状态以签名列为准。

MongoStructuredStoreProviderOptions

Mongo Structured Store Provider Options 接口,共包含 3 个公开字段或方法。

  • 种类: 接口
  • 导入: import type { MongoStructuredStoreProviderOptions } from '@codesoul-co/hypha-memory';
  • 源码模块: mongo-structured-store

声明

text
export interface MongoStructuredStoreProviderOptions {
    database: MongoDatabaseLike;
    transactionMode?: MongoTransactionMode;
    collectionPrefix?: string;
}

契约成员

成员种类签名说明
collectionPrefix属性collectionPrefix?: string公开属性;类型、只读和可选状态以签名列为准。
database属性database: MongoDatabaseLike公开属性;类型、只读和可选状态以签名列为准。
transactionMode属性transactionMode?: MongoTransactionMode公开属性;类型、只读和可选状态以签名列为准。

MongoTransactionMode

Mongo Transaction Mode 公共类型别名;完整类型表达式见声明。

  • 种类: 类型
  • 导入: import type { MongoTransactionMode } from '@codesoul-co/hypha-memory';
  • 源码模块: mongo-structured-store

声明

text
export type MongoTransactionMode = 'required' | 'preferred' | 'disabled';