fix: make canvas node sync idempotent
This commit is contained in:
parent
2d76ec1629
commit
eec475287d
@ -11,6 +11,7 @@ import {
|
|||||||
inArray,
|
inArray,
|
||||||
like,
|
like,
|
||||||
lt,
|
lt,
|
||||||
|
notInArray,
|
||||||
or,
|
or,
|
||||||
sql,
|
sql,
|
||||||
type SQL,
|
type SQL,
|
||||||
@ -1711,7 +1712,6 @@ export async function syncCanvasNodesForProject(params: {
|
|||||||
eq(mediaAssetRelation.relationType, "located_on_canvas"),
|
eq(mediaAssetRelation.relationType, "located_on_canvas"),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
await db.delete(canvasNode).where(eq(canvasNode.projectId, params.projectId));
|
|
||||||
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const insertableNodes = enrichedNodes.map((node) => {
|
const insertableNodes = enrichedNodes.map((node) => {
|
||||||
@ -1744,7 +1744,32 @@ export async function syncCanvasNodesForProject(params: {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (insertableNodes.length > 0) {
|
if (insertableNodes.length > 0) {
|
||||||
await db.insert(canvasNode).values(insertableNodes);
|
await db
|
||||||
|
.insert(canvasNode)
|
||||||
|
.values(insertableNodes)
|
||||||
|
.onConflictDoUpdate({
|
||||||
|
target: canvasNode.id,
|
||||||
|
set: {
|
||||||
|
projectId: sql`excluded."projectId"`,
|
||||||
|
type: sql`excluded."type"`,
|
||||||
|
position: sql`excluded."position"`,
|
||||||
|
size: sql`excluded."size"`,
|
||||||
|
data: sql`excluded."data"`,
|
||||||
|
connections: sql`excluded."connections"`,
|
||||||
|
parentId: sql`excluded."parentId"`,
|
||||||
|
updatedAt: sql`excluded."updatedAt"`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const activeNodeIds = insertableNodes.map((node) => node.id);
|
||||||
|
await db.delete(canvasNode).where(
|
||||||
|
and(
|
||||||
|
eq(canvasNode.projectId, params.projectId),
|
||||||
|
notInArray(canvasNode.id, activeNodeIds),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
await db.delete(canvasNode).where(eq(canvasNode.projectId, params.projectId));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const node of enrichedNodes) {
|
for (const node of enrichedNodes) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user