diff --git a/src/providers/database-history/database-history-provider.tsx b/src/providers/database-history/database-history-provider.tsx index 191be3d..2bc2bb9 100644 --- a/src/providers/database-history/database-history-provider.tsx +++ b/src/providers/database-history/database-history-provider.tsx @@ -47,18 +47,18 @@ const DatabaseHistoryProvider: React.FC = ({ children }) => { return; } const normalizedDatabase = normalizeDatabase(database); - console.log (normalizedDatabase) + const normalizedPresent = normalizeDatabase(datatbaseState.present); const differences = compare(normalizedDatabase, normalizedPresent); - + if (differences && differences.length > 0) { setIsProcessing(true); const operations: DBDiffOperation[] = mapDiffToDBDiffOperation(differences); - console.log ( operations ) ; - /* + (async () => { try { + console.log(operations) await executeDbDiffOps(operations) setIsProcessing(false); } @@ -67,7 +67,7 @@ const DatabaseHistoryProvider: React.FC = ({ children }) => { setIsProcessing(false); } })() - */ + } }, [datatbaseState.present]); diff --git a/src/providers/database-provider/database-provider.tsx b/src/providers/database-provider/database-provider.tsx index 9a901e8..abeac83 100644 --- a/src/providers/database-provider/database-provider.tsx +++ b/src/providers/database-provider/database-provider.tsx @@ -151,9 +151,7 @@ const DatabaseProvider: React.FC = ({ children }) => { }) }, [db]); - // CRUD operations for indices - const createIndex = useCallback(async (index: IndexInsertType): Promise => { return await db.insert(indices).values({ ...index, @@ -253,6 +251,26 @@ const DatabaseProvider: React.FC = ({ children }) => { } else if (operation.type === "UPDATE_RELATIONSHIP") { await tx.update(relationships).set(operation.changes).where(eq(relationships.id, operation.relationshipId)); } + else if (operation.type == "CREATE_INDEX") { + await tx.insert(indices).values(operation.index); + if (operation.index.fieldIndices && Object.values(operation.index.fieldIndices).length > 0) + await tx.insert(field_indices).values(Object.values(operation.index.fieldIndices)); + + } + else if (operation.type == "DELETE_INDEX") { + await tx.delete(indices).where(eq(indices.id, operation.indexId)); + } + else if (operation.type == "UPDATE_INDEX") { + await tx.update(indices).set(operation.changes).where(eq(indices.id, operation.indexId)); + + } else if (operation.type == "UPDATE_FIELD_INDICES") { + if (operation.delete.length > 0) { + await tx.delete(field_indices).where(inArray(field_indices.id, operation.delete)) + } + if (operation.create.length > 0) { + await tx.insert(field_indices).values(operation.create); + } + } } }) } catch (error) { diff --git a/src/utils/stackrender-connector.ts b/src/utils/stackrender-connector.ts index 660e06c..7745f21 100644 --- a/src/utils/stackrender-connector.ts +++ b/src/utils/stackrender-connector.ts @@ -66,7 +66,7 @@ export class StackRenderConnector implements PowerSyncBackendConnector { try { let batch: any[] = []; for (let operation of transaction.crud) { - + if (operation.op != UpdateType.DELETE && Object.keys(operation.opData as any).length == 0) continue @@ -78,6 +78,7 @@ export class StackRenderConnector implements PowerSyncBackendConnector { }; batch.push(payload); } + if (batch.length > 0) { const response = await fetch(`${this.config.backendUrl}/api/data`, {