Skip to main content

Getting DatabaseManager

const db = client.database(appId: string);

Collections

createCollection

Create a new collection.
await db.createCollection(name: string, options: CollectionOptions);
interface CollectionOptions {
  namespace?: string;
  primary_column?: string;
  sort_column?: string;
}
Example:
await db.createCollection('users', {
  namespace: 'users_ns',
  primary_column: 'id',
  sort_column: 'createdAt'
});

Indexes

createIndex

Create an index on a collection.
await db.createIndex(request: CreateIndexRequest);
interface CreateIndexRequest {
  name: string;
  collection: string;
  field_name: string;
  index_type: 'hash' | 'btree' | 'Price';
  options?: {
    unique?: boolean;
  };
}
Examples:
// Hash index for equality lookups
await db.createIndex({
  name: 'idx_users_email',
  collection: 'users',
  field_name: 'email',
  index_type: 'hash',
  options: { unique: true }
});

// BTree index for range queries
await db.createIndex({
  name: 'idx_users_createdAt',
  collection: 'users',
  field_name: 'createdAt',
  index_type: 'btree'
});

// Price index for value-based payments
await db.createIndex({
  name: 'idx_orders_total',
  collection: 'orders',
  field_name: 'totalPrice',
  index_type: 'Price'
});

Materialized Views

createView

Create a materialized view.
await db.createView(
  name: string,
  sourceCollections: string[],
  query: ViewQuery
);
Example:
const viewQuery = client.queryBuilder()
  .collection('orders')
  .whereField('status').equals('completed')
  .selectAll()
  .getQueryRequest();

await db.createView('completed_orders', ['orders'], viewQuery);

listViews

List all views for the app.
const views = await db.listViews();

getView

Get a specific view.
const view = await db.getView(name: string);

refreshView

Refresh/rebuild view data.
await db.refreshView(name: string);

deleteView

Delete a view.
await db.deleteView(name: string);

View with Aggregations

const aggregatedView = {
  find: {},
  select: {},
  group_by: ['category'],
  aggregate: {
    total_sales: { '$sum': 'amount' },
    order_count: { '$count': '*' },
    avg_order: { '$avg': 'amount' }
  },
  sort_by: ['total_sales'],
  limit: 100
};

await db.createView('sales_by_category', ['orders'], aggregatedView);

Aggregation Operators

OperatorDescription
$sumSum values
$avgAverage values
$countCount records
$countDistinctCount unique values
$minMinimum value
$maxMaximum value