Skip to main content

ModelCustomizer

The ModelCustomizer is the heart of Sentro's customization system. It provides a fluent API for customizing table behavior, appearance, and business logic.

Basic Usage

Register a customizer using the customize() method:

import { ModelCustomizer } from "@sentrodb/connector-node/dist/inc/customizers/modelCustomizer";

connector.customize(() => {
  const c = new ModelCustomizer("users");

  // Add customizations here
  c.rename("System Users");
  c.renameColumn("email", "Email Address");

  return c;
});

Table Display Customization

rename(name: string)

Change the display name of the table:

c.rename("Customer Accounts");

renameColumn(columnName, displayName)

Change the display name of a column:

c.renameColumn("created_at", "Created Date");
c.renameColumn("is_active", "Active Status");

addDisplayField(name, callback)

Add computed fields for display purposes:

// Add full name field
c.addDisplayField("fullName", (record) => {
  return `${record.firstName} ${record.lastName}`;
});

// Add status badge
c.addDisplayField("statusBadge", (record) => {
  return record.isActive ? "Active" : "Inactive";
});

// Add formatted date
c.addDisplayField("joinedDate", (record) => {
  return new Date(record.createdAt).toLocaleDateString();
});
Note: Display fields are computed at runtime and are not stored in the database. They're perfect for combining multiple fields or formatting data for display.

Complete API Reference

Constructor

new ModelCustomizer<TableName>(tableName: TableName)

Display Methods

MethodDescriptionReturns
rename(name: string)Set table display namethis
renameColumn(column, name)Set column display namethis
addDisplayField(name, callback)Add computed display fieldthis

Hook Methods

MethodDescriptionReturns
onBefore(op, handler)Register before hookthis
onAfter(op, handler)Register after hookthis

Field Writer Methods

MethodDescriptionReturns
replaceFieldWriting(field, handler)Replace field writing logicthis

Action Methods

MethodDescriptionReturns
registerTableAction(id, label, callback)Register bulk table actionthis
registerDetailAction(id, label, callback)Register single record actionthis
addAction(config)Unified action registrationthis

Query Methods

MethodDescriptionReturns
getTableAction(id)Get table action by IDFunction | undefined
getDetailAction(id)Get detail action by IDFunction | undefined
getTableActionIds()Get all table action IDsstring[]
getDetailActionIds()Get all detail action IDsstring[]

Chaining Methods

Most methods return this, enabling fluent chaining:

connector.customize(() => {
  return new ModelCustomizer("users")
    .rename("System Users")
    .renameColumn("email", "Email Address")
    .renameColumn("created_at", "Joined Date")
    .addDisplayField("fullName", (r) => `${r.firstName} ${r.lastName}`)
    .onBefore("CREATE", async (payload) => {
      payload.createdAt = new Date().toISOString();
      return payload;
    });
});

Multiple Table Customizations

Register customizers for multiple tables:

// Users table
connector.customize(() => {
  const c = new ModelCustomizer("users");
  c.rename("Customers");
  c.renameColumn("email", "Email Address");
  return c;
});

// Posts table
connector.customize(() => {
  const c = new ModelCustomizer("posts");
  c.rename("Blog Posts");
  c.renameColumn("created_at", "Published Date");
  return c;
});

// Orders table
connector.customize(() => {
  const c = new ModelCustomizer("orders");
  c.rename("Customer Orders");
  c.addDisplayField("total", (r) => `$${r.amount.toFixed(2)}`);
  return c;
});

Type Safety

ModelCustomizer is fully type-safe when used with TypeScript. The table name and column names are validated at compile time:

// ✅ Correct - "users" exists in schema
const c = new ModelCustomizer("users");

// ❌ Error - "nonexistent" not in schema
const c = new ModelCustomizer("nonexistent");

// ✅ Correct - "email" exists in users table
c.renameColumn("email", "Email");

// ❌ Error - "invalid_column" not in users table
c.renameColumn("invalid_column", "Invalid");
Next Steps: Learn about Hooks to intercept and modify CRUD operations.