Skip to main content

Client Configuration

interface OnDBConfig {
  endpoint: string;
  appKey?: string;       // App key for writes (X-App-Key header)
  agentKey?: string;     // Agent key with Pay permission (X-Agent-Key header)
  appId?: string;        // Application ID
  apiKey?: string;       // Deprecated: maps to appKey
  timeout?: number;      // Default: 30000
  retryCount?: number;   // Default: 3
  retryDelay?: number;   // Default: 1000
}

Store Types

interface StoreRequest {
  root?: string;         // Format: "app::collection"
  collection?: string;   // Collection name (combined with appId if provided)
  data: Record<string, any>[];
}

interface StoreResponse {
  id: string;
  namespace: string;
  block_height: number;
  transaction_hash: string;
  confirmed: boolean;
  celestia_height: number;
  ticket_id?: string;    // For async operations
}

Query Types

interface QueryRequest {
  root?: string;         // Format: "app::collection"
  collection?: string;
  find?: any;
  select?: any;
  limit?: number;
  offset?: number;
  sortBy?: string;
  sortDirection?: string;
}

interface QueryResponse<T = any> {
  records: T[];
  total: number;
  page: number;
  limit: number;
}

SQL Types

interface SqlQueryResponse {
  data: any[];
  count: number;
  query: string;
  app_id: string;
  collection: string;
}

Payment Types

type ChainType = 'cosmos' | 'evm' | 'solana';
type PaymentMethod = 'native' | 'x402-facilitator';

interface X402Quote {
  quoteId: string;
  totalCost: number;
  amountRaw: string;
  brokerAddress: string;
  description: string;
  expiresAt: number;
  chainType: ChainType;
  network: string;
  asset: string;
  tokenSymbol: string;
  tokenDecimals: number;
  paymentMethod: PaymentMethod;
  facilitator?: string;       // Facilitator URL (if applicable)
  allOptions: X402PaymentRequirement[]; // All available payment options
}

// Payment result returned from the payment callback
interface X402PaymentResult {
  txHash: string;
  network: string;
  sender: string;
  chainType: ChainType;
  paymentMethod: PaymentMethod;
}

// Facilitator payment result (for EVM/Solana via x402 facilitator)
interface X402FacilitatorPaymentResult {
  network: string;
  chainType: 'evm' | 'solana';
  paymentMethod: 'x402-facilitator';
  evmAuthorization?: {
    signature: string;
    authorization: { from: string; to: string; value: string; validAfter: string; validBefore: string; nonce: string };
  };
  solanaAuthorization?: { transaction: string };
}

// Union type for all payment callback results
type X402PaymentCallbackResult = X402PaymentResult | X402FacilitatorPaymentResult;

Pricing Types

interface PricingQuoteRequest {
  app_id: string;
  operation_type: 'read' | 'write';
  size_kb: number;
  collection: string;
  monthly_volume_kb?: number;
  data?: any;            // Sample data for price index calculation
}
PricingQuoteRequest is a type definition for constructing pricing requests. There is no getPricingQuote() client method — pricing is handled automatically via the x402 payment callback flow.

Blob Types

interface UploadBlobRequest {
  collection: string;
  blob: File | Blob | Buffer;
  metadata?: Record<string, any>;
}

interface UploadBlobResponse {
  ticket_id: string;
  blob_id: string;
  status: string;
  message: string;
}

interface BlobMetadata {
  blob_id: string;
  content_type: string;
  size_bytes: number;
  uploaded_at: string;
  tx_hash: string;
  celestia_height: number;
  [key: string]: any;
}

Task Types

type TaskStatus =
  | "Pending"
  | "PaymentBroadcast"
  | "PaymentConfirming"
  | "PaymentConfirmed"
  | "StoringData"
  | "Completed"
  | { Failed: { error: string } };

interface TaskInfo {
  ticket_id: string;
  status: TaskStatus;
  created_at: string;
  updated_at: string;
  operation_type: string;
  user_address?: string;
  transaction_hash?: string;
  block_height?: number;
  result?: any;
  progress_log: string[];
}

interface TransactionStatus {
  id: string;
  status: 'pending' | 'confirmed' | 'failed';
  block_height?: number;
  transaction_hash?: string;
  celestia_height?: number;
  error?: string;
}

Collection Schema Types

interface SimpleCollectionSchema {
  name: string;
  fields: Record<string, SimpleFieldDefinition>;
  useBaseFields?: boolean;  // Default: true. Auto-index id, createdAt, updatedAt, deletedAt
}

interface SimpleFieldDefinition {
  type: 'string' | 'number' | 'boolean' | 'date' | 'object' | 'array';
  index?: boolean;
  unique?: boolean;        // Enables automatic deduplication
  indexType?: 'btree' | 'hash' | 'fulltext' | 'price';
  readPricing?: {
    pricePerAccess?: number;
    pricePerKb?: number;
  };
}

interface SimpleCollectionSchemaWithSharding extends SimpleCollectionSchema {
  sharding?: ShardingStrategy;
}

Sharding Types

interface ShardingStrategy {
  keys: ShardKey[];              // Ordered shard keys
  enforce_in_queries: boolean;   // Require shard keys in queries
  max_shard_size?: number;       // Max bytes per shard
}

interface ShardKey {
  field: string;
  type: 'discrete' | 'time_range' | 'hash_distributed';
  granularity?: 'minute' | 'hour' | 'day' | 'week' | 'month';  // For time_range
  num_buckets?: number;          // For hash_distributed
}

