Skip to content

Getting Started

Terminal window
bun add @capgo/capacitor-fast-sql
bunx cap sync
import { CapgoCapacitorFastSql } from '@capgo/capacitor-fast-sql';

Initialize the database connection and start the HTTP server.

import { CapgoCapacitorFastSql } from '@capgo/capacitor-fast-sql';
const conn = await CapgoCapacitorFastSql.connect({ database: 'myapp' });
console.log('Connected on port:', conn.port);

Close database connection and stop the HTTP server.

import { CapgoCapacitorFastSql } from '@capgo/capacitor-fast-sql';
await CapgoCapacitorFastSql.disconnect({ database: 'myapp' });

Get the HTTP server port and token for direct communication.

import { CapgoCapacitorFastSql } from '@capgo/capacitor-fast-sql';
const info = await CapgoCapacitorFastSql.getServerInfo({ database: 'myapp' });
console.log('Server port:', info.port);

Execute a SQL query via Capacitor bridge (for simple queries). For better performance with large datasets, use the HTTP protocol directly via SQLConnection class.

import { CapgoCapacitorFastSql } from '@capgo/capacitor-fast-sql';
const result = await CapgoCapacitorFastSql.execute({
database: 'myapp',
statement: 'SELECT * FROM users WHERE age > ?',
params: [18]
});
console.log('Rows:', result.rows);

Begin a database transaction.

import { CapgoCapacitorFastSql } from '@capgo/capacitor-fast-sql';
await CapgoCapacitorFastSql.beginTransaction({ database: 'myapp' });
// Execute multiple operations
await CapgoCapacitorFastSql.commitTransaction({ database: 'myapp' });

Commit the current transaction.

import { CapgoCapacitorFastSql } from '@capgo/capacitor-fast-sql';
await CapgoCapacitorFastSql.commitTransaction({ database: 'myapp' });

Rollback the current transaction.

import { CapgoCapacitorFastSql } from '@capgo/capacitor-fast-sql';
try {
await CapgoCapacitorFastSql.beginTransaction({ database: 'myapp' });
// Operations...
await CapgoCapacitorFastSql.commitTransaction({ database: 'myapp' });
} catch (error) {
await CapgoCapacitorFastSql.rollbackTransaction({ database: 'myapp' });
}

Configure web-specific options for the sql.js WASM module.

Call this before the first connect() call to load sql.js from a locally bundled path instead of the default CDN. This method is a no-op on iOS and Android.

import { CapgoCapacitorFastSql } from '@capgo/capacitor-fast-sql';
// Configure once at app startup (web only)
await CapgoCapacitorFastSql.configureWeb({
sqlJsUrl: '/assets/sql-wasm.js',
wasmUrl: '/assets/sql-wasm.wasm',
});
const db = await FastSQL.connect({ database: 'myapp' });

Database connection options.

export interface SQLConnectionOptions {
/**
* Database name (file will be created in app data directory)
*/
database: string;
/**
* Enable encryption (iOS/Android only)
*/
encrypted?: boolean;
/**
* Encryption key (required if encrypted is true)
*/
encryptionKey?: string;
/**
* Read-only mode
*/
readOnly?: boolean;
}

SQL value types supported by the plugin.

export type SQLValue = string | number | boolean | null | Uint8Array;

Result of a SQL query execution.

export interface SQLResult {
/**
* Rows returned by the query (for SELECT statements)
*/
rows: SQLRow[];
/**
* Number of rows affected by the query (for INSERT/UPDATE/DELETE)
*/
rowsAffected: number;
/**
* ID of the last inserted row (for INSERT statements with auto-increment)
*/
insertId?: number;
}

Transaction isolation levels.

export enum IsolationLevel {
ReadUncommitted = 'READ UNCOMMITTED',
ReadCommitted = 'READ COMMITTED',
RepeatableRead = 'REPEATABLE READ',
Serializable = 'SERIALIZABLE',
}

Web platform configuration for the sql.js WASM module. Use with configureWeb() to load sql.js from a locally bundled path instead of the default CDN.

export interface WebConfig {
/**
* URL to the sql.js JavaScript file (`sql-wasm.js`).
* When omitted, the plugin loads from the cdnjs CDN.
* @example '/assets/sql-wasm.js'
*/
sqlJsUrl?: string;
/**
* URL to the sql.js WebAssembly binary (`sql-wasm.wasm`).
* When omitted, the plugin loads from the cdnjs CDN.
* @example '/assets/sql-wasm.wasm'
*/
wasmUrl?: string;
}

SQL row result - values indexed by column name.

export interface SQLRow {
[column: string]: SQLValue;
}

This page is generated from the plugin’s src/definitions.ts. Re-run the sync when the public API changes upstream.