fix(sync): proyectar el valor del servidor en conflicto a columnas locales
El backend devuelve el modelo completo (toArray) en un conflicto: incluye geometry como array y columnas inexistentes en local (uuid, created_at, deleted_at, client_updated_at...). Volcarlo tal cual en SQLite lanzaba "no such column"/error de bind y abortaba el ciclo de sync entero ante cualquier edición concurrente. Ahora se proyecta por los mismos pick* del PULL. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+35
-1
@@ -390,6 +390,20 @@ export async function getTemplates(): Promise<Template[]> {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persiste el catálogo global de plantillas (GET /templates) para tenerlas
|
||||||
|
* SIEMPRE en local y poder inspeccionar offline, aunque el proyecto concreto
|
||||||
|
* aún no se haya abierto. Upsert por id: no borra las que ya hubiera.
|
||||||
|
*/
|
||||||
|
export async function saveTemplates(templates: Template[]): Promise<void> {
|
||||||
|
const db = await getDb();
|
||||||
|
await db.withTransactionAsync(async () => {
|
||||||
|
for (const t of templates) {
|
||||||
|
await upsertById(db, 'templates', pickTemplate(t));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export async function getFeatureTypes(): Promise<FeatureType[]> {
|
export async function getFeatureTypes(): Promise<FeatureType[]> {
|
||||||
const db = await getDb();
|
const db = await getDb();
|
||||||
return db.getAllAsync<FeatureType>('SELECT * FROM feature_types ORDER BY name');
|
return db.getAllAsync<FeatureType>('SELECT * FROM feature_types ORDER BY name');
|
||||||
@@ -409,6 +423,22 @@ export async function getMediaFor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Proyecta el valor crudo del servidor (un `Model::toArray()`) a SOLO las
|
||||||
|
* columnas que existen en la tabla local, reutilizando los mismos `pick*` del
|
||||||
|
* PULL. Imprescindible: el `toArray()` trae columnas inexistentes en local
|
||||||
|
* (uuid, created_at, deleted_at, client_updated_at, properties…) y, en features,
|
||||||
|
* `geometry` como array —que SQLite no puede bindear—. Sin esto, un conflicto
|
||||||
|
* lanzaría "no such column"/error de bind y abortaría el ciclo de sync entero.
|
||||||
|
*/
|
||||||
|
const CONFLICT_PROJECTOR: Record<string, (v: never) => Record<string, unknown>> = {
|
||||||
|
features: (v) => pickFeature(v as Feature),
|
||||||
|
issues: (v) => pickIssue(v as Issue),
|
||||||
|
issue_tasks: (v) => pickIssueTask(v as IssueTask),
|
||||||
|
inspections: (v) => pickInspection(v as Inspection),
|
||||||
|
issue_comments: (v) => pickIssueComment(v as IssueComment),
|
||||||
|
};
|
||||||
|
|
||||||
/** Aplica el valor del servidor (recibido en un conflicto) a la fila local. */
|
/** Aplica el valor del servidor (recibido en un conflicto) a la fila local. */
|
||||||
export async function applyServerValue(
|
export async function applyServerValue(
|
||||||
table: string,
|
table: string,
|
||||||
@@ -416,7 +446,11 @@ export async function applyServerValue(
|
|||||||
serverValue: Record<string, unknown>,
|
serverValue: Record<string, unknown>,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const db = await getDb();
|
const db = await getDb();
|
||||||
await upsertById(db, table, { id, ...serverValue });
|
const project = CONFLICT_PROJECTOR[table];
|
||||||
|
// Si no hay proyector conocido, cae a un id-only (no-op seguro) en vez de
|
||||||
|
// volcar columnas crudas que podrían no existir en la tabla local.
|
||||||
|
const row = project ? project(serverValue as never) : { id };
|
||||||
|
await upsertById(db, table, { ...row, id });
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------- creaciones offline (optimista + reconciliación) ----------
|
// ---------- creaciones offline (optimista + reconciliación) ----------
|
||||||
|
|||||||
Reference in New Issue
Block a user