import type { D1Database } from '@cloudflare/workers-types'; import { MastraBase } from '@mastra/core/base'; import type { TABLE_NAMES, StorageColumn } from '@mastra/core/storage'; import Cloudflare from 'cloudflare'; import type { SqlQueryOptions } from '../sql-builder.js'; import type { RunFenceCheck } from './run-fencing.js'; export type D1QueryResult = Awaited>['result']; export interface D1Client { query(args: { sql: string; params: string[]; }): Promise<{ result: D1QueryResult; }>; } export interface D1DBConfig { client?: D1Client; binding?: D1Database; tablePrefix?: string; } /** * Configuration for standalone domain usage. * Accepts either: * 1. An existing D1 client or binding * 2. Config to create a new client internally */ export type D1DomainConfig = D1DomainClientConfig | D1DomainBindingConfig | D1DomainRestConfig; /** * Pass an existing D1 client (REST API) */ export interface D1DomainClientConfig { client: D1Client; tablePrefix?: string; } /** * Pass an existing D1 binding (Workers API) */ export interface D1DomainBindingConfig { binding: D1Database; tablePrefix?: string; } /** * Pass config to create a new D1 client internally (REST API) */ export interface D1DomainRestConfig { accountId: string; apiToken: string; databaseId: string; tablePrefix?: string; } /** * Resolves D1DomainConfig to D1DBConfig. * Handles creating a new D1 client if apiToken is provided. */ export declare function resolveD1Config(config: D1DomainConfig): D1DBConfig; export declare class D1DB extends MastraBase { private client?; private binding?; private tablePrefix; /** Cache of actual table columns: tableName -> Promise> (stores in-flight promise to coalesce concurrent calls) */ private tableColumnsCache; constructor(config: D1DBConfig); hasColumn(table: string, column: string): Promise; getTableName(tableName: TABLE_NAMES): string; private formatSqlParams; private executeWorkersBindingQuery; private executeRestQuery; /** * Delete up to `limit` rows keyed by `runId` whose epoch-ms `column` is older * than `cutoff`. Used to prune the run fencing tables. */ pruneRunRows({ tableName, column, cutoff, limit, }: { tableName: TABLE_NAMES; column: string; cutoff: number; limit: number; }): Promise; executeQuery(options: SqlQueryOptions): Promise[] | Record | null>; /** * Gets the set of column names that actually exist in the database table. * Results are cached; the cache is invalidated when alterTable() adds new columns. */ private getKnownColumnNames; /** * Filters a record to only include columns that exist in the actual database table. * Unknown columns are silently dropped to ensure forward compatibility. */ private filterRecordToKnownColumns; private getTableColumns; private serializeValue; protected getSqlType(type: StorageColumn['type']): string; protected getDefaultValue(type: StorageColumn['type']): string; createTable({ tableName, schema, }: { tableName: TABLE_NAMES; schema: Record; }): Promise; clearTable({ tableName }: { tableName: TABLE_NAMES; }): Promise; dropTable({ tableName }: { tableName: TABLE_NAMES; }): Promise; alterTable(args: { tableName: TABLE_NAMES; schema: Record; ifNotExists: string[]; }): Promise; insert({ tableName, record }: { tableName: TABLE_NAMES; record: Record; }): Promise; batchInsert({ tableName, records }: { tableName: TABLE_NAMES; records: Record[]; }): Promise; load({ tableName, keys }: { tableName: TABLE_NAMES; keys: Record; }): Promise; processRecord(record: Record): Promise>; /** * Upsert multiple records in a batch operation * @param tableName The table to insert into * @param records The records to insert * @param check Run fence every upsert must hold; a refused upsert throws and stops the batch */ batchUpsert({ tableName, records, check, }: { tableName: TABLE_NAMES; records: Record[]; check?: RunFenceCheck; }): Promise; } //# sourceMappingURL=index.d.ts.map