# sql\.js

SQLite compiled to WebAssembly or JavaScript, with in\-memory databases and explicit file export\.

ID: Q898 · Revision: 946

Type: software · License: MIT wrapper; SQLite is public domain[^license][^readme] · Status: active

Website: https://sql.js.org/



sql\.js compiles SQLite for JavaScript environments, normally through WebAssembly\. It can create or load a SQLite database into memory and query it in the browser\. Changes are not automatically written back to a file or durable browser store; an application must export and save the database when persistence is required\.[^readme]

## Loading the runtime

The usual WebAssembly build needs both a JavaScript loader and a matching WASM asset\. Initialization is asynchronous, and locateFile tells the loader where to obtain the binary when it is not at the expected location\. Bundlers must preserve that asset route\. An imported JavaScript module alone does not demonstrate that the database engine can initialize after deployment\.[^readme]

## Database files and saving

The database constructor accepts SQLite file bytes\. Export returns a Uint8Array that the application can save separately\. The API documents an important side effect: export closes and reopens the database, resetting pragmas to their default values\. Persistence code must account for connection settings after export rather than treating it as an entirely passive snapshot operation\.[^database]

## Bound queries and cleanup

Prepared statements support named and positional parameters and can be reused with different values\. They must be freed when finished; the documentation warns that neglected statements can retain memory and cause later table\-drop operations to report a lock\. Closing the database also makes its statements unusable\. This lifecycle is important for generated code that repeatedly reruns queries in a component or interactive editor\.[^statement]

## Moving work off the UI thread

The inspected worker implementation accepts messages for opening a database, executing SQL, iterating rows, obtaining the modified\-row count, exporting and closing\. Responses carry request IDs, and errors are returned through a message path\. This is a narrower interface than direct access to every Database method\. A client must coordinate requests and completion rather than treating a posted message as an already available result\.[^worker]

## Application\-defined functions

The direct Database API can register JavaScript scalar and aggregate functions for use in SQL\. These are runtime registrations, so their presence in a running application should not be confused with ordinary rows stored in an exported file\. Applications using them need to initialize the behavior as part of opening their database workflow\. No custom function or database was executed for this article\.[^database]

## Memory and numeric limitations

The documented model loads the database into memory rather than operating directly on a native disk file\. That makes full database size relevant to browser resource use\. BigInt support is also asymmetric in the inspected README: retrieving values can be enabled, while binding BigInt inputs is not supported\. Those limitations should be checked against application datasets and identifiers before adopting the library\.[^readme]

## License and substitution

sql\.js itself uses MIT terms, while its SQLite foundation is described as public domain\. It can be considered alongside [PGlite](/pglite) for embedded relational queries, especially when the application works with SQLite files\. PGlite embeds PostgreSQL and offers a different persistence model\. Switching requires checking SQL dialect, data types, file formats and application APIs; this is not a drop\-in engine replacement or a tested migration\.[^license][^readme][^pglite]

## Capabilities and substitutions

### SQLite file loading and export — supported

Loads database bytes and exports a SQLite file as Uint8Array; saving is application\-owned\.[^readme][^database]

Use case: Embedding SQLite queries and database\-file inspection in JavaScript applications · Platforms: Web, JavaScript, WebAssembly, Node\.js · Region: Version, build and integration requirements apply

### Automatic durable writes — unsupported

The documented default uses an in\-memory virtual database and does not persist changes automatically\.[^readme]

Use case: Embedding SQLite queries and database\-file inspection in JavaScript applications · Platforms: Web, JavaScript, WebAssembly, Node\.js · Region: Version, build and integration requirements apply

### Prepared statements — supported

Supports bound values and result iteration; statements must be freed after use\.[^statement]

Use case: Embedding SQLite queries and database\-file inspection in JavaScript applications · Platforms: Web, JavaScript, WebAssembly, Node\.js · Region: Version, build and integration requirements apply

### Worker API parity — limited

Worker exposes selected message actions rather than the complete direct Database API\.[^database][^worker]

Use case: Embedding SQLite queries and database\-file inspection in JavaScript applications · Platforms: Web, JavaScript, WebAssembly, Node\.js · Region: Version, build and integration requirements apply

### [Substitute for PGlite](/pglite) — limited

Can replace embedded SQL execution when SQLite and explicit file export fit the task; PostgreSQL schema features and storage formats are not interchangeable\.[^readme][^pglite]

Use case: Embedding SQLite queries and database\-file inspection in JavaScript applications · Platforms: Web, JavaScript, WebAssembly, Node\.js · Region: Version, build and integration requirements apply

## References

[^readme]: [sql\.js repository overview](<https://github.com/sql-js/sql.js/blob/master/README.md>) — Memory model, WASM loading, export and BigInt limitations (vendor; retrieved 2026-10-07T23:43:41Z)

[^database]: [Database API](<https://sql.js.org/documentation/Database.html>) — Database lifecycle, export pragma reset and custom functions (vendor; retrieved 2026-10-07T23:44:55Z)

[^statement]: [Statement API](<https://sql.js.org/documentation/Statement.html>) — Parameter binding, memory cleanup and locking warning (vendor; retrieved 2026-10-07T23:44:02Z)

[^worker]: [Worker implementation](<https://github.com/sql-js/sql.js/blob/master/src/worker.js>) — Supported message actions, IDs and error responses (vendor; retrieved 2026-10-07T23:44:55Z)

[^license]: [sql\.js license](<https://github.com/sql-js/sql.js/blob/master/LICENSE>) — MIT terms for sql\.js (vendor; retrieved 2026-10-07T23:43:48Z)

[^pglite]: [PGlite introduction](<https://pglite.dev/docs/about>) — Embedded PostgreSQL and storage options (vendor; retrieved 2026-10-07T23:43:41Z)

Source revision: https://substitutefor.net/entities/Q898?revision=946