Collection Management Types

interface CreateCollectionResult {
  collection: string;
  indexes: { field: string; type: string; status: 'created' | 'updated' | 'failed'; error?: string }[];
  success: boolean;
  warnings?: string[];
}

interface SyncCollectionResult {
  collection: string;
  created: { field: string; type: string }[];
  removed: { field: string; type: string }[];
  unchanged: { field: string; type: string }[];
  sharding_configured?: boolean;
  success: boolean;
  errors?: string[];
}

interface UpdateCollectionRequest {
  description?: string;
  public?: boolean;
  default_sort_column?: string;
  retention_days?: number | null;
  collection_type?: 'local' | 'offline' | 'api' | 'hybrid';
}

interface CollectionResponse {
  id: string;
  name: string;
  namespace: string;
  primary_column: string;
  sort_column?: string;
  status: string;
  collection_type: string;
  document_count: number;
  size_kb: number;
  created_at: string;
  last_modified: string;
}

Index Types

interface Index {
  name: string;
  collection: string;
  field_name: string;
  index_type: 'btree' | 'hash' | 'fulltext' | 'composite' | 'price';
  fields?: string[];
  store_values?: boolean;
  unique_constraint?: boolean;
  sort_enabled?: boolean;
  price_config?: WriteIndexConfig;       // Write-time payment (price indexes)
  read_price_config?: ReadIndexConfig;   // Read-time payment
  creator_premium_config?: CreatorPremiumConfig; // Creator revenue sharing
}

// Write-time pricing (for price indexes)
type WritePricingModel = 'field_value' | 'per_kb' | 'per_record';

interface WriteIndexConfig {
  pricing_model?: WritePricingModel;
  price_per_kb?: number;       // USDC base units (6 decimals)
  price_per_record?: number;   // USDC base units (6 decimals)
}

// Read-time pricing
type ReadPricingModel = 'per_access' | 'per_kb' | 'per_query';

interface ReadIndexConfig {
  pricing_model?: ReadPricingModel;
  price_per_access?: number;
  price_per_kb?: number;
  price_per_query?: number;
}

// Creator revenue sharing
type CreatorCutModel = 'per_kb' | 'per_value_field' | 'per_value_percent' | 'fixed_amount';

interface CreatorPremiumConfig {
  creator_address_resolution: string; // Dot-notation path to creator address
  creator_cut_model: CreatorCutModel;
  creator_cut_value: number;
  value_field_name?: string;
}

interface IndexOptions {
  unique?: boolean;
  sparse?: boolean;
  background?: boolean;
  partialFilter?: any;
  textIndexVersion?: number;
  weights?: { [field: string]: number };
  defaultLanguage?: string;
}

Retention Types

interface RetentionConfig {
  collection: string;
  retention_days?: number;
  monthly_cost_per_kb: number;
  last_cleanup?: string;
  status: string;
}

interface CollectionRetentionCost {
  collection: string;
  retention_days?: number;
  size_kb: number;
  monthly_cost_per_kb: number;
  monthly_cost: number;
  projected_size_kb: number;
  projected_monthly_cost: number;
}

interface RetentionCostResponse {
  app_id: string;
  collections: CollectionRetentionCost[];
  total_monthly_cost: number;
  projected_next_month_cost: number;
}

Predefined Query Types

interface CreateQueryRequest {
  name: string;
  source_collection: string;
  base_query: any;
  parameters?: QueryParameter[];
  description?: string;
  version?: number;
}

interface QueryParameter {
  name: string;
  field_path: string;
  default?: any;
  required?: boolean;
  description?: string;
}

interface QueryDataResponse {
  success: boolean;
  query_name: string;
  data: any[];
  count: number;
  query_time_ms: number;
}

Relation Types

interface RelationRequest {
  parent_collection: string;
  parent_field: string;
  child_collection: string;
  child_field: string;
}

interface RelationResponse {
  parent_collection: string;
  parent_field: string;
  child_field: string;
  created_at: string;
  indexes_created: {
    parent_index: boolean;
    child_index: boolean;
  };
}

App Key Types

type AppKeyPermission = 'Read' | 'Write' | 'Admin' | 'Pay';

interface PayLimits {
  expires_at?: string;           // ISO 8601 expiry
  max_payment_per_tx?: number;   // USDC in base units (6 decimals)
  spend_allowance?: number;      // Lifetime cap
  allowed_target_apps?: string[]; // Whitelist target apps
}

interface AppKeyInfo {
  key_hash: string;
  name: string;
  permissions: AppKeyPermission[];
  expires_at?: string;
  max_payment_per_tx?: number;
  spend_allowance?: number;
  spend_allowance_used?: number;
  allowed_target_apps?: string[];
}

Error Types

class OnDBError extends Error {
  code: string;
  statusCode?: number;
  details?: any;
}

class ValidationError extends OnDBError {
  // statusCode: 400
}

class TransactionError extends OnDBError {
  transactionId: string;
}

class PaymentRequiredError extends OnDBError {
  paymentRequired: X402PaymentRequiredResponse;
  // statusCode: 402
}

class PaymentVerificationError extends OnDBError {
  txHash: string;
  // statusCode: 402
}

Base Document

interface BaseDocument {
  id: string;
  createdAt: string;
  updatedAt: string;
  deletedAt?: string | null;
}