Open-source and local-first Slack and Notion alternative that puts you in control of your data
TypeScript chatchat-applicationcrdteditorelectronknowledge-baselocal-firstnotionnotion-alternativerealtime-collaborationself-hostedslacksqliteteam-collaborationwikiyjs
Store entry transaction operation as integer enum instead of text
Hakan Shehu86dba5762c58
diff --git a/apps/desktop/src/main/data/workspace/migrations.ts b/apps/desktop/src/main/data/workspace/migrations.tsindex 26c139b4..cc92f0bc 100644--- a/apps/desktop/src/main/data/workspace/migrations.ts+++ b/apps/desktop/src/main/data/workspace/migrations.ts@@ -65,7 +65,7 @@ const createEntryTransactionsTable: Migration = {.addColumn('id', 'text', (col) => col.notNull().primaryKey()).addColumn('entry_id', 'text', (col) => col.notNull()).addColumn('root_id', 'text', (col) => col.notNull())- .addColumn('operation', 'text', (col) => col.notNull())+ .addColumn('operation', 'integer', (col) => col.notNull()).addColumn('data', 'blob').addColumn('created_at', 'text', (col) => col.notNull()).addColumn('created_by', 'text', (col) => col.notNull())diff --git a/apps/desktop/src/main/data/workspace/schema.ts b/apps/desktop/src/main/data/workspace/schema.tsindex fb8fc4cb..acb5d011 100644--- a/apps/desktop/src/main/data/workspace/schema.ts+++ b/apps/desktop/src/main/data/workspace/schema.ts@@ -5,6 +5,7 @@ import {EntryType,WorkspaceRole,MessageType,+ TransactionOperation,} from '@colanode/core';import { ColumnType, Insertable, Selectable, Updateable } from 'kysely';@@ -57,7 +58,7 @@ interface EntryTransactionTable {id: ColumnType<string, string, never>;entry_id: ColumnType<string, string, never>;root_id: ColumnType<string, string, never>;- operation: ColumnType<string, string, never>;+ operation: ColumnType<TransactionOperation, TransactionOperation, never>;data: ColumnType<Uint8Array | null, Uint8Array | null, never>;created_at: ColumnType<string, string, never>;created_by: ColumnType<string, string, never>;diff --git a/apps/desktop/src/main/services/entry-service.ts b/apps/desktop/src/main/services/entry-service.tsindex 694e010b..68a252cb 100644--- a/apps/desktop/src/main/services/entry-service.ts+++ b/apps/desktop/src/main/services/entry-service.ts@@ -16,6 +16,7 @@ import {entryAttributesSchema,Entry,canCreateEntry,+ TransactionOperation,} from '@colanode/core';import { decodeState, YDoc } from '@colanode/crdt';@@ -129,7 +130,7 @@ class EntryService {id: transactionId,entry_id: input.id,root_id: root?.id ?? input.id,- operation: 'create',+ operation: TransactionOperation.Create,data: update,created_at: createdAt,created_by: userId,@@ -307,7 +308,7 @@ class EntryService {id: transactionId,entry_id: entryId,root_id: entry.rootId,- operation: 'update',+ operation: TransactionOperation.Update,data: update,created_at: updatedAt,created_by: userId,@@ -439,7 +440,7 @@ class EntryService {id: generateId(IdType.Transaction),entry_id: entryId,root_id: root.root_id,- operation: 'delete',+ operation: TransactionOperation.Delete,data: null,created_at: new Date().toISOString(),created_by: userId,@@ -497,11 +498,11 @@ class EntryService {userId: string,transaction: SyncEntryTransactionData) {- if (transaction.operation === 'create') {+ if (transaction.operation === TransactionOperation.Create) {await this.applyServerCreateTransaction(userId, transaction);- } else if (transaction.operation === 'update') {+ } else if (transaction.operation === TransactionOperation.Update) {await this.applyServerUpdateTransaction(userId, transaction);- } else if (transaction.operation === 'delete') {+ } else if (transaction.operation === TransactionOperation.Delete) {await this.applyServerDeleteTransaction(userId, transaction);}}@@ -577,7 +578,7 @@ class EntryService {id: transaction.id,entry_id: transaction.entryId,root_id: transaction.rootId,- operation: 'create',+ operation: TransactionOperation.Create,data: decodeState(transaction.data),created_at: transaction.createdAt,created_by: transaction.createdBy,@@ -729,7 +730,7 @@ class EntryService {id: transaction.id,entry_id: transaction.entryId,root_id: transaction.rootId,- operation: 'update',+ operation: TransactionOperation.Update,data: decodeState(transaction.data),created_at: transaction.createdAt,created_by: transaction.createdBy,diff --git a/apps/desktop/src/main/utils.ts b/apps/desktop/src/main/utils.tsindex 51287f3e..f904a438 100644--- a/apps/desktop/src/main/utils.ts+++ b/apps/desktop/src/main/utils.ts@@ -1,4 +1,9 @@-import { LocalTransaction, Mutation, Entry } from '@colanode/core';+import {+ LocalTransaction,+ Mutation,+ Entry,+ TransactionOperation,+} from '@colanode/core';import { encodeState } from '@colanode/crdt';import {DeleteResult,@@ -233,36 +238,36 @@ export const mapWorkspace = (row: SelectWorkspace): Workspace => {export const mapEntryTransaction = (row: SelectEntryTransaction): LocalTransaction => {- if (row.operation === 'create' && row.data) {+ if (row.operation === TransactionOperation.Create && row.data) {return {id: row.id,entryId: row.entry_id,rootId: row.root_id,- operation: 'create',+ operation: TransactionOperation.Create,data: encodeState(row.data),createdAt: row.created_at,createdBy: row.created_by,};}- if (row.operation === 'update' && row.data) {+ if (row.operation === TransactionOperation.Update && row.data) {return {id: row.id,entryId: row.entry_id,rootId: row.root_id,- operation: 'update',+ operation: TransactionOperation.Update,data: encodeState(row.data),createdAt: row.created_at,createdBy: row.created_by,};}- if (row.operation === 'delete') {+ if (row.operation === TransactionOperation.Delete) {return {id: row.id,entryId: row.entry_id,rootId: row.root_id,- operation: 'delete',+ operation: TransactionOperation.Delete,createdAt: row.created_at,createdBy: row.created_by,};diff --git a/apps/server/src/data/migrations.ts b/apps/server/src/data/migrations.tsindex e3fe79b5..133dcb14 100644--- a/apps/server/src/data/migrations.ts+++ b/apps/server/src/data/migrations.ts@@ -185,7 +185,7 @@ const createEntryTransactionsTable: Migration = {.addColumn('entry_id', 'varchar(30)', (col) => col.notNull()).addColumn('root_id', 'varchar(30)', (col) => col.notNull()).addColumn('workspace_id', 'varchar(30)', (col) => col.notNull())- .addColumn('operation', 'varchar(30)', (col) => col.notNull())+ .addColumn('operation', 'integer', (col) => col.notNull()).addColumn('data', 'bytea').addColumn('created_at', 'timestamptz', (col) => col.notNull()).addColumn('created_by', 'varchar(30)', (col) => col.notNull())diff --git a/apps/server/src/data/schema.ts b/apps/server/src/data/schema.tsindex ab3e3759..b532bc4c 100644--- a/apps/server/src/data/schema.ts+++ b/apps/server/src/data/schema.ts@@ -7,6 +7,7 @@ import {WorkspaceRole,MessageType,MessageAttributes,+ TransactionOperation,} from '@colanode/core';import {ColumnType,@@ -132,7 +133,7 @@ interface EntryTransactionTable {entry_id: ColumnType<string, string, never>;root_id: ColumnType<string, string, never>;workspace_id: ColumnType<string, string, never>;- operation: ColumnType<string, string, never>;+ operation: ColumnType<TransactionOperation, TransactionOperation, never>;data: ColumnType<Uint8Array | null, Uint8Array | null, Uint8Array | null>;created_at: ColumnType<Date, Date, never>;created_by: ColumnType<string, string, never>;diff --git a/apps/server/src/jobs/clean-entry-data.ts b/apps/server/src/jobs/clean-entry-data.tsindex a7680ba0..c6b3da3c 100644--- a/apps/server/src/jobs/clean-entry-data.ts+++ b/apps/server/src/jobs/clean-entry-data.ts@@ -1,5 +1,5 @@import { DeleteObjectCommand } from '@aws-sdk/client-s3';-import { generateId, IdType } from '@colanode/core';+import { generateId, IdType, TransactionOperation } from '@colanode/core';import { database } from '@/data/database';import {@@ -50,7 +50,7 @@ export const cleanEntryDataHandler: JobHandler<CleanEntryDataInput> = async (const deleteTransaction = deleteTransactions[0];if (!deleteTransaction?.operation ||- deleteTransaction.operation !== 'delete'+ deleteTransaction.operation !== TransactionOperation.Delete) {logger.error(`Expected delete transaction for ${input.entryId}`);return;@@ -96,7 +96,7 @@ const deleteChildren = async (parentIds: string[], userId: string) => {entry_id: descendant.id,root_id: descendant.root_id,workspace_id: descendant.workspace_id,- operation: 'delete',+ operation: TransactionOperation.Delete,created_at: new Date(),created_by: userId,server_created_at: new Date(),diff --git a/apps/server/src/services/entry-service.ts b/apps/server/src/services/entry-service.tsindex cc25b635..c7795f9f 100644--- a/apps/server/src/services/entry-service.ts+++ b/apps/server/src/services/entry-service.ts@@ -12,6 +12,7 @@ import {canCreateEntry,canUpdateEntry,canDeleteEntry,+ TransactionOperation,} from '@colanode/core';import { decodeState, YDoc } from '@colanode/crdt';import { Transaction } from 'kysely';@@ -89,7 +90,7 @@ class EntryService {root_id: input.rootId,entry_id: input.entryId,workspace_id: input.workspaceId,- operation: 'create',+ operation: TransactionOperation.Create,data: update,created_at: date,created_by: input.userId,@@ -230,7 +231,7 @@ class EntryService {entry_id: input.entryId,root_id: entry.rootId,workspace_id: input.workspaceId,- operation: 'update',+ operation: TransactionOperation.Update,data: update,created_at: date,created_by: input.userId,@@ -338,7 +339,7 @@ class EntryService {entry_id: input.entryId,root_id: input.rootId,workspace_id: user.workspace_id,- operation: 'create',+ operation: TransactionOperation.Create,data:typeof input.data === 'string' ? decodeState(input.data) : input.data,created_at: input.createdAt,@@ -490,7 +491,7 @@ class EntryService {entry_id: input.entryId,root_id: input.rootId,workspace_id: user.workspace_id,- operation: 'update',+ operation: TransactionOperation.Update,data:typeof input.data === 'string'? decodeState(input.data)@@ -614,7 +615,7 @@ class EntryService {entry_id: input.entryId,root_id: input.rootId,workspace_id: user.workspace_id,- operation: 'delete',+ operation: TransactionOperation.Delete,created_at: input.createdAt,created_by: user.id,server_created_at: new Date(),diff --git a/apps/server/src/synchronizers/entry-transactions.ts b/apps/server/src/synchronizers/entry-transactions.tsindex fa87c4fc..b624f5be 100644--- a/apps/server/src/synchronizers/entry-transactions.ts+++ b/apps/server/src/synchronizers/entry-transactions.ts@@ -2,6 +2,7 @@ import {SynchronizerOutputMessage,SyncEntryTransactionsInput,SyncEntryTransactionData,+ TransactionOperation,} from '@colanode/core';import { encodeState } from '@colanode/crdt';@@ -59,10 +60,13 @@ export class EntryTransactionSynchronizer extends BaseSynchronizer<SyncEntryTran): SynchronizerOutputMessage<SyncEntryTransactionsInput> {const items: SyncEntryTransactionData[] = unsyncedTransactions.map((transaction) => {- if (transaction.operation === 'create' && transaction.data) {+ if (+ transaction.operation === TransactionOperation.Create &&+ transaction.data+ ) {return {id: transaction.id,- operation: 'create',+ operation: TransactionOperation.Create,entryId: transaction.entry_id,rootId: transaction.root_id,workspaceId: transaction.workspace_id,@@ -74,10 +78,13 @@ export class EntryTransactionSynchronizer extends BaseSynchronizer<SyncEntryTran};}- if (transaction.operation === 'update' && transaction.data) {+ if (+ transaction.operation === TransactionOperation.Update &&+ transaction.data+ ) {return {id: transaction.id,- operation: 'update',+ operation: TransactionOperation.Update,entryId: transaction.entry_id,rootId: transaction.root_id,workspaceId: transaction.workspace_id,@@ -89,10 +96,10 @@ export class EntryTransactionSynchronizer extends BaseSynchronizer<SyncEntryTran};}- if (transaction.operation === 'delete') {+ if (transaction.operation === TransactionOperation.Delete) {return {id: transaction.id,- operation: 'delete',+ operation: TransactionOperation.Delete,entryId: transaction.entry_id,rootId: transaction.root_id,workspaceId: transaction.workspace_id,diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex 0f257390..e9d5b708 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -21,7 +21,7 @@ export * from './types/interactions';export * from './types/sockets';export * from './types/entries';export * from './types/servers';-export * from './types/sync';+export * from './types/transactions';export * from './types/workspaces';export * from './types/mutations';export * from './types/messages';diff --git a/packages/core/src/synchronizers/entry-transactions.ts b/packages/core/src/synchronizers/entry-transactions.tsindex 14fda5fb..1478debe 100644--- a/packages/core/src/synchronizers/entry-transactions.ts+++ b/packages/core/src/synchronizers/entry-transactions.ts@@ -1,3 +1,5 @@+import { TransactionOperation } from '../types/transactions';+export type SyncEntryTransactionsInput = {type: 'entry_transactions';rootId: string;@@ -5,7 +7,7 @@ export type SyncEntryTransactionsInput = {export type SyncCreateEntryTransactionData = {id: string;- operation: 'create';+ operation: TransactionOperation.Create;entryId: string;rootId: string;workspaceId: string;@@ -18,7 +20,7 @@ export type SyncCreateEntryTransactionData = {export type SyncUpdateEntryTransactionData = {id: string;- operation: 'update';+ operation: TransactionOperation.Update;entryId: string;rootId: string;workspaceId: string;@@ -31,7 +33,7 @@ export type SyncUpdateEntryTransactionData = {export type SyncDeleteEntryTransactionData = {id: string;- operation: 'delete';+ operation: TransactionOperation.Delete;entryId: string;rootId: string;workspaceId: string;diff --git a/packages/core/src/types/mutations.ts b/packages/core/src/types/mutations.tsindex f6fd9343..0f5734f5 100644--- a/packages/core/src/types/mutations.ts+++ b/packages/core/src/types/mutations.ts@@ -4,7 +4,7 @@ import {LocalCreateTransaction,LocalDeleteTransaction,LocalUpdateTransaction,-} from './sync';+} from './transactions';export type SyncMutationsInput = {mutations: Mutation[];diff --git a/packages/core/src/types/sync.ts b/packages/core/src/types/transactions.tssimilarity index 72%rename from packages/core/src/types/sync.tsrename to packages/core/src/types/transactions.tsindex de5efd3e..d2c3c787 100644--- a/packages/core/src/types/sync.ts+++ b/packages/core/src/types/transactions.ts@@ -1,3 +1,9 @@+export enum TransactionOperation {+ Create = 1,+ Update = 2,+ Delete = 3,+}+export type LocalTransaction =| LocalCreateTransaction| LocalUpdateTransaction@@ -7,7 +13,7 @@ export type LocalCreateTransaction = {id: string;entryId: string;rootId: string;- operation: 'create';+ operation: TransactionOperation.Create;data: string;createdAt: string;createdBy: string;@@ -17,7 +23,7 @@ export type LocalUpdateTransaction = {id: string;entryId: string;rootId: string;- operation: 'update';+ operation: TransactionOperation.Update;data: string;createdAt: string;createdBy: string;@@ -27,7 +33,7 @@ export type LocalDeleteTransaction = {id: string;entryId: string;rootId: string;- operation: 'delete';+ operation: TransactionOperation.Delete;createdAt: string;createdBy: string;};