Open-source and local-first Slack and Notion alternative that puts you in control of your data
TypeScript chatchat-applicationcrdteditorelectronknowledge-baselocal-firstnotionnotion-alternativerealtime-collaborationself-hostedslacksqliteteam-collaborationwikiyjs
Add a retry count for mutation sync
Hakan Shehu6226d5e492a3
diff --git a/desktop/src/data/migrations/workspace.ts b/desktop/src/data/migrations/workspace.tsindex 05235c8d..fc105bb4 100644--- a/desktop/src/data/migrations/workspace.ts+++ b/desktop/src/data/migrations/workspace.ts@@ -2,27 +2,41 @@ import { Migration, sql } from 'kysely';const createNodesTable: Migration = {up: async (db) => {- await sql`- CREATE TABLE nodes (- id TEXT PRIMARY KEY NOT NULL,- type TEXT GENERATED ALWAYS AS (json_extract(attributes, '$.type')) STORED NOT NULL,- parent_id TEXT GENERATED ALWAYS AS (json_extract(attributes, '$.parentId')) STORED REFERENCES nodes(id) ON DELETE CASCADE,- "index" TEXT GENERATED ALWAYS AS (json_extract(attributes, '$.index')) STORED,- attributes TEXT NOT NULL,- state TEXT NOT NULL,- created_at TEXT NOT NULL,- updated_at TEXT,- created_by TEXT NOT NULL,- updated_by TEXT,- version_id TEXT NOT NULL,- server_created_at TEXT,- server_updated_at TEXT,- server_version_id TEXT+ await db.schema+ .createTable('nodes')+ .addColumn('id', 'text', (col) => col.primaryKey().notNull())+ .addColumn('type', 'text', (col) =>+ col+ .notNull()+ .generatedAlwaysAs(sql`json_extract(attributes, '$.type')`)+ .stored(),)- `.execute(db);+ .addColumn('parent_id', 'text', (col) =>+ col+ .generatedAlwaysAs(sql`json_extract(attributes, '$.parentId')`)+ .stored()+ .references('nodes.id')+ .onDelete('cascade'),+ )+ .addColumn('index', 'text', (col) =>+ col+ .generatedAlwaysAs(sql`json_extract(attributes, '$.index')`)+ .stored(),+ )+ .addColumn('attributes', 'text', (col) => col.notNull())+ .addColumn('state', 'text', (col) => col.notNull())+ .addColumn('created_at', 'text', (col) => col.notNull())+ .addColumn('updated_at', 'text')+ .addColumn('created_by', 'text', (col) => col.notNull())+ .addColumn('updated_by', 'text')+ .addColumn('version_id', 'text', (col) => col.notNull())+ .addColumn('server_created_at', 'text')+ .addColumn('server_updated_at', 'text')+ .addColumn('server_version_id', 'text')+ .execute();},down: async (db) => {- await sql`DROP TABLE IF EXISTS nodes`.execute(db);+ await db.schema.dropTable('nodes').execute();},};@@ -72,6 +86,7 @@ const createMutationsTable: Migration = {.addColumn('before', 'text').addColumn('after', 'text').addColumn('created_at', 'text', (col) => col.notNull())+ .addColumn('retry_count', 'integer', (col) => col.defaultTo(0)).execute();},down: async (db) => {diff --git a/desktop/src/data/schemas/workspace.ts b/desktop/src/data/schemas/workspace.tsindex c326ba36..8926a541 100644--- a/desktop/src/data/schemas/workspace.ts+++ b/desktop/src/data/schemas/workspace.ts@@ -40,6 +40,7 @@ interface MutationTable {before: ColumnType<string | null, string | null, never>;after: ColumnType<string | null, string | null, never>;created_at: ColumnType<string, string, never>;+ retry_count: ColumnType<number, number, number>;}export type SelectMutation = Selectable<MutationTable>;diff --git a/desktop/src/data/workspace-manager.ts b/desktop/src/data/workspace-manager.tsindex 048e9260..2db4e396 100644--- a/desktop/src/data/workspace-manager.ts+++ b/desktop/src/data/workspace-manager.ts@@ -16,7 +16,10 @@ import {resultHasChanged,} from '@/data/utils';import { SubscribedQueryContext, SubscribedQueryResult } from '@/types/queries';-import { ServerMutation } from '@/types/mutations';+import {+ ServerExecuteMutationsResponse,+ ServerMutation,+} from '@/types/mutations';import { eventBus } from '@/lib/event-bus';import { AxiosInstance } from 'axios';import { debounce, isEqual } from 'lodash';@@ -191,16 +194,31 @@ export class WorkspaceManager {}try {- const { status } = await this.axios.post<ServerMutation>('v1/mutations', {- workspaceId: this.workspace.id,- mutations: mutations,- });+ const { status, data } =+ await this.axios.post<ServerExecuteMutationsResponse>('v1/mutations', {+ workspaceId: this.workspace.id,+ mutations: mutations,+ });++ if (status !== 200) {+ return;+ }- if (status === 200) {- const mutationIds = mutations.map((mutation) => mutation.id);+ const executedMutations = data.executedMutations;+ await this.database+ .deleteFrom('mutations')+ .where('id', 'in', executedMutations)+ .execute();++ const failedMutationIds = mutations+ .filter((mutation) => !executedMutations.includes(mutation.id))+ .map((mutation) => mutation.id);++ if (failedMutationIds.length > 0) {await this.database- .deleteFrom('mutations')- .where('id', 'in', mutationIds)+ .updateTable('mutations')+ .set((eb) => ({ retry_count: eb('retry_count', '+', 1) }))+ .where('id', 'in', failedMutationIds).execute();}} catch (error) {@@ -211,6 +229,8 @@ export class WorkspaceManager {public async executeServerMutation(mutation: ServerMutation): Promise<void> {if (mutation.table === 'nodes') {await this.executeNodeServerMutation(mutation);+ } else if (mutation.table === 'node_reactions') {+ await this.executeNodeReactionServerMutation(mutation);}//other cases in the future@@ -233,6 +253,27 @@ export class WorkspaceManager {}}+ private async executeNodeReactionServerMutation(+ mutation: ServerMutation,+ ): Promise<void> {+ if (mutation.action === 'insert' && mutation.after) {+ await this.syncNodeReactionFromServer(mutation.after);+ } else if (mutation.action === 'delete' && mutation.before) {+ await this.database+ .deleteFrom('node_reactions')+ .where((eb) =>+ eb.and([+ eb('node_id', '=', mutation.before.node_id),+ eb('reactor_id', '=', mutation.before.reactor_id),+ eb('reaction', '=', mutation.before.reaction),+ ]),+ )+ .execute();++ this.debouncedNotifyQuerySubscribers(['node_reactions']);+ }+ }+public isSynced(): boolean {return this.workspace.synced;}@@ -329,6 +370,8 @@ export class WorkspaceManager {}),).execute();++ this.debouncedNotifyQuerySubscribers(['node_reactions']);}public shouldUpdateNodeFromServer(diff --git a/desktop/src/types/mutations.ts b/desktop/src/types/mutations.tsindex 4ef6c246..582d5284 100644--- a/desktop/src/types/mutations.ts+++ b/desktop/src/types/mutations.ts@@ -6,3 +6,8 @@ export type ServerMutation = {before: any | null;after: any | null;};++export type ServerExecuteMutationsResponse = {+ success: boolean;+ executedMutations: number[];+};diff --git a/server/src/routes/mutations.ts b/server/src/routes/mutations.tsindex c1367864..6ed18b03 100644--- a/server/src/routes/mutations.ts+++ b/server/src/routes/mutations.ts@@ -14,42 +14,52 @@ export const mutationsRouter = Router();mutationsRouter.post('/', async (req: NeuronRequest, res: NeuronResponse) => {const input = req.body as ExecuteLocalMutationsInput;+ const executedMutations: number[] = [];for (const mutation of input.mutations) {- switch (mutation.table) {- case 'nodes': {- switch (mutation.action) {- case 'insert': {- await handleCreateNodeMutation(input.workspaceId, mutation);- break;- }- case 'update': {- await handleUpdateNodeMutation(input.workspaceId, mutation);- break;- }- case 'delete': {- await handleDeleteNodeMutation(input.workspaceId, mutation);- break;+ try {+ switch (mutation.table) {+ case 'nodes': {+ switch (mutation.action) {+ case 'insert': {+ await handleCreateNodeMutation(input.workspaceId, mutation);+ break;+ }+ case 'update': {+ await handleUpdateNodeMutation(input.workspaceId, mutation);+ break;+ }+ case 'delete': {+ await handleDeleteNodeMutation(input.workspaceId, mutation);+ break;+ }}+ break;}- break;- }- case 'node_reactions': {- switch (mutation.action) {- case 'insert': {- await handleCreateNodeReactionMutation(input.workspaceId, mutation);- break;- }- case 'delete': {- await handleDeleteNodeReactionMutation(mutation);- break;+ case 'node_reactions': {+ switch (mutation.action) {+ case 'insert': {+ await handleCreateNodeReactionMutation(+ input.workspaceId,+ mutation,+ );+ break;+ }+ case 'delete': {+ await handleDeleteNodeReactionMutation(mutation);+ break;+ }}+ break;}- break;}++ executedMutations.push(mutation.id);+ } catch (error) {+ console.error(error);}}- res.status(200).json({ success: true });+ res.status(200).json({ success: true, executedMutations });});const handleCreateNodeMutation = async (