Skip to content

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

Add root_id in nodes and transactions

Hakan Shehub97c429eae13
diff --git a/apps/desktop/src/main/data/workspace/migrations.ts b/apps/desktop/src/main/data/workspace/migrations.ts
index 18891a6f..389833fc 100644
--- a/apps/desktop/src/main/data/workspace/migrations.ts
+++ b/apps/desktop/src/main/data/workspace/migrations.ts
@@ -39,6 +39,7 @@ const createNodesTable: Migration = {
.stored()
.notNull()
)
+ .addColumn('root_id', 'text', (col) => col.notNull())
.addColumn('attributes', 'text', (col) => col.notNull())
.addColumn('created_at', 'text', (col) => col.notNull())
.addColumn('updated_at', 'text')
@@ -62,6 +63,7 @@ const createTransactionsTable: Migration = {
.createTable('transactions')
.addColumn('id', 'text', (col) => col.notNull().primaryKey())
.addColumn('node_id', 'text', (col) => col.notNull())
+ .addColumn('root_id', 'text', (col) => col.notNull())
.addColumn('operation', 'text', (col) => col.notNull())
.addColumn('data', 'blob')
.addColumn('created_at', 'text', (col) => col.notNull())
diff --git a/apps/desktop/src/main/data/workspace/schema.ts b/apps/desktop/src/main/data/workspace/schema.ts
index 8f64f1b7..ad4356bd 100644
--- a/apps/desktop/src/main/data/workspace/schema.ts
+++ b/apps/desktop/src/main/data/workspace/schema.ts
@@ -26,8 +26,9 @@ export type UpdateUser = Updateable<UserTable>;
interface NodeTable {
id: ColumnType<string, string, never>;
- parent_id: ColumnType<string, never, never>;
type: ColumnType<NodeType, never, never>;
+ parent_id: ColumnType<string, never, never>;
+ root_id: ColumnType<string, string, never>;
attributes: ColumnType<string, string, string>;
created_at: ColumnType<string, string, never>;
updated_at: ColumnType<string | null, string | null, string | null>;
@@ -51,6 +52,7 @@ export type SelectNodePath = Selectable<NodePathTable>;
interface TransactionTable {
id: ColumnType<string, string, never>;
node_id: ColumnType<string, string, never>;
+ root_id: ColumnType<string, string, never>;
operation: ColumnType<string, string, never>;
data: ColumnType<Uint8Array | null, Uint8Array | null, never>;
created_at: ColumnType<string, string, never>;
diff --git a/apps/desktop/src/main/services/node-service.ts b/apps/desktop/src/main/services/node-service.ts
index 95f9cdf6..6941c69e 100644
--- a/apps/desktop/src/main/services/node-service.ts
+++ b/apps/desktop/src/main/services/node-service.ts
@@ -104,6 +104,7 @@ class NodeService {
ancestors = ancestorRows.map(mapNode);
}
+ const rootId = ancestors[0]?.id ?? inputItem.id;
const context = new NodeMutationContext(
workspace.account_id,
workspace.workspace_id,
@@ -133,6 +134,7 @@ class NodeService {
.returningAll()
.values({
id: inputItem.id,
+ root_id: rootId,
attributes: JSON.stringify(inputItem.attributes),
created_at: createdAt,
created_by: context.userId,
@@ -152,6 +154,7 @@ class NodeService {
.values({
id: transactionId,
node_id: inputItem.id,
+ root_id: rootId,
operation: 'create',
data: update,
created_at: createdAt,
@@ -372,6 +375,7 @@ class NodeService {
.values({
id: transactionId,
node_id: nodeId,
+ root_id: node.rootId,
operation: 'update',
data: update,
created_at: updatedAt,
@@ -524,6 +528,7 @@ class NodeService {
.values({
id: generateId(IdType.Transaction),
node_id: nodeId,
+ root_id: node.rootId,
operation: 'delete',
data: null,
created_at: new Date().toISOString(),
@@ -687,6 +692,7 @@ class NodeService {
transactions.map((t) => ({
id: t.id,
node_id: t.nodeId,
+ root_id: t.rootId,
operation: t.operation,
data:
t.operation !== 'delete' && t.data
@@ -741,6 +747,7 @@ class NodeService {
.returningAll()
.values({
id: nodeId,
+ root_id: firstTransaction.rootId,
attributes: attributesJson,
created_at: firstTransaction.createdAt,
created_by: firstTransaction.createdBy,
@@ -772,6 +779,7 @@ class NodeService {
transactions.map((t) => ({
id: t.id,
node_id: t.nodeId,
+ root_id: t.rootId,
operation: t.operation,
data:
t.operation !== 'delete' && t.data ? decodeState(t.data) : null,
@@ -882,6 +890,7 @@ class NodeService {
.returningAll()
.values({
id: transaction.nodeId,
+ root_id: transaction.rootId,
attributes: JSON.stringify(attributes),
created_at: transaction.createdAt,
created_by: transaction.createdBy,
@@ -894,6 +903,7 @@ class NodeService {
.values({
id: transaction.id,
node_id: transaction.nodeId,
+ root_id: transaction.rootId,
operation: 'create',
data: decodeState(transaction.data),
created_at: transaction.createdAt,
@@ -1038,6 +1048,7 @@ class NodeService {
.values({
id: transaction.id,
node_id: transaction.nodeId,
+ root_id: transaction.rootId,
operation: 'update',
data: decodeState(transaction.data),
created_at: transaction.createdAt,
diff --git a/apps/desktop/src/main/utils.ts b/apps/desktop/src/main/utils.ts
index 99cfecc9..54f17afb 100644
--- a/apps/desktop/src/main/utils.ts
+++ b/apps/desktop/src/main/utils.ts
@@ -193,6 +193,7 @@ export const mapNode = (row: SelectNode): Node => {
id: row.id,
type: row.type,
parentId: row.parent_id,
+ rootId: row.root_id,
attributes: JSON.parse(row.attributes),
createdAt: row.created_at,
createdBy: row.created_by,
@@ -233,6 +234,7 @@ export const mapTransaction = (row: SelectTransaction): LocalTransaction => {
return {
id: row.id,
nodeId: row.node_id,
+ rootId: row.root_id,
operation: 'create',
data: encodeState(row.data),
createdAt: row.created_at,
@@ -244,6 +246,7 @@ export const mapTransaction = (row: SelectTransaction): LocalTransaction => {
return {
id: row.id,
nodeId: row.node_id,
+ rootId: row.root_id,
operation: 'update',
data: encodeState(row.data),
createdAt: row.created_at,
@@ -255,6 +258,7 @@ export const mapTransaction = (row: SelectTransaction): LocalTransaction => {
return {
id: row.id,
nodeId: row.node_id,
+ rootId: row.root_id,
operation: 'delete',
createdAt: row.created_at,
createdBy: row.created_by,
diff --git a/apps/server/src/controllers/client/workspaces/transactions/transactions-sync.ts b/apps/server/src/controllers/client/workspaces/transactions/transactions-sync.ts
index 1b4c72e2..c8486341 100644
--- a/apps/server/src/controllers/client/workspaces/transactions/transactions-sync.ts
+++ b/apps/server/src/controllers/client/workspaces/transactions/transactions-sync.ts
@@ -61,6 +61,7 @@ const handleCreateTransaction = async (
const output = await nodeService.applyCreateTransaction(user, {
id: transaction.id,
nodeId: transaction.nodeId,
+ rootId: transaction.rootId,
data: transaction.data,
createdAt: new Date(transaction.createdAt),
});
@@ -79,6 +80,7 @@ const handleUpdateTransaction = async (
const output = await nodeService.applyUpdateTransaction(user, {
id: transaction.id,
nodeId: transaction.nodeId,
+ rootId: transaction.rootId,
userId: transaction.createdBy,
data: transaction.data,
createdAt: new Date(transaction.createdAt),
@@ -98,6 +100,7 @@ const handleDeleteTransaction = async (
const output = await nodeService.applyDeleteTransaction(user, {
id: transaction.id,
nodeId: transaction.nodeId,
+ rootId: transaction.rootId,
createdAt: new Date(transaction.createdAt),
});
diff --git a/apps/server/src/data/migrations.ts b/apps/server/src/data/migrations.ts
index 369c76d7..b38e42ea 100644
--- a/apps/server/src/data/migrations.ts
+++ b/apps/server/src/data/migrations.ts
@@ -124,6 +124,7 @@ const createNodesTable: Migration = {
.stored()
.notNull()
)
+ .addColumn('root_id', 'varchar(30)', (col) => col.notNull())
.addColumn('workspace_id', 'varchar(30)', (col) => col.notNull())
.addColumn('attributes', 'jsonb', (col) => col.notNull())
.addColumn('created_at', 'timestamptz', (col) => col.notNull())
@@ -153,6 +154,7 @@ const createTransactionsTable: Migration = {
.createTable('transactions')
.addColumn('id', 'varchar(30)', (col) => col.notNull().primaryKey())
.addColumn('node_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('data', 'bytea')
diff --git a/apps/server/src/data/schema.ts b/apps/server/src/data/schema.ts
index ac0a4b67..15bd56a2 100644
--- a/apps/server/src/data/schema.ts
+++ b/apps/server/src/data/schema.ts
@@ -95,6 +95,7 @@ interface NodeTable {
id: ColumnType<string, string, never>;
type: ColumnType<NodeType, never, never>;
parent_id: ColumnType<string, never, never>;
+ root_id: ColumnType<string, string, never>;
workspace_id: ColumnType<string, string, never>;
attributes: JSONColumnType<NodeAttributes, string | null, string | null>;
created_at: ColumnType<Date, Date, never>;
@@ -111,6 +112,7 @@ export type UpdateNode = Updateable<NodeTable>;
interface TransactionTable {
id: ColumnType<string, string, never>;
node_id: ColumnType<string, string, never>;
+ root_id: ColumnType<string, string, never>;
workspace_id: ColumnType<string, string, never>;
operation: ColumnType<string, string, never>;
data: ColumnType<Uint8Array | null, Uint8Array | null, Uint8Array | null>;
diff --git a/apps/server/src/jobs/clean-node-data.ts b/apps/server/src/jobs/clean-node-data.ts
index 87eac8f6..5d0912bd 100644
--- a/apps/server/src/jobs/clean-node-data.ts
+++ b/apps/server/src/jobs/clean-node-data.ts
@@ -105,7 +105,7 @@ const deleteChildren = async (
(descendant) => ({
id: generateId(IdType.Transaction),
node_id: descendant.id,
- node_type: descendant.type,
+ root_id: descendant.root_id,
workspace_id: workspaceId,
operation: 'delete',
created_at: new Date(),
diff --git a/apps/server/src/lib/nodes.ts b/apps/server/src/lib/nodes.ts
index 0d105141..10a3a74b 100644
--- a/apps/server/src/lib/nodes.ts
+++ b/apps/server/src/lib/nodes.ts
@@ -62,6 +62,7 @@ export const mapTransaction = (
id: transaction.id,
operation: 'create',
nodeId: transaction.node_id,
+ rootId: transaction.root_id,
workspaceId: transaction.workspace_id,
data: encodeState(transaction.data),
createdAt: transaction.created_at.toISOString(),
@@ -76,6 +77,7 @@ export const mapTransaction = (
id: transaction.id,
operation: 'update',
nodeId: transaction.node_id,
+ rootId: transaction.root_id,
workspaceId: transaction.workspace_id,
data: encodeState(transaction.data),
createdAt: transaction.created_at.toISOString(),
@@ -90,6 +92,7 @@ export const mapTransaction = (
id: transaction.id,
operation: 'delete',
nodeId: transaction.node_id,
+ rootId: transaction.root_id,
workspaceId: transaction.workspace_id,
createdAt: transaction.created_at.toISOString(),
createdBy: transaction.created_by,
diff --git a/apps/server/src/services/node-service.ts b/apps/server/src/services/node-service.ts
index 948b3fab..1e54e6d7 100644
--- a/apps/server/src/services/node-service.ts
+++ b/apps/server/src/services/node-service.ts
@@ -66,6 +66,7 @@ class NodeService {
const createNode: CreateNode = {
id: input.nodeId,
+ root_id: input.rootId,
workspace_id: input.workspaceId,
attributes: attributesJson,
created_at: date,
@@ -75,6 +76,7 @@ class NodeService {
const createTransaction: CreateTransaction = {
id: transactionId,
+ root_id: input.rootId,
node_id: input.nodeId,
workspace_id: input.workspaceId,
operation: 'create',
@@ -201,6 +203,7 @@ class NodeService {
.values({
id: transactionId,
node_id: input.nodeId,
+ root_id: node.rootId,
workspace_id: input.workspaceId,
operation: 'update',
data: update,
@@ -277,6 +280,7 @@ class NodeService {
const createNode: CreateNode = {
id: input.nodeId,
+ root_id: input.rootId,
attributes: JSON.stringify(attributes),
workspace_id: context.workspaceId,
created_at: input.createdAt,
@@ -287,6 +291,7 @@ class NodeService {
const createTransaction: CreateTransaction = {
id: input.id,
node_id: input.nodeId,
+ root_id: input.rootId,
workspace_id: context.workspaceId,
operation: 'create',
data:
@@ -421,6 +426,7 @@ class NodeService {
.values({
id: input.id,
node_id: input.nodeId,
+ root_id: input.rootId,
workspace_id: context.workspaceId,
operation: 'update',
data:
@@ -544,6 +550,7 @@ class NodeService {
.values({
id: input.id,
node_id: input.nodeId,
+ root_id: input.rootId,
workspace_id: user.workspace_id,
operation: 'delete',
created_at: input.createdAt,
diff --git a/apps/server/src/services/workspace-service.ts b/apps/server/src/services/workspace-service.ts
index 1ab596ea..48031fc2 100644
--- a/apps/server/src/services/workspace-service.ts
+++ b/apps/server/src/services/workspace-service.ts
@@ -65,6 +65,7 @@ class WorkspaceService {
const createSpaceNodeOutput = await nodeService.createNode({
nodeId: spaceId,
+ rootId: spaceId,
attributes: {
type: 'space',
name: 'Home',
@@ -83,6 +84,7 @@ class WorkspaceService {
const spaceNode = mapNode(createSpaceNodeOutput.node);
await nodeService.createNode({
nodeId: generateId(IdType.Page),
+ rootId: spaceId,
attributes: {
type: 'page',
name: 'Notes',
@@ -96,6 +98,7 @@ class WorkspaceService {
await nodeService.createNode({
nodeId: generateId(IdType.Channel),
+ rootId: spaceId,
attributes: {
type: 'channel',
name: 'Discussions',
diff --git a/apps/server/src/types/nodes.ts b/apps/server/src/types/nodes.ts
index 413d9c76..eec25027 100644
--- a/apps/server/src/types/nodes.ts
+++ b/apps/server/src/types/nodes.ts
@@ -10,6 +10,7 @@ export type NodeCollaborator = {
export type CreateNodeInput = {
nodeId: string;
+ rootId: string;
attributes: NodeAttributes;
userId: string;
workspaceId: string;
@@ -36,6 +37,7 @@ export type UpdateNodeOutput = {
export type ApplyCreateTransactionInput = {
id: string;
nodeId: string;
+ rootId: string;
data: string | Uint8Array;
createdAt: Date;
};
@@ -48,6 +50,7 @@ export type ApplyCreateTransactionOutput = {
export type ApplyUpdateTransactionInput = {
id: string;
nodeId: string;
+ rootId: string;
userId: string;
data: string | Uint8Array;
createdAt: Date;
@@ -61,6 +64,7 @@ export type ApplyUpdateTransactionOutput = {
export type ApplyDeleteTransactionInput = {
id: string;
nodeId: string;
+ rootId: string;
createdAt: Date;
};
diff --git a/packages/core/src/registry/index.ts b/packages/core/src/registry/index.ts
index 9a96d88a..ca85ef80 100644
--- a/packages/core/src/registry/index.ts
+++ b/packages/core/src/registry/index.ts
@@ -12,6 +12,7 @@ import { SpaceAttributes, spaceModel } from './space';
type NodeBase = {
id: string;
parentId: string;
+ rootId: string;
createdAt: string;
createdBy: string;
updatedAt: string | null;
diff --git a/packages/core/src/types/sync.ts b/packages/core/src/types/sync.ts
index 0f598ec9..6b6f2b36 100644
--- a/packages/core/src/types/sync.ts
+++ b/packages/core/src/types/sync.ts
@@ -26,6 +26,7 @@ export type LocalTransaction =
export type LocalCreateTransaction = {
id: string;
nodeId: string;
+ rootId: string;
operation: 'create';
data: string;
createdAt: string;
@@ -35,6 +36,7 @@ export type LocalCreateTransaction = {
export type LocalUpdateTransaction = {
id: string;
nodeId: string;
+ rootId: string;
operation: 'update';
data: string;
createdAt: string;
@@ -44,6 +46,7 @@ export type LocalUpdateTransaction = {
export type LocalDeleteTransaction = {
id: string;
nodeId: string;
+ rootId: string;
operation: 'delete';
createdAt: string;
createdBy: string;
@@ -53,6 +56,7 @@ export type ServerCreateTransaction = {
id: string;
operation: 'create';
nodeId: string;
+ rootId: string;
workspaceId: string;
data: string;
createdAt: string;
@@ -65,6 +69,7 @@ export type ServerUpdateTransaction = {
id: string;
operation: 'update';
nodeId: string;
+ rootId: string;
workspaceId: string;
data: string;
createdAt: string;
@@ -77,6 +82,7 @@ export type ServerDeleteTransaction = {
id: string;
operation: 'delete';
nodeId: string;
+ rootId: string;
workspaceId: string;
createdAt: string;
createdBy: string;