Skip to main content

Model Alias Configuration

This document introduces how to use human-friendly model aliases to access datasets and how to automatically generate configurations via CLI.

Why Model Aliases?

In the Lovrabet platform, each dataset has a unique datasetCode (e.g., 8d2dcbae08b54bdd84c00be558ed48df), which is an automatically generated hash ID.

Code without aliases:

// Access using datasetCode (hard to read)
const orders = await client.models.dataset_8d2dcbae08b54bdd84c00be558ed48df.filter();
const customers = await client.models.dataset_a1b2c3d4e5f6789012345678abcdef12.filter();
const products = await client.models.dataset_f9e8d7c6b5a4321098765432fedcba98.filter();

Code with aliases:

// Access using aliases (clear and readable)
const orders = await client.models.orders.filter();
const customers = await client.models.customers.filter();
const products = await client.models.products.filter();

Model aliases make code:

  • More Readable - Instantly understand which data table is being operated on
  • Easier to Maintain - No need to memorize complex hash IDs
  • Team-Friendly - Code reviews and handoffs become easier

Two Configuration Methods

Use the lovrabet api pull command to automatically generate configuration files without manual writing.

lovrabet api pull
Prerequisites

Ensure you have completed CLI installation and login configuration.

This will generate two files in your project:

src/api/api.ts - Model configuration file:

import { registerModels, type ModelsConfig } from "@lovrabet/sdk";

export const LOVRABET_MODELS_CONFIG: ModelsConfig = {
appCode: "your-app-code",
models: [
{
datasetCode: "8d2dcbae08b54bdd84c00be558ed48df",
tableName: "orders",
alias: "orders"
},
{
datasetCode: "a1b2c3d4e5f6789012345678abcdef12",
tableName: "customers",
alias: "customers"
},
{
datasetCode: "f9e8d7c6b5a4321098765432fedcba98",
tableName: "products",
alias: "products"
},
// ... all datasets auto-generated
],
};

registerModels(LOVRABET_MODELS_CONFIG);

src/api/client.ts - Pre-configured client:

import { createClient } from "@lovrabet/sdk";
import "./api"; // Auto-register configuration

export const lovrabetClient = createClient();

Using in Your Project

import { lovrabetClient } from "@/api/client";

// Access directly using aliases
const orders = await lovrabetClient.models.orders.filter();
const customers = await lovrabetClient.models.customers.filter();
const products = await lovrabetClient.models.products.filter();
CLI Detailed Guide

For more CLI usage, see Auto-generate SDK Configuration.

Method 2: Manual Configuration

If not using CLI, you can manually configure model aliases.

import { createClient } from "@lovrabet/sdk";

const client = createClient({
appCode: "your-app-code",
models: [
{
datasetCode: "8d2dcbae08b54bdd84c00be558ed48df",
tableName: "orders",
alias: "orders"
},
{
datasetCode: "a1b2c3d4e5f6789012345678abcdef12",
tableName: "customers",
alias: "customers"
},
],
});

// Access using aliases
const orders = await client.models.orders.filter();

Alias Naming Rules

CLI Auto-generated Naming Rules

CLI automatically converts table names to camelCase format aliases:

Table NameGenerated Alias
ordersorders
order_itemsorderItems
user_profileuserProfile
sales_recordssalesRecords
product_categoriesproductCategories

Manual Configuration Naming Suggestions

If manually configuring aliases, follow these rules:

  1. Use camelCase - Consistent with JavaScript naming conventions
  2. Semantic Clarity - Alias should clearly express the table's purpose
  3. Avoid Conflicts - Ensure aliases are unique within the project
  4. Keep Concise - Avoid overly long aliases
// ✅ Good aliases
{ alias: "orders" }
{ alias: "orderItems" }
{ alias: "userProfile" }

// ❌ Not recommended
{ alias: "o" } // Too short, unclear
{ alias: "order_items" } // Uses underscores
{ alias: "theOrderItemsTable" } // Too verbose

Access Method Comparison

After configuring aliases, SDK supports three access methods:

// Method 1: Using alias (recommended)
client.models.orders.filter()

// Method 2: Using dataset_ prefix + datasetCode
client.models.dataset_8d2dcbae08b54bdd84c00be558ed48df.filter()

// Method 3: Using array index access (not recommended)
client.models["dataset_8d2dcbae08b54bdd84c00be558ed48df"].filter()

All three methods are functionally identical, but using aliases is strongly recommended for clearer, more readable code.

TypeScript Type Support

When using aliases, TypeScript provides complete type hints and auto-completion.

import { lovrabetClient } from "@/api/client";

// TypeScript auto-suggests all available model aliases
lovrabetClient.models. // After typing . shows: orders, customers, products...

// Type-safe method calls
const orders = await lovrabetClient.models.orders.filter({
where: { status: { $eq: 'pending' } }, // Complete type hints
pageSize: 20,
});

Updating Configuration

When changes occur in Lovrabet workspace (adding/removing tables), regenerate the configuration.

Update with CLI

lovrabet api pull

CLI will automatically:

  • Add new dataset configurations
  • Remove deleted datasets
  • Update changed dataset information

Manual Update

For manual configuration:

  1. Check dataset list in Lovrabet workspace
  2. Copy new dataset's datasetCode
  3. Add new model configuration to config file

FAQ

Q: Can aliases and datasetCode be used simultaneously?

Yes. After configuring aliases, both methods work:

// Using alias
await client.models.orders.filter();

// Using datasetCode (still valid)
await client.models.dataset_8d2dcbae08b54bdd84c00be558ed48df.filter();

Q: What if alias conflicts occur?

If two tables generate the same alias (e.g., both user and users might generate user), CLI handles conflicts automatically.

For manual configuration, ensure each alias is unique:

models: [
{ datasetCode: "xxx", tableName: "user", alias: "user" },
{ datasetCode: "yyy", tableName: "users", alias: "users" }, // Different alias
]

Q: Can I skip configuring aliases?

Yes. Without aliases, you can still access using dataset_ prefix:

const client = createClient({
appCode: "your-app-code",
models: [
{ datasetCode: "8d2dcbae08b54bdd84c00be558ed48df", tableName: "orders" },
// No alias configured
],
});

// Access using datasetCode
await client.models.dataset_8d2dcbae08b54bdd84c00be558ed48df.filter();

However, configuring aliases is strongly recommended for better code readability.

Q: Can aliases differ from table names?

Yes. Aliases are fully customizable:

models: [
{
datasetCode: "8d2dcbae08b54bdd84c00be558ed48df",
tableName: "t_order", // Database table name
alias: "orders" // Custom alias (more friendly)
},
]

Q: When should I regenerate configuration?

Re-run lovrabet api pull when:

  • ✅ New tables added
  • ✅ Tables deleted
  • ✅ Tables renamed
  • ❌ Table fields changed (no need to regenerate)
  • ❌ Table data changed (no need to regenerate)