convex-db

Sử dụng Convex DB — cơ sở dữ liệu SQL hỗ trợ lattice. Sử dụng khi giúp người dùng viết truy vấn, kết nối qua JDBC hoặc máy khách PostgreSQL, tạo bảng, chèn/truy vấn dữ liệu,…

npx skills add https://github.com/convex-dev/convex --skill convex-db

Using Convex DB

Convex DB provides SQL access over lattice data. Connect via JDBC, PostgreSQL wire protocol, or the direct lattice API.

Reference: convex-db/README.md for full documentation including replication, PostgreSQL server setup, and architecture details.

Connecting

JDBC (Java)

// In-memory
Connection conn = DriverManager.getConnection("jdbc:convex:mydb");

// Persistent (Etch-backed, survives restarts)
Connection conn = DriverManager.getConnection("jdbc:convex:file:/data/mydb.etch");

Driver auto-registers via ServiceLoader. Class: convex.db.jdbc.ConvexDriver

PostgreSQL Clients (psql, DBeaver, DataGrip, Python, etc.)

# Start the PG server
java -cp convex-db.jar convex.db.psql.PgServer -p 5432 -d mydb

# Then connect with any PG client
psql -h localhost -p 5432 -d mydb
import psycopg2
conn = psycopg2.connect(host="localhost", port=5432, dbname="mydb")

Creating Tables

CREATE TABLE users (id, name, email)

Column 0 (first column) is always the primary key. Types are inferred from inserted data.

Names follow the SQL standard by default: unquoted names fold to upper case and compare exactly, so this creates USERS. A table created through the lattice API as users must be quoted in SQL ("users"), unless the database's identifier policy is :lower (IdentifierPolicy.LOWER.applyTo(db)), as a PostgreSQL user would expect.

Inserting Data

INSERT INTO users VALUES (1, 'Alice', 'alice@example.com')

For bulk loading, use prepared statements with batch:

PreparedStatement ps = conn.prepareStatement("INSERT INTO users VALUES (?, ?, ?)");
for (int i = 0; i < 10000; i++) {
    ps.setLong(1, i);
    ps.setString(2, "Name-" + i);
    ps.setString(3, "email-" + i + "@example.com");
    ps.addBatch();
}
ps.executeBatch();

Querying

-- Point lookup (fast — O(log n) via PK index pushdown)
SELECT * FROM users WHERE id = 1

-- Filtering, sorting, pagination
SELECT name, email FROM users WHERE name LIKE 'A%' ORDER BY name LIMIT 10

-- Joins
SELECT c.name, o.amount
FROM customers c INNER JOIN orders o ON c.id = o.customer_id

-- Aggregations
SELECT department, COUNT(*), AVG(salary)
FROM employees GROUP BY department HAVING COUNT(*) > 5

Supported SQL

  • DDL: CREATE TABLE, DROP TABLE
  • DML: INSERT, UPDATE, DELETE
  • Queries: SELECT, WHERE, ORDER BY, LIMIT, OFFSET
  • Joins: INNER JOIN, LEFT JOIN, RIGHT JOIN, CROSS JOIN
  • Aggregations: GROUP BY, HAVING, COUNT, SUM, AVG, MIN, MAX
  • Expressions: CASE WHEN, COALESCE, CAST, BETWEEN, IN, LIKE, IS NULL
  • Functions: ABS, FLOOR, CEIL, SQRT, UPPER, LOWER, TRIM, SUBSTRING, LENGTH, CONCAT

Transactions

conn.setAutoCommit(false);
stmt.execute("INSERT INTO users VALUES (2, 'Bob', 'bob@example.com')");
stmt.execute("UPDATE users SET email = 'new@example.com' WHERE id = 1");
conn.commit();    // atomic merge — all changes become visible
// or conn.rollback() to discard

Column Types

SQL TypeCVM TypeNotes
BIGINTAIntegerAny CVM integer; read as NUMERIC (BigDecimal)
INTEGERCVMLong64-bit signed integer; read as BIGINT
DOUBLECVMDouble64-bit float
VARCHARAStringUnicode string
BOOLEANCVMBooltrue/false
VARBINARY / BLOBABlobBinary data
TIMESTAMPCVMLongMilliseconds since epoch
ANYACellDynamic type

Direct Lattice API

For programmatic access without SQL overhead:

// The merge policy is the application's choice; RowLWW merges rows by last write
ConvexDB cdb = ConvexDB.create(RowLWW::merge);
SQLDatabase db = cdb.database("mydb");

// Create table
db.tables().createTable("users", new String[]{"id", "name", "email"});

// Insert
db.tables().insert("users", 1, "Alice", "alice@example.com");

// Point lookup
AVector<ACell> row = db.tables().selectByKey("users", 1);

// Scan all
Index<ABlob, AVector<ACell>> all = db.tables().selectAll("users");

// Delete
db.tables().deleteByKey("users", 1);

Performance Tips

  • Use PK lookups (WHERE id = ?) for point queries — O(log n) via index pushdown
  • Use PreparedStatements — plans compile once, reuse across executions
  • Use batch inserts for bulk loading — significantly faster than individual statements
  • Full scans are O(n) — filter on PK when possible

Building and Testing

Run from the repository root — see the build-convex skill.

# Build (-am also builds convex-core, which this depends on)
./mvnw -B -T1C install -pl convex-db -am

# Run tests
./mvnw -B -T1C test -pl convex-db -am

Thêm skills từ convex-dev

account
convex-dev
Tạo hoặc kiểm tra tài khoản Convex. Sử dụng khi người dùng muốn thiết lập tài khoản mới, kiểm tra chi tiết tài khoản hoặc quản lý khóa.
build-convex
convex-dev
Xây dựng dự án Convex từ mã nguồn. Sử dụng khi người đóng góp muốn biên dịch, kiểm thử hoặc đóng gói Convex.
cad3-encoding
convex-dev
Định dạng mã hóa CAD3 — các ô, tham chiếu nhúng so với tham chiếu nhánh, ID giá trị, quy tắc hợp lệ. Sử dụng khi làm việc với bộ mã hóa, bộ giải mã, tuần tự hóa, băm hoặc…
cad-reference
convex-dev
Mục lục của các Tài liệu Kiến trúc Convex (CADs), các đặc tả chuẩn mực cho Convex. Dùng để tìm CAD nào quản lý một hệ thống con trước khi thay đổi giao thức,…
cns
convex-dev
Giải quyết hoặc đăng ký tên trong Hệ thống Tên Convex (CNS). Sử dụng khi người dùng muốn tra cứu tên CNS, đăng ký tên mới hoặc cập nhật mục tiêu của tên.
convex-lisp
convex-dev
Tài liệu tham khảo ngôn ngữ Convex Lisp — quy ước CVM, gọi mã thư viện, định nghĩa actor, mã juice và lỗi. Sử dụng khi viết hoặc gỡ lỗi mã nguồn CVM cho…
deploy
convex-dev
Triển khai một actor (hợp đồng thông minh) lên mạng Convex. Sử dụng khi người dùng muốn tạo một actor mới trên chuỗi với các hàm đã được xuất.
ecosystem
convex-dev
Định hướng trong hệ sinh thái Convex — thứ gì nằm trong repository nào, specs và docs ở đâu, và những thư viện client nào tồn tại. Sử dụng khi bạn cần ngữ cảnh…