Skip to content

Latest commit

 

History

History
69 lines (50 loc) · 2.85 KB

File metadata and controls

69 lines (50 loc) · 2.85 KB

perry-sqlite

Perry native package: Prisma ORM backed by sqlx + SQLite.

Architecture

Drop-in replacement for @prisma/client using sqlx with SQLite. Sibling of perry-prisma (MySQL) and perry-postgres — same architecture, ~95% identical code.

TypeScript → JSON string → FFI → Rust dispatcher → sqlx (SQLite) → JSON result → TypeScript

Key files

  • native/src/lib.rs — All Rust logic: query builder, row-to-JSON conversion, FFI exports
  • native/build.rs — Reads prisma/schema.prisma at build time, generates generated_models.rs with model metadata
  • native/Cargo.toml — Crate config; sqlx with sqlite feature
  • src/index.ts — TypeScript layer: PrismaClient, ModelClient, type definitions, FFI declarations
  • perry.config.ts — Perry compiler configuration
  • package.json — Package metadata, FFI function declarations, platform lib requirements

Schema path resolution (build.rs)

  1. PRISMA_SCHEMA_PATH env var
  2. ../../../chainblick/api/prisma/schema.prisma (relative to native/)
  3. Falls back to empty model list with compile warning

SQLite-specific differences (vs perry-prisma / MySQL)

  • Pool type: SqlitePool (not MySqlPool)
  • Identifier quoting: "col" double quotes (not `col` backticks)
  • Placeholders: ? (same as MySQL)
  • Last insert ID: last_insert_rowid() (not last_insert_id())
  • Transactions: BEGIN / COMMIT / ROLLBACK (not SET autocommit=0; START TRANSACTION)
  • Boolean binding: q.bind(*b) native bool (not q.bind(*b as i8))
  • Skip duplicates: INSERT OR IGNORE (not INSERT IGNORE)
  • Unlimited offset: LIMIT -1 OFFSET n (not LIMIT 18446744073709551615 OFFSET n)
  • No TLS libs: SQLite is local — no Security/SystemConfiguration frameworks on iOS, no crypto libs on Windows

FFI functions

All exported as sqlite_*:

Function Signature
sqlite_connect (config_json: StringHeader*) -> Promise*
sqlite_disconnect (handle: i64) -> Promise*
sqlite_execute (handle: i64, query_json: StringHeader*) -> Promise*
sqlite_transaction_begin (handle: i64) -> Promise*
sqlite_transaction_end (tx_handle: i64, options_json: StringHeader*) -> Promise*

Build & verify

# Host check
cd native && cargo check

# iOS cross-compile check
cd native && cargo check --target aarch64-apple-ios

# Perry check (from package root)
perry check

Common tasks

  • Adding a new query operation: Add handler in execute_query match, implement async fn taking &SqlitePool
  • Changing SQL dialect: All SQL generation is in build_where, build_order_by, build_select_cols, and the operation functions — grep for format! with SQL strings
  • Syncing with perry-prisma: Diff native/src/lib.rs against perry-prisma's version; apply non-dialect changes