PostgreSQL Protocol Examples

This document provides examples for using the ChronDB PostgreSQL protocol interface with psql and JavaScript clients.

PostgreSQL Protocol Overview

ChronDB implements a subset of the PostgreSQL protocol, allowing you to connect using standard PostgreSQL clients and drivers. This makes it easy to integrate with existing applications that already use PostgreSQL or to leverage the SQL query capability.

The PostgreSQL protocol server can be configured in the config.edn file:

:servers {
  :postgresql {
    :enabled true
    :host "0.0.0.0"
    :port 5432
    :username "chrondb"
    :password "chrondb"
  }
}

Data Model Mapping

ChronDB maps its document structure to SQL tables based on document keys:

  • Document keys with the format collection:id are mapped to tables

  • The part before the colon becomes the table name

  • The part after the colon becomes the row's ID

  • Document fields become columns in the table

For example:

  • A document with key user:1 becomes a row in the user table with id='1'

  • A document with key product:xyz becomes a row in the product table with id='xyz'

Supported SQL Features

ChronDB supports the following SQL operations:

DML (Data Manipulation Language)

  • SELECT - Query documents

  • INSERT - Create documents

  • UPDATE - Update documents

  • DELETE - Delete documents

DDL (Data Definition Language)

Note: ChronDB is schemaless by default. CREATE TABLE is optional - you can insert data directly without defining a schema first.

  • CREATE TABLE - Create table with explicit schema (optional)

  • CREATE TABLE IF NOT EXISTS - Create table only if it doesn't exist

  • DROP TABLE - Delete table schema

  • DROP TABLE IF EXISTS - Delete table only if it exists

  • SHOW TABLES - List all tables (both schemaless and schema-defined)

  • SHOW SCHEMAS - List all schemas (Git branches)

  • DESCRIBE table / SHOW COLUMNS FROM table - Show table structure (infers from data if no schema)

Validation Schema DDL

ChronDB supports optional JSON Schema validation per namespace:

  • CREATE VALIDATION SCHEMA FOR namespace AS 'json-schema' MODE strict|warning - Create validation schema

  • DROP VALIDATION SCHEMA FOR namespace - Delete validation schema

  • SHOW VALIDATION SCHEMA FOR namespace - Get validation schema

  • SHOW VALIDATION SCHEMAS - List all validation schemas

Special Functions

ChronDB provides special SQL functions to access version control features:

  • chrondb_history(table_name, id) - Get document history

  • chrondb_at(table_name, id, timestamp) - Get document at a point in time

  • chrondb_diff(table_name, id, t1, t2) - Compare document versions

  • chrondb_branch_list() - List branches

  • chrondb_branch_create(name) - Create a new branch

  • chrondb_branch_checkout(name) - Switch to a branch

  • chrondb_branch_merge(source, target) - Merge branches

Examples with psql

Connecting to ChronDB

Document Operations

Creating Documents (INSERT)

Querying Documents (SELECT)

Updating Documents (UPDATE)

Deleting Documents (DELETE)

Version Control Operations

DDL Operations

Schema Validation Operations

ChronDB supports optional JSON Schema validation per namespace. See the Schema Validation documentation for full details.

Validation Errors

When inserting invalid data with a strict schema:

Branch Operations

Advanced Queries

Full-Text Search with to_tsquery

ChronDB supports PostgreSQL-style full-text search using the familiar @@ operator and to_tsquery function syntax. This allows you to perform efficient text searches across your documents with a PostgreSQL-compatible syntax.

FTS Syntax

How It Works

When you use the field @@ to_tsquery('term') syntax:

  1. ChronDB parses the query and recognizes it as a full-text search condition

  2. The search term is normalized (lowercase, accent removal)

  3. If the term is short (less than 4 characters), wildcards are automatically added

  4. The query is then passed to the underlying index (Lucene) for efficient search

  5. Results are filtered to only include matches from the specified collection/table

FTS Field Optimization

For better full-text search performance, you can create specialized FTS fields with the _fts suffix:

When an indexed field ends with _fts, ChronDB will use it specifically for full-text search operations.

Comparison with FTS_MATCH

ChronDB also supports the FTS_MATCH function for backward compatibility:

The to_tsquery approach is recommended as it:

  • Follows standard PostgreSQL syntax

  • Provides better compatibility with existing PostgreSQL tools

  • Supports the same normalization and text processing features

Index Implementation Details

Full-text search operations are powered by:

  • Lucene index for efficient text search (when enabled in configuration)

  • Automatic text normalization and accent handling

  • Wildcard prefix matching for better search results

  • Fallback to basic string matching with MemoryIndex implementation

For optimal performance:

  • Enable the Lucene index in your configuration

  • Use dedicated FTS fields with the _fts suffix for frequently searched content

  • Utilize more specific search terms to reduce result sets

Examples with JavaScript (node-postgres)

The following examples use the node-postgresarrow-up-right package, a popular PostgreSQL client for Node.js.

Setting Up

Document Operations

Version Control Operations

Branch Operations

Complete Example: Customer Order System

Schema Validation with JavaScript

ChronDB supports optional JSON Schema validation per namespace. See the Schema Validation documentation for full details.

Last updated