Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

New verison for change tracking in desktop

Hakan Shehu5a1b3c0fd874
diff --git a/desktop/src/lib/nodes.ts b/desktop/src/lib/nodes.ts
index 4e90addc..2eeb83ba 100644
--- a/desktop/src/lib/nodes.ts
+++ b/desktop/src/lib/nodes.ts
@@ -1,14 +1,7 @@
-import { CreateNode, SelectNode } from '@/main/data/workspace/schema';
-import {
- LocalNode,
- LocalNodeWithChildren,
- NodeInsertInput,
-} from '@/types/nodes';
+import { SelectNode } from '@/main/data/workspace/schema';
+import { LocalNode, LocalNodeWithChildren } from '@/types/nodes';
import { generateKeyBetween } from 'fractional-indexing-jittered';
import { NodeTypes } from '@/lib/constants';
-import * as Y from 'yjs';
-import { generateId, IdType } from '@/lib/id';
-import { fromUint8Array } from 'js-base64';
export const buildNodeWithChildren = (
node: LocalNode,
@@ -34,37 +27,6 @@ export const generateNodeIndex = (
return generateKeyBetween(lower, upper);
};
-export const buildCreateNode = (
- input: NodeInsertInput,
- userId: string,
-): CreateNode => {
- const doc = new Y.Doc({
- guid: input.id,
- });
-
- const attributesMap = doc.getMap('attributes');
-
- doc.transact(() => {
- for (const [key, value] of Object.entries(input.attributes)) {
- if (value !== undefined) {
- attributesMap.set(key, value);
- }
- }
- });
-
- const attributes = JSON.stringify(attributesMap.toJSON());
- const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
-
- return {
- id: input.id,
- attributes: attributes,
- state: encodedState,
- created_at: new Date().toISOString(),
- created_by: userId,
- version_id: generateId(IdType.Version),
- };
-};
-
export const mapNode = (row: SelectNode): LocalNode => {
return {
id: row.id,
diff --git a/desktop/src/main/data/workspace/migrations.ts b/desktop/src/main/data/workspace/migrations.ts
index c0adc431..bdea5ad5 100644
--- a/desktop/src/main/data/workspace/migrations.ts
+++ b/desktop/src/main/data/workspace/migrations.ts
@@ -62,10 +62,7 @@ const createChangesTable: Migration = {
await db.schema
.createTable('changes')
.addColumn('id', 'integer', (col) => col.notNull().primaryKey())
- .addColumn('table', 'text', (col) => col.notNull())
- .addColumn('action', 'text', (col) => col.notNull())
- .addColumn('before', 'text')
- .addColumn('after', 'text')
+ .addColumn('data', 'text', (col) => col.notNull())
.addColumn('created_at', 'text', (col) => col.notNull())
.addColumn('retry_count', 'integer', (col) => col.defaultTo(0))
.execute();
@@ -75,135 +72,6 @@ const createChangesTable: Migration = {
},
};
-const createNodeInsertChangeTrigger: Migration = {
- up: async (db) => {
- await sql`
- CREATE TRIGGER node_insert_change
- AFTER INSERT ON nodes
- FOR EACH ROW
- WHEN NEW.server_version_id IS NULL
- BEGIN
- INSERT INTO changes ('action', 'table', 'after', 'created_at')
- VALUES (
- 'insert',
- 'nodes',
- json_object(
- 'id', NEW.'id',
- 'type', NEW.'type',
- 'parent_id', NEW.'parent_id',
- 'index', NEW.'index',
- 'attributes', NEW.'attributes',
- 'state', NEW.'state',
- 'created_at', NEW.'created_at',
- 'updated_at', NEW.'updated_at',
- 'created_by', NEW.'created_by',
- 'updated_by', NEW.'updated_by',
- 'version_id', NEW.'version_id',
- 'server_created_at', NEW.'server_created_at',
- 'server_updated_at', NEW.'server_updated_at',
- 'server_version_id', NEW.'server_version_id'
- ),
- datetime('now')
- );
- END;
- `.execute(db);
- },
- down: async (db) => {
- await sql`DROP TRIGGER node_insert_change`.execute(db);
- },
-};
-
-const createNodeUpdateChangeTrigger: Migration = {
- up: async (db) => {
- await sql`
- CREATE TRIGGER node_update_change
- AFTER UPDATE ON nodes
- FOR EACH ROW
- WHEN NEW.server_version_id IS NULL OR NEW.version_id != NEW.server_version_id
- BEGIN
- INSERT INTO changes ('action', 'table', 'before', 'after', 'created_at')
- VALUES (
- 'update',
- 'nodes',
- json_object(
- 'id', OLD.'id',
- 'type', OLD.'type',
- 'parent_id', OLD.'parent_id',
- 'index', OLD.'index',
- 'attributes', OLD.'attributes',
- 'state', OLD.'state',
- 'created_at', OLD.'created_at',
- 'updated_at', OLD.'updated_at',
- 'created_by', OLD.'created_by',
- 'updated_by', OLD.'updated_by',
- 'version_id', OLD.'version_id',
- 'server_created_at', OLD.'server_created_at',
- 'server_updated_at', OLD.'server_updated_at',
- 'server_version_id', OLD.'server_version_id'
- ),
- json_object(
- 'id', NEW.'id',
- 'type', NEW.'type',
- 'parent_id', NEW.'parent_id',
- 'index', NEW.'index',
- 'attributes', NEW.'attributes',
- 'state', NEW.'state',
- 'created_at', NEW.'created_at',
- 'updated_at', NEW.'updated_at',
- 'created_by', NEW.'created_by',
- 'updated_by', NEW.'updated_by',
- 'version_id', NEW.'version_id',
- 'server_created_at', NEW.'server_created_at',
- 'server_updated_at', NEW.'server_updated_at',
- 'server_version_id', NEW.'server_version_id'
- ),
- datetime('now')
- );
- END;
- `.execute(db);
- },
- down: async (db) => {
- await sql`DROP TRIGGER node_update_change`.execute(db);
- },
-};
-
-const createNodeDeleteChangeTrigger: Migration = {
- up: async (db) => {
- await sql`
- CREATE TRIGGER node_delete_change
- AFTER DELETE ON nodes
- FOR EACH ROW
- BEGIN
- INSERT INTO changes ('action', 'table', 'before', 'created_at')
- VALUES (
- 'delete',
- 'nodes',
- json_object(
- 'id', OLD.'id',
- 'type', OLD.'type',
- 'parent_id', OLD.'parent_id',
- 'index', OLD.'index',
- 'attributes', OLD.'attributes',
- 'state', OLD.'state',
- 'created_at', OLD.'created_at',
- 'updated_at', OLD.'updated_at',
- 'created_by', OLD.'created_by',
- 'updated_by', OLD.'updated_by',
- 'version_id', OLD.'version_id',
- 'server_created_at', OLD.'server_created_at',
- 'server_updated_at', OLD.'server_updated_at',
- 'server_version_id', OLD.'server_version_id'
- ),
- datetime('now')
- );
- END;
- `.execute(db);
- },
- down: async (db) => {
- await sql`DROP TRIGGER node_delete_change`.execute(db);
- },
-};
-
const createNodeInsertNameTrigger: Migration = {
up: async (db) => {
await sql`
@@ -307,12 +175,9 @@ export const workspaceDatabaseMigrations: Record<string, Migration> = {
'00001_create_nodes_table': createNodesTable,
'00002_create_node_names_table': createNodeNamesTable,
'00003_create_changes_table': createChangesTable,
- '00004_create_node_insert_change_trigger': createNodeInsertChangeTrigger,
- '00005_create_node_update_change_trigger': createNodeUpdateChangeTrigger,
- '00006_create_node_delete_change_trigger': createNodeDeleteChangeTrigger,
- '00007_create_node_insert_name_trigger': createNodeInsertNameTrigger,
- '00008_create_node_update_name_trigger': createNodeUpdateNameTrigger,
- '00009_create_node_delete_name_trigger': createNodeDeleteNameTrigger,
- '00010_create_uploads_table': createUploadsTable,
- '00011_create_downloads_table': createDownloadsTable,
+ '00004_create_node_insert_name_trigger': createNodeInsertNameTrigger,
+ '00005_create_node_update_name_trigger': createNodeUpdateNameTrigger,
+ '00006_create_node_delete_name_trigger': createNodeDeleteNameTrigger,
+ '00007_create_uploads_table': createUploadsTable,
+ '00008_create_downloads_table': createDownloadsTable,
};
diff --git a/desktop/src/main/data/workspace/schema.ts b/desktop/src/main/data/workspace/schema.ts
index b837d300..035a8ecb 100644
--- a/desktop/src/main/data/workspace/schema.ts
+++ b/desktop/src/main/data/workspace/schema.ts
@@ -23,10 +23,7 @@ export type UpdateNode = Updateable<NodeTable>;
interface ChangeTable {
id: ColumnType<number, never, never>;
- table: ColumnType<string, string, never>;
- action: ColumnType<string, string, never>;
- before: ColumnType<string | null, string | null, never>;
- after: ColumnType<string | null, string | null, never>;
+ data: ColumnType<string, string, never>;
created_at: ColumnType<string, string, never>;
retry_count: ColumnType<number, number, number>;
}
diff --git a/desktop/src/main/handlers/mutations/board-view-create.ts b/desktop/src/main/handlers/mutations/board-view-create.ts
index 0fc25b64..cdbd295f 100644
--- a/desktop/src/main/handlers/mutations/board-view-create.ts
+++ b/desktop/src/main/handlers/mutations/board-view-create.ts
@@ -1,10 +1,13 @@
+import * as Y from 'yjs';
import { databaseManager } from '@/main/data/database-manager';
import { NodeTypes, ViewNodeTypes } from '@/lib/constants';
import { generateId, IdType } from '@/lib/id';
-import { buildCreateNode, generateNodeIndex } from '@/lib/nodes';
+import { generateNodeIndex } from '@/lib/nodes';
import { compareString } from '@/lib/utils';
import { MutationHandler, MutationResult } from '@/operations/mutations';
import { BoardViewCreateMutationInput } from '@/operations/mutations/board-view-create';
+import { fromUint8Array } from 'js-base64';
+import { LocalCreateNodeChangeData } from '@/types/sync';
export class BoardViewCreateMutationHandler
implements MutationHandler<BoardViewCreateMutationInput>
@@ -35,24 +38,55 @@ export class BoardViewCreateMutationHandler
: null;
const id = generateId(IdType.BoardView);
- await workspaceDatabase
- .insertInto('nodes')
- .values(
- buildCreateNode(
- {
- id: id,
- attributes: {
- type: NodeTypes.BoardView,
- parentId: input.databaseId,
- index: generateNodeIndex(maxIndex, null),
- name: input.name,
- groupBy: input.groupBy,
- },
- },
- input.userId,
- ),
- )
- .execute();
+ const versionId = generateId(IdType.Version);
+ const createdAt = new Date().toISOString();
+
+ const doc = new Y.Doc({
+ guid: id,
+ });
+
+ const attributesMap = doc.getMap('attributes');
+ doc.transact(() => {
+ attributesMap.set('type', NodeTypes.BoardView);
+ attributesMap.set('parentId', input.databaseId);
+ attributesMap.set('index', generateNodeIndex(maxIndex, null));
+ attributesMap.set('name', input.name);
+ attributesMap.set('groupBy', input.groupBy);
+ });
+
+ const attributes = JSON.stringify(attributesMap.toJSON());
+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
+
+ const changeData: LocalCreateNodeChangeData = {
+ type: 'node_create',
+ id: id,
+ state: encodedState,
+ createdAt: createdAt,
+ createdBy: input.userId,
+ versionId: versionId,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .insertInto('nodes')
+ .values({
+ id: id,
+ attributes: attributes,
+ state: encodedState,
+ created_at: createdAt,
+ created_by: input.userId,
+ version_id: versionId,
+ })
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: createdAt,
+ })
+ .execute();
+ });
return {
output: {
diff --git a/desktop/src/main/handlers/mutations/calendar-view-create.ts b/desktop/src/main/handlers/mutations/calendar-view-create.ts
index 7bffb35e..7059ee7f 100644
--- a/desktop/src/main/handlers/mutations/calendar-view-create.ts
+++ b/desktop/src/main/handlers/mutations/calendar-view-create.ts
@@ -1,10 +1,13 @@
+import * as Y from 'yjs';
import { databaseManager } from '@/main/data/database-manager';
import { NodeTypes, ViewNodeTypes } from '@/lib/constants';
import { generateId, IdType } from '@/lib/id';
-import { buildCreateNode, generateNodeIndex } from '@/lib/nodes';
+import { generateNodeIndex } from '@/lib/nodes';
import { compareString } from '@/lib/utils';
import { MutationHandler, MutationResult } from '@/operations/mutations';
import { CalendarViewCreateMutationInput } from '@/operations/mutations/calendar-view-create';
+import { fromUint8Array } from 'js-base64';
+import { LocalCreateNodeChangeData } from '@/types/sync';
export class CalendarViewCreateMutationHandler
implements MutationHandler<CalendarViewCreateMutationInput>
@@ -35,24 +38,55 @@ export class CalendarViewCreateMutationHandler
: null;
const id = generateId(IdType.CalendarView);
- await workspaceDatabase
- .insertInto('nodes')
- .values(
- buildCreateNode(
- {
- id: id,
- attributes: {
- type: NodeTypes.CalendarView,
- parentId: input.databaseId,
- index: generateNodeIndex(maxIndex, null),
- name: input.name,
- groupBy: input.groupBy,
- },
- },
- input.userId,
- ),
- )
- .execute();
+ const versionId = generateId(IdType.Version);
+ const createdAt = new Date().toISOString();
+
+ const doc = new Y.Doc({
+ guid: id,
+ });
+
+ const attributesMap = doc.getMap('attributes');
+ doc.transact(() => {
+ attributesMap.set('type', NodeTypes.CalendarView);
+ attributesMap.set('parentId', input.databaseId);
+ attributesMap.set('index', generateNodeIndex(maxIndex, null));
+ attributesMap.set('name', input.name);
+ attributesMap.set('groupBy', input.groupBy);
+ });
+
+ const attributes = JSON.stringify(attributesMap.toJSON());
+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
+
+ const changeData: LocalCreateNodeChangeData = {
+ type: 'node_create',
+ id: id,
+ state: encodedState,
+ createdAt: createdAt,
+ createdBy: input.userId,
+ versionId: versionId,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .insertInto('nodes')
+ .values({
+ id: id,
+ attributes: attributes,
+ state: encodedState,
+ created_at: createdAt,
+ created_by: input.userId,
+ version_id: versionId,
+ })
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: createdAt,
+ })
+ .execute();
+ });
return {
output: {
@@ -64,6 +98,11 @@ export class CalendarViewCreateMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
],
};
}
diff --git a/desktop/src/main/handlers/mutations/channel-create.ts b/desktop/src/main/handlers/mutations/channel-create.ts
index f0031f06..0bebcd14 100644
--- a/desktop/src/main/handlers/mutations/channel-create.ts
+++ b/desktop/src/main/handlers/mutations/channel-create.ts
@@ -1,10 +1,13 @@
+import * as Y from 'yjs';
import { databaseManager } from '@/main/data/database-manager';
-import { NodeTypes, ViewNodeTypes } from '@/lib/constants';
+import { NodeTypes } from '@/lib/constants';
import { generateId, IdType } from '@/lib/id';
-import { buildCreateNode, generateNodeIndex } from '@/lib/nodes';
+import { generateNodeIndex } from '@/lib/nodes';
import { compareString } from '@/lib/utils';
import { MutationHandler, MutationResult } from '@/operations/mutations';
import { ChannelCreateMutationInput } from '@/operations/mutations/channel-create';
+import { fromUint8Array } from 'js-base64';
+import { LocalCreateNodeChangeData } from '@/types/sync';
export class ChannelCreateMutationHandler
implements MutationHandler<ChannelCreateMutationInput>
@@ -30,23 +33,54 @@ export class ChannelCreateMutationHandler
: null;
const id = generateId(IdType.Channel);
- await workspaceDatabase
- .insertInto('nodes')
- .values(
- buildCreateNode(
- {
- id: id,
- attributes: {
- type: NodeTypes.Channel,
- parentId: input.spaceId,
- index: generateNodeIndex(maxIndex, null),
- name: input.name,
- },
- },
- input.userId,
- ),
- )
- .execute();
+ const versionId = generateId(IdType.Version);
+ const createdAt = new Date().toISOString();
+
+ const doc = new Y.Doc({
+ guid: id,
+ });
+
+ const attributesMap = doc.getMap('attributes');
+ doc.transact(() => {
+ attributesMap.set('type', NodeTypes.Channel);
+ attributesMap.set('parentId', input.spaceId);
+ attributesMap.set('index', generateNodeIndex(maxIndex, null));
+ attributesMap.set('name', input.name);
+ });
+
+ const attributes = JSON.stringify(attributesMap.toJSON());
+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
+
+ const changeData: LocalCreateNodeChangeData = {
+ type: 'node_create',
+ id: id,
+ state: encodedState,
+ createdAt: createdAt,
+ createdBy: input.userId,
+ versionId: versionId,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .insertInto('nodes')
+ .values({
+ id: id,
+ attributes: attributes,
+ state: encodedState,
+ created_at: createdAt,
+ created_by: input.userId,
+ version_id: versionId,
+ })
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: createdAt,
+ })
+ .execute();
+ });
return {
output: {
@@ -58,6 +92,11 @@ export class ChannelCreateMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
],
};
}
diff --git a/desktop/src/main/handlers/mutations/chat-create.ts b/desktop/src/main/handlers/mutations/chat-create.ts
index 8772123e..a7ec6b37 100644
--- a/desktop/src/main/handlers/mutations/chat-create.ts
+++ b/desktop/src/main/handlers/mutations/chat-create.ts
@@ -6,6 +6,7 @@ import { ChatCreateMutationInput } from '@/operations/mutations/chat-create';
import { sql } from 'kysely';
import * as Y from 'yjs';
import { fromUint8Array } from 'js-base64';
+import { LocalCreateNodeChangeData } from '@/types/sync';
interface ChatRow {
id: string;
@@ -40,6 +41,9 @@ export class ChatCreateMutationHandler
}
const id = generateId(IdType.Chat);
+ const versionId = generateId(IdType.Version);
+ const createdAt = new Date().toISOString();
+
const doc = new Y.Doc({
guid: id,
});
@@ -58,17 +62,36 @@ export class ChatCreateMutationHandler
const attributes = JSON.stringify(attributesMap.toJSON());
const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
- await workspaceDatabase
- .insertInto('nodes')
- .values({
- id: id,
- attributes: attributes,
- state: encodedState,
- created_at: new Date().toISOString(),
- created_by: input.userId,
- version_id: generateId(IdType.Version),
- })
- .execute();
+ const changeData: LocalCreateNodeChangeData = {
+ type: 'node_create',
+ id: id,
+ state: encodedState,
+ createdAt: new Date().toISOString(),
+ createdBy: input.userId,
+ versionId: versionId,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .insertInto('nodes')
+ .values({
+ id: id,
+ attributes: attributes,
+ state: encodedState,
+ created_at: createdAt,
+ created_by: input.userId,
+ version_id: versionId,
+ })
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: createdAt,
+ })
+ .execute();
+ });
return {
output: {
diff --git a/desktop/src/main/handlers/mutations/database-create.ts b/desktop/src/main/handlers/mutations/database-create.ts
index 6a0b3b2c..0a7b2b51 100644
--- a/desktop/src/main/handlers/mutations/database-create.ts
+++ b/desktop/src/main/handlers/mutations/database-create.ts
@@ -1,10 +1,13 @@
+import * as Y from 'yjs';
import { databaseManager } from '@/main/data/database-manager';
import { NodeTypes } from '@/lib/constants';
import { generateId, IdType } from '@/lib/id';
-import { buildCreateNode, generateNodeIndex } from '@/lib/nodes';
+import { generateNodeIndex } from '@/lib/nodes';
import { compareString } from '@/lib/utils';
import { MutationHandler, MutationResult } from '@/operations/mutations';
import { DatabaseCreateMutationInput } from '@/operations/mutations/database-create';
+import { fromUint8Array } from 'js-base64';
+import { LocalCreateNodeChangeData } from '@/types/sync';
export class DatabaseCreateMutationHandler
implements MutationHandler<DatabaseCreateMutationInput>
@@ -29,52 +32,143 @@ export class DatabaseCreateMutationHandler
].index
: null;
+ const createdAt = new Date().toISOString();
+
const databaseId = generateId(IdType.Database);
+ const databaseVersionId = generateId(IdType.Version);
+
+ const databaseDoc = new Y.Doc({
+ guid: databaseId,
+ });
+
+ const attributesMap = databaseDoc.getMap('attributes');
+ databaseDoc.transact(() => {
+ attributesMap.set('type', NodeTypes.Database);
+ attributesMap.set('parentId', input.spaceId);
+ attributesMap.set('index', generateNodeIndex(maxIndex, null));
+ attributesMap.set('name', input.name);
+ });
+
+ const databaseAttributes = JSON.stringify(attributesMap.toJSON());
+ const databaseEncodedState = fromUint8Array(
+ Y.encodeStateAsUpdate(databaseDoc),
+ );
+
+ const databaseChangeData: LocalCreateNodeChangeData = {
+ type: 'node_create',
+ id: databaseId,
+ state: databaseEncodedState,
+ createdAt: createdAt,
+ createdBy: input.userId,
+ versionId: databaseVersionId,
+ };
+
const tableViewId = generateId(IdType.TableView);
+ const tableViewVersionId = generateId(IdType.Version);
+
+ const tableViewDoc = new Y.Doc({
+ guid: tableViewId,
+ });
+
+ const tableViewAttributesMap = tableViewDoc.getMap('attributes');
+ tableViewDoc.transact(() => {
+ tableViewAttributesMap.set('type', NodeTypes.TableView);
+ tableViewAttributesMap.set('parentId', databaseId);
+ tableViewAttributesMap.set('index', generateNodeIndex(null, null));
+ tableViewAttributesMap.set('name', 'Default');
+ });
+
+ const tableViewAttributes = JSON.stringify(tableViewAttributesMap.toJSON());
+ const tableViewEncodedState = fromUint8Array(
+ Y.encodeStateAsUpdate(tableViewDoc),
+ );
+
+ const tableViewChangeData: LocalCreateNodeChangeData = {
+ type: 'node_create',
+ id: tableViewId,
+ state: tableViewEncodedState,
+ createdAt: createdAt,
+ createdBy: input.userId,
+ versionId: tableViewVersionId,
+ };
+
const fieldId = generateId(IdType.Field);
+ const fieldVersionId = generateId(IdType.Version);
+
+ const fieldDoc = new Y.Doc({
+ guid: fieldId,
+ });
- await workspaceDatabase
- .insertInto('nodes')
- .values([
- buildCreateNode(
+ const fieldAttributesMap = fieldDoc.getMap('attributes');
+ fieldDoc.transact(() => {
+ fieldAttributesMap.set('type', NodeTypes.Field);
+ fieldAttributesMap.set('parentId', databaseId);
+ fieldAttributesMap.set('index', generateNodeIndex(null, null));
+ fieldAttributesMap.set('name', 'Comment');
+ fieldAttributesMap.set('dataType', 'text');
+ });
+
+ const fieldAttributes = JSON.stringify(fieldAttributesMap.toJSON());
+ const fieldEncodedState = fromUint8Array(Y.encodeStateAsUpdate(fieldDoc));
+
+ const fieldChangeData: LocalCreateNodeChangeData = {
+ type: 'node_create',
+ id: fieldId,
+ state: fieldEncodedState,
+ createdAt: createdAt,
+ createdBy: input.userId,
+ versionId: fieldVersionId,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .insertInto('nodes')
+ .values([
{
id: databaseId,
- attributes: {
- type: NodeTypes.Database,
- parentId: input.spaceId,
- index: generateNodeIndex(maxIndex, null),
- name: input.name,
- },
+ attributes: databaseAttributes,
+ state: databaseEncodedState,
+ created_at: createdAt,
+ created_by: input.userId,
+ version_id: databaseVersionId,
},
- input.userId,
- ),
- buildCreateNode(
{
id: tableViewId,
- attributes: {
- type: NodeTypes.TableView,
- parentId: databaseId,
- index: generateNodeIndex(null, null),
- name: 'Default',
- },
+ attributes: tableViewAttributes,
+ state: tableViewEncodedState,
+ created_at: createdAt,
+ created_by: input.userId,
+ version_id: tableViewVersionId,
},
- input.userId,
- ),
- buildCreateNode(
{
id: fieldId,
- attributes: {
- type: NodeTypes.Field,
- parentId: databaseId,
- index: generateNodeIndex(null, null),
- name: 'Comment',
- dataType: 'text',
- },
+ attributes: fieldAttributes,
+ state: fieldEncodedState,
+ created_at: createdAt,
+ created_by: input.userId,
+ version_id: fieldVersionId,
},
- input.userId,
- ),
- ])
- .execute();
+ ])
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values([
+ {
+ data: JSON.stringify(databaseChangeData),
+ created_at: createdAt,
+ },
+ {
+ data: JSON.stringify(tableViewChangeData),
+ created_at: createdAt,
+ },
+ {
+ data: JSON.stringify(fieldChangeData),
+ created_at: createdAt,
+ },
+ ])
+ .execute();
+ });
return {
output: {
@@ -86,6 +180,11 @@ export class DatabaseCreateMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
],
};
}
diff --git a/desktop/src/main/handlers/mutations/document-save.ts b/desktop/src/main/handlers/mutations/document-save.ts
index e34cd285..cd49c3bd 100644
--- a/desktop/src/main/handlers/mutations/document-save.ts
+++ b/desktop/src/main/handlers/mutations/document-save.ts
@@ -2,13 +2,10 @@ import * as Y from 'yjs';
import { fromUint8Array, toUint8Array } from 'js-base64';
import { mapNode } from '@/lib/nodes';
import { databaseManager } from '@/main/data/database-manager';
-import {
- MutationChange,
- MutationHandler,
- MutationResult,
-} from '@/operations/mutations';
+import { MutationHandler, MutationResult } from '@/operations/mutations';
import { DocumentSaveMutationInput } from '@/operations/mutations/document-save';
import { generateId, IdType } from '@/lib/id';
+import { LocalUpdateNodeChangeData } from '@/types/sync';
export class DocumentSaveMutationHandler
implements MutationHandler<DocumentSaveMutationInput>
@@ -36,45 +33,72 @@ export class DocumentSaveMutationHandler
}
const node = mapNode(document);
+ const versionId = generateId(IdType.Version);
+ const updatedAt = new Date().toISOString();
+ const updates: string[] = [];
+
const doc = new Y.Doc({
guid: node.id,
});
-
Y.applyUpdate(doc, toUint8Array(node.state));
+
+ doc.on('update', (update) => {
+ updates.push(fromUint8Array(update));
+ });
+
const attributesMap = doc.getMap('attributes');
attributesMap.set('content', input.content.content);
- const attributes = attributesMap.toJSON();
+ const attributes = JSON.stringify(attributesMap.toJSON());
const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
- const result = await workspaceDatabase
- .updateTable('nodes')
- .set({
- attributes: JSON.stringify(attributes),
- state: encodedState,
- updated_at: new Date().toISOString(),
- updated_by: input.userId,
- version_id: generateId(IdType.Version),
- })
- .where('id', '=', input.documentId)
- .execute();
+ const changeData: LocalUpdateNodeChangeData = {
+ type: 'node_update',
+ id: node.id,
+ updatedAt: updatedAt,
+ updatedBy: input.userId,
+ versionId: versionId,
+ updates: updates,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .updateTable('nodes')
+ .set({
+ attributes: attributes,
+ state: encodedState,
+ updated_at: updatedAt,
+ updated_by: input.userId,
+ version_id: versionId,
+ })
+ .where('id', '=', input.documentId)
+ .execute();
- const hasChanges = result.length > 0 && result[0].numUpdatedRows > 0;
- const changes: MutationChange[] = hasChanges
- ? [
- {
- type: 'workspace',
- table: 'nodes',
- userId: input.userId,
- },
- ]
- : [];
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: updatedAt,
+ })
+ .execute();
+ });
return {
output: {
success: true,
},
- changes: changes,
+ changes: [
+ {
+ type: 'workspace',
+ table: 'nodes',
+ userId: input.userId,
+ },
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
+ ],
};
}
}
diff --git a/desktop/src/main/handlers/mutations/field-create.ts b/desktop/src/main/handlers/mutations/field-create.ts
index ac689993..e2a61069 100644
--- a/desktop/src/main/handlers/mutations/field-create.ts
+++ b/desktop/src/main/handlers/mutations/field-create.ts
@@ -1,10 +1,13 @@
+import * as Y from 'yjs';
import { databaseManager } from '@/main/data/database-manager';
import { NodeTypes } from '@/lib/constants';
import { generateId, IdType } from '@/lib/id';
-import { buildCreateNode, generateNodeIndex } from '@/lib/nodes';
+import { generateNodeIndex } from '@/lib/nodes';
import { compareString } from '@/lib/utils';
import { MutationHandler, MutationResult } from '@/operations/mutations';
import { FieldCreateMutationInput } from '@/operations/mutations/field-create';
+import { fromUint8Array } from 'js-base64';
+import { LocalCreateNodeChangeData } from '@/types/sync';
export class FieldCreateMutationHandler
implements MutationHandler<FieldCreateMutationInput>
@@ -35,24 +38,55 @@ export class FieldCreateMutationHandler
: null;
const id = generateId(IdType.Field);
- await workspaceDatabase
- .insertInto('nodes')
- .values(
- buildCreateNode(
- {
- id: id,
- attributes: {
- type: NodeTypes.Field,
- parentId: input.databaseId,
- index: generateNodeIndex(maxIndex, null),
- name: input.name,
- dataType: input.dataType,
- },
- },
- input.userId,
- ),
- )
- .execute();
+ const versionId = generateId(IdType.Version);
+ const createdAt = new Date().toISOString();
+
+ const doc = new Y.Doc({
+ guid: id,
+ });
+
+ const attributesMap = doc.getMap('attributes');
+ doc.transact(() => {
+ attributesMap.set('type', NodeTypes.Field);
+ attributesMap.set('parentId', input.databaseId);
+ attributesMap.set('index', generateNodeIndex(maxIndex, null));
+ attributesMap.set('name', input.name);
+ attributesMap.set('dataType', input.dataType);
+ });
+
+ const attributes = JSON.stringify(attributesMap.toJSON());
+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
+
+ const changeData: LocalCreateNodeChangeData = {
+ type: 'node_create',
+ id: id,
+ state: encodedState,
+ createdAt: createdAt,
+ createdBy: input.userId,
+ versionId: versionId,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .insertInto('nodes')
+ .values({
+ id: id,
+ attributes: attributes,
+ state: encodedState,
+ created_at: createdAt,
+ created_by: input.userId,
+ version_id: versionId,
+ })
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: createdAt,
+ })
+ .execute();
+ });
return {
output: {
@@ -64,6 +98,11 @@ export class FieldCreateMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
],
};
}
diff --git a/desktop/src/main/handlers/mutations/file-create.ts b/desktop/src/main/handlers/mutations/file-create.ts
index 7e1df6d3..edb7c642 100644
--- a/desktop/src/main/handlers/mutations/file-create.ts
+++ b/desktop/src/main/handlers/mutations/file-create.ts
@@ -1,14 +1,15 @@
import fs from 'fs';
import mime from 'mime-types';
import path from 'path';
+import * as Y from 'yjs';
import { databaseManager } from '@/main/data/database-manager';
import { MutationHandler, MutationResult } from '@/operations/mutations';
import { generateId, IdType } from '@/lib/id';
import { FileCreateMutationInput } from '@/operations/mutations/file-create';
-import { buildCreateNode } from '@/lib/nodes';
import { NodeTypes } from '@/lib/constants';
import { fileManager } from '@/main/file-manager';
-
+import { LocalCreateNodeChangeData } from '@/types/sync';
+import { fromUint8Array } from 'js-base64';
export class FileCreateMutationHandler
implements MutationHandler<FileCreateMutationInput>
{
@@ -31,31 +32,60 @@ export class FileCreateMutationHandler
}
const id = generateId(IdType.File);
+ const versionId = generateId(IdType.Version);
+ const createdAt = new Date().toISOString();
+
fileManager.copyFileToWorkspace(filePath, id, extension, input.userId);
- await workspaceDatabase.transaction().execute(async (tx) => {
- await tx
+ const doc = new Y.Doc({
+ guid: id,
+ });
+
+ const attributesMap = doc.getMap('attributes');
+ doc.transact(() => {
+ attributesMap.set('type', NodeTypes.File);
+ attributesMap.set('parentId', input.parentId);
+ attributesMap.set('name', fileName);
+ attributesMap.set('fileName', fileName);
+ attributesMap.set('extension', extension);
+ attributesMap.set('size', size);
+ attributesMap.set('mimeType', mimeType);
+ });
+
+ const attributes = JSON.stringify(attributesMap.toJSON());
+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
+
+ const changeData: LocalCreateNodeChangeData = {
+ type: 'node_create',
+ id: id,
+ state: encodedState,
+ createdAt: createdAt,
+ createdBy: input.userId,
+ versionId: versionId,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
.insertInto('nodes')
- .values(
- buildCreateNode(
- {
- id: id,
- attributes: {
- type: NodeTypes.File,
- parentId: input.parentId,
- name: fileName,
- fileName: fileName,
- extension: extension,
- size: size,
- mimeType: mimeType,
- },
- },
- input.userId,
- ),
- )
+ .values({
+ id: id,
+ attributes: attributes,
+ state: encodedState,
+ created_at: createdAt,
+ created_by: input.userId,
+ version_id: versionId,
+ })
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: createdAt,
+ })
.execute();
- await tx
+ await trx
.insertInto('uploads')
.values({
node_id: id,
@@ -65,7 +95,7 @@ export class FileCreateMutationHandler
})
.execute();
- await tx
+ await trx
.insertInto('downloads')
.values({
node_id: id,
@@ -86,6 +116,11 @@ export class FileCreateMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
{
type: 'workspace',
table: 'downloads',
diff --git a/desktop/src/main/handlers/mutations/folder-create.ts b/desktop/src/main/handlers/mutations/folder-create.ts
index a0058664..14ef039a 100644
--- a/desktop/src/main/handlers/mutations/folder-create.ts
+++ b/desktop/src/main/handlers/mutations/folder-create.ts
@@ -1,10 +1,13 @@
+import * as Y from 'yjs';
import { databaseManager } from '@/main/data/database-manager';
import { NodeTypes } from '@/lib/constants';
import { generateId, IdType } from '@/lib/id';
-import { buildCreateNode, generateNodeIndex } from '@/lib/nodes';
+import { generateNodeIndex } from '@/lib/nodes';
import { compareString } from '@/lib/utils';
import { MutationHandler, MutationResult } from '@/operations/mutations';
import { FolderCreateMutationInput } from '@/operations/mutations/folder-create';
+import { fromUint8Array } from 'js-base64';
+import { LocalCreateNodeChangeData } from '@/types/sync';
export class FolderCreateMutationHandler
implements MutationHandler<FolderCreateMutationInput>
@@ -35,23 +38,54 @@ export class FolderCreateMutationHandler
}
const id = generateId(IdType.Folder);
- await workspaceDatabase
- .insertInto('nodes')
- .values(
- buildCreateNode(
- {
- id: id,
- attributes: {
- type: NodeTypes.Folder,
- parentId: input.parentId,
- index: index,
- name: input.name,
- },
- },
- input.userId,
- ),
- )
- .execute();
+ const versionId = generateId(IdType.Version);
+ const createdAt = new Date().toISOString();
+
+ const doc = new Y.Doc({
+ guid: id,
+ });
+
+ const attributesMap = doc.getMap('attributes');
+ doc.transact(() => {
+ attributesMap.set('type', NodeTypes.Folder);
+ attributesMap.set('parentId', input.parentId);
+ attributesMap.set('index', index);
+ attributesMap.set('name', input.name);
+ });
+
+ const attributes = JSON.stringify(attributesMap.toJSON());
+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
+
+ const changeData: LocalCreateNodeChangeData = {
+ type: 'node_create',
+ id: id,
+ state: encodedState,
+ createdAt: createdAt,
+ createdBy: input.userId,
+ versionId: versionId,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .insertInto('nodes')
+ .values({
+ id: id,
+ attributes: attributes,
+ state: encodedState,
+ created_at: createdAt,
+ created_by: input.userId,
+ version_id: versionId,
+ })
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: createdAt,
+ })
+ .execute();
+ });
return {
output: {
@@ -63,6 +97,11 @@ export class FolderCreateMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
],
};
}
diff --git a/desktop/src/main/handlers/mutations/index.ts b/desktop/src/main/handlers/mutations/index.ts
index 5e412376..c4752fdf 100644
--- a/desktop/src/main/handlers/mutations/index.ts
+++ b/desktop/src/main/handlers/mutations/index.ts
@@ -28,9 +28,6 @@ import { WorkspaceCreateMutationHandler } from '@/main/handlers/mutations/worksp
import { WorkspaceUpdateMutationHandler } from '@/main/handlers/mutations/workspace-update';
import { DocumentSaveMutationHandler } from '@/main/handlers/mutations/document-save';
import { AvatarUploadMutationHandler } from '@/main/handlers/mutations/avatar-upload';
-import { NodeServerCreateMutationHandler } from '@/main/handlers/mutations/node-server-create';
-import { NodeServerUpdateMutationHandler } from '@/main/handlers/mutations/node-server-update';
-import { NodeServerDeleteMutationHandler } from '@/main/handlers/mutations/node-server-delete';
import { LogoutMutationHandler } from '@/main/handlers/mutations/logout';
import { NodeSyncMutationHandler } from '@/main/handlers/mutations/node-sync';
import { FolderCreateMutationHandler } from '@/main/handlers/mutations/folder-create';
@@ -72,9 +69,6 @@ export const mutationHandlerMap: MutationHandlerMap = {
workspace_update: new WorkspaceUpdateMutationHandler(),
document_save: new DocumentSaveMutationHandler(),
avatar_upload: new AvatarUploadMutationHandler(),
- node_server_create: new NodeServerCreateMutationHandler(),
- node_server_update: new NodeServerUpdateMutationHandler(),
- node_server_delete: new NodeServerDeleteMutationHandler(),
logout: new LogoutMutationHandler(),
node_sync: new NodeSyncMutationHandler(),
folder_create: new FolderCreateMutationHandler(),
diff --git a/desktop/src/main/handlers/mutations/message-create.ts b/desktop/src/main/handlers/mutations/message-create.ts
index 0a0835ed..a31eb676 100644
--- a/desktop/src/main/handlers/mutations/message-create.ts
+++ b/desktop/src/main/handlers/mutations/message-create.ts
@@ -1,9 +1,11 @@
+import * as Y from 'yjs';
import { databaseManager } from '@/main/data/database-manager';
import { NodeTypes } from '@/lib/constants';
import { generateId, IdType } from '@/lib/id';
-import { buildCreateNode } from '@/lib/nodes';
import { MutationHandler, MutationResult } from '@/operations/mutations';
import { MessageCreateMutationInput } from '@/operations/mutations/message-create';
+import { fromUint8Array } from 'js-base64';
+import { LocalCreateNodeChangeData } from '@/types/sync';
export class MessageCreateMutationHandler
implements MutationHandler<MessageCreateMutationInput>
@@ -16,22 +18,53 @@ export class MessageCreateMutationHandler
);
const id = generateId(IdType.Message);
- await workspaceDatabase
- .insertInto('nodes')
- .values(
- buildCreateNode(
- {
- id: id,
- attributes: {
- type: NodeTypes.Message,
- parentId: input.conversationId,
- content: input.content.content,
- },
- },
- input.userId,
- ),
- )
- .execute();
+ const versionId = generateId(IdType.Version);
+ const createdAt = new Date().toISOString();
+
+ const doc = new Y.Doc({
+ guid: id,
+ });
+
+ const attributesMap = doc.getMap('attributes');
+ doc.transact(() => {
+ attributesMap.set('type', NodeTypes.Message);
+ attributesMap.set('parentId', input.conversationId);
+ attributesMap.set('content', input.content.content);
+ });
+
+ const attributes = JSON.stringify(attributesMap.toJSON());
+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
+
+ const changeData: LocalCreateNodeChangeData = {
+ type: 'node_create',
+ id: id,
+ state: encodedState,
+ createdAt: createdAt,
+ createdBy: input.userId,
+ versionId: versionId,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .insertInto('nodes')
+ .values({
+ id: id,
+ attributes: attributes,
+ state: encodedState,
+ created_at: createdAt,
+ created_by: input.userId,
+ version_id: versionId,
+ })
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: createdAt,
+ })
+ .execute();
+ });
return {
output: {
@@ -43,6 +76,11 @@ export class MessageCreateMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
],
};
}
diff --git a/desktop/src/main/handlers/mutations/node-attribute-delete.ts b/desktop/src/main/handlers/mutations/node-attribute-delete.ts
index 8cae5cda..65702245 100644
--- a/desktop/src/main/handlers/mutations/node-attribute-delete.ts
+++ b/desktop/src/main/handlers/mutations/node-attribute-delete.ts
@@ -4,6 +4,7 @@ import { MutationHandler, MutationResult } from '@/operations/mutations';
import { NodeAttributeDeleteMutationInput } from '@/operations/mutations/node-attribute-delete';
import { fromUint8Array, toUint8Array } from 'js-base64';
import * as Y from 'yjs';
+import { LocalUpdateNodeChangeData } from '@/types/sync';
export class NodeAttributeDeleteMutationHandler
implements MutationHandler<NodeAttributeDeleteMutationInput>
@@ -25,12 +26,19 @@ export class NodeAttributeDeleteMutationHandler
throw new Error('Node not found');
}
+ const versionId = generateId(IdType.Version);
+ const updatedAt = new Date().toISOString();
+ const updates: string[] = [];
+
const doc = new Y.Doc({
guid: node.id,
});
-
Y.applyUpdate(doc, toUint8Array(node.state));
+ doc.on('update', (update) => {
+ updates.push(fromUint8Array(update));
+ });
+
const attributesMap = doc.getMap('attributes');
if (!attributesMap.has(input.attribute)) {
return {
@@ -44,17 +52,36 @@ export class NodeAttributeDeleteMutationHandler
const attributes = JSON.stringify(attributesMap.toJSON());
const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
- await workspaceDatabase
- .updateTable('nodes')
- .set({
- state: encodedState,
- attributes: attributes,
- updated_at: new Date().toISOString(),
- updated_by: input.userId,
- version_id: generateId(IdType.Version),
- })
- .where('id', '=', input.nodeId)
- .execute();
+ const changeData: LocalUpdateNodeChangeData = {
+ type: 'node_update',
+ id: node.id,
+ updatedAt: updatedAt,
+ updatedBy: input.userId,
+ versionId: versionId,
+ updates: updates,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .updateTable('nodes')
+ .set({
+ attributes: attributes,
+ state: encodedState,
+ updated_at: updatedAt,
+ updated_by: input.userId,
+ version_id: versionId,
+ })
+ .where('id', '=', input.nodeId)
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: updatedAt,
+ })
+ .execute();
+ });
return {
output: {
@@ -66,6 +93,11 @@ export class NodeAttributeDeleteMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
],
};
}
diff --git a/desktop/src/main/handlers/mutations/node-attribute-set.ts b/desktop/src/main/handlers/mutations/node-attribute-set.ts
index 33f68cc3..33f6acd3 100644
--- a/desktop/src/main/handlers/mutations/node-attribute-set.ts
+++ b/desktop/src/main/handlers/mutations/node-attribute-set.ts
@@ -4,6 +4,7 @@ import { MutationHandler, MutationResult } from '@/operations/mutations';
import { NodeAttributeSetMutationInput } from '@/operations/mutations/node-attribute-set';
import { fromUint8Array, toUint8Array } from 'js-base64';
import * as Y from 'yjs';
+import { LocalUpdateNodeChangeData } from '@/types/sync';
export class NodeAttributeSetMutationHandler
implements MutationHandler<NodeAttributeSetMutationInput>
@@ -25,29 +26,55 @@ export class NodeAttributeSetMutationHandler
throw new Error('Node not found');
}
+ const versionId = generateId(IdType.Version);
+ const updatedAt = new Date().toISOString();
+ const updates: string[] = [];
+
const doc = new Y.Doc({
guid: node.id,
});
-
Y.applyUpdate(doc, toUint8Array(node.state));
+ doc.on('update', (update) => {
+ updates.push(fromUint8Array(update));
+ });
+
const attributesMap = doc.getMap('attributes');
attributesMap.set(input.attribute, input.value);
const attributes = JSON.stringify(attributesMap.toJSON());
const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
- await workspaceDatabase
- .updateTable('nodes')
- .set({
- state: encodedState,
- attributes: attributes,
- updated_at: new Date().toISOString(),
- updated_by: input.userId,
- version_id: generateId(IdType.Version),
- })
- .where('id', '=', input.nodeId)
- .execute();
+ const changeData: LocalUpdateNodeChangeData = {
+ type: 'node_update',
+ id: node.id,
+ updatedAt: updatedAt,
+ updatedBy: input.userId,
+ versionId: versionId,
+ updates: updates,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .updateTable('nodes')
+ .set({
+ attributes: attributes,
+ state: encodedState,
+ updated_at: updatedAt,
+ updated_by: input.userId,
+ version_id: versionId,
+ })
+ .where('id', '=', input.nodeId)
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: updatedAt,
+ })
+ .execute();
+ });
return {
output: {
@@ -59,6 +86,11 @@ export class NodeAttributeSetMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
],
};
}
diff --git a/desktop/src/main/handlers/mutations/node-collaborator-create.ts b/desktop/src/main/handlers/mutations/node-collaborator-create.ts
index 0b4abb0f..2f8ca589 100644
--- a/desktop/src/main/handlers/mutations/node-collaborator-create.ts
+++ b/desktop/src/main/handlers/mutations/node-collaborator-create.ts
@@ -3,7 +3,8 @@ import { generateId, IdType } from '@/lib/id';
import { MutationHandler, MutationResult } from '@/operations/mutations';
import { NodeCollaboratorCreateMutationInput } from '@/operations/mutations/node-collaborator-create';
import * as Y from 'yjs';
-import { fromUint8Array } from 'js-base64';
+import { fromUint8Array, toUint8Array } from 'js-base64';
+import { LocalUpdateNodeChangeData } from '@/types/sync';
export class NodeCollaboratorCreateMutationHandler
implements MutationHandler<NodeCollaboratorCreateMutationInput>
@@ -25,9 +26,18 @@ export class NodeCollaboratorCreateMutationHandler
throw new Error('Node not found');
}
+ const versionId = generateId(IdType.Version);
+ const updatedAt = new Date().toISOString();
+ const updates: string[] = [];
+
const doc = new Y.Doc({
guid: node.id,
});
+ Y.applyUpdate(doc, toUint8Array(node.state));
+
+ doc.on('update', (update) => {
+ updates.push(fromUint8Array(update));
+ });
const attributesMap = doc.getMap('attributes');
@@ -43,17 +53,36 @@ export class NodeCollaboratorCreateMutationHandler
const attributes = JSON.stringify(attributesMap.toJSON());
const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
- await workspaceDatabase
- .updateTable('nodes')
- .set({
- attributes: attributes,
- state: encodedState,
- version_id: generateId(IdType.Version),
- updated_at: new Date().toISOString(),
- updated_by: input.userId,
- })
- .where('id', '=', node.id)
- .execute();
+ const changeData: LocalUpdateNodeChangeData = {
+ type: 'node_update',
+ id: node.id,
+ updatedAt: updatedAt,
+ updatedBy: input.userId,
+ versionId: versionId,
+ updates: updates,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .updateTable('nodes')
+ .set({
+ attributes: attributes,
+ state: encodedState,
+ updated_at: updatedAt,
+ updated_by: input.userId,
+ version_id: versionId,
+ })
+ .where('id', '=', input.nodeId)
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: updatedAt,
+ })
+ .execute();
+ });
return {
output: {
@@ -65,6 +94,11 @@ export class NodeCollaboratorCreateMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
],
};
}
diff --git a/desktop/src/main/handlers/mutations/node-collaborator-delete.ts b/desktop/src/main/handlers/mutations/node-collaborator-delete.ts
index 1d7ae852..eda1043d 100644
--- a/desktop/src/main/handlers/mutations/node-collaborator-delete.ts
+++ b/desktop/src/main/handlers/mutations/node-collaborator-delete.ts
@@ -1,7 +1,9 @@
+import { generateId, IdType } from '@/lib/id';
import { databaseManager } from '@/main/data/database-manager';
import { MutationHandler, MutationResult } from '@/operations/mutations';
import { NodeCollaboratorDeleteMutationInput } from '@/operations/mutations/node-collaborator-delete';
-import { fromUint8Array } from 'js-base64';
+import { LocalUpdateNodeChangeData } from '@/types/sync';
+import { fromUint8Array, toUint8Array } from 'js-base64';
import * as Y from 'yjs';
export class NodeCollaboratorDeleteMutationHandler
@@ -24,9 +26,18 @@ export class NodeCollaboratorDeleteMutationHandler
throw new Error('Node not found');
}
+ const versionId = generateId(IdType.Version);
+ const updatedAt = new Date().toISOString();
+ const updates: string[] = [];
+
const doc = new Y.Doc({
guid: node.id,
});
+ Y.applyUpdate(doc, toUint8Array(node.state));
+
+ doc.on('update', (update) => {
+ updates.push(fromUint8Array(update));
+ });
const attributesMap = doc.getMap('attributes');
@@ -38,16 +49,36 @@ export class NodeCollaboratorDeleteMutationHandler
const attributes = JSON.stringify(attributesMap.toJSON());
const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
- await workspaceDatabase
- .updateTable('nodes')
- .set({
- attributes: attributes,
- state: encodedState,
- updated_at: new Date().toISOString(),
- updated_by: input.userId,
- })
- .where('id', '=', node.id)
- .execute();
+ const changeData: LocalUpdateNodeChangeData = {
+ type: 'node_update',
+ id: node.id,
+ updatedAt: updatedAt,
+ updatedBy: input.userId,
+ versionId: versionId,
+ updates: updates,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .updateTable('nodes')
+ .set({
+ attributes: attributes,
+ state: encodedState,
+ updated_at: updatedAt,
+ updated_by: input.userId,
+ version_id: versionId,
+ })
+ .where('id', '=', input.nodeId)
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: updatedAt,
+ })
+ .execute();
+ });
return {
output: {
@@ -59,6 +90,11 @@ export class NodeCollaboratorDeleteMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
],
};
}
diff --git a/desktop/src/main/handlers/mutations/node-collaborator-update.ts b/desktop/src/main/handlers/mutations/node-collaborator-update.ts
index b5e073b9..657da82c 100644
--- a/desktop/src/main/handlers/mutations/node-collaborator-update.ts
+++ b/desktop/src/main/handlers/mutations/node-collaborator-update.ts
@@ -3,7 +3,9 @@ import { generateId, IdType } from '@/lib/id';
import { MutationHandler, MutationResult } from '@/operations/mutations';
import { NodeCollaboratorUpdateMutationInput } from '@/operations/mutations/node-collaborator-update';
import * as Y from 'yjs';
-import { fromUint8Array } from 'js-base64';
+import { fromUint8Array, toUint8Array } from 'js-base64';
+import { LocalUpdateNodeChangeData } from '@/types/sync';
+
export class NodeCollaboratorUpdateMutationHandler
implements MutationHandler<NodeCollaboratorUpdateMutationInput>
{
@@ -24,9 +26,18 @@ export class NodeCollaboratorUpdateMutationHandler
throw new Error('Node not found');
}
+ const versionId = generateId(IdType.Version);
+ const updatedAt = new Date().toISOString();
+ const updates: string[] = [];
+
const doc = new Y.Doc({
guid: node.id,
});
+ Y.applyUpdate(doc, toUint8Array(node.state));
+
+ doc.on('update', (update) => {
+ updates.push(fromUint8Array(update));
+ });
const attributesMap = doc.getMap('attributes');
@@ -42,17 +53,36 @@ export class NodeCollaboratorUpdateMutationHandler
const attributes = JSON.stringify(attributesMap.toJSON());
const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
- await workspaceDatabase
- .updateTable('nodes')
- .set({
- attributes: attributes,
- state: encodedState,
- updated_at: new Date().toISOString(),
- updated_by: input.userId,
- version_id: generateId(IdType.Version),
- })
- .where('id', '=', node.id)
- .execute();
+ const changeData: LocalUpdateNodeChangeData = {
+ type: 'node_update',
+ id: node.id,
+ updatedAt: updatedAt,
+ updatedBy: input.userId,
+ versionId: versionId,
+ updates: updates,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .updateTable('nodes')
+ .set({
+ attributes: attributes,
+ state: encodedState,
+ updated_at: updatedAt,
+ updated_by: input.userId,
+ version_id: versionId,
+ })
+ .where('id', '=', input.nodeId)
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: updatedAt,
+ })
+ .execute();
+ });
return {
output: {
@@ -64,6 +94,11 @@ export class NodeCollaboratorUpdateMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
],
};
}
diff --git a/desktop/src/main/handlers/mutations/node-delete.ts b/desktop/src/main/handlers/mutations/node-delete.ts
index 4772b4cb..509b192d 100644
--- a/desktop/src/main/handlers/mutations/node-delete.ts
+++ b/desktop/src/main/handlers/mutations/node-delete.ts
@@ -1,6 +1,7 @@
import { databaseManager } from '@/main/data/database-manager';
import { MutationHandler, MutationResult } from '@/operations/mutations';
import { NodeDeleteMutationInput } from '@/operations/mutations/node-delete';
+import { LocalDeleteNodeChangeData } from '@/types/sync';
export class NodeDeleteMutationHandler
implements MutationHandler<NodeDeleteMutationInput>
@@ -12,10 +13,38 @@ export class NodeDeleteMutationHandler
input.userId,
);
- await workspaceDatabase
- .deleteFrom('nodes')
+ const node = await workspaceDatabase
+ .selectFrom('nodes')
+ .select(['id'])
.where('id', '=', input.nodeId)
- .execute();
+ .executeTakeFirst();
+
+ if (!node) {
+ return {
+ output: {
+ success: false,
+ },
+ };
+ }
+
+ const changeData: LocalDeleteNodeChangeData = {
+ type: 'node_delete',
+ id: node.id,
+ deletedAt: new Date().toISOString(),
+ deletedBy: input.userId,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx.deleteFrom('nodes').where('id', '=', input.nodeId).execute();
+
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: new Date().toISOString(),
+ })
+ .execute();
+ });
return {
output: {
@@ -27,6 +56,11 @@ export class NodeDeleteMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
],
};
}
diff --git a/desktop/src/main/handlers/mutations/node-reaction-create.ts b/desktop/src/main/handlers/mutations/node-reaction-create.ts
index 1b71e195..c1c03cf4 100644
--- a/desktop/src/main/handlers/mutations/node-reaction-create.ts
+++ b/desktop/src/main/handlers/mutations/node-reaction-create.ts
@@ -2,6 +2,7 @@ import { generateId, IdType } from '@/lib/id';
import { databaseManager } from '@/main/data/database-manager';
import { MutationHandler, MutationResult } from '@/operations/mutations';
import { NodeReactionCreateMutationInput } from '@/operations/mutations/node-reaction-create';
+import { LocalUpdateNodeChangeData } from '@/types/sync';
import { fromUint8Array, toUint8Array } from 'js-base64';
import * as Y from 'yjs';
@@ -25,12 +26,19 @@ export class NodeReactionCreateMutationHandler
throw new Error('Node not found');
}
+ const versionId = generateId(IdType.Version);
+ const updatedAt = new Date().toISOString();
+ const updates: string[] = [];
+
const doc = new Y.Doc({
guid: node.id,
});
-
Y.applyUpdate(doc, toUint8Array(node.state));
+ doc.on('update', (update) => {
+ updates.push(fromUint8Array(update));
+ });
+
const attributesMap = doc.getMap('attributes');
doc.transact(() => {
@@ -58,17 +66,36 @@ export class NodeReactionCreateMutationHandler
return;
}
- await workspaceDatabase
- .updateTable('nodes')
- .set({
- attributes: attributes,
- state: encodedState,
- updated_at: new Date().toISOString(),
- updated_by: input.userId,
- version_id: generateId(IdType.Version),
- })
- .where('id', '=', node.id)
- .execute();
+ const changeData: LocalUpdateNodeChangeData = {
+ type: 'node_update',
+ id: node.id,
+ updatedAt: updatedAt,
+ updatedBy: input.userId,
+ versionId: versionId,
+ updates: updates,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .updateTable('nodes')
+ .set({
+ attributes: attributes,
+ state: encodedState,
+ updated_at: updatedAt,
+ updated_by: input.userId,
+ version_id: versionId,
+ })
+ .where('id', '=', input.nodeId)
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: updatedAt,
+ })
+ .execute();
+ });
return {
output: {
@@ -80,6 +107,11 @@ export class NodeReactionCreateMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
],
};
}
diff --git a/desktop/src/main/handlers/mutations/node-reaction-delete.ts b/desktop/src/main/handlers/mutations/node-reaction-delete.ts
index 3b24a50c..f56e8cef 100644
--- a/desktop/src/main/handlers/mutations/node-reaction-delete.ts
+++ b/desktop/src/main/handlers/mutations/node-reaction-delete.ts
@@ -4,6 +4,7 @@ import { NodeReactionDeleteMutationInput } from '@/operations/mutations/node-rea
import * as Y from 'yjs';
import { fromUint8Array, toUint8Array } from 'js-base64';
import { generateId, IdType } from '@/lib/id';
+import { LocalUpdateNodeChangeData } from '@/types/sync';
export class NodeReactionDeleteMutationHandler
implements MutationHandler<NodeReactionDeleteMutationInput>
@@ -25,12 +26,19 @@ export class NodeReactionDeleteMutationHandler
throw new Error('Node not found');
}
+ const versionId = generateId(IdType.Version);
+ const updatedAt = new Date().toISOString();
+ const updates: string[] = [];
+
const doc = new Y.Doc({
guid: node.id,
});
-
Y.applyUpdate(doc, toUint8Array(node.state));
+ doc.on('update', (update) => {
+ updates.push(fromUint8Array(update));
+ });
+
const attributesMap = doc.getMap('attributes');
doc.transact(() => {
@@ -60,17 +68,36 @@ export class NodeReactionDeleteMutationHandler
return;
}
- await workspaceDatabase
- .updateTable('nodes')
- .set({
- attributes: attributes,
- state: encodedState,
- updated_at: new Date().toISOString(),
- updated_by: input.userId,
- version_id: generateId(IdType.Version),
- })
- .where('id', '=', node.id)
- .execute();
+ const changeData: LocalUpdateNodeChangeData = {
+ type: 'node_update',
+ id: node.id,
+ updatedAt: updatedAt,
+ updatedBy: input.userId,
+ versionId: versionId,
+ updates: updates,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .updateTable('nodes')
+ .set({
+ attributes: attributes,
+ state: encodedState,
+ updated_at: updatedAt,
+ updated_by: input.userId,
+ version_id: versionId,
+ })
+ .where('id', '=', input.nodeId)
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: updatedAt,
+ })
+ .execute();
+ });
return {
output: {
@@ -82,6 +109,11 @@ export class NodeReactionDeleteMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
],
};
}
diff --git a/desktop/src/main/handlers/mutations/node-server-create.ts b/desktop/src/main/handlers/mutations/node-server-create.ts
deleted file mode 100644
index ec1a901e..00000000
--- a/desktop/src/main/handlers/mutations/node-server-create.ts
+++ /dev/null
@@ -1,79 +0,0 @@
-import { databaseManager } from '@/main/data/database-manager';
-import { MutationHandler, MutationResult } from '@/operations/mutations';
-import { NodeServerCreateMutationInput } from '@/operations/mutations/node-server-create';
-import { toUint8Array } from 'js-base64';
-import * as Y from 'yjs';
-
-export class NodeServerCreateMutationHandler
- implements MutationHandler<NodeServerCreateMutationInput>
-{
- public async handleMutation(
- input: NodeServerCreateMutationInput,
- ): Promise<MutationResult<NodeServerCreateMutationInput>> {
- const workspace = await databaseManager.appDatabase
- .selectFrom('workspaces')
- .selectAll()
- .where((eb) =>
- eb.and([
- eb('account_id', '=', input.accountId),
- eb('workspace_id', '=', input.workspaceId),
- ]),
- )
- .executeTakeFirst();
-
- if (!workspace) {
- return {
- output: {
- success: false,
- },
- };
- }
-
- const userId = workspace.user_id;
- const workspaceDatabase =
- await databaseManager.getWorkspaceDatabase(userId);
-
- const doc = new Y.Doc({
- guid: input.id,
- });
- Y.applyUpdate(doc, toUint8Array(input.state));
-
- const attributesMap = doc.getMap('attributes');
- const attributes = JSON.stringify(attributesMap.toJSON());
-
- await workspaceDatabase
- .insertInto('nodes')
- .values({
- id: input.id,
- attributes: attributes,
- state: input.state,
- created_at: input.createdAt,
- created_by: input.createdBy,
- version_id: input.versionId,
- server_created_at: input.serverCreatedAt,
- server_version_id: input.versionId,
- })
- .onConflict((cb) =>
- cb
- .doUpdateSet({
- server_created_at: input.serverCreatedAt,
- server_version_id: input.versionId,
- })
- .where('version_id', '=', input.versionId),
- )
- .execute();
-
- return {
- output: {
- success: true,
- },
- changes: [
- {
- type: 'workspace',
- table: 'nodes',
- userId: userId,
- },
- ],
- };
- }
-}
diff --git a/desktop/src/main/handlers/mutations/node-server-delete.ts b/desktop/src/main/handlers/mutations/node-server-delete.ts
deleted file mode 100644
index 0413d2ba..00000000
--- a/desktop/src/main/handlers/mutations/node-server-delete.ts
+++ /dev/null
@@ -1,52 +0,0 @@
-import { databaseManager } from '@/main/data/database-manager';
-import { MutationHandler, MutationResult } from '@/operations/mutations';
-import { NodeServerDeleteMutationInput } from '@/operations/mutations/node-server-delete';
-
-export class NodeServerDeleteMutationHandler
- implements MutationHandler<NodeServerDeleteMutationInput>
-{
- public async handleMutation(
- input: NodeServerDeleteMutationInput,
- ): Promise<MutationResult<NodeServerDeleteMutationInput>> {
- const workspace = await databaseManager.appDatabase
- .selectFrom('workspaces')
- .selectAll()
- .where((eb) =>
- eb.and([
- eb('account_id', '=', input.accountId),
- eb('workspace_id', '=', input.workspaceId),
- ]),
- )
- .executeTakeFirst();
-
- if (!workspace) {
- return {
- output: {
- success: false,
- },
- };
- }
-
- const userId = workspace.user_id;
- const workspaceDatabase =
- await databaseManager.getWorkspaceDatabase(userId);
-
- await workspaceDatabase
- .deleteFrom('nodes')
- .where('id', '=', input.id)
- .execute();
-
- return {
- output: {
- success: true,
- },
- changes: [
- {
- type: 'workspace',
- table: 'nodes',
- userId: userId,
- },
- ],
- };
- }
-}
diff --git a/desktop/src/main/handlers/mutations/node-server-update.ts b/desktop/src/main/handlers/mutations/node-server-update.ts
deleted file mode 100644
index 55d3757b..00000000
--- a/desktop/src/main/handlers/mutations/node-server-update.ts
+++ /dev/null
@@ -1,88 +0,0 @@
-import { databaseManager } from '@/main/data/database-manager';
-import { MutationHandler, MutationResult } from '@/operations/mutations';
-import { NodeServerUpdateMutationInput } from '@/operations/mutations/node-server-update';
-import { fromUint8Array, toUint8Array } from 'js-base64';
-import * as Y from 'yjs';
-
-export class NodeServerUpdateMutationHandler
- implements MutationHandler<NodeServerUpdateMutationInput>
-{
- public async handleMutation(
- input: NodeServerUpdateMutationInput,
- ): Promise<MutationResult<NodeServerUpdateMutationInput>> {
- const workspace = await databaseManager.appDatabase
- .selectFrom('workspaces')
- .selectAll()
- .where((eb) =>
- eb.and([
- eb('account_id', '=', input.accountId),
- eb('workspace_id', '=', input.workspaceId),
- ]),
- )
- .executeTakeFirst();
-
- if (!workspace) {
- return {
- output: {
- success: false,
- },
- };
- }
-
- const userId = workspace.user_id;
- const workspaceDatabase =
- await databaseManager.getWorkspaceDatabase(userId);
-
- const node = await workspaceDatabase
- .selectFrom('nodes')
- .selectAll()
- .where('id', '=', input.id)
- .executeTakeFirst();
-
- if (!node) {
- return {
- output: {
- success: false,
- },
- };
- }
-
- const doc = new Y.Doc({
- guid: input.id,
- });
-
- Y.applyUpdate(doc, toUint8Array(node.state));
- Y.applyUpdate(doc, toUint8Array(input.update));
-
- const attributesMap = doc.getMap('attributes');
- const attributes = JSON.stringify(attributesMap.toJSON());
- const state = fromUint8Array(Y.encodeStateAsUpdate(doc));
-
- await workspaceDatabase
- .updateTable('nodes')
- .set({
- attributes: attributes,
- state: state,
- updated_at: input.updatedAt,
- updated_by: input.updatedBy,
- version_id: input.versionId,
- server_version_id: input.versionId,
- server_updated_at: input.serverUpdatedAt,
- })
- .where('id', '=', input.id)
- .execute();
-
- return {
- output: {
- success: true,
- },
- changes: [
- {
- type: 'workspace',
- table: 'nodes',
- userId: userId,
- },
- ],
- };
- }
-}
diff --git a/desktop/src/main/handlers/mutations/page-create.ts b/desktop/src/main/handlers/mutations/page-create.ts
index 3b14b97a..b6a92d02 100644
--- a/desktop/src/main/handlers/mutations/page-create.ts
+++ b/desktop/src/main/handlers/mutations/page-create.ts
@@ -1,10 +1,13 @@
+import * as Y from 'yjs';
import { databaseManager } from '@/main/data/database-manager';
import { NodeTypes } from '@/lib/constants';
import { generateId, IdType } from '@/lib/id';
-import { buildCreateNode, generateNodeIndex } from '@/lib/nodes';
+import { generateNodeIndex } from '@/lib/nodes';
import { compareString } from '@/lib/utils';
import { MutationHandler, MutationResult } from '@/operations/mutations';
import { PageCreateMutationInput } from '@/operations/mutations/page-create';
+import { fromUint8Array } from 'js-base64';
+import { LocalCreateNodeChangeData } from '@/types/sync';
export class PageCreateMutationHandler
implements MutationHandler<PageCreateMutationInput>
@@ -35,23 +38,54 @@ export class PageCreateMutationHandler
}
const id = generateId(IdType.Page);
- await workspaceDatabase
- .insertInto('nodes')
- .values(
- buildCreateNode(
- {
- id: id,
- attributes: {
- type: NodeTypes.Page,
- parentId: input.parentId,
- index: index,
- name: input.name,
- },
- },
- input.userId,
- ),
- )
- .execute();
+ const versionId = generateId(IdType.Version);
+ const createdAt = new Date().toISOString();
+
+ const doc = new Y.Doc({
+ guid: id,
+ });
+
+ const attributesMap = doc.getMap('attributes');
+ doc.transact(() => {
+ attributesMap.set('type', NodeTypes.Page);
+ attributesMap.set('parentId', input.parentId);
+ attributesMap.set('index', index);
+ attributesMap.set('name', input.name);
+ });
+
+ const attributes = JSON.stringify(attributesMap.toJSON());
+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
+
+ const changeData: LocalCreateNodeChangeData = {
+ type: 'node_create',
+ id: id,
+ state: encodedState,
+ createdAt: createdAt,
+ createdBy: input.userId,
+ versionId: versionId,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .insertInto('nodes')
+ .values({
+ id: id,
+ attributes: attributes,
+ state: encodedState,
+ created_at: createdAt,
+ created_by: input.userId,
+ version_id: versionId,
+ })
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: createdAt,
+ })
+ .execute();
+ });
return {
output: {
@@ -63,6 +97,11 @@ export class PageCreateMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
],
};
}
diff --git a/desktop/src/main/handlers/mutations/record-create.ts b/desktop/src/main/handlers/mutations/record-create.ts
index 2676ef65..b6c6fad3 100644
--- a/desktop/src/main/handlers/mutations/record-create.ts
+++ b/desktop/src/main/handlers/mutations/record-create.ts
@@ -1,9 +1,12 @@
+import * as Y from 'yjs';
import { databaseManager } from '@/main/data/database-manager';
import { NodeTypes } from '@/lib/constants';
import { generateId, IdType } from '@/lib/id';
-import { buildCreateNode, generateNodeIndex } from '@/lib/nodes';
+import { generateNodeIndex } from '@/lib/nodes';
import { MutationHandler, MutationResult } from '@/operations/mutations';
import { RecordCreateMutationInput } from '@/operations/mutations/record-create';
+import { fromUint8Array } from 'js-base64';
+import { LocalCreateNodeChangeData } from '@/types/sync';
export class RecordCreateMutationHandler
implements MutationHandler<RecordCreateMutationInput>
@@ -31,22 +34,53 @@ export class RecordCreateMutationHandler
const maxIndex = lastChild?.index ? lastChild.index : null;
const id = generateId(IdType.Record);
- await workspaceDatabase
- .insertInto('nodes')
- .values(
- buildCreateNode(
- {
- id: id,
- attributes: {
- type: NodeTypes.Record,
- parentId: input.databaseId,
- index: generateNodeIndex(maxIndex, null),
- },
- },
- input.userId,
- ),
- )
- .execute();
+ const versionId = generateId(IdType.Version);
+ const createdAt = new Date().toISOString();
+
+ const doc = new Y.Doc({
+ guid: id,
+ });
+
+ const attributesMap = doc.getMap('attributes');
+ doc.transact(() => {
+ attributesMap.set('type', NodeTypes.Record);
+ attributesMap.set('parentId', input.databaseId);
+ attributesMap.set('index', generateNodeIndex(maxIndex, null));
+ });
+
+ const attributes = JSON.stringify(attributesMap.toJSON());
+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
+
+ const changeData: LocalCreateNodeChangeData = {
+ type: 'node_create',
+ id: id,
+ state: encodedState,
+ createdAt: createdAt,
+ createdBy: input.userId,
+ versionId: versionId,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .insertInto('nodes')
+ .values({
+ id: id,
+ attributes: attributes,
+ state: encodedState,
+ created_at: createdAt,
+ created_by: input.userId,
+ version_id: versionId,
+ })
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: createdAt,
+ })
+ .execute();
+ });
return {
output: {
@@ -58,6 +92,11 @@ export class RecordCreateMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
],
};
}
diff --git a/desktop/src/main/handlers/mutations/select-option-create.ts b/desktop/src/main/handlers/mutations/select-option-create.ts
index 3b1c5bf0..b848796b 100644
--- a/desktop/src/main/handlers/mutations/select-option-create.ts
+++ b/desktop/src/main/handlers/mutations/select-option-create.ts
@@ -1,9 +1,12 @@
+import * as Y from 'yjs';
import { databaseManager } from '@/main/data/database-manager';
import { NodeTypes } from '@/lib/constants';
import { generateId, IdType } from '@/lib/id';
-import { buildCreateNode, generateNodeIndex } from '@/lib/nodes';
+import { generateNodeIndex } from '@/lib/nodes';
import { MutationHandler, MutationResult } from '@/operations/mutations';
import { SelectOptionCreateMutationInput } from '@/operations/mutations/select-option-create';
+import { fromUint8Array } from 'js-base64';
+import { LocalCreateNodeChangeData } from '@/types/sync';
export class SelectOptionCreateMutationHandler
implements MutationHandler<SelectOptionCreateMutationInput>
@@ -30,24 +33,55 @@ export class SelectOptionCreateMutationHandler
const maxIndex = lastChild?.index ? lastChild.index : null;
const id = generateId(IdType.SelectOption);
- await workspaceDatabase
- .insertInto('nodes')
- .values(
- buildCreateNode(
- {
- id: id,
- attributes: {
- type: NodeTypes.SelectOption,
- parentId: input.fieldId,
- index: generateNodeIndex(maxIndex, null),
- name: input.name,
- color: input.color,
- },
- },
- input.userId,
- ),
- )
- .execute();
+ const versionId = generateId(IdType.Version);
+ const createdAt = new Date().toISOString();
+
+ const doc = new Y.Doc({
+ guid: id,
+ });
+
+ const attributesMap = doc.getMap('attributes');
+ doc.transact(() => {
+ attributesMap.set('type', NodeTypes.SelectOption);
+ attributesMap.set('parentId', input.fieldId);
+ attributesMap.set('index', generateNodeIndex(maxIndex, null));
+ attributesMap.set('name', input.name);
+ attributesMap.set('color', input.color);
+ });
+
+ const attributes = JSON.stringify(attributesMap.toJSON());
+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
+
+ const changeData: LocalCreateNodeChangeData = {
+ type: 'node_create',
+ id: id,
+ state: encodedState,
+ createdAt: createdAt,
+ createdBy: input.userId,
+ versionId: versionId,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .insertInto('nodes')
+ .values({
+ id: id,
+ attributes: attributes,
+ state: encodedState,
+ created_at: createdAt,
+ created_by: input.userId,
+ version_id: versionId,
+ })
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: createdAt,
+ })
+ .execute();
+ });
return {
output: {
@@ -59,6 +93,11 @@ export class SelectOptionCreateMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
],
};
}
diff --git a/desktop/src/main/handlers/mutations/space-create.ts b/desktop/src/main/handlers/mutations/space-create.ts
index 74079979..9e516a97 100644
--- a/desktop/src/main/handlers/mutations/space-create.ts
+++ b/desktop/src/main/handlers/mutations/space-create.ts
@@ -6,6 +6,7 @@ import { SpaceCreateMutationInput } from '@/operations/mutations/space-create';
import * as Y from 'yjs';
import { NodeTypes } from '@/lib/constants';
import { fromUint8Array } from 'js-base64';
+import { LocalCreateNodeChangeData } from '@/types/sync';
export class SpaceCreateMutationHandler
implements MutationHandler<SpaceCreateMutationInput>
@@ -17,7 +18,10 @@ export class SpaceCreateMutationHandler
input.userId,
);
+ const createdAt = new Date().toISOString();
+
const spaceId = generateId(IdType.Space);
+ const spaceVersionId = generateId(IdType.Version);
const spaceDoc = new Y.Doc({
guid: spaceId,
});
@@ -45,8 +49,18 @@ export class SpaceCreateMutationHandler
const spaceAttributes = JSON.stringify(spaceAttributesMap.toJSON());
const spaceState = fromUint8Array(Y.encodeStateAsUpdate(spaceDoc));
+ const spaceChangeData: LocalCreateNodeChangeData = {
+ type: 'node_create',
+ id: spaceId,
+ state: spaceState,
+ createdAt: createdAt,
+ createdBy: input.userId,
+ versionId: spaceVersionId,
+ };
+
const pageId = generateId(IdType.Page);
const pageIndex = generateNodeIndex(null, null);
+ const pageVersionId = generateId(IdType.Version);
const pageDoc = new Y.Doc({
guid: pageId,
});
@@ -62,8 +76,18 @@ export class SpaceCreateMutationHandler
const pageAttributes = JSON.stringify(pageAttributesMap.toJSON());
const pageState = fromUint8Array(Y.encodeStateAsUpdate(pageDoc));
+ const pageChangeData: LocalCreateNodeChangeData = {
+ type: 'node_create',
+ id: pageId,
+ state: pageState,
+ createdAt: createdAt,
+ createdBy: input.userId,
+ versionId: pageVersionId,
+ };
+
const channelId = generateId(IdType.Channel);
const channelIndex = generateNodeIndex(pageIndex, null);
+ const channelVersionId = generateId(IdType.Version);
const channelDoc = new Y.Doc({
guid: channelId,
});
@@ -79,6 +103,15 @@ export class SpaceCreateMutationHandler
const channelAttributes = JSON.stringify(channelAttributesMap.toJSON());
const channelState = fromUint8Array(Y.encodeStateAsUpdate(channelDoc));
+ const channelChangeData: LocalCreateNodeChangeData = {
+ type: 'node_create',
+ id: channelId,
+ state: channelState,
+ createdAt: createdAt,
+ createdBy: input.userId,
+ versionId: channelVersionId,
+ };
+
await workspaceDatabase.transaction().execute(async (trx) => {
await trx
.insertInto('nodes')
@@ -89,7 +122,7 @@ export class SpaceCreateMutationHandler
state: spaceState,
created_at: new Date().toISOString(),
created_by: input.userId,
- version_id: generateId(IdType.Version),
+ version_id: spaceVersionId,
},
{
id: pageId,
@@ -97,7 +130,7 @@ export class SpaceCreateMutationHandler
state: pageState,
created_at: new Date().toISOString(),
created_by: input.userId,
- version_id: generateId(IdType.Version),
+ version_id: pageVersionId,
},
{
id: channelId,
@@ -105,7 +138,25 @@ export class SpaceCreateMutationHandler
state: channelState,
created_at: new Date().toISOString(),
created_by: input.userId,
- version_id: generateId(IdType.Version),
+ version_id: channelVersionId,
+ },
+ ])
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values([
+ {
+ data: JSON.stringify(spaceChangeData),
+ created_at: createdAt,
+ },
+ {
+ data: JSON.stringify(pageChangeData),
+ created_at: createdAt,
+ },
+ {
+ data: JSON.stringify(channelChangeData),
+ created_at: createdAt,
},
])
.execute();
@@ -121,6 +172,11 @@ export class SpaceCreateMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
],
};
}
diff --git a/desktop/src/main/handlers/mutations/space-update.ts b/desktop/src/main/handlers/mutations/space-update.ts
index f6358cd1..4c675882 100644
--- a/desktop/src/main/handlers/mutations/space-update.ts
+++ b/desktop/src/main/handlers/mutations/space-update.ts
@@ -5,6 +5,7 @@ import { SpaceUpdateMutationInput } from '@/operations/mutations/space-update';
import * as Y from 'yjs';
import { fromUint8Array, toUint8Array } from 'js-base64';
import { LocalNodeAttributes } from '@/types/nodes';
+import { LocalUpdateNodeChangeData } from '@/types/sync';
export class SpaceUpdateMutationHandler
implements MutationHandler<SpaceUpdateMutationInput>
@@ -30,42 +31,68 @@ export class SpaceUpdateMutationHandler
};
}
- const spaceDoc = new Y.Doc({
+ const versionId = generateId(IdType.Version);
+ const updatedAt = new Date().toISOString();
+ const updates: string[] = [];
+
+ const doc = new Y.Doc({
guid: space.id,
});
+ Y.applyUpdate(doc, toUint8Array(space.state));
- Y.applyUpdate(spaceDoc, toUint8Array(space.state));
+ doc.on('update', (update) => {
+ updates.push(fromUint8Array(update));
+ });
- const attributes: LocalNodeAttributes = JSON.parse(space.attributes);
- const spaceAttributesMap = spaceDoc.getMap('attributes');
- spaceDoc.transact(() => {
- if (input.name !== attributes.name) {
- spaceAttributesMap.set('name', input.name);
+ const currentAttributes: LocalNodeAttributes = JSON.parse(space.attributes);
+ const attributesMap = doc.getMap('attributes');
+ doc.transact(() => {
+ if (input.name !== currentAttributes.name) {
+ attributesMap.set('name', input.name);
}
- if (input.description !== attributes.description) {
- spaceAttributesMap.set('description', input.description);
+ if (input.description !== currentAttributes.description) {
+ attributesMap.set('description', input.description);
}
- if (input.avatar !== attributes.avatar) {
- spaceAttributesMap.set('avatar', input.avatar);
+ if (input.avatar !== currentAttributes.avatar) {
+ attributesMap.set('avatar', input.avatar);
}
});
- const spaceAttributes = JSON.stringify(spaceAttributesMap.toJSON());
- const spaceState = fromUint8Array(Y.encodeStateAsUpdate(spaceDoc));
+ const attributes = JSON.stringify(attributesMap.toJSON());
+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
+
+ const changeData: LocalUpdateNodeChangeData = {
+ type: 'node_update',
+ id: space.id,
+ updatedAt: updatedAt,
+ updatedBy: input.userId,
+ versionId: versionId,
+ updates: updates,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .updateTable('nodes')
+ .set({
+ attributes: attributes,
+ state: encodedState,
+ updated_at: updatedAt,
+ updated_by: input.userId,
+ version_id: versionId,
+ })
+ .where('id', '=', input.id)
+ .execute();
- await workspaceDatabase
- .updateTable('nodes')
- .set({
- attributes: spaceAttributes,
- state: spaceState,
- updated_at: new Date().toISOString(),
- updated_by: input.userId,
- version_id: generateId(IdType.Version),
- })
- .where('id', '=', space.id)
- .execute();
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: updatedAt,
+ })
+ .execute();
+ });
return {
output: {
@@ -77,6 +104,11 @@ export class SpaceUpdateMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
],
};
}
diff --git a/desktop/src/main/handlers/mutations/table-view-create.ts b/desktop/src/main/handlers/mutations/table-view-create.ts
index 6d32b0ac..9d02a699 100644
--- a/desktop/src/main/handlers/mutations/table-view-create.ts
+++ b/desktop/src/main/handlers/mutations/table-view-create.ts
@@ -1,10 +1,13 @@
+import * as Y from 'yjs';
import { databaseManager } from '@/main/data/database-manager';
import { NodeTypes, ViewNodeTypes } from '@/lib/constants';
import { generateId, IdType } from '@/lib/id';
-import { buildCreateNode, generateNodeIndex } from '@/lib/nodes';
+import { generateNodeIndex } from '@/lib/nodes';
import { compareString } from '@/lib/utils';
import { MutationHandler, MutationResult } from '@/operations/mutations';
import { TableViewCreateMutationInput } from '@/operations/mutations/table-view-create';
+import { fromUint8Array } from 'js-base64';
+import { LocalCreateNodeChangeData } from '@/types/sync';
export class TableViewCreateMutationHandler
implements MutationHandler<TableViewCreateMutationInput>
@@ -35,23 +38,54 @@ export class TableViewCreateMutationHandler
: null;
const id = generateId(IdType.TableView);
- await workspaceDatabase
- .insertInto('nodes')
- .values(
- buildCreateNode(
- {
- id: id,
- attributes: {
- type: NodeTypes.TableView,
- parentId: input.databaseId,
- index: generateNodeIndex(maxIndex, null),
- name: input.name,
- },
- },
- input.userId,
- ),
- )
- .execute();
+ const versionId = generateId(IdType.Version);
+ const createdAt = new Date().toISOString();
+
+ const doc = new Y.Doc({
+ guid: id,
+ });
+
+ const attributesMap = doc.getMap('attributes');
+ doc.transact(() => {
+ attributesMap.set('type', NodeTypes.TableView);
+ attributesMap.set('parentId', input.databaseId);
+ attributesMap.set('index', generateNodeIndex(maxIndex, null));
+ attributesMap.set('name', input.name);
+ });
+
+ const attributes = JSON.stringify(attributesMap.toJSON());
+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
+
+ const changeData: LocalCreateNodeChangeData = {
+ type: 'node_create',
+ id: id,
+ state: encodedState,
+ createdAt: createdAt,
+ createdBy: input.userId,
+ versionId: versionId,
+ };
+
+ await workspaceDatabase.transaction().execute(async (trx) => {
+ await trx
+ .insertInto('nodes')
+ .values({
+ id: id,
+ attributes: attributes,
+ state: encodedState,
+ created_at: createdAt,
+ created_by: input.userId,
+ version_id: versionId,
+ })
+ .execute();
+
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: createdAt,
+ })
+ .execute();
+ });
return {
output: {
@@ -63,6 +97,11 @@ export class TableViewCreateMutationHandler
table: 'nodes',
userId: input.userId,
},
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
],
};
}
diff --git a/desktop/src/main/handlers/mutations/workspace-user-role-update.ts b/desktop/src/main/handlers/mutations/workspace-user-role-update.ts
index 2e5dcf51..d058e1aa 100644
--- a/desktop/src/main/handlers/mutations/workspace-user-role-update.ts
+++ b/desktop/src/main/handlers/mutations/workspace-user-role-update.ts
@@ -76,6 +76,7 @@ export class WorkspaceUserRoleUpdateMutationHandler
.updateTable('nodes')
.set({
attributes: JSON.stringify(data.user.attributes),
+ state: data.user.state,
updated_at: data.user.updatedAt,
updated_by: data.user.updatedBy,
version_id: data.user.versionId,
diff --git a/desktop/src/operations/mutations/node-server-create.ts b/desktop/src/operations/mutations/node-server-create.ts
deleted file mode 100644
index db59dd8e..00000000
--- a/desktop/src/operations/mutations/node-server-create.ts
+++ /dev/null
@@ -1,24 +0,0 @@
-export type NodeServerCreateMutationInput = {
- type: 'node_server_create';
- accountId: string;
- workspaceId: string;
- id: string;
- state: string;
- createdAt: string;
- createdBy: string;
- versionId: string;
- serverCreatedAt: string;
-};
-
-export type NodeServerCreateMutationOutput = {
- success: boolean;
-};
-
-declare module '@/operations/mutations' {
- interface MutationMap {
- node_server_create: {
- input: NodeServerCreateMutationInput;
- output: NodeServerCreateMutationOutput;
- };
- }
-}
diff --git a/desktop/src/operations/mutations/node-server-delete.ts b/desktop/src/operations/mutations/node-server-delete.ts
deleted file mode 100644
index 4b120dfd..00000000
--- a/desktop/src/operations/mutations/node-server-delete.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-export type NodeServerDeleteMutationInput = {
- type: 'node_server_delete';
- accountId: string;
- workspaceId: string;
- id: string;
-};
-
-export type NodeServerDeleteMutationOutput = {
- success: boolean;
-};
-
-declare module '@/operations/mutations' {
- interface MutationMap {
- node_server_delete: {
- input: NodeServerDeleteMutationInput;
- output: NodeServerDeleteMutationOutput;
- };
- }
-}
diff --git a/desktop/src/operations/mutations/node-server-update.ts b/desktop/src/operations/mutations/node-server-update.ts
deleted file mode 100644
index 0e70bec9..00000000
--- a/desktop/src/operations/mutations/node-server-update.ts
+++ /dev/null
@@ -1,24 +0,0 @@
-export type NodeServerUpdateMutationInput = {
- type: 'node_server_update';
- accountId: string;
- workspaceId: string;
- id: string;
- update: string;
- updatedAt: string;
- updatedBy: string;
- versionId: string;
- serverUpdatedAt: string;
-};
-
-export type NodeServerUpdateMutationOutput = {
- success: boolean;
-};
-
-declare module '@/operations/mutations' {
- interface MutationMap {
- node_server_update: {
- input: NodeServerUpdateMutationInput;
- output: NodeServerUpdateMutationOutput;
- };
- }
-}
diff --git a/desktop/src/types/sync.ts b/desktop/src/types/sync.ts
index 773e0148..5244ef1d 100644
--- a/desktop/src/types/sync.ts
+++ b/desktop/src/types/sync.ts
@@ -24,3 +24,33 @@ export type ServerNodeSyncData = {
serverUpdatedAt?: string | null;
serverDeletedAt?: string | null;
};
+
+export type LocalCreateNodeChangeData = {
+ type: 'node_create';
+ id: string;
+ state: string;
+ createdAt: string;
+ createdBy: string;
+ versionId: string;
+};
+
+export type LocalUpdateNodeChangeData = {
+ type: 'node_update';
+ id: string;
+ updates: string[];
+ updatedAt: string;
+ updatedBy: string;
+ versionId: string;
+};
+
+export type LocalDeleteNodeChangeData = {
+ type: 'node_delete';
+ id: string;
+ deletedAt: string;
+ deletedBy: string;
+};
+
+export type LocalNodeChangeData =
+ | LocalCreateNodeChangeData
+ | LocalUpdateNodeChangeData
+ | LocalDeleteNodeChangeData;
diff --git a/server/src/routes/sync.ts b/server/src/routes/sync.ts
index d6c67621..d49e9121 100644
--- a/server/src/routes/sync.ts
+++ b/server/src/routes/sync.ts
@@ -3,7 +3,10 @@ import { database } from '@/data/database';
import { Router } from 'express';
import {
LocalChange,
+ LocalCreateNodeChangeData,
+ LocalDeleteNodeChangeData,
LocalNodeChangeData,
+ LocalUpdateNodeChangeData,
ServerSyncChangeResult,
SyncLocalChangeResult,
SyncLocalChangesInput,
@@ -82,15 +85,16 @@ const handleLocalChange = async (
workspaceUser: SelectWorkspaceUser,
change: LocalChange,
): Promise<SyncLocalChangeResult> => {
- switch (change.action) {
- case 'insert': {
- return handleCreateNodeChange(workspaceUser, change);
+ const changeData = JSON.parse(change.data) as LocalNodeChangeData;
+ switch (changeData.type) {
+ case 'node_create': {
+ return handleCreateNodeChange(workspaceUser, changeData);
}
- case 'update': {
- return handleUpdateNodeChange(workspaceUser, change);
+ case 'node_update': {
+ return handleUpdateNodeChange(workspaceUser, changeData);
}
- case 'delete': {
- return handleDeleteNodeChange(workspaceUser, change);
+ case 'node_delete': {
+ return handleDeleteNodeChange(workspaceUser, changeData);
}
default: {
return {
@@ -102,18 +106,11 @@ const handleLocalChange = async (
const handleCreateNodeChange = async (
workspaceUser: SelectWorkspaceUser,
- change: LocalChange,
+ changeData: LocalCreateNodeChangeData,
): Promise<SyncLocalChangeResult> => {
- if (!change.after) {
- return {
- status: 'error',
- };
- }
-
- const nodeData = JSON.parse(change.after) as LocalNodeChangeData;
const existingNode = await database
.selectFrom('nodes')
- .where('id', '=', nodeData.id)
+ .where('id', '=', changeData.id)
.executeTakeFirst();
if (existingNode) {
@@ -122,7 +119,15 @@ const handleCreateNodeChange = async (
};
}
- const attributes: ServerNodeAttributes = JSON.parse(nodeData.attributes);
+ const doc = new Y.Doc({
+ guid: changeData.id,
+ });
+
+ Y.applyUpdate(doc, toUint8Array(changeData.state));
+
+ const attributesMap = doc.getMap('attributes');
+ const attributes = attributesMap.toJSON() as ServerNodeAttributes;
+
if (attributes.parentId) {
const parentRole = await fetchCollaboratorRole(
attributes.parentId,
@@ -142,18 +147,18 @@ const handleCreateNodeChange = async (
await database
.insertInto('nodes')
.values({
- id: nodeData.id,
- attributes: nodeData.attributes,
+ id: changeData.id,
+ attributes: JSON.stringify(attributes),
workspace_id: workspaceUser.workspace_id,
- state: nodeData.state,
- created_at: new Date(nodeData.created_at),
- created_by: nodeData.created_by,
- version_id: nodeData.version_id,
+ state: changeData.state,
+ created_at: new Date(changeData.createdAt),
+ created_by: changeData.createdBy,
+ version_id: changeData.versionId,
server_created_at: new Date(),
})
.execute();
- await publishChange(nodeData.id, workspaceUser.workspace_id);
+ await publishChange(changeData.id, workspaceUser.workspace_id);
return {
status: 'success',
@@ -162,19 +167,12 @@ const handleCreateNodeChange = async (
const handleUpdateNodeChange = async (
workspaceUser: SelectWorkspaceUser,
- change: LocalChange,
+ changeData: LocalUpdateNodeChangeData,
): Promise<SyncLocalChangeResult> => {
- if (!change.after) {
- return {
- status: 'error',
- };
- }
-
- const nodeData = JSON.parse(change.after) as LocalNodeChangeData;
const existingNode = await database
.selectFrom('nodes')
.select(['id', 'workspace_id', 'state'])
- .where('id', '=', nodeData.id)
+ .where('id', '=', changeData.id)
.executeTakeFirst();
if (
@@ -186,24 +184,22 @@ const handleUpdateNodeChange = async (
};
}
- const role = await fetchCollaboratorRole(nodeData.id, workspaceUser.id);
+ const role = await fetchCollaboratorRole(changeData.id, workspaceUser.id);
if (role === null) {
return {
status: 'error',
};
}
- const updatedAt = nodeData.updated_at
- ? new Date(nodeData.updated_at)
- : new Date();
- const updatedBy = nodeData.updated_by ?? workspaceUser.id;
-
const doc = new Y.Doc({
- guid: nodeData.id,
+ guid: changeData.id,
});
Y.applyUpdate(doc, toUint8Array(existingNode.state));
- Y.applyUpdate(doc, toUint8Array(nodeData.state));
+
+ for (const update of changeData.updates) {
+ Y.applyUpdate(doc, toUint8Array(update));
+ }
const attributesMap = doc.getMap('attributes');
const attributes = JSON.stringify(attributesMap.toJSON());
@@ -214,15 +210,15 @@ const handleUpdateNodeChange = async (
.set({
attributes: attributes,
state: encodedState,
- updated_at: updatedAt,
- updated_by: updatedBy,
- version_id: nodeData.version_id,
+ updated_at: new Date(changeData.updatedAt),
+ updated_by: changeData.updatedBy,
+ version_id: changeData.versionId,
server_updated_at: new Date(),
})
- .where('id', '=', nodeData.id)
+ .where('id', '=', changeData.id)
.execute();
- await publishChange(nodeData.id, workspaceUser.workspace_id);
+ await publishChange(changeData.id, workspaceUser.workspace_id);
return {
status: 'success',
@@ -231,18 +227,11 @@ const handleUpdateNodeChange = async (
const handleDeleteNodeChange = async (
workspaceUser: SelectWorkspaceUser,
- change: LocalChange,
+ changeData: LocalDeleteNodeChangeData,
): Promise<SyncLocalChangeResult> => {
- if (!change.before) {
- return {
- status: 'error',
- };
- }
-
- const nodeData = JSON.parse(change.before) as LocalNodeChangeData;
const existingNode = await database
.selectFrom('nodes')
- .where('id', '=', nodeData.id)
+ .where('id', '=', changeData.id)
.select(['id', 'workspace_id'])
.executeTakeFirst();
@@ -258,25 +247,15 @@ const handleDeleteNodeChange = async (
};
}
- const role = await fetchCollaboratorRole(nodeData.id, workspaceUser.id);
+ const role = await fetchCollaboratorRole(changeData.id, workspaceUser.id);
if (role === null) {
return {
status: 'error',
};
}
- await database
- .updateTable('nodes')
- .set({
- deleted_at: new Date(),
- deleted_by: workspaceUser.id,
- version_id: nodeData.version_id,
- server_deleted_at: new Date(),
- })
- .where('id', '=', nodeData.id)
- .execute();
-
- await publishChange(nodeData.id, workspaceUser.workspace_id);
+ await database.deleteFrom('nodes').where('id', '=', changeData.id).execute();
+ await publishChange(changeData.id, workspaceUser.workspace_id);
return {
status: 'success',
diff --git a/server/src/types/sync.ts b/server/src/types/sync.ts
index b916db4d..ac9b5685 100644
--- a/server/src/types/sync.ts
+++ b/server/src/types/sync.ts
@@ -15,27 +15,40 @@ export type ServerSyncChangeResult = {
export type LocalChange = {
id: number;
- table: string;
- action: 'insert' | 'update' | 'delete';
- before?: string | null;
- after?: string | null;
+ data: string;
createdAt: string;
};
-export type LocalNodeChangeData = {
+export type LocalCreateNodeChangeData = {
+ type: 'node_create';
id: string;
- attributes: string;
state: string;
- created_at: string;
- updated_at?: string | null;
- created_by: string;
- updated_by?: string | null;
- version_id: string;
- server_created_at: string;
- server_updated_at: string;
- server_version_id: string;
+ createdAt: string;
+ createdBy: string;
+ versionId: string;
};
+export type LocalUpdateNodeChangeData = {
+ type: 'node_update';
+ id: string;
+ updates: string[];
+ updatedAt: string;
+ updatedBy: string;
+ versionId: string;
+};
+
+export type LocalDeleteNodeChangeData = {
+ type: 'node_delete';
+ id: string;
+ deletedAt: string;
+ deletedBy: string;
+};
+
+export type LocalNodeChangeData =
+ | LocalCreateNodeChangeData
+ | LocalUpdateNodeChangeData
+ | LocalDeleteNodeChangeData;
+
export type ServerNodeSyncData = {
id: string;
workspaceId: string;