Skip to main content

Auto-generate SDK Configuration

About This Guide

This guide explains how to use the CLI's api pull command to automatically generate SDK configuration files, saving you the trouble of manual configuration.

If you want to learn about complete SDK configuration documentation (manual configuration, multi-environment configuration, etc.), please refer to the SDK Configuration Guide.

Understanding the Background

When you connect a database in the Lovrabet workspace, the system automatically generates a Dataset for each data table.

Each dataset has:

  • A unique datasetCode (e.g., 8d2dcbae08b54bdd84c00be558ed48df)
  • A corresponding data table name (e.g., orders)
  • Auto-generated CRUD APIs
┌─────────────────────────────────────────────────────────┐
│ Lovrabet Workspace │
├─────────────────────────────────────────────────────────┤
│ Connect Database │
│ ↓ │
│ Auto-analyze Table Structure │
│ ↓ │
│ Generate Dataset for Each Table │
│ ↓ │
│ Each Dataset Has Auto-generated APIs (CRUD) │
└─────────────────────────────────────────────────────────┘

What Configuration Does the SDK Need?

To use the Lovrabet SDK in your frontend project, you need to tell the SDK "which datasets you want to access":

import { registerModels, createClient } from '@lovrabet/sdk';

// This configuration tells the SDK: which datasets does my application have
registerModels({
appCode: 'your-app-code',
models: [
{ datasetCode: '8d2dcbae08b54bdd84c00be558ed48df', tableName: 'orders', alias: 'orders' },
{ datasetCode: 'a1b2c3d4e5f6789012345678abcdef12', tableName: 'customers', alias: 'customers' },
{ datasetCode: 'f9e8d7c6b5a4321098765432fedcba98', tableName: 'products', alias: 'products' },
// ... possibly dozens of datasets
],
});

const client = createClient();

The problem: Where do these datasetCode values come from? Copy and paste them one by one manually?

CLI Auto-generates This Configuration

The lovrabet api pull command solves this problem:

lovrabet api pull

It will:

  1. Connect to the Lovrabet backend
  2. Get all dataset information under your application
  3. Automatically generate the configuration files needed by the SDK
┌─────────────────────────────────────────────────────────┐
│ lovrabet api pull │
├─────────────────────────────────────────────────────────┤
│ 1. Read your appCode │
│ 2. Get all datasets from Lovrabet backend │
│ 3. Generate src/api/api.ts (configuration file) │
│ 4. Generate src/api/client.ts (ready-to-use client) │
└─────────────────────────────────────────────────────────┘

How to Use

Step 1: Ensure You Are Logged In (Skip if already logged in)

lovrabet auth

Step 2: Set Application Code (Skip if already configured)

lovrabet config set app your-app-code

Or specify directly in the command:

lovrabet api pull --appcode your-app-code

Step 3: Pull Configuration

lovrabet api pull

After execution, two files will be generated:

src/api/api.ts - Contains configuration for all datasets:

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" },
// ... all auto-generated datasets
],
};

registerModels(LOVRABET_MODELS_CONFIG);

src/api/client.ts - Wrapped client:

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

export const lovrabetClient = createClient();

Step 4: Use in Your Project

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

// Use directly, all configuration is automatically complete
const orders = await lovrabetClient.models.orders.filter();
const customers = await lovrabetClient.models.customers.filter();

When Do You Need to Re-pull?

When there are changes in the Lovrabet workspace:

  • New data tables added (new datasets)
  • Data tables deleted
  • Dataset configuration changes

Just re-run:

lovrabet api pull

Custom Output Directory

By default, generates to ./src/api/, you can customize:

lovrabet api pull --output ./lib/api

Naming Rules

Generated model aliases use camelCase naming:

Table NameGenerated Alias
order_itemsorderItems
user_profileuserProfile
sales_recordssalesRecords

Appendix: Generate API Documentation

Besides generating SDK configuration, the CLI can also generate API usage documentation:

lovrabet api doc

Generates Markdown format documentation, including field definitions, request examples, etc., convenient for team sharing.

# Generate documentation only for specified datasets
lovrabet api doc --datasetcode orders,customers

# Customize output directory
lovrabet api doc --output ./docs/api

Common Issues

Prompt "Please configure application Code first"

# Set configuration
lovrabet config set app your-app-code

# Or pass parameter directly
lovrabet api pull --appcode your-app-code

API Pull Failed

  1. Check login status: lovrabet auth
  2. Confirm application code is correct: lovrabet config get app
  3. Check network connection

Generated Code Has Compilation Errors

Backend data structure may have changed, re-pull:

lovrabet api pull