You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hey Guys, I'm facing the record duplication error.
Backend: Have integer and sequenced id's Frontend: Watermelon DB managed id's
In table's I've server_id to represent backend generated id.
Step 01: I've created a record on watermelon db. A record is created with server_id null.
Step 02: Sync function is called, I checked if the records being pushed to server don't have server_id, then get them synced. Server returns me two id's. i) client_id (which is watermelon id) ii) id (backend generated id).
Step 03: Based on client_id, I find record in watermelon db and updated that record server_id with id returned from server.
Step 04: I pulled changes, server returned me two things, created array and updated array, both have same information, and both have id's which server created, only way to differentiate between them is, before creating a new record on watermelon db, I've to check, if I've server_id matching with created array object id on watermelon db, then ignores it. However for updated record I'm not doing any checks, and watermelon db handles that logic.
Step 05: Now after above pull, a new record get's created with same information as I've above, with new watermelon db id and server_id null. Now it synced with server and cycle repeats.
Step 06: On 2nd pull, I get that record only in created array and not in updated array, and new duplication doesn't happens.
That's how things are going up, I tried debugging and logging and it's the only thing I found, If anyone knows what's causing the issues. Please reply to this.
Here's my sync code
`import { synchronize } from "@nozbe/watermelondb/sync";
import { client } from "../utils/api-client";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { database } from "./database";
import { Q } from "@nozbe/watermelondb";
/**
Transform server response to match WatermelonDB schema
*/
const transformServerResponse = (serverData: any, tableName: string) => {
switch (tableName) {
case "locations":
return {
id: serverData.id.toString(),
name: serverData.name,
address: serverData.address,
organization: serverData.organization,
server_id: serverData.id,
synced: true,
created_date: serverData.created_date || new Date().toISOString(),
item_count: serverData.item_count || 0,
};
Hey Guys, I'm facing the record duplication error.
Backend: Have integer and sequenced id's
Frontend: Watermelon DB managed id's
In table's I've server_id to represent backend generated id.
Step 01: I've created a record on watermelon db. A record is created with server_id null.
Step 02: Sync function is called, I checked if the records being pushed to server don't have server_id, then get them synced. Server returns me two id's. i) client_id (which is watermelon id) ii) id (backend generated id).
Step 03: Based on client_id, I find record in watermelon db and updated that record server_id with id returned from server.
Step 04: I pulled changes, server returned me two things, created array and updated array, both have same information, and both have id's which server created, only way to differentiate between them is, before creating a new record on watermelon db, I've to check, if I've server_id matching with created array object id on watermelon db, then ignores it. However for updated record I'm not doing any checks, and watermelon db handles that logic.
Step 05: Now after above pull, a new record get's created with same information as I've above, with new watermelon db id and server_id null. Now it synced with server and cycle repeats.
Step 06: On 2nd pull, I get that record only in created array and not in updated array, and new duplication doesn't happens.
That's how things are going up, I tried debugging and logging and it's the only thing I found, If anyone knows what's causing the issues. Please reply to this.
Here's my sync code
`import { synchronize } from "@nozbe/watermelondb/sync";
import { client } from "../utils/api-client";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { database } from "./database";
import { Q } from "@nozbe/watermelondb";
/**
Transform server response to match WatermelonDB schema
*/
const transformServerResponse = (serverData: any, tableName: string) => {
switch (tableName) {
case "locations":
return {
id: serverData.id.toString(),
name: serverData.name,
address: serverData.address,
organization: serverData.organization,
server_id: serverData.id,
synced: true,
created_date: serverData.created_date || new Date().toISOString(),
item_count: serverData.item_count || 0,
};
default:
return serverData;
}
};
export const syncDatabase = async ({ lastPulledAt, orgId }: any) => {
await synchronize({
database,
pullChanges: async () => {
const response = await client(
sync/pull/${orgId}/
, {method: "POST",
data: {
lastPulledAt,
schemaVersion: 1,
migrations: null,
},
});
});
};
`
The text was updated successfully, but these errors were encountered: