跳过内容

开始使用

GitHub

安装

安装

您可以使用我们的 AI 助手设置来安装插件。将 Capgo 技能添加到您的 AI 工具中,使用以下命令:

终端窗口
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

然后使用以下提示:

Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-fast-sql` plugin in my project.

如果您prefer手动设置,安装插件并运行以下命令并遵循以下平台特定说明:

终端窗口
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);

execute

执行

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

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

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

调用此

在开始工作新功能之前 首次 第一步 connect() 类型参考

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

SQL连接选项

数据库连接选项

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

SQLValue

SQL值类型

插件支持的SQL值类型

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

事务隔离级别

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

IsolationLevel

事务隔离级别

复制到剪贴板

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

WebConfig

WebConfig

sql.js WASM 模块的 Web 平台配置。使用 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;
}

SQLRow

SQLRow

SQL 行结果 - 值按列名索引。

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

真实数据来源

本页由插件的

重新同步公共 __CAPGO_KEEP_0__ 时更改上游。 src/definitions.ts重新同步时,应根据上游的公共API变动。

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