Skip to main content

TableRegistry

The TableRegistry maps database table names to compact short tags and Redis version counter keys. It also supports alias mappings between logical ORM model names and physical database table names.

import { type TableRegistry, resolveTable, type ResolvedTable } from 'cachemate';

Type Definitions

export interface TableRegistry {
/** Map of physical table names to their tag and version key */
entries: Record<string, {
/** Short alphanumeric tag used in composite cache key generation (e.g. 'u', 'o', 'usr') */
tag: string;
/** Redis key holding the integer version counter (e.g. 'cm:ver:users') */
versionKey: string;
}>;
/** Optional alias map from logical ORM entity names to physical database table names */
alias?: Record<string, string>;
}

export interface ResolvedTable {
physical: string;
tag: string;
versionKey: string;
}

Example Configuration

import { Client, type TableRegistry } from 'cachemate';

const registry: TableRegistry = {
entries: {
users: {
tag: 'u',
versionKey: 'cm:ver:users',
},
orders: {
tag: 'o',
versionKey: 'cm:ver:orders',
},
order_items: {
tag: 'oi',
versionKey: 'cm:ver:order_items',
},
products: {
tag: 'p',
versionKey: 'cm:ver:products',
},
},
alias: {
// Drizzle / Prisma / CamelCase model names mapped to physical tables
orderItems: 'order_items',
User: 'users',
Product: 'products',
},
};

const client = new Client({
memoryMaxMb: 128,
registry,
});

resolveTable() Function

The resolveTable() helper resolves a table name (whether physical or alias) to its ResolvedTable metadata.

import { resolveTable } from 'cachemate';

const resolved = resolveTable(registry, 'orderItems');
console.log(resolved);
// Output:
// {
// physical: 'order_items',
// tag: 'oi',
// versionKey: 'cm:ver:order_items'
// }

Error Handling

If a table is queried via client.cache() or client.rows() that has not been defined in the TableRegistry:

  • resolveTable throws an error: Unregistered table: <name> (physical: <physical>).
  • Cachemate catches this error, logs a diagnostic warning, and safely bypasses the cache to invoke the loader directly, preventing runtime application crashes.