Skip to content

feat: make the SQL engine pluggable and reconstruct types from schema - #21

Open
mpicciolli wants to merge 9 commits into
mainfrom
feat/pluggable-sql-engine
Open

feat: make the SQL engine pluggable and reconstruct types from schema#21
mpicciolli wants to merge 9 commits into
mainfrom
feat/pluggable-sql-engine

Conversation

@mpicciolli

@mpicciolli mpicciolli commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Decouples SqlDatabase/SqlEngine from sql.js (previously typed via sql.js's Database) so any compatible SQL backend can be plugged in
  • Adds samples/node-sqlite-engine and samples/node-better-sqlite3-engine demonstrating node:sqlite and better-sqlite3 as drop-in engines
  • Improves column type reconstruction for normalized SQLite schemas

Decouples SqlDatabase/SqlEngine from sql.js so any compatible backend can be plugged in (e.g. node:sqlite, better-sqlite3), with samples/ demonstrating both. Also improves column type reconstruction from the normalized schema.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 9, 2026 13:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the library’s public surface so cdbToSql/sqlToCdb can operate against a minimal, engine-agnostic SqlEngine/SqlDatabase interface instead of being coupled to sql.js, and adds Node sample adapters demonstrating alternative SQLite backends.

Changes:

  • Introduces new public SqlEngine, SqlDatabase, SqlExecResult, and SqlValue types and updates call sites/tests to use them.
  • Adjusts sqlToCdb metadata probing to handle engines that return empty result sets differently.
  • Adds two Node samples showing node:sqlite and better-sqlite3 engine adapters, and updates README documentation accordingly.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
test/roundtrip.test.ts Updates round-trip test typing to use SqlEngine and removes unnecessary casts.
test/mocks/mockSqlDatabase.ts Simplifies mocks to target the new minimal SqlDatabase interface.
test/cdbToSql.test.ts Updates SQL engine mocks/types to the new engine-agnostic interfaces.
src/types.ts Defines the new public SQL engine/database abstraction types.
src/sqlToCdb.ts Makes DB_STRUCTURE probing more robust against empty/absent result sets.
src/index.ts Re-exports new SQL abstraction types from the package root.
src/cdbToSql.ts Updates cdbToSql signature to accept SqlEngine and relies on new database typing.
samples/node-sqlite-engine/README.md Documents using a Node node:sqlite-backed engine adapter sample.
samples/node-sqlite-engine/index.js Sample script converting a .cdb using the node:sqlite engine adapter.
samples/node-sqlite-engine/engine.js Implements the node:sqlite adapter to SqlEngine/SqlDatabase.
samples/node-better-sqlite3-engine/README.md Documents using a better-sqlite3 engine adapter sample.
samples/node-better-sqlite3-engine/package.json Adds better-sqlite3 dependency for the sample.
samples/node-better-sqlite3-engine/index.js Sample script converting a .cdb using the better-sqlite3 engine adapter.
samples/node-better-sqlite3-engine/engine.js Implements the better-sqlite3 adapter to SqlEngine/SqlDatabase.
README.md Updates API reference and adds guidance/sample links for swapping SQLite engines.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/types.ts
export type ColumnData = Array<string | number | boolean>;

export interface SqlDatabase extends Database {}
export type SqlValue = string | number | Uint8Array | null;
Comment thread README.md Outdated
Comment on lines 203 to 207
- **`SQL`** — a `SqlEngine` (the module returned by `initSqlJs()` satisfies this out of the box).
- **returns** — a `SqlDatabase` with the CDB tables loaded.
- **`options.normalize`** — `boolean` (default `false`). Reconstruct PK/FK constraints from PCM naming conventions. See [Normalized schema](#normalized-schema).
- **`options.indexForeignKeys`** — `boolean` (default `false`). When normalizing, also index every FK column for faster JOINs (roughly doubles the output size).
- **returns** — a `sql.js` `Database` with the CDB tables loaded.
Comment thread README.md Outdated

The library's public API is not tied to `sql.js` — it is typed against the minimal, self-contained `SqlEngine`/`SqlDatabase` interfaces exported from the package root, so any object matching that shape works, with no change required to the library:

- [Node.js — swapping the SQLite engine](./samples/node-sqlite-engine/) — uses the bundled, tested [`node:sqlite`](https://nodejs.org/api/sqlite.html) adapter (`cdb-converter/adapters/node-sqlite`).
Comment thread samples/node-sqlite-engine/README.md Outdated
@@ -0,0 +1,33 @@
# Node.js — swapping the SQLite engine

This sample shows that `cdb-converter` isn't tied to `sql.js`: it uses the bundled `node:sqlite` adapter (`cdb-converter/adapters/node-sqlite`) as a drop-in replacement for `initSqlJs()`.
Comment thread samples/node-sqlite-engine/README.md Outdated

## Using your own engine

Any object matching the `SqlEngine`/`SqlDatabase` interfaces exported from `cdb-converter` works in place of `sql.js`. See [`src/adapters/node-sqlite.ts`](../../src/adapters/node-sqlite.ts) for a reference implementation to adapt to other engines (e.g. `better-sqlite3`, `wa-sqlite`).
@@ -0,0 +1,35 @@
# Node.js — using better-sqlite3 as the SQLite engine

This sample shows that `cdb-converter` isn't tied to `sql.js` or the bundled `node:sqlite` adapter: [`engine.js`](./engine.js) is a small hand-written `SqlEngine`/`SqlDatabase` adapter wrapping [`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3), used as a drop-in replacement for `initSqlJs()`.
Comment on lines +24 to +26
function DatabaseEngine(data) {
return wrap(new Database(data ?? ":memory:"));
}
…re logic

- Removed the node-better-sqlite3-engine sample and its package.json.
- Updated node-cdb-to-sql and node-sql-to-cdb samples to use better-sqlite3 engine instead of sql.js.
- Deleted the node-sqlite-engine sample and its associated files.
- Introduced better-sqlite3 engine implementation in src/engines/better-sqlite3.ts.
- Added sql-js engine creation function in src/engines/sql-js.ts for potential future use.
- Updated CLI to utilize better-sqlite3 engine for database operations.
- Adjusted tests to validate functionality with better-sqlite3 engine.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants