Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:APIJSON APIJSON Multi Database Support

From Leeroopedia
Knowledge Sources
Domains ORM, Multi_Database, SQL_Generation
Last Updated 2026-10-11 00:00 GMT

Overview

A database abstraction pattern that supports 30+ database engines by detecting the target dialect from a request directive and generating SQL with the correct quoting, pagination, and upsert syntax for each engine.

Description

Multi-Database Support allows a single APIJSON instance to serve requests targeting different database engines. The client specifies the target database via the @database directive (e.g., "@database": "POSTGRESQL"). The SQL generator then uses dialect-specific syntax for:

  • Column and table quoting: MySQL uses backticks, PostgreSQL/Oracle use double quotes
  • Pagination: MySQL/PostgreSQL use LIMIT/OFFSET, SQL Server/Oracle use OFFSET ... ROWS FETCH NEXT ... ROWS ONLY
  • UPSERT: MySQL uses ON DUPLICATE KEY UPDATE, PostgreSQL uses ON CONFLICT DO UPDATE, Oracle/SQL Server/DB2 use MERGE INTO
  • Update/delete syntax: ClickHouse uses ALTER TABLE ... UPDATE/DELETE instead of standard UPDATE/DELETE
  • Dialect families: Databases are grouped into MySQL-family (isMSQL()), PostgreSQL-family (isPSQL()), and T-SQL-family (isTSQL()) based on SQL syntax compatibility

Supported databases include: MySQL, PostgreSQL, Oracle, SQL Server, DB2, ClickHouse, MongoDB, Redis, Kafka, TiDB, MariaDB, SQLite, TDengine, CockroachDB, openGauss, InfluxDB, TimescaleDB, QuestDB, DuckDB, Dameng, KingbaseES, Milvus, and more.

The problem solved is avoiding database lock-in. Applications can switch databases by changing the @database directive without modifying code, and a single deployment can serve multiple databases.

Usage

Use this principle when:

  • An application needs to support multiple database backends
  • Different clients or tenants use different databases
  • Database portability is a requirement

Theoretical Basis

# Abstract algorithm (NOT real implementation):
def get_dialect(database_name):
    if database_name in MYSQL_FAMILY:
        return MySQLDialect(quote="`", limit="LIMIT %d OFFSET %d")
    elif database_name in POSTGRESQL_FAMILY:
        return PostgreSQLDialect(quote='"', limit="LIMIT %d OFFSET %d")
    elif database_name in TSQL_FAMILY:
        return TSQLDialect(quote='"', limit="OFFSET %d ROWS FETCH NEXT %d ROWS ONLY")
    
def generate_upsert(table, columns, values, dialect):
    if dialect.is_mysql():
        return f"INSERT INTO {table} ... ON DUPLICATE KEY UPDATE ..."
    elif dialect.is_postgresql():
        return f"INSERT INTO {table} ... ON CONFLICT DO UPDATE SET ..."
    elif dialect.is_tsql():
        return f"MERGE INTO {table} ... WHEN MATCHED THEN UPDATE ..."

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment