Auto-generate SDK Configuration
This document explains how to use the CLI's api pull command to automatically generate SDK configuration files, eliminating the need for manual configuration.
If you want to learn about complete SDK configuration (manual configuration, multi-environment setup, etc.), please refer to 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) - Corresponding table name (e.g.,
orders) - Auto-generated CRUD APIs
┌─────────────────────────────────────────────────────────┐
│ Lovrabet Workspace │
├─────────────────────────────────────────────────────────┤
│ Connect Database │
│ ↓ │
│ Auto-analyze Table Structure │
│ ↓ │
│ Generate Dataset for Each Table │
│ ↓ │
│ Each Dataset Automatically Has APIs (CRUD) │
└─────────────────────────────────────────────────────────┘
What Configuration Does the SDK Need?
To use Lovrabet SDK in your frontend project, you need to tell the SDK "which datasets I want to access":
import { registerModels, createClient } from '@lovrabet/sdk';
// This configuration tells the SDK: what datasets my application has
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();
Here's the problem: Where do these datasetCode values come from? Copy and paste them one by one manually?
CLI Auto-generates These Configurations
The lovrabet api pull command solves this problem:
lovrabet api pull
It will:
- Connect to Lovrabet backend
- Retrieve all dataset information under your application
- Auto-generate the configuration files needed by the SDK
┌─────────────────────────────────────────────────────────┐
│ lovrabet api pull │
├─────────────────────────────────────────────────────────┤
│ 1. Read your appCode │
│ 2. Fetch all datasets from Lovrabet backend │
│ 3. Generate src/api/api.ts (config file) │
│ 4. Generate src/api/client.ts (ready-to-use client) │
└─────────────────────────────────────────────────────────┘
Usage
Step 1: Ensure You're 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
This will generate two files:
src/api/api.ts - Contains all dataset configurations:
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 - Pre-configured 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 done
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 changed
Just re-run:
lovrabet api pull
Custom Output Directory
Default output is ./src/api/, which can be customized:
lovrabet api pull --output ./lib/api
Naming Convention
Generated model aliases use camelCase naming:
| Table Name | Generated Alias |
|---|---|
order_items | orderItems |
user_profile | userProfile |
sales_records | salesRecords |
Appendix: Generate API Documentation
Besides generating SDK configuration, CLI can also generate API usage documentation:
lovrabet api doc
Generates Markdown format documentation containing field definitions, request examples, etc., for easy team sharing.
# Generate documentation for specific datasets only
lovrabet api doc --datasetcode orders,customers
# Custom output directory
lovrabet api doc --output ./docs/api
FAQ
"Please configure app code first" message
# Set configuration
lovrabet config set app your-app-code
# Or pass as parameter
lovrabet api pull --appcode your-app-code
API Pull Failed
- Check login status:
lovrabet auth - Confirm app code is correct:
lovrabet config get app - Check network connection
Generated Code Has Compilation Errors
Backend data structure may have changed, re-pull:
lovrabet api pull