synchronize - 如何使用 watermelondb 为同步功能传递数据
问题描述
大家好,我正在使用 watermelondb,我有下面的代码,但我不知道如何实际使用它。我是 watermelondb 的新手,我不知道如何将数据作为道具传递给pullChanges和pushChanges对象。当我调用同步函数时,如何将必要的数据(如更改和lastPulledAt)从数据库传递到同步函数中。而且我还需要对 migrationsEnabledAtVersion: 1 进行更多解释。提前感谢您的亲切回答。
import { synchronize } from '@nozbe/watermelondb/sync'
async function mySync() {
await synchronize({
database,
pullChanges: async ({ lastPulledAt, schemaVersion, migration }) => {
const urlParams = `last_pulled_at=${lastPulledAt}&schema_version=${schemaVersion}&migration=${encodeURIComponent(JSON.stringify(migration))}`
const response = await fetch(`https://my.backend/sync?${urlParams}`)
if (!response.ok) {
throw new Error(await response.text())
}
const { changes, timestamp } = await response.json()
return { changes, timestamp }
},
pushChanges: async ({ changes, lastPulledAt }) => {
const response = await fetch(`https://my.backend/sync?last_pulled_at=${lastPulledAt}`, {
method: 'POST',
body: JSON.stringify(changes)
})
if (!response.ok) {
throw new Error(await response.text())
}
},
migrationsEnabledAtVersion: 1,
})
}
解决方案
推荐阅读
- php - 如何从 MYSQL 数据库中每秒获取数据
- go - 如何使用 Golang 在 Kubernetes 外部以编程方式从 Kubeconfig 文件生成承载令牌
- javascript - 如何从 Apps 脚本图像按钮中移除焦点(Google 表格)
- r - 如何使选项(宽度= 60)在R Markdown中用作RStudio中的笔记本的代码块中持续存在?
- middleware - Traefik 中使用本地插件时中间件不存在
- javascript - 在 Vue3-carousel 中显示多个图像
- menu - 如何包装一个空的子菜单?
- java - 即使网络是由 android 上的其他设备创建的,如何删除特定网络?
- c++ - 两个 timeval 结构的 tv_sec 怎么可能相同,但减去它们会给我一个非零值
- powerbi - 应用于 DAX 中的虚拟表和计算表时,与 sumx 函数的结果不同