Skip to content

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

Separate documents into it's own table

Hakan Shehud8745d84cd7e
diff --git a/apps/desktop/src/main/databases/workspace/migrations/00004-create-node-transactions-table.ts b/apps/desktop/src/main/databases/workspace/migrations/00004-create-node-updates-table.ts
similarity index 66%
rename from apps/desktop/src/main/databases/workspace/migrations/00004-create-node-transactions-table.ts
rename to apps/desktop/src/main/databases/workspace/migrations/00004-create-node-updates-table.ts
index 14ace5df..76ddbb61 100644
--- a/apps/desktop/src/main/databases/workspace/migrations/00004-create-node-transactions-table.ts
+++ b/apps/desktop/src/main/databases/workspace/migrations/00004-create-node-updates-table.ts
@@ -1,19 +1,18 @@
import { Migration } from 'kysely';
-export const createNodeTransactionsTable: Migration = {
+export const createNodeUpdatesTable: Migration = {
up: async (db) => {
await db.schema
- .createTable('node_transactions')
+ .createTable('node_updates')
.addColumn('id', 'text', (col) => col.notNull().primaryKey())
.addColumn('node_id', 'text', (col) =>
col.notNull().references('nodes.id').onDelete('cascade')
)
- .addColumn('operation', 'integer', (col) => col.notNull())
.addColumn('data', 'blob', (col) => col.notNull())
.addColumn('created_at', 'text', (col) => col.notNull())
.execute();
},
down: async (db) => {
- await db.schema.dropTable('node_transactions').execute();
+ await db.schema.dropTable('node_updates').execute();
},
};
diff --git a/apps/desktop/src/main/databases/workspace/migrations/00013-create-metadata-table.tsx b/apps/desktop/src/main/databases/workspace/migrations/00013-create-metadata-table.ts
similarity index 100%
rename from apps/desktop/src/main/databases/workspace/migrations/00013-create-metadata-table.tsx
rename to apps/desktop/src/main/databases/workspace/migrations/00013-create-metadata-table.ts
diff --git a/apps/desktop/src/main/databases/workspace/migrations/00014-create-documents-table.ts b/apps/desktop/src/main/databases/workspace/migrations/00014-create-documents-table.ts
new file mode 100644
index 00000000..5b2af904
--- /dev/null
+++ b/apps/desktop/src/main/databases/workspace/migrations/00014-create-documents-table.ts
@@ -0,0 +1,19 @@
+import { Migration } from 'kysely';
+
+export const createDocumentsTable: Migration = {
+ up: async (db) => {
+ await db.schema
+ .createTable('documents')
+ .addColumn('id', 'text', (col) => col.notNull().primaryKey())
+ .addColumn('revision', 'integer', (col) => col.notNull())
+ .addColumn('state', 'blob', (col) => col.notNull())
+ .addColumn('created_at', 'text', (col) => col.notNull())
+ .addColumn('created_by', 'text', (col) => col.notNull())
+ .addColumn('updated_at', 'text')
+ .addColumn('updated_by', 'text')
+ .execute();
+ },
+ down: async (db) => {
+ await db.schema.dropTable('documents').execute();
+ },
+};
diff --git a/apps/desktop/src/main/databases/workspace/migrations/00015-create-document-updates-table.ts b/apps/desktop/src/main/databases/workspace/migrations/00015-create-document-updates-table.ts
new file mode 100644
index 00000000..6955c211
--- /dev/null
+++ b/apps/desktop/src/main/databases/workspace/migrations/00015-create-document-updates-table.ts
@@ -0,0 +1,16 @@
+import { Migration } from 'kysely';
+
+export const createDocumentUpdatesTable: Migration = {
+ up: async (db) => {
+ await db.schema
+ .createTable('document_updates')
+ .addColumn('id', 'text', (col) => col.notNull().primaryKey())
+ .addColumn('document_id', 'text', (col) => col.notNull())
+ .addColumn('data', 'blob', (col) => col.notNull())
+ .addColumn('created_at', 'text', (col) => col.notNull())
+ .execute();
+ },
+ down: async (db) => {
+ await db.schema.dropTable('document_updates').execute();
+ },
+};
diff --git a/apps/desktop/src/main/databases/workspace/migrations/index.ts b/apps/desktop/src/main/databases/workspace/migrations/index.ts
index 456d5541..01ee9280 100644
--- a/apps/desktop/src/main/databases/workspace/migrations/index.ts
+++ b/apps/desktop/src/main/databases/workspace/migrations/index.ts
@@ -3,7 +3,7 @@ import { Migration } from 'kysely';
import { createUsersTable } from './00001-create-users-table';
import { createNodesTable } from './00002-create-nodes-table';
import { createNodeStatesTable } from './00003-create-node-states-table';
-import { createNodeTransactionsTable } from './00004-create-node-transactions-table';
+import { createNodeUpdatesTable } from './00004-create-node-updates-table';
import { createNodeInteractionsTable } from './00005-create-node-interactions-table';
import { createNodeReactionsTable } from './00006-create-node-reactions-table';
import { createCollaborationsTable } from './00007-create-collaborations-table';
@@ -13,12 +13,14 @@ import { createTombstonesTable } from './00010-create-tombstones-table';
import { createTextsTable } from './00011-create-texts-table';
import { createCursorsTable } from './00012-create-cursors-table';
import { createMetadataTable } from './00013-create-metadata-table';
+import { createDocumentsTable } from './00014-create-documents-table';
+import { createDocumentUpdatesTable } from './00015-create-document-updates-table';
export const workspaceDatabaseMigrations: Record<string, Migration> = {
'00001-create-users-table': createUsersTable,
'00002-create-nodes-table': createNodesTable,
'00003-create-node-states-table': createNodeStatesTable,
- '00004-create-node-transactions-table': createNodeTransactionsTable,
+ '00004-create-node-updates-table': createNodeUpdatesTable,
'00005-create-node-interactions-table': createNodeInteractionsTable,
'00006-create-node-reactions-table': createNodeReactionsTable,
'00007-create-collaborations-table': createCollaborationsTable,
@@ -28,4 +30,6 @@ export const workspaceDatabaseMigrations: Record<string, Migration> = {
'00011-create-texts-table': createTextsTable,
'00012-create-cursors-table': createCursorsTable,
'00013-create-metadata-table': createMetadataTable,
+ '00014-create-documents-table': createDocumentsTable,
+ '00015-create-document-updates-table': createDocumentUpdatesTable,
};
diff --git a/apps/desktop/src/main/databases/workspace/schema.ts b/apps/desktop/src/main/databases/workspace/schema.ts
index 99a00204..a86fa658 100644
--- a/apps/desktop/src/main/databases/workspace/schema.ts
+++ b/apps/desktop/src/main/databases/workspace/schema.ts
@@ -4,7 +4,6 @@ import {
MutationType,
NodeType,
WorkspaceRole,
- TransactionOperation,
UserStatus,
} from '@colanode/core';
import { ColumnType, Insertable, Selectable, Updateable } from 'kysely';
@@ -57,17 +56,16 @@ export type SelectNodeState = Selectable<NodeStateTable>;
export type CreateNodeState = Insertable<NodeStateTable>;
export type UpdateNodeState = Updateable<NodeStateTable>;
-interface NodeTransactionTable {
+interface NodeUpdateTable {
id: ColumnType<string, string, never>;
node_id: ColumnType<string, string, never>;
- operation: ColumnType<TransactionOperation, TransactionOperation, never>;
data: ColumnType<Uint8Array, Uint8Array, never>;
created_at: ColumnType<string, string, never>;
}
-export type SelectNodeTransaction = Selectable<NodeTransactionTable>;
-export type CreateNodeTransaction = Insertable<NodeTransactionTable>;
-export type UpdateNodeTransaction = Updateable<NodeTransactionTable>;
+export type SelectNodeUpdate = Selectable<NodeUpdateTable>;
+export type CreateNodeUpdate = Insertable<NodeUpdateTable>;
+export type UpdateNodeUpdate = Updateable<NodeUpdateTable>;
interface NodeInteractionTable {
node_id: ColumnType<string, string, never>;
@@ -193,12 +191,37 @@ export type SelectWorkspaceMetadata = Selectable<MetadataTable>;
export type CreateWorkspaceMetadata = Insertable<MetadataTable>;
export type UpdateWorkspaceMetadata = Updateable<MetadataTable>;
+interface DocumentTable {
+ id: ColumnType<string, string, never>;
+ revision: ColumnType<bigint, bigint, bigint>;
+ state: ColumnType<Uint8Array, Uint8Array, Uint8Array>;
+ created_at: ColumnType<string, string, never>;
+ created_by: ColumnType<string, string, never>;
+ updated_at: ColumnType<string | null, string | null, string | null>;
+ updated_by: ColumnType<string | null, string | null, string | null>;
+}
+
+export type SelectDocument = Selectable<DocumentTable>;
+export type CreateDocument = Insertable<DocumentTable>;
+export type UpdateDocument = Updateable<DocumentTable>;
+
+interface DocumentUpdateTable {
+ id: ColumnType<string, string, never>;
+ document_id: ColumnType<string, string, never>;
+ data: ColumnType<Uint8Array, Uint8Array, never>;
+ created_at: ColumnType<string, string, never>;
+}
+
+export type SelectDocumentUpdate = Selectable<DocumentUpdateTable>;
+export type CreateDocumentUpdate = Insertable<DocumentUpdateTable>;
+export type UpdateDocumentUpdate = Updateable<DocumentUpdateTable>;
+
export interface WorkspaceDatabaseSchema {
users: UserTable;
nodes: NodeTable;
node_states: NodeStateTable;
node_interactions: NodeInteractionTable;
- node_transactions: NodeTransactionTable;
+ node_updates: NodeUpdateTable;
node_reactions: NodeReactionTable;
collaborations: CollaborationTable;
files: FileTable;
@@ -207,4 +230,6 @@ export interface WorkspaceDatabaseSchema {
texts: TextTable;
cursors: CursorTable;
metadata: MetadataTable;
+ documents: DocumentTable;
+ document_updates: DocumentUpdateTable;
}
diff --git a/apps/desktop/src/main/lib/mappers.ts b/apps/desktop/src/main/lib/mappers.ts
index e71e9593..f570f25b 100644
--- a/apps/desktop/src/main/lib/mappers.ts
+++ b/apps/desktop/src/main/lib/mappers.ts
@@ -1,5 +1,4 @@
-import { LocalNodeTransaction, Mutation } from '@colanode/core';
-import { encodeState } from '@colanode/crdt';
+import { Mutation } from '@colanode/core';
import {
SelectAccount,
@@ -13,7 +12,6 @@ import {
SelectFile,
SelectMutation,
SelectNode,
- SelectNodeTransaction,
SelectUser,
SelectNodeInteraction,
SelectNodeReaction,
@@ -92,18 +90,6 @@ export const mapWorkspace = (row: SelectWorkspace): Workspace => {
};
};
-export const mapNodeTransaction = (
- row: SelectNodeTransaction
-): LocalNodeTransaction => {
- return {
- id: row.id,
- nodeId: row.node_id,
- operation: row.operation,
- data: encodeState(row.data),
- createdAt: row.created_at,
- };
-};
-
export const mapMutation = (row: SelectMutation): Mutation => {
return {
id: row.id,
diff --git a/apps/desktop/src/main/mutations/databases/database-create.ts b/apps/desktop/src/main/mutations/databases/database-create.ts
index e90a99b9..917f65af 100644
--- a/apps/desktop/src/main/mutations/databases/database-create.ts
+++ b/apps/desktop/src/main/mutations/databases/database-create.ts
@@ -3,8 +3,8 @@ import {
generateId,
generateNodeIndex,
IdType,
+ DatabaseViewAttributes,
} from '@colanode/core';
-import { DatabaseViewAttributes } from '@colanode/core/src/registry/database-view';
import { MutationHandler } from '@/main/lib/types';
import {
diff --git a/apps/desktop/src/main/mutations/documents/document-update.ts b/apps/desktop/src/main/mutations/documents/document-update.ts
new file mode 100644
index 00000000..5c9c50f4
--- /dev/null
+++ b/apps/desktop/src/main/mutations/documents/document-update.ts
@@ -0,0 +1,22 @@
+import { MutationHandler } from '@/main/lib/types';
+import {
+ DocumentUpdateMutationInput,
+ DocumentUpdateMutationOutput,
+} from '@/shared/mutations/documents/document-update';
+import { WorkspaceMutationHandlerBase } from '@/main/mutations/workspace-mutation-handler-base';
+
+export class DocumentUpdateMutationHandler
+ extends WorkspaceMutationHandlerBase
+ implements MutationHandler<DocumentUpdateMutationInput>
+{
+ async handleMutation(
+ input: DocumentUpdateMutationInput
+ ): Promise<DocumentUpdateMutationOutput> {
+ const workspace = this.getWorkspace(input.accountId, input.workspaceId);
+ await workspace.documents.updateDocument(input.documentId, input.update);
+
+ return {
+ success: true,
+ };
+ }
+}
diff --git a/apps/desktop/src/main/mutations/index.ts b/apps/desktop/src/main/mutations/index.ts
index 81e63127..caa5189a 100644
--- a/apps/desktop/src/main/mutations/index.ts
+++ b/apps/desktop/src/main/mutations/index.ts
@@ -1,5 +1,3 @@
-import { PageContentUpdateMutationHandler } from '@/main/mutations/pages/page-content-update';
-import { RecordContentUpdateMutationHandler } from '@/main/mutations/records/record-content-update';
import { AccountLogoutMutationHandler } from '@/main/mutations/accounts/account-logout';
import { AccountUpdateMutationHandler } from '@/main/mutations/accounts/account-update';
import { AvatarUploadMutationHandler } from '@/main/mutations/avatars/avatar-upload';
@@ -60,6 +58,7 @@ import { UserRoleUpdateMutationHandler } from '@/main/mutations/users/user-role-
import { UsersInviteMutationHandler } from '@/main/mutations/users/users-invite';
import { WorkspaceMetadataSaveMutationHandler } from '@/main/mutations/workspaces/workspace-metadata-save';
import { WorkspaceMetadataDeleteMutationHandler } from '@/main/mutations/workspaces/workspace-metadata-delete';
+import { DocumentUpdateMutationHandler } from '@/main/mutations/documents/document-update';
import { MutationHandler } from '@/main/lib/types';
import { MutationMap } from '@/shared/mutations';
@@ -90,13 +89,11 @@ export const mutationHandlerMap: MutationHandlerMap = {
node_mark_seen: new NodeMarkSeenMutationHandler(),
page_create: new PageCreateMutationHandler(),
page_delete: new PageDeleteMutationHandler(),
- page_content_update: new PageContentUpdateMutationHandler(),
node_reaction_create: new NodeReactionCreateMutationHandler(),
node_reaction_delete: new NodeReactionDeleteMutationHandler(),
message_delete: new MessageDeleteMutationHandler(),
record_create: new RecordCreateMutationHandler(),
record_delete: new RecordDeleteMutationHandler(),
- record_content_update: new RecordContentUpdateMutationHandler(),
record_avatar_update: new RecordAvatarUpdateMutationHandler(),
record_name_update: new RecordNameUpdateMutationHandler(),
record_field_value_delete: new RecordFieldValueDeleteMutationHandler(),
@@ -130,4 +127,5 @@ export const mutationHandlerMap: MutationHandlerMap = {
database_update: new DatabaseUpdateMutationHandler(),
workspace_metadata_save: new WorkspaceMetadataSaveMutationHandler(),
workspace_metadata_delete: new WorkspaceMetadataDeleteMutationHandler(),
+ document_update: new DocumentUpdateMutationHandler(),
};
diff --git a/apps/desktop/src/main/mutations/messages/message-create.ts b/apps/desktop/src/main/mutations/messages/message-create.ts
index 971d65b0..45cf5ea1 100644
--- a/apps/desktop/src/main/mutations/messages/message-create.ts
+++ b/apps/desktop/src/main/mutations/messages/message-create.ts
@@ -1,5 +1,4 @@
import {
- Block,
canCreateNode,
EditorNodeTypes,
generateId,
@@ -66,7 +65,7 @@ export class MessageCreateMutationHandler
const blocks = mapContentsToBlocks(messageId, editorContent, new Map());
// check if there are nested nodes (files, pages, folders etc.)
- for (const block of blocks) {
+ for (const block of Object.values(blocks)) {
if (block.type === EditorNodeTypes.FilePlaceholder) {
const path = block.attrs?.path;
const fileId = generateId(IdType.File);
@@ -79,19 +78,11 @@ export class MessageCreateMutationHandler
}
}
- const blocksRecord = blocks.reduce(
- (acc, block) => {
- acc[block.id] = block;
- return acc;
- },
- {} as Record<string, Block>
- );
-
const messageAttributes: MessageAttributes = {
type: 'message',
subtype: 'standard',
parentId: input.parentId,
- content: blocksRecord,
+ content: blocks,
referenceId: input.referenceId,
};
diff --git a/apps/desktop/src/main/mutations/pages/page-content-update.ts b/apps/desktop/src/main/mutations/pages/page-content-update.ts
deleted file mode 100644
index 55a51d82..00000000
--- a/apps/desktop/src/main/mutations/pages/page-content-update.ts
+++ /dev/null
@@ -1,94 +0,0 @@
-import { PageAttributes } from '@colanode/core';
-import { isEqual } from 'lodash-es';
-
-import { MutationHandler } from '@/main/lib/types';
-import { mapContentsToBlocks } from '@/shared/lib/editor';
-import {
- PageContentUpdateMutationInput,
- PageContentUpdateMutationOutput,
-} from '@/shared/mutations/pages/page-content-update';
-import { MutationError, MutationErrorCode } from '@/shared/mutations';
-import { WorkspaceMutationHandlerBase } from '@/main/mutations/workspace-mutation-handler-base';
-
-export class PageContentUpdateMutationHandler
- extends WorkspaceMutationHandlerBase
- implements MutationHandler<PageContentUpdateMutationInput>
-{
- async handleMutation(
- input: PageContentUpdateMutationInput
- ): Promise<PageContentUpdateMutationOutput> {
- const workspace = this.getWorkspace(input.accountId, input.workspaceId);
-
- if (isEqual(input.before.content, input.after.content)) {
- return {
- success: true,
- };
- }
-
- const result = await workspace.nodes.updateNode<PageAttributes>(
- input.pageId,
- (attributes) => {
- const indexMap = new Map<string, string>();
- if (attributes.content) {
- for (const [key, value] of Object.entries(attributes.content)) {
- indexMap.set(key, value.index);
- }
- }
-
- const beforeBlocks = mapContentsToBlocks(
- input.pageId,
- input.before.content ?? [],
- indexMap
- );
-
- const afterBlocks = mapContentsToBlocks(
- input.pageId,
- input.after.content ?? [],
- indexMap
- );
-
- const content = attributes.content ?? {};
- for (const afterBlock of afterBlocks) {
- const beforeBlock = beforeBlocks.find(
- (block) => block.id === afterBlock.id
- );
-
- if (!isEqual(beforeBlock, afterBlock)) {
- content[afterBlock.id] = afterBlock;
- }
- }
-
- for (const beforeBlock of beforeBlocks) {
- const afterBlock = afterBlocks.find(
- (block) => block.id === beforeBlock.id
- );
-
- if (!afterBlock) {
- delete content[beforeBlock.id];
- }
- }
-
- attributes.content = content;
- return attributes;
- }
- );
-
- if (result === 'unauthorized') {
- throw new MutationError(
- MutationErrorCode.PageUpdateForbidden,
- "You don't have permission to update this page."
- );
- }
-
- if (result !== 'success') {
- throw new MutationError(
- MutationErrorCode.PageUpdateFailed,
- 'Something went wrong while updating the page content.'
- );
- }
-
- return {
- success: true,
- };
- }
-}
diff --git a/apps/desktop/src/main/mutations/records/record-content-update.ts b/apps/desktop/src/main/mutations/records/record-content-update.ts
deleted file mode 100644
index 094db37c..00000000
--- a/apps/desktop/src/main/mutations/records/record-content-update.ts
+++ /dev/null
@@ -1,94 +0,0 @@
-import { RecordAttributes } from '@colanode/core';
-import { isEqual } from 'lodash-es';
-
-import { MutationHandler } from '@/main/lib/types';
-import { mapContentsToBlocks } from '@/shared/lib/editor';
-import {
- RecordContentUpdateMutationInput,
- RecordContentUpdateMutationOutput,
-} from '@/shared/mutations/records/record-content-update';
-import { MutationError, MutationErrorCode } from '@/shared/mutations';
-import { WorkspaceMutationHandlerBase } from '@/main/mutations/workspace-mutation-handler-base';
-
-export class RecordContentUpdateMutationHandler
- extends WorkspaceMutationHandlerBase
- implements MutationHandler<RecordContentUpdateMutationInput>
-{
- async handleMutation(
- input: RecordContentUpdateMutationInput
- ): Promise<RecordContentUpdateMutationOutput> {
- const workspace = this.getWorkspace(input.accountId, input.workspaceId);
-
- if (isEqual(input.before.content, input.after.content)) {
- return {
- success: true,
- };
- }
-
- const result = await workspace.nodes.updateNode<RecordAttributes>(
- input.recordId,
- (attributes) => {
- const indexMap = new Map<string, string>();
- if (attributes.content) {
- for (const [key, value] of Object.entries(attributes.content)) {
- indexMap.set(key, value.index);
- }
- }
-
- const beforeBlocks = mapContentsToBlocks(
- input.recordId,
- input.before.content ?? [],
- indexMap
- );
-
- const afterBlocks = mapContentsToBlocks(
- input.recordId,
- input.after.content ?? [],
- indexMap
- );
-
- const content = attributes.content ?? {};
- for (const afterBlock of afterBlocks) {
- const beforeBlock = beforeBlocks.find(
- (block) => block.id === afterBlock.id
- );
-
- if (!isEqual(beforeBlock, afterBlock)) {
- content[afterBlock.id] = afterBlock;
- }
- }
-
- for (const beforeBlock of beforeBlocks) {
- const afterBlock = afterBlocks.find(
- (block) => block.id === beforeBlock.id
- );
-
- if (!afterBlock) {
- delete content[beforeBlock.id];
- }
- }
-
- attributes.content = content;
- return attributes;
- }
- );
-
- if (result === 'unauthorized') {
- throw new MutationError(
- MutationErrorCode.RecordUpdateForbidden,
- "You don't have permission to update this record."
- );
- }
-
- if (result !== 'success') {
- throw new MutationError(
- MutationErrorCode.RecordUpdateFailed,
- 'Something went wrong while updating the record content. Please try again later.'
- );
- }
-
- return {
- success: true,
- };
- }
-}
diff --git a/apps/desktop/src/main/queries/documents/document-get.ts b/apps/desktop/src/main/queries/documents/document-get.ts
new file mode 100644
index 00000000..6065d78b
--- /dev/null
+++ b/apps/desktop/src/main/queries/documents/document-get.ts
@@ -0,0 +1,124 @@
+import { YDoc } from '@colanode/crdt';
+
+import { Document } from '@/shared/types/documents';
+import { ChangeCheckResult, QueryHandler } from '@/main/lib/types';
+import { Event } from '@/shared/types/events';
+import { WorkspaceQueryHandlerBase } from '@/main/queries/workspace-query-handler-base';
+import { DocumentGetQueryInput } from '@/shared/queries/documents/document-get';
+
+export class DocumentGetQueryHandler
+ extends WorkspaceQueryHandlerBase
+ implements QueryHandler<DocumentGetQueryInput>
+{
+ public async handleQuery(
+ input: DocumentGetQueryInput
+ ): Promise<Document | null> {
+ const workspace = this.getWorkspace(input.accountId, input.workspaceId);
+
+ const document = await workspace.database
+ .selectFrom('documents')
+ .selectAll()
+ .where('id', '=', input.documentId)
+ .executeTakeFirst();
+
+ const documentUpdates = await workspace.database
+ .selectFrom('document_updates')
+ .selectAll()
+ .where('document_id', '=', input.documentId)
+ .orderBy('id', 'asc')
+ .execute();
+
+ if (documentUpdates.length === 0) {
+ if (!document) {
+ return null;
+ }
+
+ return {
+ id: document.id,
+ revision: document.revision,
+ state: document.state,
+ createdAt: document.created_at,
+ createdBy: document.created_by,
+ updatedAt: document.updated_at,
+ updatedBy: document.updated_by,
+ };
+ }
+
+ const ydoc = new YDoc(document?.state);
+ for (const update of documentUpdates) {
+ ydoc.applyUpdate(update.data);
+ }
+
+ const firstUpdate = documentUpdates[0]!;
+ const lastUpdate = documentUpdates[documentUpdates.length - 1]!;
+
+ return {
+ id: input.documentId,
+ revision: 0n,
+ state: ydoc.getState(),
+ createdAt: firstUpdate.created_at,
+ createdBy: workspace.userId,
+ updatedAt: lastUpdate.created_at,
+ updatedBy: workspace.userId,
+ };
+ }
+
+ public async checkForChanges(
+ event: Event,
+ input: DocumentGetQueryInput,
+ _: Document | null
+ ): Promise<ChangeCheckResult<DocumentGetQueryInput>> {
+ if (
+ event.type === 'workspace_deleted' &&
+ event.workspace.accountId === input.accountId &&
+ event.workspace.id === input.workspaceId
+ ) {
+ return {
+ hasChanges: true,
+ result: null,
+ };
+ }
+
+ if (
+ event.type === 'document_updated' &&
+ event.accountId === input.accountId &&
+ event.workspaceId === input.workspaceId &&
+ event.documentId === input.documentId
+ ) {
+ const newOutput = await this.handleQuery(input);
+ return {
+ hasChanges: true,
+ result: newOutput,
+ };
+ }
+
+ if (
+ event.type === 'document_update_created' &&
+ event.accountId === input.accountId &&
+ event.workspaceId === input.workspaceId &&
+ event.documentId === input.documentId
+ ) {
+ const newOutput = await this.handleQuery(input);
+ return {
+ hasChanges: true,
+ result: newOutput,
+ };
+ }
+
+ if (
+ event.type === 'node_deleted' &&
+ event.accountId === input.accountId &&
+ event.workspaceId === input.workspaceId &&
+ event.node.id === input.documentId
+ ) {
+ return {
+ hasChanges: true,
+ result: null,
+ };
+ }
+
+ return {
+ hasChanges: false,
+ };
+ }
+}
diff --git a/apps/desktop/src/main/queries/index.ts b/apps/desktop/src/main/queries/index.ts
index 8a217fbf..6466ccb5 100644
--- a/apps/desktop/src/main/queries/index.ts
+++ b/apps/desktop/src/main/queries/index.ts
@@ -31,6 +31,7 @@ import { RecordSearchQueryHandler } from '@/main/queries/records/record-search';
import { UserGetQueryHandler } from '@/main/queries/users/user-get';
import { SpaceListQueryHandler } from '@/main/queries/spaces/space-list';
import { ChatListQueryHandler } from '@/main/queries/chats/chat-list';
+import { DocumentGetQueryHandler } from '@/main/queries/documents/document-get';
import { WorkspaceMetadataListQueryHandler } from '@/main/queries/workspaces/workspace-metadata-list';
import { QueryHandler } from '@/main/lib/types';
import { QueryMap } from '@/shared/queries';
@@ -74,4 +75,5 @@ export const queryHandlerMap: QueryHandlerMap = {
chat_list: new ChatListQueryHandler(),
space_list: new SpaceListQueryHandler(),
workspace_metadata_list: new WorkspaceMetadataListQueryHandler(),
+ document_get: new DocumentGetQueryHandler(),
};
diff --git a/apps/desktop/src/main/services/workspaces/document-service.ts b/apps/desktop/src/main/services/workspaces/document-service.ts
new file mode 100644
index 00000000..38ccd7f4
--- /dev/null
+++ b/apps/desktop/src/main/services/workspaces/document-service.ts
@@ -0,0 +1,252 @@
+import {
+ createDebugger,
+ generateId,
+ getNodeModel,
+ IdType,
+ SyncDocumentUpdateData,
+ UpdateDocumentMutationData,
+} from '@colanode/core';
+import { encodeState, YDoc } from '@colanode/crdt';
+
+import { WorkspaceService } from '@/main/services/workspaces/workspace-service';
+import { eventBus } from '@/shared/lib/event-bus';
+
+const UPDATE_RETRIES_LIMIT = 10;
+
+export class DocumentService {
+ private readonly debug = createDebugger('desktop:service:document');
+ private readonly workspace: WorkspaceService;
+
+ constructor(workspaceService: WorkspaceService) {
+ this.workspace = workspaceService;
+ }
+
+ public async updateDocument(id: string, update: Uint8Array) {
+ const node = await this.workspace.database
+ .selectFrom('nodes')
+ .selectAll()
+ .where('id', '=', id)
+ .executeTakeFirst();
+
+ if (!node) {
+ throw new Error('Node not found');
+ }
+
+ const model = getNodeModel(node.type);
+ if (!model.documentSchema) {
+ throw new Error('Node does not have a document schema');
+ }
+
+ const document = await this.workspace.database
+ .selectFrom('documents')
+ .selectAll()
+ .where('id', '=', node.id)
+ .executeTakeFirst();
+
+ const documentUpdates = await this.workspace.database
+ .selectFrom('document_updates')
+ .selectAll()
+ .where('document_id', '=', node.id)
+ .orderBy('id', 'asc')
+ .execute();
+
+ const ydoc = new YDoc(document?.state);
+ for (const update of documentUpdates) {
+ ydoc.applyUpdate(update.data);
+ }
+
+ ydoc.applyUpdate(update);
+
+ const updateId = generateId(IdType.Update);
+ const updatedAt = new Date().toISOString();
+
+ const { createdUpdate, createdMutation } = await this.workspace.database
+ .transaction()
+ .execute(async (trx) => {
+ const createdUpdate = await trx
+ .insertInto('document_updates')
+ .returningAll()
+ .values({
+ id: updateId,
+ document_id: node.id,
+ data: update,
+ created_at: updatedAt,
+ })
+ .executeTakeFirst();
+
+ if (!createdUpdate) {
+ throw new Error('Failed to create update');
+ }
+
+ const mutationData: UpdateDocumentMutationData = {
+ documentId: id,
+ updateId: updateId,
+ data: encodeState(update),
+ createdAt: updatedAt,
+ };
+
+ const createdMutation = await trx
+ .insertInto('mutations')
+ .returningAll()
+ .values({
+ id: generateId(IdType.Mutation),
+ type: 'update_document',
+ data: JSON.stringify(mutationData),
+ created_at: updatedAt,
+ retries: 0,
+ })
+ .executeTakeFirst();
+
+ if (!createdMutation) {
+ throw new Error('Failed to create mutation');
+ }
+
+ return {
+ createdUpdate,
+ createdMutation,
+ };
+ });
+
+ if (createdUpdate) {
+ eventBus.publish({
+ type: 'document_update_created',
+ accountId: this.workspace.accountId,
+ workspaceId: this.workspace.id,
+ documentId: node.id,
+ updateId: createdUpdate.id,
+ });
+ }
+
+ if (createdMutation) {
+ this.workspace.mutations.triggerSync();
+ }
+ }
+
+ public async syncServerDocumentUpdate(data: SyncDocumentUpdateData) {
+ for (let count = 0; count < UPDATE_RETRIES_LIMIT; count++) {
+ try {
+ const result = await this.trySyncDocumentUpdate(data);
+ if (result) {
+ return;
+ }
+ } catch (error) {
+ this.debug(`Failed to sync document update ${data.id}: ${error}`);
+ }
+ }
+ }
+
+ private async trySyncDocumentUpdate(
+ data: SyncDocumentUpdateData
+ ): Promise<boolean> {
+ const document = await this.workspace.database
+ .selectFrom('documents')
+ .selectAll()
+ .where('id', '=', data.documentId)
+ .executeTakeFirst();
+
+ const documentUpdates = await this.workspace.database
+ .selectFrom('document_updates')
+ .selectAll()
+ .where('document_id', '=', data.documentId)
+ .orderBy('id', 'asc')
+ .execute();
+
+ const mergedUpdateIds =
+ data.mergedUpdates?.map((update) => update.id) ?? [];
+
+ const ydoc = new YDoc(document?.state);
+ for (const update of documentUpdates) {
+ if (update.id === data.id || mergedUpdateIds.includes(update.id)) {
+ continue;
+ }
+
+ ydoc.applyUpdate(update.data);
+ }
+
+ ydoc.applyUpdate(data.data);
+ const revision = BigInt(data.revision);
+ const updatesToDelete = [data.id, ...mergedUpdateIds];
+
+ if (document) {
+ const { updatedDocument } = await this.workspace.database
+ .transaction()
+ .execute(async (trx) => {
+ const updatedDocument = await trx
+ .updateTable('documents')
+ .returningAll()
+ .set({
+ state: ydoc.getState(),
+ revision: revision,
+ updated_at: data.createdAt,
+ updated_by: data.createdBy,
+ })
+ .where('id', '=', data.documentId)
+ .where('revision', '=', document.revision)
+ .executeTakeFirst();
+
+ if (!updatedDocument) {
+ throw new Error('Failed to update document');
+ }
+
+ await trx
+ .deleteFrom('document_updates')
+ .where('id', 'in', updatesToDelete)
+ .execute();
+
+ return { updatedDocument };
+ });
+
+ if (!updatedDocument) {
+ return false;
+ }
+
+ eventBus.publish({
+ type: 'document_updated',
+ accountId: this.workspace.accountId,
+ workspaceId: this.workspace.id,
+ documentId: data.documentId,
+ });
+ } else {
+ const { createdDocument } = await this.workspace.database
+ .transaction()
+ .execute(async (trx) => {
+ const createdDocument = await trx
+ .insertInto('documents')
+ .returningAll()
+ .values({
+ id: data.documentId,
+ state: ydoc.getState(),
+ revision: revision,
+ created_at: data.createdAt,
+ created_by: data.createdBy,
+ })
+ .onConflict((cb) => cb.doNothing())
+ .executeTakeFirst();
+
+ if (!createdDocument) {
+ throw new Error('Failed to create document');
+ }
+
+ await trx
+ .deleteFrom('document_updates')
+ .where('id', 'in', updatesToDelete)
+ .execute();
+
+ return { createdDocument };
+ });
+
+ if (!createdDocument) {
+ return false;
+ }
+
+ eventBus.publish({
+ type: 'document_updated',
+ accountId: this.workspace.accountId,
+ workspaceId: this.workspace.id,
+ documentId: data.documentId,
+ });
+ }
+
+ return true;
+ }
+}
diff --git a/apps/desktop/src/main/services/workspaces/file-service.ts b/apps/desktop/src/main/services/workspaces/file-service.ts
index 8989fdb7..cb782b58 100644
--- a/apps/desktop/src/main/services/workspaces/file-service.ts
+++ b/apps/desktop/src/main/services/workspaces/file-service.ts
@@ -737,7 +737,7 @@ export class FileService {
this.debug(`Server file ${file.id} has been synced`);
}
- public async revertFileCreation(fileId: string) {
+ public async revertFileCreate(fileId: string) {
const deletedFile = await this.workspace.database
.deleteFrom('files')
.returningAll()
diff --git a/apps/desktop/src/main/services/workspaces/mutation-service.ts b/apps/desktop/src/main/services/workspaces/mutation-service.ts
index fba3c728..0d342f83 100644
--- a/apps/desktop/src/main/services/workspaces/mutation-service.ts
+++ b/apps/desktop/src/main/services/workspaces/mutation-service.ts
@@ -144,17 +144,19 @@ export class MutationService {
const mutation = mapMutation(mutationRow);
if (mutation.type === 'create_file') {
- await this.workspace.files.revertFileCreation(mutation.id);
- } else if (mutation.type === 'apply_node_transaction') {
- await this.workspace.nodes.revertNodeTransaction(mutation.data);
+ await this.workspace.files.revertFileCreate(mutation.id);
+ } else if (mutation.type === 'create_node') {
+ await this.workspace.nodes.revertNodeCreate(mutation.data);
+ } else if (mutation.type === 'update_node') {
+ await this.workspace.nodes.revertNodeUpdate(mutation.data);
} else if (mutation.type === 'delete_node') {
await this.workspace.nodes.revertNodeDelete(mutation.data);
} else if (mutation.type === 'create_node_reaction') {
- await this.workspace.nodeReactions.revertNodeReactionCreation(
+ await this.workspace.nodeReactions.revertNodeReactionCreate(
mutation.data
);
} else if (mutation.type === 'delete_node_reaction') {
- await this.workspace.nodeReactions.revertNodeReactionDeletion(
+ await this.workspace.nodeReactions.revertNodeReactionDelete(
mutation.data
);
}
@@ -212,11 +214,24 @@ export class MutationService {
}
if (
- previousMutation.type === 'apply_node_transaction' &&
+ previousMutation.type === 'create_node' &&
previousMutation.data.id === mutation.data.id
) {
deletedMutationIds.add(mutation.id);
deletedMutationIds.add(previousMutation.id);
+ } else if (
+ previousMutation.type === 'update_node' &&
+ previousMutation.data.id === mutation.data.id
+ ) {
+ deletedMutationIds.add(mutation.id);
+ deletedMutationIds.add(previousMutation.id);
+ } else if (
+ previousMutation.type === 'delete_node' &&
+ previousMutation.data.id === mutation.data.id
+ ) {
+ deletedMutationIds.add(previousMutation.id);
+ } else if (previousMutation.type === 'update_document') {
+ deletedMutationIds.add(previousMutation.id);
} else if (
previousMutation.type === 'mark_node_seen' &&
previousMutation.data.nodeId === mutation.data.id
diff --git a/apps/desktop/src/main/services/workspaces/node-reaction-service.ts b/apps/desktop/src/main/services/workspaces/node-reaction-service.ts
index 1205a23f..cb84f335 100644
--- a/apps/desktop/src/main/services/workspaces/node-reaction-service.ts
+++ b/apps/desktop/src/main/services/workspaces/node-reaction-service.ts
@@ -331,7 +331,7 @@ export class NodeReactionService {
);
}
- public async revertNodeReactionCreation(
+ public async revertNodeReactionCreate(
nodeReaction: CreateNodeReactionMutationData
) {
const deletedNodeReaction = await this.workspace.database
@@ -354,7 +354,7 @@ export class NodeReactionService {
});
}
- public async revertNodeReactionDeletion(
+ public async revertNodeReactionDelete(
nodeReaction: DeleteNodeReactionMutationData
) {
const tombstoneId = this.generateTombstoneId(
diff --git a/apps/desktop/src/main/services/workspaces/node-service.ts b/apps/desktop/src/main/services/workspaces/node-service.ts
index df0a9b61..d6bf31a4 100644
--- a/apps/desktop/src/main/services/workspaces/node-service.ts
+++ b/apps/desktop/src/main/services/workspaces/node-service.ts
@@ -1,24 +1,24 @@
import {
generateId,
IdType,
- TransactionOperation,
createDebugger,
NodeAttributes,
Node,
canCreateNode,
- nodeAttributesSchema,
extractNodeText,
canUpdateNode,
canDeleteNode,
DeleteNodeMutationData,
SyncNodeData,
SyncNodeTombstoneData,
- LocalNodeTransaction,
+ getNodeModel,
+ CreateNodeMutationData,
+ UpdateNodeMutationData,
} from '@colanode/core';
-import { decodeState, YDoc } from '@colanode/crdt';
+import { decodeState, encodeState, YDoc } from '@colanode/crdt';
import { fetchNode } from '@/main/lib/utils';
-import { mapFile, mapNode, mapNodeTransaction } from '@/main/lib/mappers';
+import { mapFile, mapNode } from '@/main/lib/mappers';
import { eventBus } from '@/shared/lib/event-bus';
import { WorkspaceService } from '@/main/services/workspaces/workspace-service';
import { SelectNode } from '@/main/databases/workspace';
@@ -84,18 +84,15 @@ export class NodeService {
throw new Error('Insufficient permissions');
}
+ const model = getNodeModel(input.attributes.type);
const ydoc = new YDoc();
- const update = ydoc.updateAttributes(
- nodeAttributesSchema,
- input.attributes
- );
+ const update = ydoc.update(model.attributesSchema, input.attributes);
if (!update) {
throw new Error('Invalid attributes');
}
const createdAt = new Date().toISOString();
- const transactionId = generateId(IdType.Transaction);
const text = extractNodeText(input.id, input.attributes);
const { createdNode, createdMutation } = await this.workspace.database
@@ -119,29 +116,33 @@ export class NodeService {
throw new Error('Failed to create entry');
}
- const createdTransaction = await trx
- .insertInto('node_transactions')
+ const createdState = await trx
+ .insertInto('node_states')
.returningAll()
.values({
- id: transactionId,
- node_id: input.id,
- operation: TransactionOperation.Create,
- data: update,
- created_at: createdAt,
+ id: input.id,
+ state: update,
+ revision: BigInt(0),
})
.executeTakeFirst();
- if (!createdTransaction) {
- throw new Error('Failed to create transaction');
+ if (!createdState) {
+ throw new Error('Failed to create state');
}
+ const mutationData: CreateNodeMutationData = {
+ id: input.id,
+ data: encodeState(update),
+ createdAt: createdAt,
+ };
+
const createdMutation = await trx
.insertInto('mutations')
.returningAll()
.values({
id: generateId(IdType.Mutation),
- type: 'apply_node_transaction',
- data: JSON.stringify(mapNodeTransaction(createdTransaction)),
+ type: 'create_node',
+ data: JSON.stringify(mutationData),
created_at: createdAt,
retries: 0,
})
@@ -216,10 +217,11 @@ export class NodeService {
}
const node = mapNode(nodeRow);
- const transactionId = generateId(IdType.Transaction);
+ const updateId = generateId(IdType.Update);
const updatedAt = new Date().toISOString();
const updatedAttributes = updater(node.attributes as T);
+ const model = getNodeModel(updatedAttributes.type);
const ydoc = new YDoc();
const state = await this.workspace.database
@@ -232,20 +234,17 @@ export class NodeService {
ydoc.applyUpdate(state.state);
}
- const transactions = await this.workspace.database
- .selectFrom('node_transactions')
+ const updates = await this.workspace.database
+ .selectFrom('node_updates')
.where('node_id', '=', nodeId)
.selectAll()
.execute();
- for (const transaction of transactions) {
- ydoc.applyUpdate(transaction.data);
+ for (const update of updates) {
+ ydoc.applyUpdate(update.data);
}
- const update = ydoc.updateAttributes(
- nodeAttributesSchema,
- updatedAttributes
- );
+ const update = ydoc.update(model.attributesSchema, updatedAttributes);
if (
!canUpdateNode(
@@ -267,7 +266,7 @@ export class NodeService {
return 'success';
}
- const attributes = ydoc.getAttributes<NodeAttributes>();
+ const attributes = ydoc.getObject<NodeAttributes>();
const text = extractNodeText(nodeId, updatedAttributes);
const localRevision = BigInt(node.localRevision) + BigInt(1);
@@ -288,32 +287,38 @@ export class NodeService {
.executeTakeFirst();
if (!updatedNode) {
- return { updatedNode: undefined };
+ throw new Error('Failed to update node');
}
- const createdTransaction = await trx
- .insertInto('node_transactions')
+ const createdUpdate = await trx
+ .insertInto('node_updates')
.returningAll()
.values({
- id: transactionId,
+ id: updateId,
node_id: nodeId,
- operation: TransactionOperation.Update,
data: update,
created_at: updatedAt,
})
.executeTakeFirst();
- if (!createdTransaction) {
- throw new Error('Failed to create transaction');
+ if (!createdUpdate) {
+ throw new Error('Failed to create update');
}
+ const mutationData: UpdateNodeMutationData = {
+ id: nodeId,
+ updateId: updateId,
+ data: encodeState(update),
+ createdAt: updatedAt,
+ };
+
const createdMutation = await trx
.insertInto('mutations')
.returningAll()
.values({
id: generateId(IdType.Mutation),
- type: 'apply_node_transaction',
- data: JSON.stringify(mapNodeTransaction(createdTransaction)),
+ type: 'update_node',
+ data: JSON.stringify(mutationData),
created_at: updatedAt,
retries: 0,
})
@@ -400,7 +405,7 @@ export class NodeService {
.executeTakeFirst();
if (!deletedNode) {
- return { deletedNode: undefined };
+ throw new Error('Failed to delete node');
}
await trx
@@ -471,18 +476,7 @@ export class NodeService {
const serverRevision = BigInt(node.revision);
const ydoc = new YDoc(node.state);
- const transactions = await this.workspace.database
- .selectFrom('node_transactions')
- .selectAll()
- .where('node_id', '=', node.id)
- .orderBy('id', 'asc')
- .execute();
-
- for (const transaction of transactions) {
- ydoc.applyUpdate(transaction.data);
- }
-
- const attributes = ydoc.getAttributes<NodeAttributes>();
+ const attributes = ydoc.getObject<NodeAttributes>();
const text = extractNodeText(node.id, attributes);
const { createdNode } = await this.workspace.database
@@ -503,7 +497,7 @@ export class NodeService {
.executeTakeFirst();
if (!createdNode) {
- return { createdNode: undefined };
+ throw new Error('Failed to create node');
}
if (text) {
@@ -543,10 +537,14 @@ export class NodeService {
public async updateServerNode(node: SyncNodeData): Promise<void> {
for (let count = 0; count < UPDATE_RETRIES_LIMIT; count++) {
- const result = await this.tryUpdateServerNode(node);
+ try {
+ const result = await this.tryUpdateServerNode(node);
- if (result) {
- return;
+ if (result) {
+ return;
+ }
+ } catch (error) {
+ this.debug(`Failed to update node ${node.id}: ${error}`);
}
}
}
@@ -566,18 +564,18 @@ export class NodeService {
}
const ydoc = new YDoc(node.state);
- const transactions = await this.workspace.database
- .selectFrom('node_transactions')
+ const updates = await this.workspace.database
+ .selectFrom('node_updates')
.selectAll()
.where('node_id', '=', node.id)
.orderBy('id', 'asc')
.execute();
- for (const transaction of transactions) {
- ydoc.applyUpdate(transaction.data);
+ for (const update of updates) {
+ ydoc.applyUpdate(update.data);
}
- const attributes = ydoc.getAttributes<NodeAttributes>();
+ const attributes = ydoc.getObject<NodeAttributes>();
const text = extractNodeText(node.id, attributes);
const localRevision = BigInt(existingNode.local_revision) + BigInt(1);
@@ -599,7 +597,7 @@ export class NodeService {
.executeTakeFirst();
if (!updatedNode) {
- return { updatedNode: undefined };
+ throw new Error('Failed to update node');
}
await trx
@@ -657,7 +655,7 @@ export class NodeService {
.executeTakeFirst();
await trx
- .deleteFrom('node_transactions')
+ .deleteFrom('node_updates')
.where('node_id', '=', tombstone.id)
.execute();
@@ -712,64 +710,40 @@ export class NodeService {
}
}
- public async revertNodeTransaction(transaction: LocalNodeTransaction) {
- if (transaction.operation === TransactionOperation.Create) {
- return this.revertCreateTransaction(transaction);
- } else if (transaction.operation === TransactionOperation.Update) {
- return this.revertUpdateTransaction(transaction);
- }
- }
-
- public async revertCreateTransaction(transaction: LocalNodeTransaction) {
+ public async revertNodeCreate(mutation: CreateNodeMutationData) {
const node = await this.workspace.database
.selectFrom('nodes')
.selectAll()
- .where('id', '=', transaction.nodeId)
+ .where('id', '=', mutation.id)
.executeTakeFirst();
if (!node) {
return;
}
- const transactionRow = await this.workspace.database
- .selectFrom('node_transactions')
- .selectAll()
- .where('id', '=', transaction.id)
- .executeTakeFirst();
-
- if (!transactionRow) {
- return;
- }
-
await this.workspace.database.transaction().execute(async (tx) => {
- await tx
- .deleteFrom('nodes')
- .where('id', '=', transaction.nodeId)
- .execute();
+ await tx.deleteFrom('nodes').where('id', '=', mutation.id).execute();
await tx
- .deleteFrom('node_transactions')
- .where('node_id', '=', transaction.nodeId)
+ .deleteFrom('node_updates')
+ .where('node_id', '=', mutation.id)
.execute();
await tx
.deleteFrom('node_interactions')
- .where('node_id', '=', transaction.nodeId)
+ .where('node_id', '=', mutation.id)
.execute();
- await tx
- .deleteFrom('texts')
- .where('id', '=', transaction.nodeId)
- .execute();
+ await tx.deleteFrom('texts').where('id', '=', mutation.id).execute();
await tx
.deleteFrom('node_reactions')
- .where('node_id', '=', transaction.nodeId)
+ .where('node_id', '=', mutation.id)
.execute();
await tx
.deleteFrom('node_states')
- .where('id', '=', transaction.nodeId)
+ .where('id', '=', mutation.id)
.execute();
});
@@ -781,9 +755,9 @@ export class NodeService {
});
}
- public async revertUpdateTransaction(transaction: LocalNodeTransaction) {
+ public async revertNodeUpdate(mutation: UpdateNodeMutationData) {
for (let count = 0; count < UPDATE_RETRIES_LIMIT; count++) {
- const result = await this.tryRevertUpdateTransaction(transaction);
+ const result = await this.tryRevertNodeUpdate(mutation);
if (result) {
return;
@@ -791,66 +765,66 @@ export class NodeService {
}
}
- private async tryRevertUpdateTransaction(
- transaction: LocalNodeTransaction
+ private async tryRevertNodeUpdate(
+ mutation: UpdateNodeMutationData
): Promise<boolean> {
const node = await this.workspace.database
.selectFrom('nodes')
.selectAll()
- .where('id', '=', transaction.nodeId)
+ .where('id', '=', mutation.id)
.executeTakeFirst();
if (!node) {
// Make sure we don't have any data left behind
await this.workspace.database
- .deleteFrom('node_transactions')
- .where('id', '=', transaction.id)
+ .deleteFrom('node_updates')
+ .where('id', '=', mutation.id)
.execute();
await this.workspace.database
.deleteFrom('node_interactions')
- .where('node_id', '=', transaction.nodeId)
+ .where('node_id', '=', mutation.id)
.execute();
await this.workspace.database
.deleteFrom('texts')
- .where('id', '=', transaction.nodeId)
+ .where('id', '=', mutation.id)
.execute();
await this.workspace.database
.deleteFrom('node_reactions')
- .where('node_id', '=', transaction.nodeId)
+ .where('node_id', '=', mutation.id)
.execute();
await this.workspace.database
.deleteFrom('node_states')
- .where('id', '=', transaction.nodeId)
+ .where('id', '=', mutation.id)
.execute();
return true;
}
- const transactionRow = await this.workspace.database
- .selectFrom('node_transactions')
+ const updateRow = await this.workspace.database
+ .selectFrom('node_updates')
.selectAll()
- .where('id', '=', transaction.id)
+ .where('id', '=', mutation.id)
.executeTakeFirst();
- if (!transactionRow) {
+ if (!updateRow) {
return true;
}
- const nodeTransactions = await this.workspace.database
- .selectFrom('node_transactions')
+ const nodeUpdates = await this.workspace.database
+ .selectFrom('node_updates')
.selectAll()
- .where('node_id', '=', transaction.nodeId)
+ .where('node_id', '=', mutation.id)
.orderBy('id', 'asc')
.execute();
const state = await this.workspace.database
.selectFrom('node_states')
.selectAll()
- .where('id', '=', transaction.nodeId)
+ .where('id', '=', mutation.id)
.executeTakeFirst();
if (!state) {
@@ -858,18 +832,18 @@ export class NodeService {
}
const ydoc = new YDoc(state.state);
- for (const nodeTransaction of nodeTransactions) {
- if (nodeTransaction.id === transaction.id) {
+ for (const nodeUpdate of nodeUpdates) {
+ if (nodeUpdate.id === mutation.updateId) {
continue;
}
- if (nodeTransaction.data) {
- ydoc.applyUpdate(nodeTransaction.data);
+ if (nodeUpdate.data) {
+ ydoc.applyUpdate(nodeUpdate.data);
}
}
- const attributes = ydoc.getAttributes<NodeAttributes>();
- const text = extractNodeText(transaction.nodeId, attributes);
+ const attributes = ydoc.getObject<NodeAttributes>();
+ const text = extractNodeText(mutation.id, attributes);
const updatedNode = await this.workspace.database
.transaction()
@@ -880,7 +854,7 @@ export class NodeService {
.set({
attributes: JSON.stringify(attributes),
})
- .where('id', '=', transaction.nodeId)
+ .where('id', '=', mutation.id)
.where('local_revision', '=', node.local_revision)
.executeTakeFirst();
@@ -889,17 +863,14 @@ export class NodeService {
}
if (text !== undefined) {
- await trx
- .deleteFrom('texts')
- .where('id', '=', transaction.nodeId)
- .execute();
+ await trx.deleteFrom('texts').where('id', '=', mutation.id).execute();
}
if (text) {
await trx
.insertInto('texts')
.values({
- id: transaction.nodeId,
+ id: mutation.id,
name: text.name,
text: text.text,
})
@@ -907,8 +878,8 @@ export class NodeService {
}
await trx
- .deleteFrom('node_transactions')
- .where('id', '=', transaction.id)
+ .deleteFrom('node_updates')
+ .where('id', '=', mutation.updateId)
.execute();
});
@@ -947,19 +918,19 @@ export class NodeService {
return;
}
- const nodeTransactions = await this.workspace.database
- .selectFrom('node_transactions')
+ const nodeUpdates = await this.workspace.database
+ .selectFrom('node_updates')
.selectAll()
.where('node_id', '=', mutation.id)
.orderBy('id', 'asc')
.execute();
const ydoc = new YDoc(state.state);
- for (const nodeTransaction of nodeTransactions) {
- ydoc.applyUpdate(nodeTransaction.data);
+ for (const nodeUpdate of nodeUpdates) {
+ ydoc.applyUpdate(nodeUpdate.data);
}
- const attributes = ydoc.getAttributes<NodeAttributes>();
+ const attributes = ydoc.getObject<NodeAttributes>();
const text = extractNodeText(mutation.id, attributes);
const deletedNode = JSON.parse(tombstone.data) as SelectNode;
diff --git a/apps/desktop/src/main/services/workspaces/sync-service.ts b/apps/desktop/src/main/services/workspaces/sync-service.ts
index 9d161e5d..d1effaa1 100644
--- a/apps/desktop/src/main/services/workspaces/sync-service.ts
+++ b/apps/desktop/src/main/services/workspaces/sync-service.ts
@@ -14,6 +14,8 @@ import {
SyncUserData,
SyncCollaborationData,
SyncFileData,
+ SyncDocumentUpdatesInput,
+ SyncDocumentUpdateData,
} from '@colanode/core';
import { Event } from '@/shared/types/events';
@@ -27,6 +29,7 @@ interface RootSynchronizers {
nodeReactions: Synchronizer<SyncNodeReactionsInput>;
nodeTombstones: Synchronizer<SyncNodeTombstonesInput>;
files: Synchronizer<SyncFilesInput>;
+ documentUpdates: Synchronizer<SyncDocumentUpdatesInput>;
}
type SyncHandlers = {
@@ -37,6 +40,7 @@ type SyncHandlers = {
nodeReactions: (data: SyncNodeReactionData) => Promise<void>;
nodeTombstones: (data: SyncNodeTombstoneData) => Promise<void>;
files: (data: SyncFileData) => Promise<void>;
+ documentUpdates: (data: SyncDocumentUpdateData) => Promise<void>;
};
export class SyncService {
@@ -73,6 +77,9 @@ export class SyncService {
this.workspace.nodes
),
files: this.workspace.files.syncServerFile.bind(this.workspace.files),
+ documentUpdates: this.workspace.documents.syncServerDocumentUpdate.bind(
+ this.workspace.documents
+ ),
};
eventBus.subscribe(this.handleEvent.bind(this));
}
@@ -143,6 +150,7 @@ export class SyncService {
rootSynchronizers.nodeReactions.destroy();
rootSynchronizers.nodeTombstones.destroy();
rootSynchronizers.files.destroy();
+ rootSynchronizers.documentUpdates.destroy();
}
private async initRootSynchronizers(rootId: string): Promise<void> {
@@ -185,6 +193,12 @@ export class SyncService {
`${rootId}_files`,
this.syncHandlers.files
),
+ documentUpdates: new Synchronizer(
+ this.workspace,
+ { type: 'document_updates', rootId },
+ `${rootId}_document_updates`,
+ this.syncHandlers.documentUpdates
+ ),
};
this.rootSynchronizers.set(rootId, rootSynchronizers);
diff --git a/apps/desktop/src/main/services/workspaces/workspace-service.ts b/apps/desktop/src/main/services/workspaces/workspace-service.ts
index 199ff092..2a737606 100644
--- a/apps/desktop/src/main/services/workspaces/workspace-service.ts
+++ b/apps/desktop/src/main/services/workspaces/workspace-service.ts
@@ -21,6 +21,7 @@ import { UserService } from '@/main/services/workspaces/user-service';
import { CollaborationService } from '@/main/services/workspaces/collaboration-service';
import { SyncService } from '@/main/services/workspaces/sync-service';
import { RadarService } from '@/main/services/workspaces/radar-service';
+import { DocumentService } from '@/main/services/workspaces/document-service';
import { eventBus } from '@/shared/lib/event-bus';
export class WorkspaceService {
@@ -30,6 +31,7 @@ export class WorkspaceService {
public readonly database: Kysely<WorkspaceDatabaseSchema>;
public readonly account: AccountService;
public readonly nodes: NodeService;
+ public readonly documents: DocumentService;
public readonly nodeInteractions: NodeInteractionService;
public readonly nodeReactions: NodeReactionService;
public readonly files: FileService;
@@ -67,6 +69,7 @@ export class WorkspaceService {
this.nodes = new NodeService(this);
this.nodeInteractions = new NodeInteractionService(this);
this.nodeReactions = new NodeReactionService(this);
+ this.documents = new DocumentService(this);
this.files = new FileService(this);
this.mutations = new MutationService(this);
this.users = new UserService(this);
diff --git a/apps/desktop/src/renderer/components/documents/document-editor.tsx b/apps/desktop/src/renderer/components/documents/document-editor.tsx
index 6cfff18a..4f6aba16 100644
--- a/apps/desktop/src/renderer/components/documents/document-editor.tsx
+++ b/apps/desktop/src/renderer/components/documents/document-editor.tsx
@@ -8,6 +8,8 @@ import {
} from '@tiptap/react';
import { debounce, isEqual } from 'lodash-es';
import React from 'react';
+import { YDoc } from '@colanode/crdt';
+import { RichTextContent, richTextContentSchema } from '@colanode/core';
import { useWorkspace } from '@/renderer/contexts/workspace';
import {
@@ -67,45 +69,83 @@ import { ToolbarMenu, ActionMenu } from '@/renderer/editor/menus';
import {
restoreRelativeSelection,
getRelativeSelection,
+ mapContentsToBlocks,
+ buildEditorContent,
} from '@/shared/lib/editor';
+import { LocalNode } from '@/shared/types/nodes';
+import { Document } from '@/shared/types/documents';
+import { toast } from '@/renderer/hooks/use-toast';
interface DocumentEditorProps {
- documentId: string;
- rootId: string;
- content: JSONContent;
- revision: bigint;
+ node: LocalNode;
+ document: Document | null | undefined;
canEdit: boolean;
- onUpdate: (before: JSONContent, after: JSONContent) => void;
autoFocus?: FocusPosition;
}
export const DocumentEditor = ({
- documentId,
- rootId,
- content,
- revision,
+ node,
+ document,
canEdit,
- onUpdate,
autoFocus,
}: DocumentEditorProps) => {
const workspace = useWorkspace();
const hasPendingChanges = React.useRef(false);
- const revisionRef = React.useRef(revision);
- const contentRef = React.useRef(content);
+ const revisionRef = React.useRef(document?.revision ?? 0);
+ const ydocRef = React.useRef<YDoc>(new YDoc(document?.state));
const debouncedSave = React.useMemo(
() =>
- debounce((content: JSONContent) => {
- const before = contentRef.current;
- const after = content;
+ debounce(async (content: JSONContent) => {
+ const beforeContent = ydocRef.current.getObject<RichTextContent>();
+ const beforeBlocks = beforeContent?.blocks;
+ const indexMap = new Map<string, string>();
+ if (beforeBlocks) {
+ for (const [key, value] of Object.entries(beforeBlocks)) {
+ indexMap.set(key, value.index);
+ }
+ }
+
+ const afterBlocks = mapContentsToBlocks(
+ node.id,
+ content.content ?? [],
+ indexMap
+ );
+
+ const afterContent: RichTextContent = {
+ type: 'rich_text',
+ blocks: afterBlocks,
+ };
+
+ const update = ydocRef.current.update(
+ richTextContentSchema,
+ afterContent
+ );
- contentRef.current = content;
hasPendingChanges.current = false;
- onUpdate(before, after);
+ if (!update) {
+ return;
+ }
+
+ const result = await window.colanode.executeMutation({
+ type: 'document_update',
+ accountId: workspace.accountId,
+ workspaceId: workspace.id,
+ documentId: node.id,
+ update,
+ });
+
+ if (!result.success) {
+ toast({
+ title: 'Failed to save changes',
+ description: result.error.message,
+ variant: 'destructive',
+ });
+ }
}, 500),
- [onUpdate]
+ [node.id]
);
const editor = useEditor(
@@ -119,8 +159,8 @@ export const DocumentEditor = ({
context: {
accountId: workspace.accountId,
workspaceId: workspace.id,
- documentId,
- rootId,
+ documentId: node.id,
+ rootId: node.rootId,
},
}),
TextNode,
@@ -165,10 +205,10 @@ export const DocumentEditor = ({
FolderCommand,
],
context: {
- documentId,
+ documentId: node.id,
accountId: workspace.accountId,
workspaceId: workspace.id,
- rootId,
+ rootId: node.rootId,
},
}),
BoldMark,
@@ -186,7 +226,10 @@ export const DocumentEditor = ({
spellCheck: 'false',
},
},
- content: content,
+ content: buildEditorContent(
+ node.id,
+ ydocRef.current.getObject<RichTextContent>()
+ ),
editable: canEdit,
shouldRerenderOnTransaction: false,
autofocus: autoFocus,
@@ -197,7 +240,7 @@ export const DocumentEditor = ({
}
},
},
- [documentId]
+ [node.id]
);
React.useEffect(() => {
@@ -205,28 +248,36 @@ export const DocumentEditor = ({
return;
}
+ if (!document) {
+ return;
+ }
+
if (hasPendingChanges.current) {
return;
}
- if (revisionRef.current === revision) {
+ if (revisionRef.current === document?.revision) {
return;
}
- if (isEqual(content, contentRef.current)) {
+ const beforeContent = ydocRef.current.getObject<RichTextContent>();
+ ydocRef.current.applyUpdate(document.state);
+ const afterContent = ydocRef.current.getObject<RichTextContent>();
+
+ if (isEqual(beforeContent, afterContent)) {
return;
}
- revisionRef.current = revision;
- contentRef.current = content;
+ const editorContent = buildEditorContent(node.id, afterContent);
+ revisionRef.current = document.revision;
const relativeSelection = getRelativeSelection(editor);
- editor.chain().setContent(content).run();
+ editor.chain().setContent(editorContent).run();
if (relativeSelection != null) {
restoreRelativeSelection(editor, relativeSelection);
}
- }, [content, revision]);
+ }, [document?.revision]);
return (
<div className="min-h-[500px]">
diff --git a/apps/desktop/src/renderer/components/documents/document.tsx b/apps/desktop/src/renderer/components/documents/document.tsx
index fb34dd15..784d6073 100644
--- a/apps/desktop/src/renderer/components/documents/document.tsx
+++ b/apps/desktop/src/renderer/components/documents/document.tsx
@@ -1,51 +1,36 @@
-import { Block } from '@colanode/core';
-import { FocusPosition, JSONContent } from '@tiptap/core';
+import { FocusPosition } from '@tiptap/core';
import { DocumentEditor } from '@/renderer/components/documents/document-editor';
-import { mapBlocksToContents } from '@/shared/lib/editor';
+import { LocalNode } from '@/shared/types/nodes';
+import { useQuery } from '@/renderer/hooks/use-query';
+import { useWorkspace } from '@/renderer/contexts/workspace';
interface DocumentProps {
- entryId: string;
- rootId: string;
- content?: Record<string, Block> | null;
- revision: bigint;
+ node: LocalNode;
canEdit: boolean;
- onUpdate: (before: JSONContent, after: JSONContent) => void;
autoFocus?: FocusPosition;
}
-export const Document = ({
- entryId,
- rootId,
- content,
- revision,
- canEdit,
- onUpdate,
- autoFocus,
-}: DocumentProps) => {
- const entryBlocks = Object.values(content ?? {});
- const contents = mapBlocksToContents(entryId, entryBlocks);
+export const Document = ({ node, canEdit, autoFocus }: DocumentProps) => {
+ const workspace = useWorkspace();
- if (!contents.length) {
- contents.push({
- type: 'paragraph',
- });
- }
+ const { data, isPending } = useQuery({
+ type: 'document_get',
+ documentId: node.id,
+ accountId: workspace.accountId,
+ workspaceId: workspace.id,
+ });
- const tiptapContent = {
- type: 'doc',
- content: contents,
- };
+ if (isPending) {
+ return null;
+ }
return (
<DocumentEditor
- key={entryId}
- documentId={entryId}
- rootId={rootId}
- content={tiptapContent}
- revision={revision}
+ key={node.id}
+ node={node}
+ document={data}
canEdit={canEdit}
- onUpdate={onUpdate}
autoFocus={autoFocus}
/>
);
diff --git a/apps/desktop/src/renderer/components/pages/page-body.tsx b/apps/desktop/src/renderer/components/pages/page-body.tsx
index b39f7494..edc14cea 100644
--- a/apps/desktop/src/renderer/components/pages/page-body.tsx
+++ b/apps/desktop/src/renderer/components/pages/page-body.tsx
@@ -1,11 +1,7 @@
-import { useCallback } from 'react';
import { NodeRole, hasNodeRole } from '@colanode/core';
-import { JSONContent } from '@tiptap/core';
import { Document } from '@/renderer/components/documents/document';
import { ScrollArea } from '@/renderer/components/ui/scroll-area';
-import { useWorkspace } from '@/renderer/contexts/workspace';
-import { toast } from '@/renderer/hooks/use-toast';
import { LocalPageNode } from '@/shared/types/nodes';
interface PageBodyProps {
@@ -14,42 +10,11 @@ interface PageBodyProps {
}
export const PageBody = ({ page, role }: PageBodyProps) => {
- const workspace = useWorkspace();
const canEdit = hasNodeRole(role, 'editor');
- const handleUpdate = useCallback(
- async (before: JSONContent, after: JSONContent) => {
- const result = await window.colanode.executeMutation({
- type: 'page_content_update',
- accountId: workspace.accountId,
- workspaceId: workspace.id,
- pageId: page.id,
- before,
- after,
- });
-
- if (!result.success) {
- toast({
- title: 'Failed to update page',
- description: result.error.message,
- variant: 'destructive',
- });
- }
- },
- [workspace.accountId, workspace.id, page.id]
- );
-
return (
<ScrollArea className="h-full max-h-full w-full overflow-y-auto">
- <Document
- entryId={page.id}
- rootId={page.rootId}
- content={page.attributes.content}
- revision={page.localRevision}
- canEdit={canEdit}
- onUpdate={handleUpdate}
- autoFocus="start"
- />
+ <Document node={page} canEdit={canEdit} autoFocus="start" />
</ScrollArea>
);
};
diff --git a/apps/desktop/src/renderer/components/records/record-body.tsx b/apps/desktop/src/renderer/components/records/record-body.tsx
index f99303f1..01b6ad33 100644
--- a/apps/desktop/src/renderer/components/records/record-body.tsx
+++ b/apps/desktop/src/renderer/components/records/record-body.tsx
@@ -1,6 +1,4 @@
import { NodeRole, hasNodeRole } from '@colanode/core';
-import { JSONContent } from '@tiptap/core';
-import { useCallback } from 'react';
import { Document } from '@/renderer/components/documents/document';
import { RecordAttributes } from '@/renderer/components/records/record-attributes';
@@ -8,7 +6,6 @@ import { RecordProvider } from '@/renderer/components/records/record-provider';
import { ScrollArea } from '@/renderer/components/ui/scroll-area';
import { Separator } from '@/renderer/components/ui/separator';
import { useWorkspace } from '@/renderer/contexts/workspace';
-import { toast } from '@/renderer/hooks/use-toast';
import { RecordDatabase } from '@/renderer/components/records/record-database';
import { LocalRecordNode } from '@/shared/types/nodes';
@@ -22,29 +19,6 @@ export const RecordBody = ({ record, role }: RecordBodyProps) => {
const canEdit =
record.createdBy === workspace.userId || hasNodeRole(role, 'editor');
-
- const handleUpdate = useCallback(
- async (before: JSONContent, after: JSONContent) => {
- const result = await window.colanode.executeMutation({
- type: 'record_content_update',
- accountId: workspace.accountId,
- workspaceId: workspace.id,
- recordId: record.id,
- before,
- after,
- });
-
- if (!result.success) {
- toast({
- title: 'Failed to update page',
- description: result.error.message,
- variant: 'destructive',
- });
- }
- },
- [workspace.accountId, workspace.id, record.id]
- );
-
return (
<RecordDatabase id={record.attributes.databaseId} role={role}>
<ScrollArea className="h-full max-h-full w-full overflow-y-auto">
@@ -52,15 +26,7 @@ export const RecordBody = ({ record, role }: RecordBodyProps) => {
<RecordAttributes />
</RecordProvider>
<Separator className="my-4 w-full" />
- <Document
- entryId={record.id}
- rootId={record.rootId}
- content={record.attributes.content}
- revision={record.localRevision}
- canEdit={canEdit}
- onUpdate={handleUpdate}
- autoFocus={false}
- />
+ <Document node={record} canEdit={canEdit} autoFocus={false} />
</ScrollArea>
</RecordDatabase>
);
diff --git a/apps/desktop/src/shared/lib/editor.ts b/apps/desktop/src/shared/lib/editor.ts
index 7e26bb30..0661aec3 100644
--- a/apps/desktop/src/shared/lib/editor.ts
+++ b/apps/desktop/src/shared/lib/editor.ts
@@ -6,6 +6,7 @@ import {
generateId,
generateNodeIndex,
IdType,
+ RichTextContent,
} from '@colanode/core';
import { Editor, JSONContent } from '@tiptap/core';
import { Node as ProseMirrorNode, ResolvedPos } from '@tiptap/pm/model';
@@ -24,11 +25,20 @@ export const mapContentsToBlocks = (
parentId: string,
contents: JSONContent[],
indexMap: Map<string, string>
-): Block[] => {
+): Record<string, Block> => {
const blocks: Block[] = [];
mapAndPushContentsToBlocks(contents, parentId, blocks, indexMap);
validateBlocksIndexes(blocks);
- return blocks;
+
+ const blocksRecord: Record<string, Block> = blocks.reduce(
+ (acc, block) => {
+ acc[block.id] = block;
+ return acc;
+ },
+ {} as Record<string, Block>
+ );
+
+ return blocksRecord;
};
const mapAndPushContentsToBlocks = (
@@ -109,6 +119,29 @@ const mapContentsToBlockLeafs = (
return nodeBlocks;
};
+export const buildEditorContent = (
+ documentId: string,
+ content: RichTextContent | null | undefined
+): JSONContent => {
+ const blocks = content && content.blocks ? Object.values(content.blocks) : [];
+ const contents = mapBlocksToContents(documentId, blocks);
+
+ if (contents.length === 0) {
+ contents.push({
+ type: 'paragraph',
+ content: [],
+ attrs: {
+ id: generateId(IdType.Block),
+ },
+ });
+ }
+
+ return {
+ type: 'doc',
+ content: contents,
+ };
+};
+
export const mapBlocksToContents = (
parentId: string,
blocks: Block[]
diff --git a/apps/desktop/src/shared/mutations/documents/document-update.ts b/apps/desktop/src/shared/mutations/documents/document-update.ts
new file mode 100644
index 00000000..a8a6ee85
--- /dev/null
+++ b/apps/desktop/src/shared/mutations/documents/document-update.ts
@@ -0,0 +1,20 @@
+export type DocumentUpdateMutationInput = {
+ type: 'document_update';
+ accountId: string;
+ workspaceId: string;
+ documentId: string;
+ update: Uint8Array;
+};
+
+export type DocumentUpdateMutationOutput = {
+ success: boolean;
+};
+
+declare module '@/shared/mutations' {
+ interface MutationMap {
+ document_update: {
+ input: DocumentUpdateMutationInput;
+ output: DocumentUpdateMutationOutput;
+ };
+ }
+}
diff --git a/apps/desktop/src/shared/mutations/pages/page-content-update.ts b/apps/desktop/src/shared/mutations/pages/page-content-update.ts
deleted file mode 100644
index 6fbbe508..00000000
--- a/apps/desktop/src/shared/mutations/pages/page-content-update.ts
+++ /dev/null
@@ -1,23 +0,0 @@
-import { JSONContent } from '@tiptap/core';
-
-export type PageContentUpdateMutationInput = {
- type: 'page_content_update';
- accountId: string;
- workspaceId: string;
- pageId: string;
- before: JSONContent;
- after: JSONContent;
-};
-
-export type PageContentUpdateMutationOutput = {
- success: boolean;
-};
-
-declare module '@/shared/mutations' {
- interface MutationMap {
- page_content_update: {
- input: PageContentUpdateMutationInput;
- output: PageContentUpdateMutationOutput;
- };
- }
-}
diff --git a/apps/desktop/src/shared/mutations/records/record-content-update.ts b/apps/desktop/src/shared/mutations/records/record-content-update.ts
deleted file mode 100644
index 5587d115..00000000
--- a/apps/desktop/src/shared/mutations/records/record-content-update.ts
+++ /dev/null
@@ -1,23 +0,0 @@
-import { JSONContent } from '@tiptap/core';
-
-export type RecordContentUpdateMutationInput = {
- type: 'record_content_update';
- accountId: string;
- workspaceId: string;
- recordId: string;
- before: JSONContent;
- after: JSONContent;
-};
-
-export type RecordContentUpdateMutationOutput = {
- success: boolean;
-};
-
-declare module '@/shared/mutations' {
- interface MutationMap {
- record_content_update: {
- input: RecordContentUpdateMutationInput;
- output: RecordContentUpdateMutationOutput;
- };
- }
-}
diff --git a/apps/desktop/src/shared/queries/documents/document-get.ts b/apps/desktop/src/shared/queries/documents/document-get.ts
new file mode 100644
index 00000000..2fd137c3
--- /dev/null
+++ b/apps/desktop/src/shared/queries/documents/document-get.ts
@@ -0,0 +1,17 @@
+import { Document } from '@/shared/types/documents';
+
+export type DocumentGetQueryInput = {
+ type: 'document_get';
+ documentId: string;
+ accountId: string;
+ workspaceId: string;
+};
+
+declare module '@/shared/queries' {
+ interface QueryMap {
+ document_get: {
+ input: DocumentGetQueryInput;
+ output: Document | null;
+ };
+ }
+}
diff --git a/apps/desktop/src/shared/types/documents.ts b/apps/desktop/src/shared/types/documents.ts
new file mode 100644
index 00000000..fb1a33f6
--- /dev/null
+++ b/apps/desktop/src/shared/types/documents.ts
@@ -0,0 +1,9 @@
+export type Document = {
+ id: string;
+ revision: bigint;
+ state: Uint8Array;
+ createdAt: string;
+ createdBy: string;
+ updatedAt: string | null;
+ updatedBy: string | null;
+};
diff --git a/apps/desktop/src/shared/types/events.ts b/apps/desktop/src/shared/types/events.ts
index 3cb9bd62..12ff7684 100644
--- a/apps/desktop/src/shared/types/events.ts
+++ b/apps/desktop/src/shared/types/events.ts
@@ -202,6 +202,21 @@ export type WorkspaceMetadataDeletedEvent = {
metadata: WorkspaceMetadata;
};
+export type DocumentUpdatedEvent = {
+ type: 'document_updated';
+ accountId: string;
+ workspaceId: string;
+ documentId: string;
+};
+
+export type DocumentUpdateCreatedEvent = {
+ type: 'document_update_created';
+ accountId: string;
+ workspaceId: string;
+ documentId: string;
+ updateId: string;
+};
+
export type Event =
| UserCreatedEvent
| UserUpdatedEvent
@@ -234,4 +249,6 @@ export type Event =
| AppMetadataUpdatedEvent
| AppMetadataDeletedEvent
| WorkspaceMetadataUpdatedEvent
- | WorkspaceMetadataDeletedEvent;
+ | WorkspaceMetadataDeletedEvent
+ | DocumentUpdatedEvent
+ | DocumentUpdateCreatedEvent;
diff --git a/apps/server/src/api/client/controllers/workspaces/mutations/mutations-sync.ts b/apps/server/src/api/client/controllers/workspaces/mutations/mutations-sync.ts
index 0a599915..74f2f5d5 100644
--- a/apps/server/src/api/client/controllers/workspaces/mutations/mutations-sync.ts
+++ b/apps/server/src/api/client/controllers/workspaces/mutations/mutations-sync.ts
@@ -4,19 +4,26 @@ import {
SyncMutationResult,
SyncMutationStatus,
Mutation,
- ApplyNodeTransactionMutation,
+ CreateNodeMutation,
+ DeleteNodeMutation,
CreateNodeReactionMutation,
DeleteNodeReactionMutation,
MarkNodeSeenMutation,
MarkNodeOpenedMutation,
- DeleteNodeMutation,
+ UpdateNodeMutation,
+ UpdateDocumentMutation,
} from '@colanode/core';
import { SelectUser } from '@/data/schema';
import { ResponseBuilder } from '@/lib/response-builder';
-import { applyNodeTransaction, deleteNode } from '@/lib/nodes';
+import {
+ createNodeFromMutation,
+ updateNodeFromMutation,
+ deleteNode,
+} from '@/lib/nodes';
import { createNodeReaction, deleteNodeReaction } from '@/lib/node-reactions';
import { markNodeAsOpened, markNodeAsSeen } from '@/lib/node-interactions';
+import { updateDocumentFromMutation } from '@/lib/documents';
export const mutationsSyncHandler = async (
req: Request,
@@ -48,8 +55,10 @@ const handleMutation = async (
user: SelectUser,
mutation: Mutation
): Promise<SyncMutationStatus> => {
- if (mutation.type === 'apply_node_transaction') {
- return await handleApplyNodeTransaction(user, mutation);
+ if (mutation.type === 'create_node') {
+ return await handleCreateNode(user, mutation);
+ } else if (mutation.type === 'update_node') {
+ return await handleUpdateNode(user, mutation);
} else if (mutation.type === 'delete_node') {
return await handleDeleteNode(user, mutation);
} else if (mutation.type === 'create_node_reaction') {
@@ -60,22 +69,31 @@ const handleMutation = async (
return await handleMarkNodeSeen(user, mutation);
} else if (mutation.type === 'mark_node_opened') {
return await handleMarkNodeOpened(user, mutation);
+ } else if (mutation.type === 'update_document') {
+ return await handleUpdateDocument(user, mutation);
} else {
return 'error';
}
};
-const handleApplyNodeTransaction = async (
+const handleCreateNode = async (
user: SelectUser,
- mutation: ApplyNodeTransactionMutation
+ mutation: CreateNodeMutation
): Promise<SyncMutationStatus> => {
- const output = await applyNodeTransaction(user, {
- id: mutation.data.id,
- nodeId: mutation.data.nodeId,
- data: mutation.data.data,
- createdAt: new Date(mutation.data.createdAt),
- operation: mutation.data.operation,
- });
+ const output = await createNodeFromMutation(user, mutation.data);
+
+ if (!output) {
+ return 'error';
+ }
+
+ return 'success';
+};
+
+const handleUpdateNode = async (
+ user: SelectUser,
+ mutation: UpdateNodeMutation
+): Promise<SyncMutationStatus> => {
+ const output = await updateNodeFromMutation(user, mutation.data);
if (!output) {
return 'error';
@@ -132,3 +150,11 @@ const handleMarkNodeOpened = async (
const output = await markNodeAsOpened(user, mutation);
return output ? 'success' : 'error';
};
+
+const handleUpdateDocument = async (
+ user: SelectUser,
+ mutation: UpdateDocumentMutation
+): Promise<SyncMutationStatus> => {
+ const output = await updateDocumentFromMutation(user, mutation.data);
+ return output ? 'success' : 'error';
+};
diff --git a/apps/server/src/data/migrations/00009-create-node-tombstones-table.ts b/apps/server/src/data/migrations/00008-create-node-tombstones-table.ts
similarity index 100%
rename from apps/server/src/data/migrations/00009-create-node-tombstones-table.ts
rename to apps/server/src/data/migrations/00008-create-node-tombstones-table.ts
diff --git a/apps/server/src/data/migrations/00010-create-node-paths-table.ts b/apps/server/src/data/migrations/00009-create-node-paths-table.ts
similarity index 100%
rename from apps/server/src/data/migrations/00010-create-node-paths-table.ts
rename to apps/server/src/data/migrations/00009-create-node-paths-table.ts
diff --git a/apps/server/src/data/migrations/00011-create-collaborations-table.ts b/apps/server/src/data/migrations/00010-create-collaborations-table.ts
similarity index 100%
rename from apps/server/src/data/migrations/00011-create-collaborations-table.ts
rename to apps/server/src/data/migrations/00010-create-collaborations-table.ts
diff --git a/apps/server/src/data/migrations/00012-create-files-table.ts b/apps/server/src/data/migrations/00011-create-files-table.ts
similarity index 100%
rename from apps/server/src/data/migrations/00012-create-files-table.ts
rename to apps/server/src/data/migrations/00011-create-files-table.ts
diff --git a/apps/server/src/data/migrations/00012-create-documents-table.ts b/apps/server/src/data/migrations/00012-create-documents-table.ts
new file mode 100644
index 00000000..082f1f89
--- /dev/null
+++ b/apps/server/src/data/migrations/00012-create-documents-table.ts
@@ -0,0 +1,21 @@
+import { Migration } from 'kysely';
+
+export const createDocumentsTable: Migration = {
+ up: async (db) => {
+ await db.schema
+ .createTable('documents')
+ .addColumn('id', 'varchar(30)', (col) => col.notNull().primaryKey())
+ .addColumn('type', 'varchar(30)', (col) => col.notNull())
+ .addColumn('workspace_id', 'varchar(30)', (col) => col.notNull())
+ .addColumn('revision', 'bigint', (col) => col.notNull())
+ .addColumn('content', 'jsonb', (col) => col.notNull())
+ .addColumn('created_at', 'timestamptz', (col) => col.notNull())
+ .addColumn('created_by', 'varchar(30)', (col) => col.notNull())
+ .addColumn('updated_at', 'timestamptz')
+ .addColumn('updated_by', 'varchar(30)')
+ .execute();
+ },
+ down: async (db) => {
+ await db.schema.dropTable('documents').execute();
+ },
+};
diff --git a/apps/server/src/data/migrations/00013-create-document-updates-table.ts b/apps/server/src/data/migrations/00013-create-document-updates-table.ts
new file mode 100644
index 00000000..94c5d381
--- /dev/null
+++ b/apps/server/src/data/migrations/00013-create-document-updates-table.ts
@@ -0,0 +1,68 @@
+import { sql, Migration } from 'kysely';
+
+export const createDocumentUpdatesTable: Migration = {
+ up: async (db) => {
+ await sql`
+ CREATE SEQUENCE IF NOT EXISTS document_updates_revision_sequence
+ START WITH 1000000000
+ INCREMENT BY 1
+ NO MINVALUE
+ NO MAXVALUE
+ CACHE 1;
+ `.execute(db);
+
+ await db.schema
+ .createTable('document_updates')
+ .addColumn('id', 'varchar(30)', (col) => col.notNull().primaryKey())
+ .addColumn('document_id', 'varchar(30)', (col) => col.notNull())
+ .addColumn('root_id', 'varchar(30)', (col) => col.notNull())
+ .addColumn('workspace_id', 'varchar(30)', (col) => col.notNull())
+ .addColumn('revision', 'bigint', (col) =>
+ col
+ .notNull()
+ .defaultTo(sql`nextval('document_updates_revision_sequence')`)
+ )
+ .addColumn('data', 'bytea', (col) => col.notNull())
+ .addColumn('merged_updates', 'jsonb')
+ .addColumn('created_at', 'timestamptz', (col) => col.notNull())
+ .addColumn('created_by', 'varchar(30)', (col) => col.notNull())
+ .execute();
+
+ await sql`
+ CREATE OR REPLACE FUNCTION update_document_update_revision() RETURNS TRIGGER AS $$
+ BEGIN
+ NEW.revision = nextval('document_updates_revision_sequence');
+ RETURN NEW;
+ END;
+ $$ LANGUAGE plpgsql;
+
+ CREATE TRIGGER trg_update_document_update_revision
+ BEFORE UPDATE ON document_updates
+ FOR EACH ROW
+ EXECUTE FUNCTION update_document_update_revision();
+ `.execute(db);
+
+ await db.schema
+ .createIndex('document_updates_root_id_revision_idx')
+ .on('document_updates')
+ .columns(['root_id', 'revision'])
+ .execute();
+
+ await db.schema
+ .createIndex('document_updates_document_id_idx')
+ .on('document_updates')
+ .columns(['document_id'])
+ .execute();
+ },
+ down: async (db) => {
+ await sql`
+ DROP TRIGGER IF EXISTS trg_update_document_update_revision ON document_updates;
+ DROP FUNCTION IF EXISTS update_document_update_revision();
+ `.execute(db);
+
+ await db.schema.dropTable('document_updates').execute();
+ await sql`DROP SEQUENCE IF EXISTS document_updates_revision_sequence`.execute(
+ db
+ );
+ },
+};
diff --git a/apps/server/src/data/migrations/index.ts b/apps/server/src/data/migrations/index.ts
index 16144070..4380a778 100644
--- a/apps/server/src/data/migrations/index.ts
+++ b/apps/server/src/data/migrations/index.ts
@@ -7,10 +7,12 @@ import { createUsersTable } from './00004-create-users-table';
import { createNodesTable } from './00005-create-nodes-table';
import { createNodeReactionsTable } from './00006-create-node-reactions-table';
import { createNodeInteractionsTable } from './00007-create-node-interactions-table';
-import { createNodeTombstonesTable } from './00009-create-node-tombstones-table';
-import { createNodePathsTable } from './00010-create-node-paths-table';
-import { createCollaborationsTable } from './00011-create-collaborations-table';
-import { createFilesTable } from './00012-create-files-table';
+import { createNodeTombstonesTable } from './00008-create-node-tombstones-table';
+import { createNodePathsTable } from './00009-create-node-paths-table';
+import { createCollaborationsTable } from './00010-create-collaborations-table';
+import { createFilesTable } from './00011-create-files-table';
+import { createDocumentsTable } from './00012-create-documents-table';
+import { createDocumentUpdatesTable } from './00013-create-document-updates-table';
export const databaseMigrations: Record<string, Migration> = {
'00001_create_accounts_table': createAccountsTable,
@@ -18,10 +20,12 @@ export const databaseMigrations: Record<string, Migration> = {
'00003_create_workspaces_table': createWorkspacesTable,
'00004_create_users_table': createUsersTable,
'00005_create_nodes_table': createNodesTable,
- '00006_create_node_interactions_table': createNodeInteractionsTable,
- '00007_create_node_reactions_table': createNodeReactionsTable,
+ '00006_create_node_reactions_table': createNodeReactionsTable,
+ '00007_create_node_interactions_table': createNodeInteractionsTable,
'00008_create_node_tombstones_table': createNodeTombstonesTable,
'00009_create_node_paths_table': createNodePathsTable,
'00010_create_collaborations_table': createCollaborationsTable,
'00011_create_files_table': createFilesTable,
+ '00012_create_documents_table': createDocumentsTable,
+ '00013_create_document_updates_table': createDocumentUpdatesTable,
};
diff --git a/apps/server/src/data/schema.ts b/apps/server/src/data/schema.ts
index 67e50cdd..fd78f2de 100644
--- a/apps/server/src/data/schema.ts
+++ b/apps/server/src/data/schema.ts
@@ -6,6 +6,9 @@ import {
NodeType,
WorkspaceRole,
UserStatus,
+ DocumentType,
+ DocumentContent,
+ DocumentUpdateMergeMetadata,
} from '@colanode/core';
import {
ColumnType,
@@ -207,6 +210,42 @@ export type SelectFile = Selectable<FileTable>;
export type CreateFile = Insertable<FileTable>;
export type UpdateFile = Updateable<FileTable>;
+interface DocumentTable {
+ id: ColumnType<string, string, never>;
+ type: ColumnType<DocumentType, DocumentType, DocumentType>;
+ workspace_id: ColumnType<string, string, never>;
+ revision: ColumnType<bigint, bigint, bigint>;
+ content: JSONColumnType<DocumentContent, string, string>;
+ created_at: ColumnType<Date, Date, never>;
+ created_by: ColumnType<string, string, never>;
+ updated_at: ColumnType<Date | null, Date | null, Date>;
+ updated_by: ColumnType<string | null, string | null, string>;
+}
+
+export type SelectDocument = Selectable<DocumentTable>;
+export type CreateDocument = Insertable<DocumentTable>;
+export type UpdateDocument = Updateable<DocumentTable>;
+
+interface DocumentUpdateTable {
+ id: ColumnType<string, string, never>;
+ document_id: ColumnType<string, string, never>;
+ root_id: ColumnType<string, string, never>;
+ workspace_id: ColumnType<string, string, never>;
+ revision: ColumnType<bigint, never, never>;
+ data: ColumnType<Uint8Array, Uint8Array, Uint8Array>;
+ created_at: ColumnType<Date, Date, never>;
+ created_by: ColumnType<string, string, never>;
+ merged_updates: ColumnType<
+ DocumentUpdateMergeMetadata[] | null,
+ string | null,
+ string | null
+ >;
+}
+
+export type SelectDocumentUpdate = Selectable<DocumentUpdateTable>;
+export type CreateDocumentUpdate = Insertable<DocumentUpdateTable>;
+export type UpdateDocumentUpdate = Updateable<DocumentUpdateTable>;
+
export interface DatabaseSchema {
accounts: AccountTable;
devices: DeviceTable;
@@ -219,4 +258,6 @@ export interface DatabaseSchema {
node_tombstones: NodeTombstoneTable;
collaborations: CollaborationTable;
files: FileTable;
+ documents: DocumentTable;
+ document_updates: DocumentUpdateTable;
}
diff --git a/apps/server/src/lib/documents.ts b/apps/server/src/lib/documents.ts
new file mode 100644
index 00000000..299cdb93
--- /dev/null
+++ b/apps/server/src/lib/documents.ts
@@ -0,0 +1,183 @@
+import {
+ createDebugger,
+ DocumentContent,
+ UpdateDocumentMutationData,
+} from '@colanode/core';
+import { decodeState, YDoc } from '@colanode/crdt';
+
+import { database } from '@/data/database';
+import { SelectUser } from '@/data/schema';
+import { ConcurrentUpdateResult, UpdateDocumentOutput } from '@/types/nodes';
+import { eventBus } from '@/lib/event-bus';
+import { fetchNode } from '@/lib/nodes';
+
+const debug = createDebugger('server:lib:documents');
+
+const UPDATE_RETRIES_LIMIT = 10;
+
+export const updateDocumentFromMutation = async (
+ user: SelectUser,
+ mutation: UpdateDocumentMutationData
+): Promise<UpdateDocumentOutput | null> => {
+ for (let count = 0; count < UPDATE_RETRIES_LIMIT; count++) {
+ const result = await tryUpdateDocumentFromMutation(user, mutation);
+
+ if (result.type === 'success') {
+ return result.output;
+ }
+
+ if (result.type === 'error') {
+ return null;
+ }
+ }
+
+ return null;
+};
+
+const tryUpdateDocumentFromMutation = async (
+ user: SelectUser,
+ mutation: UpdateDocumentMutationData
+): Promise<ConcurrentUpdateResult<UpdateDocumentOutput>> => {
+ const node = await fetchNode(mutation.documentId);
+ if (!node) {
+ return { type: 'error', output: null };
+ }
+
+ const root = await fetchNode(node.root_id);
+ if (!root) {
+ return { type: 'error', output: null };
+ }
+
+ const document = await database
+ .selectFrom('documents')
+ .where('id', '=', mutation.documentId)
+ .selectAll()
+ .executeTakeFirst();
+
+ const documentUpdates = await database
+ .selectFrom('document_updates')
+ .where('document_id', '=', mutation.documentId)
+ .selectAll()
+ .execute();
+
+ const ydoc = new YDoc();
+ for (const update of documentUpdates) {
+ ydoc.applyUpdate(update.data);
+ }
+
+ ydoc.applyUpdate(mutation.data);
+ const content = ydoc.getObject<DocumentContent>();
+
+ try {
+ const { updatedDocument, createdDocumentUpdate } = await database
+ .transaction()
+ .execute(async (trx) => {
+ if (document) {
+ const createdDocumentUpdate = await trx
+ .insertInto('document_updates')
+ .returningAll()
+ .values({
+ id: mutation.updateId,
+ document_id: mutation.documentId,
+ workspace_id: user.workspace_id,
+ root_id: node.root_id,
+ data: decodeState(mutation.data),
+ created_at: new Date(mutation.createdAt),
+ created_by: user.id,
+ merged_updates: null,
+ })
+ .executeTakeFirst();
+
+ if (!createdDocumentUpdate) {
+ throw new Error('Failed to create document update');
+ }
+
+ const updatedDocument = await trx
+ .updateTable('documents')
+ .returningAll()
+ .set({
+ content: JSON.stringify(content),
+ updated_at: new Date(mutation.createdAt),
+ updated_by: user.id,
+ revision: createdDocumentUpdate.revision,
+ })
+ .where('id', '=', mutation.documentId)
+ .where('revision', '=', document.revision)
+ .executeTakeFirst();
+
+ if (!updatedDocument) {
+ throw new Error('Failed to update document');
+ }
+
+ return { updatedDocument, createdDocumentUpdate };
+ }
+
+ const createdDocumentUpdate = await trx
+ .insertInto('document_updates')
+ .returningAll()
+ .values({
+ id: mutation.updateId,
+ document_id: mutation.documentId,
+ root_id: node.root_id,
+ workspace_id: user.workspace_id,
+ data: decodeState(mutation.data),
+ created_at: new Date(mutation.createdAt),
+ created_by: user.id,
+ merged_updates: null,
+ })
+ .executeTakeFirst();
+
+ if (!createdDocumentUpdate) {
+ throw new Error('Failed to create document update');
+ }
+
+ const updatedDocument = await trx
+ .insertInto('documents')
+ .returningAll()
+ .values({
+ id: mutation.documentId,
+ workspace_id: user.workspace_id,
+ type: 'rich_text',
+ content: JSON.stringify(content),
+ created_at: new Date(mutation.createdAt),
+ created_by: user.id,
+ revision: createdDocumentUpdate.revision,
+ })
+ .onConflict((cb) => cb.doNothing())
+ .executeTakeFirst();
+
+ if (!updatedDocument) {
+ throw new Error('Failed to create document');
+ }
+
+ return { updatedDocument, createdDocumentUpdate };
+ });
+
+ if (!updatedDocument || !createdDocumentUpdate) {
+ throw new Error('Failed to update document');
+ }
+
+ eventBus.publish({
+ type: 'document_updated',
+ documentId: mutation.documentId,
+ workspaceId: user.workspace_id,
+ });
+
+ eventBus.publish({
+ type: 'document_update_created',
+ documentId: mutation.documentId,
+ rootId: node.root_id,
+ workspaceId: user.workspace_id,
+ });
+
+ return {
+ type: 'success',
+ output: {
+ update: createdDocumentUpdate,
+ },
+ };
+ } catch (error) {
+ debug(`Failed to update document: ${error}`);
+ return { type: 'retry', output: null };
+ }
+};
diff --git a/apps/server/src/lib/nodes.ts b/apps/server/src/lib/nodes.ts
index 6b3ba80f..759680d7 100644
--- a/apps/server/src/lib/nodes.ts
+++ b/apps/server/src/lib/nodes.ts
@@ -3,11 +3,12 @@ import {
canDeleteNode,
canUpdateNode,
createDebugger,
+ CreateNodeMutationData,
extractNodeCollaborators,
+ getNodeModel,
Node,
NodeAttributes,
- nodeAttributesSchema,
- TransactionOperation,
+ UpdateNodeMutationData,
} from '@colanode/core';
import { YDoc } from '@colanode/crdt';
import { cloneDeep } from 'lodash-es';
@@ -20,8 +21,7 @@ import {
SelectUser,
} from '@/data/schema';
import {
- ApplyNodeTransactionInput,
- ApplyNodeTransactionOutput,
+ ConcurrentUpdateResult,
CreateNodeInput,
CreateNodeOutput,
DeleteNodeInput,
@@ -40,11 +40,6 @@ const debug = createDebugger('server:lib:nodes');
const UPDATE_RETRIES_LIMIT = 10;
-type UpdateResult<T> = {
- type: 'success' | 'error' | 'retry';
- output: T | null;
-};
-
export const mapNode = (node: SelectNode): Node => {
return {
id: node.id,
@@ -99,14 +94,15 @@ export const fetchNodeDescendants = async (
export const createNode = async (
input: CreateNodeInput
): Promise<CreateNodeOutput | null> => {
+ const model = getNodeModel(input.attributes.type);
const ydoc = new YDoc();
- const update = ydoc.updateAttributes(nodeAttributesSchema, input.attributes);
+ const update = ydoc.update(model.attributesSchema, input.attributes);
if (!update) {
return null;
}
- const attributes = ydoc.getAttributes<NodeAttributes>();
+ const attributes = ydoc.getObject<NodeAttributes>();
const attributesJson = JSON.stringify(attributes);
const state = ydoc.getState();
const date = new Date();
@@ -204,26 +200,27 @@ export const updateNode = async (
export const tryUpdateNode = async (
input: UpdateNodeInput
-): Promise<UpdateResult<UpdateNodeOutput>> => {
+): Promise<ConcurrentUpdateResult<UpdateNodeOutput>> => {
const node = await fetchNode(input.nodeId);
if (!node) {
return { type: 'error', output: null };
}
+ const model = getNodeModel(node.type);
const ydoc = new YDoc(node.state);
- const currentAttributes = ydoc.getAttributes<NodeAttributes>();
+ const currentAttributes = ydoc.getObject<NodeAttributes>();
const updatedAttributes = input.updater(cloneDeep(currentAttributes));
if (!updatedAttributes) {
return { type: 'error', output: null };
}
- const update = ydoc.updateAttributes(nodeAttributesSchema, updatedAttributes);
+ const update = ydoc.update(model.attributesSchema, updatedAttributes);
if (!update) {
return { type: 'success', output: null };
}
- const attributes = ydoc.getAttributes<NodeAttributes>();
+ const attributes = ydoc.getObject<NodeAttributes>();
const attributesJson = JSON.stringify(attributes);
const date = new Date();
@@ -306,29 +303,12 @@ export const tryUpdateNode = async (
}
};
-export const applyNodeTransaction = async (
- user: SelectUser,
- input: ApplyNodeTransactionInput
-): Promise<ApplyNodeTransactionOutput | null> => {
- if (input.operation === TransactionOperation.Create) {
- return applyNodeCreateTransaction(user, input);
- }
-
- if (input.operation === TransactionOperation.Update) {
- return applyNodeUpdateTransaction(user, input);
- }
-
- return null;
-};
-
-const applyNodeCreateTransaction = async (
+export const createNodeFromMutation = async (
user: SelectUser,
- input: ApplyNodeTransactionInput
-): Promise<ApplyNodeTransactionOutput | null> => {
- const ydoc = new YDoc();
- ydoc.applyUpdate(input.data);
-
- const attributes = ydoc.getAttributes<NodeAttributes>();
+ mutation: CreateNodeMutationData
+): Promise<CreateNodeOutput | null> => {
+ const ydoc = new YDoc(mutation.data);
+ const attributes = ydoc.getObject<NodeAttributes>();
let root: SelectNode | null = null;
if (attributes.type !== 'space' && attributes.type !== 'chat') {
@@ -356,11 +336,11 @@ const applyNodeCreateTransaction = async (
}
const createNode: CreateNode = {
- id: input.nodeId,
- root_id: root?.id ?? input.nodeId,
+ id: mutation.id,
+ root_id: root?.id ?? mutation.id,
attributes: JSON.stringify(attributes),
workspace_id: user.workspace_id,
- created_at: input.createdAt,
+ created_at: new Date(mutation.createdAt),
created_by: user.id,
state: ydoc.getState(),
};
@@ -369,7 +349,7 @@ const applyNodeCreateTransaction = async (
extractNodeCollaborators(attributes)
).map(([userId, role]) => ({
collaborator_id: userId,
- node_id: input.nodeId,
+ node_id: mutation.id,
workspace_id: user.workspace_id,
role,
created_at: new Date(),
@@ -405,8 +385,8 @@ const applyNodeCreateTransaction = async (
eventBus.publish({
type: 'node_created',
- nodeId: input.nodeId,
- rootId: root?.id ?? input.nodeId,
+ nodeId: mutation.id,
+ rootId: root?.id ?? mutation.id,
workspaceId: user.workspace_id,
});
@@ -414,7 +394,7 @@ const applyNodeCreateTransaction = async (
eventBus.publish({
type: 'collaboration_created',
collaboratorId: createdCollaboration.collaborator_id,
- nodeId: input.nodeId,
+ nodeId: mutation.id,
workspaceId: user.workspace_id,
});
}
@@ -428,12 +408,12 @@ const applyNodeCreateTransaction = async (
}
};
-const applyNodeUpdateTransaction = async (
+export const updateNodeFromMutation = async (
user: SelectUser,
- input: ApplyNodeTransactionInput
-): Promise<ApplyNodeTransactionOutput | null> => {
+ mutation: UpdateNodeMutationData
+): Promise<UpdateNodeOutput | null> => {
for (let count = 0; count < UPDATE_RETRIES_LIMIT; count++) {
- const result = await tryApplyNodeUpdateTransaction(user, input);
+ const result = await tryUpdateNodeFromMutation(user, mutation);
if (result.type === 'success') {
return result.output;
@@ -447,11 +427,11 @@ const applyNodeUpdateTransaction = async (
return null;
};
-const tryApplyNodeUpdateTransaction = async (
+const tryUpdateNodeFromMutation = async (
user: SelectUser,
- input: ApplyNodeTransactionInput
-): Promise<UpdateResult<ApplyNodeTransactionOutput>> => {
- const node = await fetchNode(input.nodeId);
+ mutation: UpdateNodeMutationData
+): Promise<ConcurrentUpdateResult<UpdateNodeOutput>> => {
+ const node = await fetchNode(mutation.id);
if (!node) {
return { type: 'error', output: null };
}
@@ -462,9 +442,9 @@ const tryApplyNodeUpdateTransaction = async (
}
const ydoc = new YDoc(node.state);
- ydoc.applyUpdate(input.data);
+ ydoc.applyUpdate(mutation.data);
- const attributes = ydoc.getAttributes<NodeAttributes>();
+ const attributes = ydoc.getObject<NodeAttributes>();
const attributesJson = JSON.stringify(attributes);
if (
@@ -496,11 +476,11 @@ const tryApplyNodeUpdateTransaction = async (
.returningAll()
.set({
attributes: attributesJson,
- updated_at: input.createdAt,
+ updated_at: new Date(mutation.createdAt),
updated_by: user.id,
state: ydoc.getState(),
})
- .where('id', '=', input.nodeId)
+ .where('id', '=', mutation.id)
.where('revision', '=', node.revision)
.executeTakeFirst();
@@ -511,7 +491,7 @@ const tryApplyNodeUpdateTransaction = async (
const { createdCollaborations, updatedCollaborations } =
await applyCollaboratorUpdates(
trx,
- input.nodeId,
+ mutation.id,
user.id,
user.workspace_id,
collaboratorChanges
@@ -526,7 +506,7 @@ const tryApplyNodeUpdateTransaction = async (
eventBus.publish({
type: 'node_updated',
- nodeId: input.nodeId,
+ nodeId: mutation.id,
rootId: root.id,
workspaceId: user.workspace_id,
});
@@ -535,7 +515,7 @@ const tryApplyNodeUpdateTransaction = async (
eventBus.publish({
type: 'collaboration_created',
collaboratorId: createdCollaboration.collaborator_id,
- nodeId: input.nodeId,
+ nodeId: mutation.id,
workspaceId: user.workspace_id,
});
}
@@ -544,7 +524,7 @@ const tryApplyNodeUpdateTransaction = async (
eventBus.publish({
type: 'collaboration_updated',
collaboratorId: updatedCollaboration.collaborator_id,
- nodeId: input.nodeId,
+ nodeId: mutation.id,
workspaceId: user.workspace_id,
});
}
diff --git a/apps/server/src/lib/workspaces.ts b/apps/server/src/lib/workspaces.ts
index 21e858de..8cc3bcaa 100644
--- a/apps/server/src/lib/workspaces.ts
+++ b/apps/server/src/lib/workspaces.ts
@@ -11,10 +11,7 @@ import { SelectAccount } from '@/data/schema';
import { database } from '@/data/database';
import { configuration } from '@/lib/configuration';
import { eventBus } from '@/lib/event-bus';
-import {
- generateWelcomePageBlocks,
- generateInitialMessageBlocks,
-} from '@/lib/blocks';
+import { generateInitialMessageBlocks } from '@/lib/blocks';
import { createNode } from '@/lib/nodes';
export const createWorkspace = async (
@@ -92,7 +89,6 @@ export const createWorkspace = async (
type: 'page',
name: 'Welcome',
parentId: spaceId,
- content: generateWelcomePageBlocks(pageId),
},
userId: userId,
workspaceId: workspaceId,
diff --git a/apps/server/src/services/socket-connection.ts b/apps/server/src/services/socket-connection.ts
index c69a7c7b..9f482539 100644
--- a/apps/server/src/services/socket-connection.ts
+++ b/apps/server/src/services/socket-connection.ts
@@ -28,6 +28,7 @@ import { NodeReactionSynchronizer } from '@/synchronizers/node-reactions';
import { NodeTombstoneSynchronizer } from '@/synchronizers/node-tombstones';
import { NodeInteractionSynchronizer } from '@/synchronizers/node-interactions';
import { FileSynchronizer } from '@/synchronizers/files';
+import { DocumentUpdateSynchronizer } from '@/synchronizers/document-updates';
type SocketUser = {
user: ConnectedUser;
@@ -183,6 +184,17 @@ export class SocketConnection {
message.input,
cursor
);
+ } else if (message.input.type === 'document_updates') {
+ if (!user.rootIds.has(message.input.rootId)) {
+ return null;
+ }
+
+ return new DocumentUpdateSynchronizer(
+ message.id,
+ user.user,
+ message.input,
+ cursor
+ );
}
return null;
diff --git a/apps/server/src/synchronizers/document-updates.ts b/apps/server/src/synchronizers/document-updates.ts
new file mode 100644
index 00000000..5c389adb
--- /dev/null
+++ b/apps/server/src/synchronizers/document-updates.ts
@@ -0,0 +1,95 @@
+import {
+ SynchronizerOutputMessage,
+ SyncDocumentUpdatesInput,
+ SyncDocumentUpdateData,
+} from '@colanode/core';
+import { encodeState } from '@colanode/crdt';
+
+import { BaseSynchronizer } from '@/synchronizers/base';
+import { Event } from '@/types/events';
+import { database } from '@/data/database';
+import { SelectDocumentUpdate } from '@/data/schema';
+
+export class DocumentUpdateSynchronizer extends BaseSynchronizer<SyncDocumentUpdatesInput> {
+ public async fetchData(): Promise<SynchronizerOutputMessage<SyncDocumentUpdatesInput> | null> {
+ const documentUpdates = await this.fetchDocumentUpdates();
+ if (documentUpdates.length === 0) {
+ return null;
+ }
+
+ return this.buildMessage(documentUpdates);
+ }
+
+ public async fetchDataFromEvent(
+ event: Event
+ ): Promise<SynchronizerOutputMessage<SyncDocumentUpdatesInput> | null> {
+ if (!this.shouldFetch(event)) {
+ return null;
+ }
+
+ const documentUpdates = await this.fetchDocumentUpdates();
+ if (documentUpdates.length === 0) {
+ return null;
+ }
+
+ return this.buildMessage(documentUpdates);
+ }
+
+ private async fetchDocumentUpdates() {
+ if (this.status === 'fetching') {
+ return [];
+ }
+
+ this.status = 'fetching';
+ const documentUpdates = await database
+ .selectFrom('document_updates')
+ .selectAll()
+ .where('root_id', '=', this.input.rootId)
+ .where('revision', '>', this.cursor)
+ .orderBy('revision', 'asc')
+ .limit(20)
+ .execute();
+
+ this.status = 'pending';
+ return documentUpdates;
+ }
+
+ private buildMessage(
+ unsyncedDocumentUpdates: SelectDocumentUpdate[]
+ ): SynchronizerOutputMessage<SyncDocumentUpdatesInput> {
+ const items: SyncDocumentUpdateData[] = unsyncedDocumentUpdates.map(
+ (documentUpdate) => ({
+ id: documentUpdate.id,
+ documentId: documentUpdate.document_id,
+ rootId: documentUpdate.root_id,
+ workspaceId: documentUpdate.workspace_id,
+ revision: documentUpdate.revision.toString(),
+ data: encodeState(documentUpdate.data),
+ createdAt: documentUpdate.created_at.toISOString(),
+ createdBy: documentUpdate.created_by,
+ mergedUpdates: documentUpdate.merged_updates,
+ })
+ );
+
+ return {
+ type: 'synchronizer_output',
+ userId: this.user.userId,
+ id: this.id,
+ items: items.map((item) => ({
+ cursor: item.revision,
+ data: item,
+ })),
+ };
+ }
+
+ private shouldFetch(event: Event) {
+ if (
+ event.type === 'document_update_created' &&
+ event.rootId === this.input.rootId
+ ) {
+ return true;
+ }
+
+ return false;
+ }
+}
diff --git a/apps/server/src/types/events.ts b/apps/server/src/types/events.ts
index f6be2dc9..a80b1f9d 100644
--- a/apps/server/src/types/events.ts
+++ b/apps/server/src/types/events.ts
@@ -126,6 +126,19 @@ export type DeviceDeletedEvent = {
deviceId: string;
};
+export type DocumentUpdatedEvent = {
+ type: 'document_updated';
+ documentId: string;
+ workspaceId: string;
+};
+
+export type DocumentUpdateCreatedEvent = {
+ type: 'document_update_created';
+ documentId: string;
+ rootId: string;
+ workspaceId: string;
+};
+
export type Event =
| NodeCreatedEvent
| NodeUpdatedEvent
@@ -145,4 +158,6 @@ export type Event =
| FileCreatedEvent
| FileUpdatedEvent
| FileDeletedEvent
- | FileInteractionUpdatedEvent;
+ | FileInteractionUpdatedEvent
+ | DocumentUpdatedEvent
+ | DocumentUpdateCreatedEvent;
diff --git a/apps/server/src/types/nodes.ts b/apps/server/src/types/nodes.ts
index 8d32f03e..3e357519 100644
--- a/apps/server/src/types/nodes.ts
+++ b/apps/server/src/types/nodes.ts
@@ -1,6 +1,6 @@
-import { NodeAttributes, TransactionOperation } from '@colanode/core';
+import { NodeAttributes } from '@colanode/core';
-import { SelectNode } from '@/data/schema';
+import { SelectDocumentUpdate, SelectNode } from '@/data/schema';
export type NodeCollaborator = {
nodeId: string;
@@ -31,18 +31,6 @@ export type UpdateNodeOutput = {
node: SelectNode;
};
-export type ApplyNodeTransactionInput = {
- id: string;
- nodeId: string;
- operation: TransactionOperation;
- data: string | Uint8Array;
- createdAt: Date;
-};
-
-export type ApplyNodeTransactionOutput = {
- node: SelectNode;
-};
-
export type DeleteNodeInput = {
id: string;
rootId: string;
@@ -52,3 +40,12 @@ export type DeleteNodeInput = {
export type DeleteNodeOutput = {
node: SelectNode;
};
+
+export type UpdateDocumentOutput = {
+ update: SelectDocumentUpdate;
+};
+
+export type ConcurrentUpdateResult<T> = {
+ type: 'success' | 'error' | 'retry';
+ output: T | null;
+};
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 517305a6..287c2069 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -1,30 +1,29 @@
export * from './lib/constants';
export * from './lib/files';
export * from './lib/id';
-export * from './lib/interactions';
export * from './lib/utils';
export * from './registry/block';
-export * from './registry/channel';
-export * from './registry/chat';
-export * from './registry/core';
-export * from './registry/database';
-export * from './registry/database-view';
-export * from './registry/field';
-export * from './registry/field-value';
-export * from './registry/folder';
-export * from './registry/index';
-export * from './registry/message';
-export * from './registry/page';
-export * from './registry/record';
-export * from './registry/space';
-export * from './registry/file';
+export * from './registry/nodes/channel';
+export * from './registry/nodes/chat';
+export * from './registry/nodes/core';
+export * from './registry/nodes/database';
+export * from './registry/nodes/database-view';
+export * from './registry/nodes/field';
+export * from './registry/nodes/field-value';
+export * from './registry/nodes/folder';
+export * from './registry/nodes/index';
+export * from './registry/nodes/message';
+export * from './registry/nodes/page';
+export * from './registry/nodes/record';
+export * from './registry/nodes/space';
+export * from './registry/nodes/file';
+export * from './registry/documents/index';
+export * from './registry/documents/rich-text';
export * from './registry/zod';
export * from './types/accounts';
export * from './types/files';
-export * from './types/interactions';
export * from './types/sockets';
export * from './types/servers';
-export * from './types/transactions';
export * from './types/workspaces';
export * from './types/mutations';
export * from './synchronizers';
@@ -33,3 +32,4 @@ export * from './lib/texts';
export * from './lib/permissions';
export * from './types/api';
export * from './lib/debugger';
+export * from './types/documents';
diff --git a/packages/core/src/lib/id.ts b/packages/core/src/lib/id.ts
index 27428173..f2e7884e 100644
--- a/packages/core/src/lib/id.ts
+++ b/packages/core/src/lib/id.ts
@@ -32,7 +32,7 @@ export enum IdType {
FilePlaceholder = 'fp',
Device = 'de',
Upload = 'up',
- Transaction = 'tx',
+ Update = 'ud',
Event = 'ev',
Host = 'ht',
Block = 'bl',
diff --git a/packages/core/src/lib/interactions.ts b/packages/core/src/lib/interactions.ts
deleted file mode 100644
index 8e337ef4..00000000
--- a/packages/core/src/lib/interactions.ts
+++ /dev/null
@@ -1,78 +0,0 @@
-import { compareDate, compareString } from './utils';
-
-import {
- InteractionAttribute,
- InteractionAttributes,
-} from '../types/interactions';
-
-export const mergeInteractionAttributes = (
- attributes: InteractionAttributes | null | undefined,
- attribute: InteractionAttribute,
- value: string
-): InteractionAttributes | null => {
- if (!attributes) {
- return { [attribute]: value };
- }
-
- if (attribute === 'firstSeenAt') {
- const date = new Date(value);
-
- if (
- !attributes.firstSeenAt ||
- compareDate(attributes.firstSeenAt, date) > 0
- ) {
- return { ...attributes, firstSeenAt: date };
- }
- }
-
- if (attribute === 'lastSeenAt') {
- const date = new Date(value);
-
- if (
- !attributes.lastSeenAt ||
- compareDate(attributes.lastSeenAt, date) < 0
- ) {
- return { ...attributes, lastSeenAt: date };
- }
- }
-
- if (attribute === 'lastReceivedTransactionId') {
- if (
- !attributes.lastReceivedTransactionId ||
- compareString(attributes.lastReceivedTransactionId, value) < 0
- ) {
- return { ...attributes, lastReceivedTransactionId: value };
- }
- }
-
- if (attribute === 'lastSeenTransactionId') {
- if (
- !attributes.lastSeenTransactionId ||
- compareString(attributes.lastSeenTransactionId, value) < 0
- ) {
- return { ...attributes, lastSeenTransactionId: value };
- }
- }
-
- if (attribute === 'lastOpenedAt') {
- const date = new Date(value);
-
- if (
- !attributes.lastOpenedAt ||
- compareDate(attributes.lastOpenedAt, date) < 0
- ) {
- return { ...attributes, lastOpenedAt: date };
- }
- }
-
- if (attribute === 'lastOpenedTransactionId') {
- if (
- !attributes.lastOpenedTransactionId ||
- compareString(attributes.lastOpenedTransactionId, value) < 0
- ) {
- return { ...attributes, lastOpenedTransactionId: value };
- }
- }
-
- return null;
-};
diff --git a/packages/core/src/lib/texts.ts b/packages/core/src/lib/texts.ts
index 868efbd6..fcb75bac 100644
--- a/packages/core/src/lib/texts.ts
+++ b/packages/core/src/lib/texts.ts
@@ -1,5 +1,5 @@
import { Block } from '../registry/block';
-import { NodeAttributes } from '../registry';
+import { NodeAttributes } from '../registry/nodes';
export type TextResult = {
id: string;
@@ -11,21 +11,21 @@ export const extractNodeText = (
id: string,
attributes: NodeAttributes
): TextResult | undefined => {
- if (attributes.type === 'page') {
- return {
- id,
- name: attributes.name,
- text: extractBlockTexts(id, attributes.content),
- };
- }
+ // if (attributes.type === 'page') {
+ // return {
+ // id,
+ // name: attributes.name,
+ // text: extractBlockTexts(id, attributes.content),
+ // };
+ // }
- if (attributes.type === 'record') {
- return {
- id,
- name: attributes.name,
- text: extractBlockTexts(id, attributes.content),
- };
- }
+ // if (attributes.type === 'record') {
+ // return {
+ // id,
+ // name: attributes.name,
+ // text: extractBlockTexts(id, attributes.content),
+ // };
+ // }
return undefined;
};
diff --git a/packages/core/src/registry/channel.ts b/packages/core/src/registry/channel.ts
deleted file mode 100644
index c5651292..00000000
--- a/packages/core/src/registry/channel.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-import { z } from 'zod';
-
-export const channelAttributesSchema = z.object({
- type: z.literal('channel'),
- name: z.string(),
- avatar: z.string().nullable().optional(),
- parentId: z.string(),
-});
-
-export type ChannelAttributes = z.infer<typeof channelAttributesSchema>;
diff --git a/packages/core/src/registry/chat.ts b/packages/core/src/registry/chat.ts
deleted file mode 100644
index 99c07d0e..00000000
--- a/packages/core/src/registry/chat.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-import { z } from 'zod';
-
-import { nodeRoleEnum } from './core';
-
-export const chatAttributesSchema = z.object({
- type: z.literal('chat'),
- collaborators: z.record(z.string(), nodeRoleEnum),
-});
-
-export type ChatAttributes = z.infer<typeof chatAttributesSchema>;
diff --git a/packages/core/src/registry/core.ts b/packages/core/src/registry/core.ts
deleted file mode 100644
index f2fc747c..00000000
--- a/packages/core/src/registry/core.ts
+++ /dev/null
@@ -1,4 +0,0 @@
-import { z } from 'zod';
-
-export type NodeRole = 'admin' | 'editor' | 'commenter' | 'viewer';
-export const nodeRoleEnum = z.enum(['admin', 'editor', 'commenter', 'viewer']);
diff --git a/packages/core/src/registry/database.ts b/packages/core/src/registry/database.ts
deleted file mode 100644
index 795236eb..00000000
--- a/packages/core/src/registry/database.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-import { z } from 'zod';
-
-import { fieldAttributesSchema } from './field';
-
-export const databaseAttributesSchema = z.object({
- type: z.literal('database'),
- name: z.string(),
- avatar: z.string().nullable().optional(),
- parentId: z.string(),
- fields: z.record(z.string(), fieldAttributesSchema),
-});
-
-export type DatabaseAttributes = z.infer<typeof databaseAttributesSchema>;
diff --git a/packages/core/src/registry/documents/index.ts b/packages/core/src/registry/documents/index.ts
new file mode 100644
index 00000000..7c25f62a
--- /dev/null
+++ b/packages/core/src/registry/documents/index.ts
@@ -0,0 +1,10 @@
+import { z } from 'zod';
+
+import { richTextContentSchema, RichTextContent } from './rich-text';
+
+export const documentContentSchema = z.discriminatedUnion('type', [
+ richTextContentSchema,
+]);
+
+export type DocumentContent = RichTextContent;
+export type DocumentType = DocumentContent['type'];
diff --git a/packages/core/src/registry/documents/rich-text.ts b/packages/core/src/registry/documents/rich-text.ts
new file mode 100644
index 00000000..24a099ec
--- /dev/null
+++ b/packages/core/src/registry/documents/rich-text.ts
@@ -0,0 +1,10 @@
+import { z } from 'zod';
+
+import { blockSchema } from '../block';
+
+export const richTextContentSchema = z.object({
+ type: z.literal('rich_text'),
+ blocks: z.record(blockSchema),
+});
+
+export type RichTextContent = z.infer<typeof richTextContentSchema>;
diff --git a/packages/core/src/registry/file.ts b/packages/core/src/registry/file.ts
deleted file mode 100644
index 4862273c..00000000
--- a/packages/core/src/registry/file.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-import { z } from 'zod';
-
-export const fileAttributesSchema = z.object({
- type: z.literal('file'),
- name: z.string().optional(),
- parentId: z.string(),
- index: z.string().optional(),
-});
-
-export type FileAttributes = z.infer<typeof fileAttributesSchema>;
diff --git a/packages/core/src/registry/folder.ts b/packages/core/src/registry/folder.ts
deleted file mode 100644
index 5b4e2a7b..00000000
--- a/packages/core/src/registry/folder.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-import { z } from 'zod';
-
-export const folderAttributesSchema = z.object({
- type: z.literal('folder'),
- name: z.string(),
- avatar: z.string().nullable().optional(),
- parentId: z.string(),
-});
-
-export type FolderAttributes = z.infer<typeof folderAttributesSchema>;
diff --git a/packages/core/src/registry/message.ts b/packages/core/src/registry/message.ts
deleted file mode 100644
index 32b004d0..00000000
--- a/packages/core/src/registry/message.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import { z } from 'zod';
-
-import { blockSchema } from './block';
-
-export const messageAttributesSchema = z.object({
- type: z.literal('message'),
- subtype: z.enum(['standard']),
- name: z.string().optional(),
- parentId: z.string(),
- referenceId: z.string().nullable().optional(),
- content: z.record(blockSchema).optional().nullable(),
-});
-
-export type MessageAttributes = z.infer<typeof messageAttributesSchema>;
diff --git a/packages/core/src/registry/nodes/channel.ts b/packages/core/src/registry/nodes/channel.ts
new file mode 100644
index 00000000..e91985f4
--- /dev/null
+++ b/packages/core/src/registry/nodes/channel.ts
@@ -0,0 +1,41 @@
+import { z } from 'zod';
+
+import { NodeModel } from './core';
+
+import { NodeAttributes } from '.';
+
+export const channelAttributesSchema = z.object({
+ type: z.literal('channel'),
+ name: z.string(),
+ avatar: z.string().nullable().optional(),
+ parentId: z.string(),
+});
+
+export type ChannelAttributes = z.infer<typeof channelAttributesSchema>;
+
+export const channelModel: NodeModel = {
+ type: 'channel',
+ attributesSchema: channelAttributesSchema,
+ canCreate: async (context, _) => {
+ return context.hasEditorAccess();
+ },
+ canUpdate: async (context, _) => {
+ return context.hasEditorAccess();
+ },
+ canDelete: async (context, _) => {
+ return context.hasAdminAccess();
+ },
+ getName: function (
+ _: string,
+ attributes: NodeAttributes
+ ): string | null | undefined {
+ if (attributes.type !== 'channel') {
+ return null;
+ }
+
+ return attributes.name;
+ },
+ getText: function (_: string, __: NodeAttributes): string | null | undefined {
+ return undefined;
+ },
+};
diff --git a/packages/core/src/registry/nodes/chat.ts b/packages/core/src/registry/nodes/chat.ts
new file mode 100644
index 00000000..707d5f22
--- /dev/null
+++ b/packages/core/src/registry/nodes/chat.ts
@@ -0,0 +1,32 @@
+import { z } from 'zod';
+
+import { NodeModel, nodeRoleEnum } from './core';
+
+import { NodeAttributes } from '.';
+
+export const chatAttributesSchema = z.object({
+ type: z.literal('chat'),
+ collaborators: z.record(z.string(), nodeRoleEnum),
+});
+
+export type ChatAttributes = z.infer<typeof chatAttributesSchema>;
+
+export const chatModel: NodeModel = {
+ type: 'chat',
+ attributesSchema: chatAttributesSchema,
+ canCreate: async (_, __) => {
+ return true;
+ },
+ canUpdate: async (_, __) => {
+ return false;
+ },
+ canDelete: async (_, __) => {
+ return false;
+ },
+ getName: function (_: string, __: NodeAttributes): string | null | undefined {
+ return undefined;
+ },
+ getText: function (_: string, __: NodeAttributes): string | null | undefined {
+ return undefined;
+ },
+};
diff --git a/packages/core/src/registry/nodes/core.ts b/packages/core/src/registry/nodes/core.ts
new file mode 100644
index 00000000..3c7e3060
--- /dev/null
+++ b/packages/core/src/registry/nodes/core.ts
@@ -0,0 +1,69 @@
+import { z, ZodSchema } from 'zod';
+
+import { WorkspaceRole } from '../../types/workspaces';
+import { hasNodeRole } from '../../lib/permissions';
+import { extractNodeRole } from '../../lib/nodes';
+
+import { Node, NodeAttributes } from '.';
+
+export type NodeRole = 'admin' | 'editor' | 'commenter' | 'viewer';
+export const nodeRoleEnum = z.enum(['admin', 'editor', 'commenter', 'viewer']);
+
+export interface NodeMutationUser {
+ id: string;
+ role: WorkspaceRole;
+ workspaceId: string;
+ accountId: string;
+}
+
+export class NodeMutationContext {
+ public readonly user: NodeMutationUser;
+ public readonly root: Node | null;
+ public readonly role: NodeRole | null;
+
+ constructor(user: NodeMutationUser, root: Node | null) {
+ this.user = user;
+ this.root = root;
+ this.role = root ? extractNodeRole(root, user.id) : null;
+ }
+
+ public hasAdminAccess = () => {
+ return this.role ? hasNodeRole(this.role, 'admin') : false;
+ };
+
+ public hasEditorAccess = () => {
+ return this.role ? hasNodeRole(this.role, 'editor') : false;
+ };
+
+ public hasCollaboratorAccess = () => {
+ return this.role ? hasNodeRole(this.role, 'commenter') : false;
+ };
+
+ public hasViewerAccess = () => {
+ return this.role ? hasNodeRole(this.role, 'viewer') : false;
+ };
+}
+
+export interface NodeModel {
+ type: string;
+ attributesSchema: ZodSchema;
+ documentSchema?: ZodSchema;
+ canCreate: (
+ context: NodeMutationContext,
+ attributes: NodeAttributes
+ ) => Promise<boolean>;
+ canUpdate: (
+ context: NodeMutationContext,
+ node: Node,
+ attributes: NodeAttributes
+ ) => Promise<boolean>;
+ canDelete: (context: NodeMutationContext, node: Node) => Promise<boolean>;
+ getName: (
+ id: string,
+ attributes: NodeAttributes
+ ) => string | null | undefined;
+ getText: (
+ id: string,
+ attributes: NodeAttributes
+ ) => string | null | undefined;
+}
diff --git a/packages/core/src/registry/database-view.ts b/packages/core/src/registry/nodes/database-view.ts
similarity index 75%
rename from packages/core/src/registry/database-view.ts
rename to packages/core/src/registry/nodes/database-view.ts
index 64da6e13..0447405e 100644
--- a/packages/core/src/registry/database-view.ts
+++ b/packages/core/src/registry/nodes/database-view.ts
@@ -1,5 +1,9 @@
import { z } from 'zod';
+import { NodeModel } from './core';
+
+import { NodeAttributes } from '.';
+
export const databaseViewFieldAttributesSchema = z.object({
id: z.string(),
width: z.number().nullable().optional(),
@@ -83,3 +87,30 @@ export type DatabaseViewAttributes = z.infer<
typeof databaseViewAttributesSchema
>;
export type DatabaseViewLayout = 'table' | 'board' | 'calendar';
+
+export const databaseViewModel: NodeModel = {
+ type: 'database_view',
+ attributesSchema: databaseViewAttributesSchema,
+ canCreate: async (context, _) => {
+ return context.hasEditorAccess();
+ },
+ canUpdate: async (context, _) => {
+ return context.hasEditorAccess();
+ },
+ canDelete: async (context, _) => {
+ return context.hasEditorAccess();
+ },
+ getName: function (
+ _: string,
+ attributes: NodeAttributes
+ ): string | null | undefined {
+ if (attributes.type !== 'database_view') {
+ return null;
+ }
+
+ return attributes.name;
+ },
+ getText: function (_: string, __: NodeAttributes): string | null | undefined {
+ return undefined;
+ },
+};
diff --git a/packages/core/src/registry/nodes/database.ts b/packages/core/src/registry/nodes/database.ts
new file mode 100644
index 00000000..aafffa05
--- /dev/null
+++ b/packages/core/src/registry/nodes/database.ts
@@ -0,0 +1,43 @@
+import { z } from 'zod';
+
+import { fieldAttributesSchema } from './field';
+import { NodeModel } from './core';
+
+import { NodeAttributes } from '.';
+
+export const databaseAttributesSchema = z.object({
+ type: z.literal('database'),
+ name: z.string(),
+ avatar: z.string().nullable().optional(),
+ parentId: z.string(),
+ fields: z.record(z.string(), fieldAttributesSchema),
+});
+
+export type DatabaseAttributes = z.infer<typeof databaseAttributesSchema>;
+
+export const databaseModel: NodeModel = {
+ type: 'database',
+ attributesSchema: databaseAttributesSchema,
+ canCreate: async (context, _) => {
+ return context.hasEditorAccess();
+ },
+ canUpdate: async (context, _) => {
+ return context.hasEditorAccess();
+ },
+ canDelete: async (context, _) => {
+ return context.hasEditorAccess();
+ },
+ getName: function (
+ _: string,
+ attributes: NodeAttributes
+ ): string | null | undefined {
+ if (attributes.type !== 'database') {
+ return null;
+ }
+
+ return attributes.name;
+ },
+ getText: function (_: string, __: NodeAttributes): string | null | undefined {
+ return undefined;
+ },
+};
diff --git a/packages/core/src/registry/field-value.ts b/packages/core/src/registry/nodes/field-value.ts
similarity index 100%
rename from packages/core/src/registry/field-value.ts
rename to packages/core/src/registry/nodes/field-value.ts
diff --git a/packages/core/src/registry/field.ts b/packages/core/src/registry/nodes/field.ts
similarity index 100%
rename from packages/core/src/registry/field.ts
rename to packages/core/src/registry/nodes/field.ts
diff --git a/packages/core/src/registry/nodes/file.ts b/packages/core/src/registry/nodes/file.ts
new file mode 100644
index 00000000..c7c9720b
--- /dev/null
+++ b/packages/core/src/registry/nodes/file.ts
@@ -0,0 +1,41 @@
+import { z } from 'zod';
+
+import { NodeModel } from './core';
+
+import { NodeAttributes } from '.';
+
+export const fileAttributesSchema = z.object({
+ type: z.literal('file'),
+ name: z.string().optional(),
+ parentId: z.string(),
+ index: z.string().optional(),
+});
+
+export type FileAttributes = z.infer<typeof fileAttributesSchema>;
+
+export const fileModel: NodeModel = {
+ type: 'file',
+ attributesSchema: fileAttributesSchema,
+ canCreate: async (context, _) => {
+ return context.hasEditorAccess();
+ },
+ canUpdate: async (context, _) => {
+ return context.hasEditorAccess();
+ },
+ canDelete: async (context, _) => {
+ return context.hasEditorAccess();
+ },
+ getName: function (
+ _: string,
+ attributes: NodeAttributes
+ ): string | null | undefined {
+ if (attributes.type !== 'file') {
+ return null;
+ }
+
+ return attributes.name;
+ },
+ getText: function (_: string, __: NodeAttributes): string | null | undefined {
+ return undefined;
+ },
+};
diff --git a/packages/core/src/registry/nodes/folder.ts b/packages/core/src/registry/nodes/folder.ts
new file mode 100644
index 00000000..2a9e4978
--- /dev/null
+++ b/packages/core/src/registry/nodes/folder.ts
@@ -0,0 +1,41 @@
+import { z } from 'zod';
+
+import { NodeModel } from './core';
+
+import { NodeAttributes } from '.';
+
+export const folderAttributesSchema = z.object({
+ type: z.literal('folder'),
+ name: z.string(),
+ avatar: z.string().nullable().optional(),
+ parentId: z.string(),
+});
+
+export type FolderAttributes = z.infer<typeof folderAttributesSchema>;
+
+export const folderModel: NodeModel = {
+ type: 'folder',
+ attributesSchema: folderAttributesSchema,
+ canCreate: async (context, _) => {
+ return context.hasEditorAccess();
+ },
+ canUpdate: async (context, _) => {
+ return context.hasEditorAccess();
+ },
+ canDelete: async (context, _) => {
+ return context.hasEditorAccess();
+ },
+ getName: function (
+ _: string,
+ attributes: NodeAttributes
+ ): string | null | undefined {
+ if (attributes.type !== 'folder') {
+ return null;
+ }
+
+ return attributes.name;
+ },
+ getText: function (_: string, __: NodeAttributes): string | null | undefined {
+ return undefined;
+ },
+};
diff --git a/packages/core/src/registry/index.ts b/packages/core/src/registry/nodes/index.ts
similarity index 58%
rename from packages/core/src/registry/index.ts
rename to packages/core/src/registry/nodes/index.ts
index 783210e9..97624e7b 100644
--- a/packages/core/src/registry/index.ts
+++ b/packages/core/src/registry/nodes/index.ts
@@ -1,18 +1,13 @@
-import { z } from 'zod';
-
-import { ChannelAttributes, channelAttributesSchema } from './channel';
-import { ChatAttributes, chatAttributesSchema } from './chat';
-import { DatabaseAttributes, databaseAttributesSchema } from './database';
-import { FolderAttributes, folderAttributesSchema } from './folder';
-import { PageAttributes, pageAttributesSchema } from './page';
-import { RecordAttributes, recordAttributesSchema } from './record';
-import { SpaceAttributes, spaceAttributesSchema } from './space';
-import { MessageAttributes, messageAttributesSchema } from './message';
-import {
- DatabaseViewAttributes,
- databaseViewAttributesSchema,
-} from './database-view';
-import { FileAttributes, fileAttributesSchema } from './file';
+import { ChannelAttributes, channelModel } from './channel';
+import { ChatAttributes, chatModel } from './chat';
+import { DatabaseAttributes, databaseModel } from './database';
+import { FolderAttributes, folderModel } from './folder';
+import { PageAttributes, pageModel } from './page';
+import { RecordAttributes, recordModel } from './record';
+import { SpaceAttributes, spaceModel } from './space';
+import { MessageAttributes, messageModel } from './message';
+import { DatabaseViewAttributes, databaseViewModel } from './database-view';
+import { FileAttributes, fileModel } from './file';
type NodeBase = {
id: string;
@@ -100,15 +95,27 @@ export type Node =
| MessageNode
| FileNode;
-export const nodeAttributesSchema = z.discriminatedUnion('type', [
- channelAttributesSchema,
- chatAttributesSchema,
- databaseAttributesSchema,
- databaseViewAttributesSchema,
- fileAttributesSchema,
- folderAttributesSchema,
- messageAttributesSchema,
- pageAttributesSchema,
- recordAttributesSchema,
- spaceAttributesSchema,
-]);
+export const getNodeModel = (type: NodeType) => {
+ switch (type) {
+ case 'channel':
+ return channelModel;
+ case 'chat':
+ return chatModel;
+ case 'database':
+ return databaseModel;
+ case 'database_view':
+ return databaseViewModel;
+ case 'folder':
+ return folderModel;
+ case 'page':
+ return pageModel;
+ case 'record':
+ return recordModel;
+ case 'space':
+ return spaceModel;
+ case 'message':
+ return messageModel;
+ case 'file':
+ return fileModel;
+ }
+};
diff --git a/packages/core/src/registry/nodes/message.ts b/packages/core/src/registry/nodes/message.ts
new file mode 100644
index 00000000..342108f3
--- /dev/null
+++ b/packages/core/src/registry/nodes/message.ts
@@ -0,0 +1,45 @@
+import { z } from 'zod';
+
+import { NodeModel } from './core';
+
+import { blockSchema } from '../block';
+
+import { NodeAttributes } from '.';
+
+export const messageAttributesSchema = z.object({
+ type: z.literal('message'),
+ subtype: z.enum(['standard']),
+ name: z.string().optional(),
+ parentId: z.string(),
+ referenceId: z.string().nullable().optional(),
+ content: z.record(blockSchema).optional().nullable(),
+});
+
+export type MessageAttributes = z.infer<typeof messageAttributesSchema>;
+
+export const messageModel: NodeModel = {
+ type: 'message',
+ attributesSchema: messageAttributesSchema,
+ canCreate: async (context, _) => {
+ return context.hasCollaboratorAccess();
+ },
+ canUpdate: async (context, _) => {
+ return context.hasCollaboratorAccess();
+ },
+ canDelete: async (context, _) => {
+ return context.hasCollaboratorAccess();
+ },
+ getName: function (
+ _: string,
+ attributes: NodeAttributes
+ ): string | null | undefined {
+ if (attributes.type !== 'message') {
+ return null;
+ }
+
+ return attributes.name;
+ },
+ getText: function (_: string, __: NodeAttributes): string | null | undefined {
+ return undefined;
+ },
+};
diff --git a/packages/core/src/registry/nodes/page.ts b/packages/core/src/registry/nodes/page.ts
new file mode 100644
index 00000000..d8ac99c1
--- /dev/null
+++ b/packages/core/src/registry/nodes/page.ts
@@ -0,0 +1,44 @@
+import { z } from 'zod';
+
+import { NodeModel } from './core';
+
+import { richTextContentSchema } from '../documents/rich-text';
+
+import { NodeAttributes } from '.';
+
+export const pageAttributesSchema = z.object({
+ type: z.literal('page'),
+ name: z.string(),
+ avatar: z.string().nullable().optional(),
+ parentId: z.string(),
+});
+
+export type PageAttributes = z.infer<typeof pageAttributesSchema>;
+
+export const pageModel: NodeModel = {
+ type: 'page',
+ attributesSchema: pageAttributesSchema,
+ documentSchema: richTextContentSchema,
+ canCreate: async (context, _) => {
+ return context.hasEditorAccess();
+ },
+ canUpdate: async (context, _) => {
+ return context.hasEditorAccess();
+ },
+ canDelete: async (context, _) => {
+ return context.hasEditorAccess();
+ },
+ getName: function (
+ _: string,
+ attributes: NodeAttributes
+ ): string | null | undefined {
+ if (attributes.type !== 'page') {
+ return null;
+ }
+
+ return attributes.name;
+ },
+ getText: function (_: string, __: NodeAttributes): string | null | undefined {
+ return undefined;
+ },
+};
diff --git a/packages/core/src/registry/nodes/record.ts b/packages/core/src/registry/nodes/record.ts
new file mode 100644
index 00000000..34023d91
--- /dev/null
+++ b/packages/core/src/registry/nodes/record.ts
@@ -0,0 +1,47 @@
+import { z } from 'zod';
+
+import { fieldValueSchema } from './field-value';
+import { NodeModel } from './core';
+
+import { richTextContentSchema } from '../documents/rich-text';
+
+import { NodeAttributes } from '.';
+
+export const recordAttributesSchema = z.object({
+ type: z.literal('record'),
+ parentId: z.string(),
+ databaseId: z.string(),
+ name: z.string(),
+ avatar: z.string().nullable().optional(),
+ fields: z.record(z.string(), fieldValueSchema),
+});
+
+export type RecordAttributes = z.infer<typeof recordAttributesSchema>;
+
+export const recordModel: NodeModel = {
+ type: 'record',
+ attributesSchema: recordAttributesSchema,
+ documentSchema: richTextContentSchema,
+ canCreate: async (context, _) => {
+ return context.hasCollaboratorAccess();
+ },
+ canUpdate: async (context, _) => {
+ return context.hasCollaboratorAccess();
+ },
+ canDelete: async (context, _) => {
+ return context.hasCollaboratorAccess();
+ },
+ getName: function (
+ _: string,
+ attributes: NodeAttributes
+ ): string | null | undefined {
+ if (attributes.type !== 'record') {
+ return null;
+ }
+
+ return attributes.name;
+ },
+ getText: function (_: string, __: NodeAttributes): string | null | undefined {
+ return undefined;
+ },
+};
diff --git a/packages/core/src/registry/nodes/space.ts b/packages/core/src/registry/nodes/space.ts
new file mode 100644
index 00000000..4ac6a4ac
--- /dev/null
+++ b/packages/core/src/registry/nodes/space.ts
@@ -0,0 +1,43 @@
+import { z } from 'zod';
+
+import { NodeModel, nodeRoleEnum } from './core';
+
+import { NodeAttributes } from '.';
+
+export const spaceAttributesSchema = z.object({
+ type: z.literal('space'),
+ name: z.string(),
+ description: z.string().nullable().optional(),
+ avatar: z.string().nullable().optional(),
+ collaborators: z.record(z.string(), nodeRoleEnum),
+ visibility: z.enum(['public', 'private']).default('private'),
+});
+
+export type SpaceAttributes = z.infer<typeof spaceAttributesSchema>;
+
+export const spaceModel: NodeModel = {
+ type: 'space',
+ attributesSchema: spaceAttributesSchema,
+ canCreate: async (context, _) => {
+ return context.hasCollaboratorAccess();
+ },
+ canUpdate: async (context, _) => {
+ return context.hasCollaboratorAccess();
+ },
+ canDelete: async (context, _) => {
+ return context.hasCollaboratorAccess();
+ },
+ getName: function (
+ _: string,
+ attributes: NodeAttributes
+ ): string | null | undefined {
+ if (attributes.type !== 'space') {
+ return null;
+ }
+
+ return attributes.name;
+ },
+ getText: function (_: string, __: NodeAttributes): string | null | undefined {
+ return undefined;
+ },
+};
diff --git a/packages/core/src/registry/page.ts b/packages/core/src/registry/page.ts
deleted file mode 100644
index af6706a3..00000000
--- a/packages/core/src/registry/page.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-import { z } from 'zod';
-
-import { blockSchema } from './block';
-
-export const pageAttributesSchema = z.object({
- type: z.literal('page'),
- name: z.string(),
- avatar: z.string().nullable().optional(),
- parentId: z.string(),
- content: z.record(blockSchema).optional().nullable(),
-});
-
-export type PageAttributes = z.infer<typeof pageAttributesSchema>;
diff --git a/packages/core/src/registry/record.ts b/packages/core/src/registry/record.ts
deleted file mode 100644
index bccd3908..00000000
--- a/packages/core/src/registry/record.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-import { z } from 'zod';
-
-import { blockSchema } from './block';
-import { fieldValueSchema } from './field-value';
-
-export const recordAttributesSchema = z.object({
- type: z.literal('record'),
- parentId: z.string(),
- databaseId: z.string(),
- name: z.string(),
- avatar: z.string().nullable().optional(),
- fields: z.record(z.string(), fieldValueSchema),
- content: z.record(blockSchema).nullable().optional(),
-});
-
-export type RecordAttributes = z.infer<typeof recordAttributesSchema>;
diff --git a/packages/core/src/registry/space.ts b/packages/core/src/registry/space.ts
deleted file mode 100644
index 4c81f7d2..00000000
--- a/packages/core/src/registry/space.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import { z } from 'zod';
-
-import { nodeRoleEnum } from './core';
-
-export const spaceAttributesSchema = z.object({
- type: z.literal('space'),
- name: z.string(),
- description: z.string().nullable().optional(),
- avatar: z.string().nullable().optional(),
- collaborators: z.record(z.string(), nodeRoleEnum),
- visibility: z.enum(['public', 'private']).default('private'),
-});
-
-export type SpaceAttributes = z.infer<typeof spaceAttributesSchema>;
diff --git a/packages/core/src/synchronizers/collaborations.ts b/packages/core/src/synchronizers/collaborations.ts
index 4a41169d..65edf2a1 100644
--- a/packages/core/src/synchronizers/collaborations.ts
+++ b/packages/core/src/synchronizers/collaborations.ts
@@ -1,4 +1,4 @@
-import { NodeRole } from '../registry/core';
+import { NodeRole } from '../registry/nodes/core';
export type SyncCollaborationsInput = {
type: 'collaborations';
diff --git a/packages/core/src/synchronizers/document-updates.ts b/packages/core/src/synchronizers/document-updates.ts
new file mode 100644
index 00000000..6c00fc0f
--- /dev/null
+++ b/packages/core/src/synchronizers/document-updates.ts
@@ -0,0 +1,25 @@
+import { DocumentUpdateMergeMetadata } from '../types/documents';
+
+export type SyncDocumentUpdatesInput = {
+ type: 'document_updates';
+ rootId: string;
+};
+
+export type SyncDocumentUpdateData = {
+ id: string;
+ documentId: string;
+ data: string;
+ revision: string;
+ createdBy: string;
+ createdAt: string;
+ mergedUpdates: DocumentUpdateMergeMetadata[] | null | undefined;
+};
+
+declare module '@colanode/core' {
+ interface SynchronizerMap {
+ document_updates: {
+ input: SyncDocumentUpdatesInput;
+ data: SyncDocumentUpdateData;
+ };
+ }
+}
diff --git a/packages/core/src/synchronizers/index.ts b/packages/core/src/synchronizers/index.ts
index c8d525a9..00f09371 100644
--- a/packages/core/src/synchronizers/index.ts
+++ b/packages/core/src/synchronizers/index.ts
@@ -5,6 +5,7 @@ export * from './node-reactions';
export * from './node-interactions';
export * from './node-tombstones';
export * from './collaborations';
+export * from './document-updates';
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface SynchronizerMap {}
diff --git a/packages/core/src/types/documents.ts b/packages/core/src/types/documents.ts
new file mode 100644
index 00000000..8ebc22cd
--- /dev/null
+++ b/packages/core/src/types/documents.ts
@@ -0,0 +1,5 @@
+export type DocumentUpdateMergeMetadata = {
+ id: string;
+ createdAt: string;
+ createdBy: string;
+};
diff --git a/packages/core/src/types/interactions.ts b/packages/core/src/types/interactions.ts
deleted file mode 100644
index c67f99d4..00000000
--- a/packages/core/src/types/interactions.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-export type InteractionAttributes = {
- lastReceivedTransactionId?: string | null;
- lastSeenTransactionId?: string | null;
- firstSeenAt?: Date | null;
- lastSeenAt?: Date | null;
- lastOpenedAt?: Date | null;
- lastOpenedTransactionId?: string | null;
-};
-
-export type InteractionAttribute = keyof InteractionAttributes;
-
-export type InteractionEvent = {
- attribute: InteractionAttribute;
- value: string;
- createdAt: string;
-};
diff --git a/packages/core/src/types/mutations.ts b/packages/core/src/types/mutations.ts
index 6ba4bf3f..3747314c 100644
--- a/packages/core/src/types/mutations.ts
+++ b/packages/core/src/types/mutations.ts
@@ -1,5 +1,4 @@
import { FileType } from './files';
-import { LocalNodeTransaction } from './transactions';
export type SyncMutationsInput = {
mutations: Mutation[];
@@ -21,27 +20,32 @@ export type MutationBase = {
createdAt: string;
};
-export type CreateFileMutationData = {
+export type CreateNodeMutationData = {
id: string;
- type: FileType;
- parentId: string;
- rootId: string;
- name: string;
- originalName: string;
- extension: string;
- mimeType: string;
- size: number;
createdAt: string;
+ data: string;
};
-export type CreateFileMutation = MutationBase & {
- type: 'create_file';
- data: CreateFileMutationData;
+export type CreateNodeMutation = MutationBase & {
+ type: 'create_node';
+ data: CreateNodeMutationData;
+};
+
+export type ApplyNodeUpdateMutationData = {
+ id: string;
+ data: string;
};
-export type ApplyNodeTransactionMutation = MutationBase & {
- type: 'apply_node_transaction';
- data: LocalNodeTransaction;
+export type UpdateNodeMutationData = {
+ id: string;
+ updateId: string;
+ data: string;
+ createdAt: string;
+};
+
+export type UpdateNodeMutation = MutationBase & {
+ type: 'update_node';
+ data: UpdateNodeMutationData;
};
export type DeleteNodeMutationData = {
@@ -101,13 +105,45 @@ export type MarkNodeOpenedMutation = MutationBase & {
data: MarkNodeOpenedMutationData;
};
+export type CreateFileMutationData = {
+ id: string;
+ type: FileType;
+ parentId: string;
+ rootId: string;
+ name: string;
+ originalName: string;
+ extension: string;
+ mimeType: string;
+ size: number;
+ createdAt: string;
+};
+
+export type CreateFileMutation = MutationBase & {
+ type: 'create_file';
+ data: CreateFileMutationData;
+};
+
+export type UpdateDocumentMutationData = {
+ documentId: string;
+ updateId: string;
+ data: string;
+ createdAt: string;
+};
+
+export type UpdateDocumentMutation = MutationBase & {
+ type: 'update_document';
+ data: UpdateDocumentMutationData;
+};
+
export type Mutation =
- | CreateFileMutation
- | ApplyNodeTransactionMutation
+ | CreateNodeMutation
+ | UpdateNodeMutation
| DeleteNodeMutation
| CreateNodeReactionMutation
| DeleteNodeReactionMutation
| MarkNodeSeenMutation
- | MarkNodeOpenedMutation;
+ | MarkNodeOpenedMutation
+ | CreateFileMutation
+ | UpdateDocumentMutation;
export type MutationType = Mutation['type'];
diff --git a/packages/core/src/types/transactions.ts b/packages/core/src/types/transactions.ts
deleted file mode 100644
index 0fed71c5..00000000
--- a/packages/core/src/types/transactions.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-export enum TransactionOperation {
- Create = 1,
- Update = 2,
-}
-
-export type LocalNodeTransaction = {
- id: string;
- nodeId: string;
- operation: TransactionOperation;
- data: string;
- createdAt: string;
-};
diff --git a/packages/crdt/src/index.ts b/packages/crdt/src/index.ts
index b44c3f99..9b619e7f 100644
--- a/packages/crdt/src/index.ts
+++ b/packages/crdt/src/index.ts
@@ -36,16 +36,16 @@ export class YDoc {
}
}
- public updateAttributes(
+ public update(
schema: ZodSchema,
- attributes: z.infer<typeof schema>
+ object: z.infer<typeof schema>
): Uint8Array | null {
- if (!schema.safeParse(attributes).success) {
- throw new Error('Invalid attributes', schema.safeParse(attributes).error);
+ if (!schema.safeParse(object).success) {
+ throw new Error('Invalid object', schema.safeParse(object).error);
}
- const attributesSchema = this.extractType(schema, attributes);
- if (!(attributesSchema instanceof z.ZodObject)) {
+ const objectSchema = this.extractType(schema, object);
+ if (!(objectSchema instanceof z.ZodObject)) {
throw new Error('Schema must be a ZodObject');
}
@@ -56,13 +56,13 @@ export class YDoc {
this.doc.on('update', onUpdateCallback);
- const attributesMap = this.doc.getMap('attributes');
+ const objectMap = this.doc.getMap('object');
this.doc.transact(() => {
- this.applyObjectChanges(attributesSchema, attributes, attributesMap);
+ this.applyObjectChanges(objectSchema, object, objectMap);
- const parseResult = schema.safeParse(attributesMap.toJSON());
+ const parseResult = schema.safeParse(objectMap.toJSON());
if (!parseResult.success) {
- throw new Error('Invalid attributes', parseResult.error);
+ throw new Error('Invalid object', parseResult.error);
}
});
@@ -84,10 +84,10 @@ export class YDoc {
return update;
}
- public getAttributes<T>(): T {
- const attributesMap = this.doc.getMap('attributes');
- const attributes = attributesMap.toJSON() as T;
- return attributes;
+ public getObject<T>(): T {
+ const objectMap = this.doc.getMap('object');
+ const object = objectMap.toJSON() as T;
+ return object;
}
public applyUpdate(update: Uint8Array | string) {