mirror of
https://github.com/colanode/colanode.git
synced 2025-12-29 00:25:03 +01:00
Perform node replace on sync fail
This commit is contained in:
@@ -508,6 +508,67 @@ class NodeService {
|
||||
return false;
|
||||
}
|
||||
|
||||
public async serverUpsert(userId: string, node: ServerNodeState) {
|
||||
const workspaceDatabase =
|
||||
await databaseService.getWorkspaceDatabase(userId);
|
||||
|
||||
const existingNode = await workspaceDatabase
|
||||
.selectFrom('nodes')
|
||||
.where('id', '=', node.id)
|
||||
.selectAll()
|
||||
.executeTakeFirst();
|
||||
|
||||
const ydoc = new YDoc(node.id, existingNode?.state);
|
||||
const attributes = ydoc.getAttributes();
|
||||
|
||||
const result = await workspaceDatabase
|
||||
.insertInto('nodes')
|
||||
.returningAll()
|
||||
.values({
|
||||
id: node.id,
|
||||
attributes: JSON.stringify(attributes),
|
||||
state: ydoc.getState(),
|
||||
created_at: node.createdAt,
|
||||
created_by: node.createdBy,
|
||||
version_id: node.versionId,
|
||||
server_created_at: node.serverCreatedAt,
|
||||
server_version_id: node.versionId,
|
||||
})
|
||||
.onConflict((cb) =>
|
||||
cb.columns(['id']).doUpdateSet({
|
||||
state: ydoc.getState(),
|
||||
attributes: JSON.stringify(attributes),
|
||||
updated_at: node.updatedAt,
|
||||
updated_by: node.updatedBy,
|
||||
version_id: node.versionId,
|
||||
server_created_at: node.serverCreatedAt,
|
||||
server_updated_at: node.serverUpdatedAt,
|
||||
server_version_id: node.versionId,
|
||||
})
|
||||
)
|
||||
.executeTakeFirst();
|
||||
|
||||
if (result) {
|
||||
if (existingNode) {
|
||||
eventBus.publish({
|
||||
type: 'node_updated',
|
||||
userId: userId,
|
||||
node: mapNode(result),
|
||||
});
|
||||
} else {
|
||||
eventBus.publish({
|
||||
type: 'node_created',
|
||||
userId: userId,
|
||||
node: mapNode(result),
|
||||
});
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public async serverDelete(userId: string, nodeId: string) {
|
||||
const workspaceDatabase =
|
||||
await databaseService.getWorkspaceDatabase(userId);
|
||||
|
||||
@@ -205,7 +205,7 @@ class SyncService {
|
||||
continue;
|
||||
}
|
||||
|
||||
const nodeSynced = await nodeService.serverSync(userId, states.node);
|
||||
const nodeSynced = await nodeService.serverUpsert(userId, states.node);
|
||||
if (nodeSynced) {
|
||||
changesToDelete.push(...changeIds);
|
||||
socketService.sendMessage(workspace.account_id, {
|
||||
|
||||
Reference in New Issue
Block a user