Zum Inhalt springen

Einstieg

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

Initialisiere die Datenbankverbindung und starte den HTTP-Server.

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

Schließe die Datenbankverbindung und stoppe den HTTP-Server.

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

Ermitteln Sie den HTTP-Server-Port und den Token für direkte Kommunikation.

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

Führen Sie eine SQL-Anfrage über Capacitor-Brücke (für einfache Abfragen) aus. Für bessere Leistung bei großen Datensätzen verwenden Sie das HTTP-Protokoll direkt über die SQLConnection-Klasse.

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);

Beginnen Sie eine Datenbanktransaktion.

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

Bestätigen Sie die aktuelle Transaktion.

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

Die aktuelle Transaktion rückgängig machen.

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' });
}

Konfigurieren Sie web-spezifische Optionen für das sql.js WASM-Modul.

Aufrufen Sie dies vor dem ersten connect() Aufruf von sql.js aus einem lokal verpackten Pfad anstatt der Standard-CDN. Diese Methode ist auf iOS und Android ein No-Op.

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' });

Datenbankverbindungsoptionen.

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-Werttypen, die vom Plugin unterstützt werden.

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

Ergebnis der Ausführung einer SQL-Anfrage.

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;
}

Transaktionsisolationsebenen.

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

Konfiguration des Web-Plattform für das sql.js-WASM-Modul. Verwenden Sie dies, um configureWeb() um sql.js aus einem lokal verpackten Pfad zu laden, anstatt den Standard-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-Rohdatenwert - Werte indexiert nach Spaltenname.

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

Diese Seite wird aus der Plugin- src/definitions.tserneut synchronisieren, wenn die öffentliche API upstream geändert wird.