Skip to main content

store

Store documents in a collection with payment.
TypeScript
const result = await client.store(
  {
    collection: 'users',
    data: [{ email: 'alice@example.com', name: 'Alice', active: true }]
  },
  async (quote) => {
    // quote contains: totalCost, brokerAddress, tokenSymbol, network, chainType
    const txHash = await processPayment(quote);
    return { txHash, network: quote.network, sender: walletAddress, chainType: quote.chainType, paymentMethod: 'native' };
  },
  true // waitForConfirmation
);

console.log('Stored at block:', result.block_height);

Parameters

ParameterTypeDescription
requestStoreRequestCollection name and data array
paymentCallbackfunctionCallback that receives a quote and returns payment result
waitForConfirmationbooleanWhether to wait for confirmation

Bulk Creation

Store multiple documents at once:
TypeScript
const result = await client.store(
  {
    collection: 'users',
    data: [
      { email: 'alice@example.com', name: 'Alice' },
      { email: 'bob@example.com', name: 'Bob' },
      { email: 'charlie@example.com', name: 'Charlie' }
    ]
  },
  async (quote) => {
    const txHash = await processPayment(quote);
    return { txHash, network: quote.network, sender: walletAddress, chainType: quote.chainType, paymentMethod: 'native' };
  },
  true
);

console.log('Stored at block:', result.block_height);

Error Handling

TypeScript
import { ValidationError, OnDBError } from '@ondb/sdk';

try {
  await client.store(
    { collection: 'users', data: [{ email: 'alice@example.com' }] },
    paymentCallback,
    true
  );
} catch (error) {
  if (error instanceof ValidationError) {
    console.log('Validation failed:', error.message);
  } else if (error instanceof OnDBError) {
    console.log('OnDB error:', error.code);
  }
}

Next Steps

Read

Find and query documents

Update

Update existing documents