跳过内容

Getting Started

GitHub
终端窗口
bun add @capgo/capacitor-fast-sql
bunx cap sync

导入

导入
import { CapgoCapacitorFastSql } from '@capgo/capacitor-fast-sql';

API概述

API概述

connect

连接

初始化数据库连接并启动HTTP服务器。

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

disconnect

断开

关闭数据库连接并停止HTTP服务器。

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

获取 HTTP 服务器端口和令牌进行直接通信。

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

通过 Capacitor 桥执行一个 SQL 查询(适用于简单的查询)。 对于大型数据集的更好性能,使用 SQLConnection 类直接通过 HTTP 协议。

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

beginTransaction

事务部分

开始一个数据库事务。

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

commitTransaction

事务部分

提交当前事务。

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

rollbackTransaction

事务部分

回滚当前事务。

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

configureWeb

标题:配置Web

为sql.js WASM模块配置Web特定的选项。

调用此 第一个 connect() 从本地捆绑路径加载sql.js之前

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

复制到剪贴板

类型参考

SQLConnectionOptions

标题:类型参考

数据库连接选项。

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值类型。

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

SQL查询执行结果。

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

事务隔离级别。

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

Web 平台配置项为 sql.js WASM 模块。使用此选项 configureWeb() 来加载 sql.js 从一个本地打包路径而不是默认的 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 行结果 - 值按列名索引。

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

本页面是由插件的 src/definitions.ts重新运行同步时,公共 API 上游发生变化。

如果您正在使用 入门 为了规划仪表板和API操作,连接它 使用@capgo/capacitor-fast-sql 使用@capgo/capacitor-fast-sql的本地能力 API概述 API概述的实现细节 介绍 介绍的实现细节 API密钥 API密钥的实现细节 设备 __CAPGO_KEEP_0__