| Crates.io | tauri-plugin-pg-sync |
| lib.rs | tauri-plugin-pg-sync |
| version | 0.1.11 |
| created_at | 2026-01-16 04:22:46.318681+00 |
| updated_at | 2026-01-17 04:28:42.109678+00 |
| description | Offline-first PostgreSQL sync plugin for Tauri apps |
| homepage | |
| repository | https://github.com/bishen/tauri-plugin-pg-sync |
| max_upload_size | |
| id | 2047866 |
| size | 489,033 |
离线优先的 PostgreSQL 同步插件,支持全平台。
| 平台 | 架构 | 状态 |
|---|---|---|
| Windows | x86_64, aarch64 | ✅ |
| macOS | x86_64, aarch64 | ✅ |
| Linux | x86_64, aarch64 | ✅ |
| Android | arm64-v8a, armeabi-v7a, x86_64 | ✅ |
| iOS | arm64, x86_64 (模拟器) | ✅ |
在 src-tauri/Cargo.toml 中添加:
[dependencies]
tauri-plugin-pg-sync = "0.1"
npm install @bishen/tauri-plugin-pg-sync
# 或
pnpm add @bishen/tauri-plugin-pg-sync
# 或
yarn add @bishen/tauri-plugin-pg-sync
在 src-tauri/src/main.rs 或 src-tauri/src/lib.rs 中:
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_pg_sync::init())
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
在 src-tauri/capabilities/default.json 中添加:
{
"permissions": [
"pg-sync:default"
]
}
import { smartInit, table, sync } from '@bishen/tauri-plugin-pg-sync';
// 智能初始化:自动检测本地/远程状态
const result = await smartInit({
remoteUrl: 'postgres://user:pass@host:5432/db'
});
console.log('模式:', result.mode);
// 可能值: 'local_only' | 'offline' | 'pulled_from_remote' | 'pushed_to_remote' | 'synced'
// 定义表并操作
const users = table('users', {
columns: [['name', 'TEXT'], ['age', 'INTEGER']]
});
const id = await users.insert({ name: '张三', age: 25 });
const all = await users.findAll();
// 手动同步
await sync.now();
import { syncManager, smartInit, table } from '@bishen/tauri-plugin-pg-sync';
await smartInit({ remoteUrl: 'postgres://...' });
// 启动自动同步(轮询 + 实时监听)
await syncManager.start({
pollInterval: 30000,
enableRealtime: true,
onSync: (result) => {
if (result.pulled > 0) refreshUI();
},
onStateChange: ({ mode }) => {
console.log('同步状态:', mode); // 'offline' | 'online' | 'syncing' | 'error'
}
});
// 停止
syncManager.stop();
import {
initDatabase,
ensureTable,
insert,
findAll,
connectRemote,
syncNow,
} from '@bishen/tauri-plugin-pg-sync';
// 1. 初始化本地数据库
const nodeId = await initDatabase();
// 2. 定义表结构
await ensureTable('users', {
columns: [
['name', 'TEXT NOT NULL'],
['email', 'TEXT'],
['age', 'INTEGER'],
],
});
// 3. CRUD 操作(离线可用)
const id = await insert('users', {
name: 'Alice',
email: 'alice@example.com',
age: 25,
});
const users = await findAll('users');
// 4. 连接远程数据库并同步
await connectRemote('postgres://user:pass@host:5432/db');
const result = await syncNow();
console.log(`Pushed: ${result.pushed}, Pulled: ${result.pulled}`);
import { query, findWhere, count } from '@bishen/tauri-plugin-pg-sync';
// 条件查询
const adults = await findWhere('users', { age: { $gte: 18 } });
// 高级查询
const results = await query('users', {
where: { age: { $gte: 18 } },
orderBy: 'name',
orderDesc: false,
limit: 10,
offset: 0,
select: ['id', 'name', 'email'],
});
// 计数
const total = await count('users');
const adultCount = await count('users', { age: { $gte: 18 } });
import { insertMany, updateMany, deleteMany } from '@bishen/tauri-plugin-pg-sync';
// 批量插入
const ids = await insertMany('users', [
{ name: 'Bob', email: 'bob@example.com' },
{ name: 'Charlie', email: 'charlie@example.com' },
]);
// 批量更新
await updateMany('users', [
[ids[0], { age: 30 }],
[ids[1], { age: 25 }],
]);
// 批量删除
await deleteMany('users', ids);
import {
pushTableSchema,
pullTableSchema,
listLocalTables,
listRemoteTables,
} from '@bishen/tauri-plugin-pg-sync';
// 推送本地表结构到远程
await pushTableSchema('users');
// 从远程拉取表结构
await pullTableSchema('products');
// 列出表
const localTables = await listLocalTables();
const remoteTables = await listRemoteTables();
完整 API 请参考 API.md
| 函数 | 描述 |
|---|---|
smartInit(options) |
智能初始化(推荐) |
initDatabase() |
初始化本地数据库(桌面端) |
initDatabaseMobile() |
初始化本地数据库(移动端优化) |
getDbPath() |
获取数据库文件路径 |
| 函数 | 描述 |
|---|---|
connectRemote(url) |
连接远程 PostgreSQL |
connectRemoteWithRetry(url) |
带重试的连接(弱网优化) |
startAutoReconnect() |
启动自动重连 |
disconnectRemote() |
断开连接 |
syncNow() |
立即同步 |
isOnline() |
检查是否在线 |
| 函数 | 描述 |
|---|---|
insert(table, data) |
插入记录 |
update(table, id, data) |
更新记录 |
remove(table, id) |
删除记录(软删除) |
findById(table, id) |
根据 ID 查找 |
findAll(table, limit?, offset?) |
查找所有 |
findWhere(table, conditions) |
条件查询 |
query(table, options) |
高级查询 |
count(table, conditions?) |
计数 |
| 函数 | 描述 |
|---|---|
insertMany(table, items) |
批量插入 |
updateMany(table, updates) |
批量更新 |
deleteMany(table, ids) |
批量删除 |
clearTable(table) |
清空表 |
MIT
Copyright (c) 2026 BiShen bishen@live.com 算金山™ (https://www.suanjinshan.com/)