Skip to content

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

Refactor transactions into mutations

Hakan Shehu09b55ae20d8d
diff --git a/desktop/src/components/accounts/login.tsx b/desktop/src/components/accounts/login.tsx
index f5fdc40c..a3863cc0 100644
--- a/desktop/src/components/accounts/login.tsx
+++ b/desktop/src/components/accounts/login.tsx
@@ -5,7 +5,7 @@ import { EmailRegister } from '@/components/accounts/email-register';
import { observer } from 'mobx-react-lite';
import { useAppDatabase } from '@/contexts/app-database';
-const serverUrl = 'http://localhost:3000';
+const SERVER_URL = 'http://localhost:3000';
export const Login = observer(() => {
const appDatabase = useAppDatabase();
@@ -67,9 +67,9 @@ export const Login = observer(() => {
</div>
<div className="flex flex-col gap-4">
{showRegister ? (
- <EmailRegister serverUrl={serverUrl} onRegister={handleLogin} />
+ <EmailRegister serverUrl={SERVER_URL} onRegister={handleLogin} />
) : (
- <EmailLogin serverUrl={serverUrl} onLogin={handleLogin} />
+ <EmailLogin serverUrl={SERVER_URL} onLogin={handleLogin} />
)}
<p
className="text-center text-sm text-muted-foreground hover:cursor-pointer hover:underline"
diff --git a/desktop/src/components/app.tsx b/desktop/src/components/app.tsx
index e677b265..37992e88 100644
--- a/desktop/src/components/app.tsx
+++ b/desktop/src/components/app.tsx
@@ -18,7 +18,7 @@ import {
import { AppDatabaseSchema } from '@/data/schemas/app';
import { useQuery } from '@tanstack/react-query';
-const serverUrl = 'http://localhost:3000';
+const SERVER_URL = 'http://localhost:3000';
const appDatabase = new Kysely<AppDatabaseSchema>({
dialect: {
@@ -68,7 +68,7 @@ export const App = observer(() => {
const account = accountsQuery.data.rows[0];
const axios = Axios.create({
- baseURL: serverUrl,
+ baseURL: SERVER_URL,
headers: {
Authorization: `Bearer ${account.token}`,
},
diff --git a/desktop/src/components/messages/message-delete-button.tsx b/desktop/src/components/messages/message-delete-button.tsx
index a72dacca..4c12a117 100644
--- a/desktop/src/components/messages/message-delete-button.tsx
+++ b/desktop/src/components/messages/message-delete-button.tsx
@@ -11,6 +11,7 @@ import {
import { Button } from '@/components/ui/button';
import { Icon } from '@/components/ui/icon';
import { useWorkspace } from '@/contexts/workspace';
+import { useMutation } from '@tanstack/react-query';
interface MessageDeleteButtonProps {
id: string;
@@ -18,7 +19,16 @@ interface MessageDeleteButtonProps {
export const MessageDeleteButton = ({ id }: MessageDeleteButtonProps) => {
const [showDeleteModal, setShowDeleteModal] = React.useState(false);
- const [isDeleting, setIsDeleting] = React.useState(false);
+ const { mutate, isPending } = useMutation({
+ mutationFn: async (messageId: string) => {
+ await workspace.mutate({
+ type: 'delete_node',
+ data: {
+ id: messageId,
+ },
+ });
+ },
+ });
const workspace = useWorkspace();
return (
@@ -39,18 +49,13 @@ export const MessageDeleteButton = ({ id }: MessageDeleteButtonProps) => {
<AlertDialogCancel>Cancel</AlertDialogCancel>
<Button
variant="destructive"
- disabled={isDeleting}
+ disabled={isPending}
onClick={async () => {
- setIsDeleting(true);
-
- const query = workspace.schema
- .deleteFrom('nodes')
- .where('id', '=', id)
- .compile();
-
- await workspace.executeQuery(query);
- setIsDeleting(false);
- setShowDeleteModal(false);
+ mutate(id, {
+ onSuccess: () => {
+ setShowDeleteModal(false);
+ },
+ });
}}
>
Delete
diff --git a/desktop/src/components/spaces/space-create-dialog.tsx b/desktop/src/components/spaces/space-create-dialog.tsx
index c57eb555..b9d837ab 100644
--- a/desktop/src/components/spaces/space-create-dialog.tsx
+++ b/desktop/src/components/spaces/space-create-dialog.tsx
@@ -26,6 +26,7 @@ import { zodResolver } from '@hookform/resolvers/zod';
import { NeuronId } from '@/lib/id';
import { NodeTypes } from '@/lib/constants';
import { generateNodeIndex } from '@/lib/nodes';
+import { useMutation } from '@tanstack/react-query';
const formSchema = z.object({
name: z.string().min(3, 'Name must be at least 3 characters long.'),
@@ -42,7 +43,6 @@ export const SpaceCreateDialog = ({
onOpenChange,
}: SpaceCreateDialogProps) => {
const workspace = useWorkspace();
- const [isPending, setIsPending] = React.useState(false);
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
@@ -51,65 +51,76 @@ export const SpaceCreateDialog = ({
},
});
+ const { mutate, isPending } = useMutation({
+ mutationFn: async (values: z.infer<typeof formSchema>) => {
+ const spaceId = NeuronId.generate(NeuronId.Type.Space);
+ const firstIndex = generateNodeIndex(null, null);
+ const secondIndex = generateNodeIndex(firstIndex, null);
+ await workspace.mutate({
+ type: 'create_nodes',
+ data: {
+ nodes: [
+ {
+ id: spaceId,
+ type: NodeTypes.Space,
+ parentId: null,
+ workspaceId: workspace.id,
+ index: generateNodeIndex(null, null),
+ attrs: {
+ name: values.name,
+ description: values.description,
+ },
+ content: null,
+ createdAt: new Date().toISOString(),
+ createdBy: workspace.userId,
+ versionId: NeuronId.generate(NeuronId.Type.Version),
+ },
+ {
+ id: NeuronId.generate(NeuronId.Type.Page),
+ type: NodeTypes.Page,
+ parentId: spaceId,
+ workspaceId: workspace.id,
+ index: firstIndex,
+ attrs: {
+ name: 'Home',
+ },
+ content: null,
+ createdAt: new Date().toISOString(),
+ createdBy: workspace.userId,
+ versionId: NeuronId.generate(NeuronId.Type.Version),
+ },
+ {
+ id: NeuronId.generate(NeuronId.Type.Channel),
+ type: NodeTypes.Channel,
+ parentId: spaceId,
+ workspaceId: workspace.id,
+ index: secondIndex,
+ attrs: {
+ name: 'Discussions',
+ },
+ content: null,
+ createdAt: new Date().toISOString(),
+ createdBy: workspace.userId,
+ versionId: NeuronId.generate(NeuronId.Type.Version),
+ },
+ ],
+ },
+ });
+ },
+ });
+
const handleCancel = () => {
form.reset();
onOpenChange(false);
};
const handleSubmit = async (values: z.infer<typeof formSchema>) => {
- setIsPending(true);
-
- const spaceId = NeuronId.generate(NeuronId.Type.Space);
- const query = workspace.schema
- .insertInto('nodes')
- .values([
- {
- id: spaceId,
- type: NodeTypes.Space,
- index: generateNodeIndex(null, null),
- parent_id: null,
- workspace_id: workspace.id,
- created_at: new Date().toISOString(),
- created_by: workspace.userId,
- version_id: NeuronId.generate(NeuronId.Type.Version),
- attrs: JSON.stringify({
- name: values.name,
- description: values.description,
- }),
- },
- {
- id: NeuronId.generate(NeuronId.Type.Page),
- type: NodeTypes.Page,
- index: generateNodeIndex(null, null),
- parent_id: spaceId,
- workspace_id: workspace.id,
- created_at: new Date().toISOString(),
- created_by: workspace.userId,
- version_id: NeuronId.generate(NeuronId.Type.Version),
- attrs: JSON.stringify({
- name: 'Home',
- }),
- },
- {
- id: NeuronId.generate(NeuronId.Type.Channel),
- type: NodeTypes.Channel,
- index: generateNodeIndex(null, null),
- parent_id: spaceId,
- workspace_id: workspace.id,
- created_at: new Date().toISOString(),
- created_by: workspace.userId,
- version_id: NeuronId.generate(NeuronId.Type.Version),
- attrs: JSON.stringify({
- name: 'Discussions',
- }),
- },
- ])
- .compile();
-
- await workspace.executeQuery(query);
- setIsPending(false);
- onOpenChange(false);
- form.reset();
+ mutate(values, {
+ onSuccess: () => {
+ onOpenChange(false);
+ form.reset();
+ },
+ });
};
return (
diff --git a/desktop/src/components/workspaces/container-header.tsx b/desktop/src/components/workspaces/container-header.tsx
index 329b0d51..6931d616 100644
--- a/desktop/src/components/workspaces/container-header.tsx
+++ b/desktop/src/components/workspaces/container-header.tsx
@@ -29,6 +29,7 @@ import { sql } from 'kysely';
import { mapNode } from '@/lib/nodes';
import { useQuery } from '@tanstack/react-query';
import { NodesTableSchema } from '@/data/schemas/workspace';
+import { NeuronId } from '@/lib/id';
interface BreadcrumbNodeProps {
node: Node;
@@ -61,13 +62,23 @@ export const BreadcrumbNodeEditor = observer(
const handleNameChange = React.useMemo(
() =>
debounce(async (newName: string) => {
- const query = workspace.schema
- .updateTable('nodes')
- .set('attrs', sql`json_set(attrs, '$.name', ${newName})`)
- .where('id', '=', node.id)
- .compile();
-
- await workspace.executeQuery(query);
+ await workspace.mutate({
+ type: 'update_node',
+ data: {
+ id: node.id,
+ type: node.type,
+ index: node.index,
+ content: node.content,
+ attrs: {
+ ...node.attrs,
+ name: newName,
+ },
+ parentId: node.parentId,
+ updatedAt: new Date().toISOString(),
+ updatedBy: workspace.userId,
+ versionId: NeuronId.generate(NeuronId.Type.Version),
+ },
+ });
}, 500),
[node.id],
);
@@ -111,7 +122,7 @@ export const ContainerHeader = observer(({ node }: ContainerHeaderProps) => {
`.compile(workspace.schema);
const queryId = queryKey[0];
- return await workspace.executeQueryAndSubscribe(queryId, query);
+ return await workspace.queryAndSubscribe(queryId, query);
},
});
diff --git a/desktop/src/components/workspaces/container.tsx b/desktop/src/components/workspaces/container.tsx
index 126a4a60..de950b54 100644
--- a/desktop/src/components/workspaces/container.tsx
+++ b/desktop/src/components/workspaces/container.tsx
@@ -25,7 +25,7 @@ export const Container = observer(() => {
.compile();
const queryId = queryKey[0];
- return await workspace.executeQueryAndSubscribe(queryId, query);
+ return await workspace.queryAndSubscribe(queryId, query);
},
});
diff --git a/desktop/src/components/workspaces/sidebar.tsx b/desktop/src/components/workspaces/sidebar.tsx
index 3263f031..c3f9bcd2 100644
--- a/desktop/src/components/workspaces/sidebar.tsx
+++ b/desktop/src/components/workspaces/sidebar.tsx
@@ -51,7 +51,7 @@ export const Sidebar = observer(() => {
.compile();
const queryId = queryKey[0];
- return await workspace.executeQueryAndSubscribe(queryId, query);
+ return await workspace.queryAndSubscribe(queryId, query);
},
});
diff --git a/desktop/src/components/workspaces/workspace.tsx b/desktop/src/components/workspaces/workspace.tsx
index a80e744a..49b2b7e9 100644
--- a/desktop/src/components/workspaces/workspace.tsx
+++ b/desktop/src/components/workspaces/workspace.tsx
@@ -88,13 +88,19 @@ export const Workspace = observer(() => {
value={{
...workspace,
schema: workspaceDatabase,
- executeQuery: (query) =>
+ mutate: (input) =>
+ window.neuron.executeWorkspaceMutation(
+ workspace.accountId,
+ workspace.id,
+ input,
+ ),
+ query: (query) =>
window.neuron.executeWorkspaceQuery(
workspace.accountId,
workspace.id,
query,
),
- executeQueryAndSubscribe: (queryId, query) =>
+ queryAndSubscribe: (queryId, query) =>
window.neuron.executeWorkspaceQueryAndSubscribe(
workspace.accountId,
workspace.id,
diff --git a/desktop/src/contexts/app-database.tsx b/desktop/src/contexts/app-database.ts
similarity index 100%
rename from desktop/src/contexts/app-database.tsx
rename to desktop/src/contexts/app-database.ts
diff --git a/desktop/src/contexts/workspace.ts b/desktop/src/contexts/workspace.ts
index 56308df9..5bd400f8 100644
--- a/desktop/src/contexts/workspace.ts
+++ b/desktop/src/contexts/workspace.ts
@@ -2,11 +2,13 @@ import { createContext, useContext } from 'react';
import { Workspace } from '@/types/workspaces';
import { CompiledQuery, Kysely, QueryResult } from 'kysely';
import { WorkspaceDatabaseSchema } from '@/data/schemas/workspace';
+import { LocalMutation } from '@/types/mutations';
interface WorkspaceContext extends Workspace {
schema: Kysely<WorkspaceDatabaseSchema>;
- executeQuery: <R>(query: CompiledQuery<R>) => Promise<QueryResult<R>>;
- executeQueryAndSubscribe: <R>(
+ mutate: (mutation: LocalMutation) => Promise<void>;
+ query: <R>(query: CompiledQuery<R>) => Promise<QueryResult<R>>;
+ queryAndSubscribe: <R>(
queryId: string,
query: CompiledQuery<R>,
) => Promise<QueryResult<R>>;
diff --git a/desktop/src/data/account-manager.ts b/desktop/src/data/account-manager.ts
index 3f1a0bbf..a0a3a41f 100644
--- a/desktop/src/data/account-manager.ts
+++ b/desktop/src/data/account-manager.ts
@@ -3,19 +3,17 @@ import { Account } from '@/types/accounts';
import { Workspace } from '@/types/workspaces';
import { WorkspaceManager } from '@/data/workspace-manager';
import { SocketManager } from '@/data/socket-manager';
-import { Update } from '@/types/updates';
+import { ServerMutation } from '@/types/mutations';
export class AccountManager {
private readonly accountPath: string;
private readonly socket: SocketManager;
private readonly workspaces: Map<string, WorkspaceManager>;
private interval: NodeJS.Timeout;
- private sentTransactions: Set<string>;
constructor(account: Account, appPath: string, workspaces: Workspace[]) {
this.socket = new SocketManager(account);
this.workspaces = new Map<string, WorkspaceManager>();
- this.sentTransactions = new Set<string>();
this.accountPath = `${appPath}/${account.id}`;
if (!fs.existsSync(this.accountPath)) {
@@ -28,6 +26,21 @@ export class AccountManager {
new WorkspaceManager(account, workspace, this.accountPath),
);
}
+
+ this.socket.on('mutation', async (mutation) => {
+ const serverMutation = mutation as ServerMutation;
+ const workspace = this.workspaces.get(serverMutation.workspaceId);
+ if (workspace) {
+ await workspace.executeServerMutation(serverMutation);
+ this.socket.send({
+ type: 'mutation_ack',
+ payload: {
+ id: serverMutation.id,
+ workspaceId: serverMutation.workspaceId,
+ },
+ });
+ }
+ });
}
public async init() {
@@ -36,31 +49,6 @@ export class AccountManager {
}
this.socket.init();
- this.socket.on('transaction_ack', async (payload) => {
- const transactionId = payload.id;
- const workspaceId = payload.workspaceId;
-
- if (!this.workspaces.has(workspaceId)) {
- return;
- }
-
- const workspace = this.workspaces.get(workspaceId);
- await workspace?.acknowledgeTransaction(transactionId);
- this.sentTransactions.delete(transactionId);
- });
-
- this.socket.on('update', async (payload) => {
- const update = payload as Update;
- const workspaceId = update.workspaceId;
-
- if (!this.workspaces.has(workspaceId)) {
- return;
- }
-
- const workspace = this.workspaces.get(workspaceId);
- await workspace?.applyUpdate(update);
- });
-
this.startEventLoop();
}
@@ -93,33 +81,14 @@ export class AccountManager {
} catch (error) {
console.error('Error in event loop: ', error);
}
- }, 15);
+ }, 1000);
}
private async executeEventLoop() {
this.socket.checkConnection();
- if (!this.socket.isConnected()) {
- return;
- }
-
for (const workspace of this.workspaces.values()) {
- const transactions = await workspace.getTransactions();
- if (transactions.length === 0) {
- continue;
- }
-
- for (const transaction of transactions) {
- if (this.sentTransactions.has(transaction.id)) {
- continue;
- }
-
- this.sentTransactions.add(transaction.id);
- this.socket.send({
- type: 'transaction',
- payload: transaction,
- });
- }
+ await workspace.sendMutations();
}
}
}
diff --git a/desktop/src/data/migrations/workspace.ts b/desktop/src/data/migrations/workspace.ts
index f2d8c4e6..475f34c6 100644
--- a/desktop/src/data/migrations/workspace.ts
+++ b/desktop/src/data/migrations/workspace.ts
@@ -1,4 +1,4 @@
-import { Migration, sql } from 'kysely';
+import { Migration } from 'kysely';
const createNodesTable: Migration = {
up: async (db) => {
@@ -28,163 +28,22 @@ const createNodesTable: Migration = {
},
};
-const createTransactionsTable: Migration = {
+const createMutationsTable: Migration = {
up: async (db) => {
await db.schema
- .createTable('transactions')
- .addColumn('id', 'text', (col) => col.notNull().primaryKey())
- .addColumn('action', 'text', (col) => col.notNull())
- .addColumn('table', 'text', (col) => col.notNull())
- .addColumn('after', 'json')
- .addColumn('before', 'json')
+ .createTable('mutations')
+ .addColumn('id', 'integer', (col) => col.notNull().primaryKey())
+ .addColumn('type', 'text', (col) => col.notNull())
+ .addColumn('data', 'text', (col) => col.notNull())
.addColumn('created_at', 'text', (col) => col.notNull())
.execute();
},
down: async (db) => {
- await db.schema.dropTable('transactions').execute();
- },
-};
-
-const createNodesInsertTrigger: Migration = {
- up: async (db) => {
- await sql`
- CREATE TRIGGER after_insert_nodes
- AFTER INSERT ON nodes
- FOR EACH ROW
- WHEN NEW.server_version_id is null
- BEGIN
- INSERT INTO transactions ('id', 'action', 'table', 'after', 'created_at')
- VALUES (
- transaction_id(),
- 'insert',
- 'nodes',
- json_object(
- 'id', NEW.'id',
- 'workspace_id', NEW.'workspace_id',
- 'parent_id', NEW.'parent_id',
- 'type', NEW.'type',
- 'index', NEW.'index',
- 'attrs', NEW.'attrs',
- 'content', NEW.'content',
- 'created_at', NEW.'created_at',
- 'updated_at', NEW.'updated_at',
- 'created_by', NEW.'created_by',
- 'updated_by', NEW.'updated_by',
- 'version_id', NEW.'version_id',
- 'server_created_at', NEW.'server_created_at',
- 'server_updated_at', NEW.'server_updated_at',
- 'server_version_id', NEW.'server_version_id'
- ),
- datetime('now')
- );
- END;
- `.execute(db);
- },
- down: async (db) => {
- await sql`DROP TRIGGER after_insert_nodes`.execute(db);
- },
-};
-
-const createNodesUpdateTrigger: Migration = {
- up: async (db) => {
- await sql`
- CREATE TRIGGER after_update_nodes
- AFTER UPDATE ON nodes
- FOR EACH ROW
- WHEN NEW.version_id != NEW.server_version_id
- BEGIN
- INSERT INTO transactions ('id', 'action', 'table', 'before', 'after', 'created_at')
- VALUES (
- transaction_id(),
- 'update',
- 'nodes',
- json_object(
- 'id', OLD.'id',
- 'workspace_id', OLD.'workspace_id',
- 'parent_id', OLD.'parent_id',
- 'type', OLD.'type',
- 'index', OLD.'index',
- 'attrs', OLD.'attrs',
- 'content', OLD.'content',
- 'created_at', OLD.'created_at',
- 'updated_at', OLD.'updated_at',
- 'created_by', OLD.'created_by',
- 'updated_by', OLD.'updated_by',
- 'version_id', OLD.'version_id',
- 'server_created_at', OLD.'server_created_at',
- 'server_updated_at', OLD.'server_updated_at',
- 'server_version_id', OLD.'server_version_id'
- ),
- json_object(
- 'id', NEW.'id',
- 'workspace_id', NEW.'workspace_id',
- 'parent_id', NEW.'parent_id',
- 'type', NEW.'type',
- 'index', NEW.'index',
- 'attrs', NEW.'attrs',
- 'content', NEW.'content',
- 'created_at', NEW.'created_at',
- 'updated_at', NEW.'updated_at',
- 'created_by', NEW.'created_by',
- 'updated_by', NEW.'updated_by',
- 'version_id', NEW.'version_id',
- 'server_created_at', NEW.'server_created_at',
- 'server_updated_at', NEW.'server_updated_at',
- 'server_version_id', NEW.'server_version_id'
- ),
- datetime('now')
- );
- END;
- `.execute(db);
- },
- down: async (db) => {
- await sql`DROP TRIGGER after_update_nodes`.execute(db);
- },
-};
-
-const createDeleteNodesTrigger: Migration = {
- up: async (db) => {
- await sql`
- CREATE TRIGGER after_delete_nodes
- AFTER DELETE ON nodes
- FOR EACH ROW
- BEGIN
- INSERT INTO transactions ('id', 'action', 'table', 'before', 'created_at')
- VALUES (
- transaction_id(),
- 'delete',
- 'nodes',
- json_object(
- 'id', OLD.'id',
- 'workspace_id', OLD.'workspace_id',
- 'parent_id', OLD.'parent_id',
- 'type', OLD.'type',
- 'index', OLD.'index',
- 'attrs', OLD.'attrs',
- 'content', OLD.'content',
- 'created_at', OLD.'created_at',
- 'updated_at', OLD.'updated_at',
- 'created_by', OLD.'created_by',
- 'updated_by', OLD.'updated_by',
- 'version_id', OLD.'version_id',
- 'server_created_at', OLD.'server_created_at',
- 'server_updated_at', OLD.'server_updated_at',
- 'server_version_id', OLD.'server_version_id'
- ),
- datetime('now')
- );
- END;
- `.execute(db);
- },
- down: async (db) => {
- await sql`DROP TRIGGER after_delete_nodes`.execute(db);
+ await db.schema.dropTable('mutations').execute();
},
};
export const workspaceDatabaseMigrations: Record<string, Migration> = {
'202408011756_create_nodes_table': createNodesTable,
- '202408011757_create_transactions_table': createTransactionsTable,
- '202408011758_create_nodes_insert_trigger': createNodesInsertTrigger,
- '202408011759_create_nodes_update_trigger': createNodesUpdateTrigger,
- '202408011800_create_nodes_delete_trigger': createDeleteNodesTrigger,
+ '202408011757_create_mutations_table': createMutationsTable,
};
diff --git a/desktop/src/data/mutation-manager.ts b/desktop/src/data/mutation-manager.ts
new file mode 100644
index 00000000..01561bfd
--- /dev/null
+++ b/desktop/src/data/mutation-manager.ts
@@ -0,0 +1,398 @@
+import { Kysely } from 'kysely';
+import {
+ NodesTableSchema,
+ WorkspaceDatabaseSchema,
+} from '@/data/schemas/workspace';
+import {
+ LocalMutation,
+ LocalDeleteNodesMutation,
+ LocalCreateNodeMutation,
+ LocalUpdateNodeMutation,
+ LocalDeleteNodeMutation,
+ LocalCreateNodesMutation,
+ ServerMutation,
+ ServerCreateNodeMutation,
+ ServerCreateNodesMutation,
+ ServerUpdateNodeMutation,
+} from '@/types/mutations';
+import { Workspace } from '@/types/workspaces';
+import { Account } from '@/types/accounts';
+import { Node } from '@/types/nodes';
+import Axios, { AxiosInstance } from 'axios';
+import { NeuronId } from '@/lib/id';
+
+const SERVER_URL = 'http://localhost:3000';
+
+export class MutationManager {
+ private readonly account: Account;
+ private readonly workspace: Workspace;
+ private readonly database: Kysely<WorkspaceDatabaseSchema>;
+ private readonly axios: AxiosInstance;
+ private readonly mutationListeners: Map<
+ string,
+ (affectedTables: string[]) => void
+ >;
+
+ constructor(
+ account: Account,
+ workspace: Workspace,
+ database: Kysely<WorkspaceDatabaseSchema>,
+ ) {
+ this.account = account;
+ this.workspace = workspace;
+ this.database = database;
+ this.axios = Axios.create({
+ baseURL: SERVER_URL,
+ headers: {
+ Authorization: `Bearer ${account.token}`,
+ },
+ });
+ this.mutationListeners = new Map();
+ }
+
+ public onMutation(listener: (affectedTables: string[]) => void) {
+ const id = NeuronId.generate(NeuronId.Type.Subscriber);
+ this.mutationListeners.set(id, listener);
+ return () => {
+ this.mutationListeners.delete(id);
+ };
+ }
+
+ private notifyMutationListeners(affectedTables: string[]) {
+ for (const listener of this.mutationListeners.values()) {
+ listener(affectedTables);
+ }
+ }
+
+ public async executeLocalMutation(mutation: LocalMutation) {
+ switch (mutation.type) {
+ case 'create_node':
+ return this.executeLocalCreateNodeMutation(mutation);
+ case 'create_nodes':
+ return this.executeLocalCreateNodesMutation(mutation);
+ case 'update_node':
+ return this.executeLocalUpdateNodeMutation(mutation);
+ case 'delete_node':
+ return this.executeLocalDeleteNodeMutation(mutation);
+ case 'delete_nodes':
+ return this.executeLocalDeleteNodesMutation(mutation);
+ }
+ }
+
+ private async executeLocalCreateNodeMutation(
+ mutation: LocalCreateNodeMutation,
+ ) {
+ await this.database.transaction().execute(async (trx) => {
+ const node = mutation.data.node;
+ await trx
+ .insertInto('nodes')
+ .values({
+ id: node.id,
+ type: node.type,
+ parent_id: node.parentId,
+ workspace_id: node.workspaceId,
+ index: node.index,
+ content: node.content ? JSON.stringify(node.content) : null,
+ attrs: node.attrs ? JSON.stringify(node.attrs) : null,
+ created_at: node.createdAt,
+ created_by: node.createdBy,
+ version_id: node.versionId,
+ })
+ .execute();
+
+ await trx
+ .insertInto('mutations')
+ .values({
+ type: mutation.type,
+ data: JSON.stringify(mutation.data),
+ created_at: new Date().toISOString(),
+ })
+ .execute();
+ });
+
+ this.notifyMutationListeners(['nodes']);
+ }
+
+ private async executeLocalCreateNodesMutation(
+ mutation: LocalCreateNodesMutation,
+ ) {
+ await this.database.transaction().execute(async (trx) => {
+ const data = mutation.data;
+ await trx
+ .insertInto('nodes')
+ .values(
+ data.nodes.map((node) => ({
+ id: node.id,
+ type: node.type,
+ parent_id: node.parentId,
+ workspace_id: node.workspaceId,
+ index: node.index,
+ content: node.content ? JSON.stringify(node.content) : null,
+ attrs: node.attrs ? JSON.stringify(node.attrs) : null,
+ created_at: node.createdAt,
+ created_by: node.createdBy,
+ version_id: node.versionId,
+ })),
+ )
+ .execute();
+
+ await trx
+ .insertInto('mutations')
+ .values({
+ type: mutation.type,
+ data: JSON.stringify(mutation.data),
+ created_at: new Date().toISOString(),
+ })
+ .execute();
+ });
+
+ this.notifyMutationListeners(['nodes']);
+ }
+
+ private async executeLocalUpdateNodeMutation(
+ mutation: LocalUpdateNodeMutation,
+ ) {
+ await this.database.transaction().execute(async (trx) => {
+ const data = mutation.data;
+ await trx
+ .updateTable('nodes')
+ .set({
+ type: data.type,
+ parent_id: data.parentId,
+ index: data.index,
+ content: data.content ? JSON.stringify(data.content) : null,
+ attrs: data.attrs ? JSON.stringify(data.attrs) : null,
+ updated_at: data.updatedAt,
+ updated_by: data.updatedBy,
+ version_id: data.versionId,
+ })
+ .where('id', '=', data.id)
+ .execute();
+
+ await trx
+ .insertInto('mutations')
+ .values({
+ type: mutation.type,
+ data: JSON.stringify(mutation.data),
+ created_at: new Date().toISOString(),
+ })
+ .execute();
+ });
+
+ this.notifyMutationListeners(['nodes']);
+ }
+
+ private async executeLocalDeleteNodeMutation(
+ mutation: LocalDeleteNodeMutation,
+ ) {
+ await this.database.transaction().execute(async (trx) => {
+ await trx
+ .deleteFrom('nodes')
+ .where('id', '=', mutation.data.id)
+ .execute();
+
+ await trx
+ .insertInto('mutations')
+ .values({
+ type: mutation.type,
+ data: JSON.stringify(mutation.data),
+ created_at: new Date().toISOString(),
+ })
+ .execute();
+ });
+
+ this.notifyMutationListeners(['nodes']);
+ }
+
+ private async executeLocalDeleteNodesMutation(
+ mutation: LocalDeleteNodesMutation,
+ ) {
+ await this.database.transaction().execute(async (trx) => {
+ await trx
+ .deleteFrom('nodes')
+ .where('id', 'in', mutation.data.ids)
+ .execute();
+
+ await trx
+ .insertInto('mutations')
+ .values({
+ type: mutation.type,
+ data: JSON.stringify(mutation.data),
+ created_at: new Date().toISOString(),
+ })
+ .execute();
+ });
+
+ this.notifyMutationListeners(['nodes']);
+ }
+
+ public async executeServerMutation(mutation: ServerMutation) {
+ switch (mutation.type) {
+ case 'create_node':
+ return this.executeServerCreateNodeMutation(mutation);
+ case 'create_nodes':
+ return this.executeServerCreateNodesMutation(mutation);
+ case 'update_node':
+ return this.executeServerUpdateNodeMutation(mutation);
+ case 'delete_node':
+ return this.executeServerDeleteNodeMutation(mutation);
+ case 'delete_nodes':
+ return this.executeServerDeleteNodesMutation(mutation);
+ }
+ }
+
+ public async executeServerCreateNodeMutation(
+ mutation: ServerCreateNodeMutation,
+ ) {
+ const node = mutation.data.node;
+ await this.syncNodeFromServer(node);
+ }
+
+ public async executeServerCreateNodesMutation(
+ mutation: ServerCreateNodesMutation,
+ ) {
+ for (const node of mutation.data.nodes) {
+ await this.syncNodeFromServer(node);
+ }
+ }
+
+ public async executeServerUpdateNodeMutation(
+ mutation: ServerUpdateNodeMutation,
+ ) {
+ await this.syncNodeFromServer(mutation.data.node);
+ }
+
+ public async executeServerDeleteNodeMutation(
+ mutation: LocalDeleteNodeMutation,
+ ) {
+ await this.database
+ .deleteFrom('nodes')
+ .where('id', '=', mutation.data.id)
+ .execute();
+ }
+
+ public async executeServerDeleteNodesMutation(
+ mutation: LocalDeleteNodesMutation,
+ ) {
+ await this.database
+ .deleteFrom('nodes')
+ .where('id', 'in', mutation.data.ids)
+ .execute();
+
+ this.notifyMutationListeners(['nodes']);
+ }
+
+ public async syncNodeFromServer(node: Node) {
+ const existingNode = await this.database
+ .selectFrom('nodes')
+ .selectAll()
+ .where('id', '=', node.id)
+ .executeTakeFirst();
+
+ if (!existingNode) {
+ await this.database
+ .insertInto('nodes')
+ .values({
+ id: node.id,
+ type: node.type,
+ parent_id: node.parentId,
+ workspace_id: node.workspaceId,
+ index: node.index,
+ content: node.content ? JSON.stringify(node.content) : null,
+ attrs: node.attrs ? JSON.stringify(node.attrs) : null,
+ created_at: node.createdAt,
+ created_by: node.createdBy,
+ updated_by: node.updatedBy,
+ updated_at: node.updatedAt,
+ version_id: node.versionId,
+ server_created_at: node.serverCreatedAt,
+ server_updated_at: node.serverUpdatedAt,
+ server_version_id: node.serverVersionId,
+ })
+ .execute();
+
+ this.notifyMutationListeners(['nodes']);
+ return;
+ }
+
+ if (this.shouldUpdateNodeFromServer(existingNode, node)) {
+ await this.database
+ .updateTable('nodes')
+ .set({
+ type: node.type,
+ parent_id: node.parentId,
+ index: node.index,
+ content: node.content ? JSON.stringify(node.content) : null,
+ attrs: node.attrs ? JSON.stringify(node.attrs) : null,
+ updated_at: node.updatedAt,
+ updated_by: node.updatedBy,
+ version_id: node.versionId,
+ server_created_at: node.serverCreatedAt,
+ server_updated_at: node.serverUpdatedAt,
+ server_version_id: node.serverVersionId,
+ })
+ .where('id', '=', node.id)
+ .execute();
+
+ this.notifyMutationListeners(['nodes']);
+ }
+ }
+
+ public shouldUpdateNodeFromServer(
+ localNode: NodesTableSchema,
+ serverNode: Node,
+ ) {
+ if (localNode.server_version_id === serverNode.serverVersionId) {
+ return false;
+ }
+
+ if (localNode.updated_at) {
+ if (!serverNode.updatedAt) {
+ return false;
+ }
+
+ const localUpdatedAt = new Date(localNode.updated_at);
+ const serverUpdatedAt = new Date(serverNode.updatedAt);
+
+ if (localUpdatedAt > serverUpdatedAt) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ public async sendMutations() {
+ do {
+ const nextMutation = await this.database
+ .selectFrom('mutations')
+ .selectAll()
+ .orderBy('id')
+ .limit(1)
+ .executeTakeFirst();
+
+ if (!nextMutation) {
+ break;
+ }
+
+ const { data, status } = await this.axios.post<ServerMutation>(
+ 'v1/mutations',
+ {
+ deviceId: this.account.deviceId,
+ workspaceId: this.workspace.id,
+ type: nextMutation.type,
+ data: JSON.parse(nextMutation.data),
+ },
+ );
+
+ if (status === 200) {
+ await this.database
+ .deleteFrom('mutations')
+ .where('id', '=', nextMutation.id)
+ .execute();
+
+ await this.executeServerMutation(data);
+ }
+ } while (true);
+ }
+}
diff --git a/desktop/src/data/schemas/workspace.ts b/desktop/src/data/schemas/workspace.ts
index c4e8f597..48178fd5 100644
--- a/desktop/src/data/schemas/workspace.ts
+++ b/desktop/src/data/schemas/workspace.ts
@@ -16,16 +16,14 @@ export interface NodesTableSchema {
server_version_id: string | null;
}
-export interface TransactionsTableSchema {
- id: string;
- action: string;
- table: string;
- after?: string | null;
- before?: string | null;
+export interface MutationsTableSchema {
+ id: number;
+ type: string;
+ data: string;
created_at: string;
}
export interface WorkspaceDatabaseSchema {
nodes: NodesTableSchema;
- transactions: TransactionsTableSchema;
+ mutations: MutationsTableSchema;
}
diff --git a/desktop/src/data/utils.ts b/desktop/src/data/utils.ts
index 516d9d18..8da8457b 100644
--- a/desktop/src/data/utils.ts
+++ b/desktop/src/data/utils.ts
@@ -1,6 +1,5 @@
import SQLite from 'better-sqlite3';
import { Parser, AST, Select } from 'node-sql-parser';
-import { NeuronId } from '@/lib/id';
import { QueryResult } from 'kysely';
const parser = new Parser();
@@ -9,10 +8,6 @@ const parseOptions = { database: 'Sqlite' };
export const buildSqlite = (filename: string) => {
const database = new SQLite(filename);
database.pragma('journal_mode = WAL');
- database.function('transaction_id', () =>
- NeuronId.generate(NeuronId.Type.Transaction),
- );
-
return database;
};
diff --git a/desktop/src/data/workspace-manager.ts b/desktop/src/data/workspace-manager.ts
index 76e8debc..126a174f 100644
--- a/desktop/src/data/workspace-manager.ts
+++ b/desktop/src/data/workspace-manager.ts
@@ -17,22 +17,16 @@ import {
resultHasChanged,
} from '@/data/utils';
import { SubscribedQueryData } from '@/types/databases';
-import {
- Transaction,
- TransactionAction,
- TransactionTable,
-} from '@/types/transactions';
+import { LocalMutation, ServerMutation } from '@/types/mutations';
import { eventBus } from '@/lib/event-bus';
-import { Update } from '@/types/updates';
-import { Node } from '@/types/nodes';
+import { MutationManager } from '@/data/mutation-manager';
export class WorkspaceManager {
private readonly account: Account;
private readonly workspace: Workspace;
private readonly database: Kysely<WorkspaceDatabaseSchema>;
private readonly subscribers: Map<string, SubscribedQueryData<unknown>>;
-
- private lastChangeAt: Date = new Date();
+ private readonly mutationManager: MutationManager;
constructor(account: Account, workspace: Workspace, accountPath: string) {
this.account = account;
@@ -51,19 +45,14 @@ export class WorkspaceManager {
this.database = new Kysely<WorkspaceDatabaseSchema>({
dialect,
});
- }
-
- public async init() {
- await this.migrate();
- }
-
- public async execute<T>(query: CompiledQuery<T>): Promise<QueryResult<T>> {
- const result = await this.database.executeQuery(query);
- if (result.numAffectedRows > 0) {
- this.lastChangeAt = new Date();
- const affectedTables = extractTablesFromSql(query.sql);
+ this.mutationManager = new MutationManager(
+ account,
+ workspace,
+ this.database,
+ );
+ this.mutationManager.onMutation(async (affectedTables) => {
for (const [subscriberId, subscriberData] of this.subscribers) {
const hasAffectedTables = subscriberData.tables.some((table) =>
affectedTables.includes(table),
@@ -87,16 +76,33 @@ export class WorkspaceManager {
});
}
}
+ });
+ }
+
+ public async init() {
+ await this.migrate();
+ }
+
+ public async executeQuery<T>(
+ query: CompiledQuery<T>,
+ ): Promise<QueryResult<T>> {
+ const result = await this.database.executeQuery(query);
+
+ //only transactions should have side effects
+ if (result.numAffectedRows > 0) {
+ throw new Error('Query should not have any side effects');
}
return result;
}
- public async executeAndSubscribe<T>(
+ public async executeQueryAndSubscribe<T>(
queryId: string,
query: CompiledQuery<T>,
): Promise<QueryResult<T>> {
const result = await this.database.executeQuery(query);
+
+ // only transactions should have side effects
if (result.numAffectedRows > 0) {
throw new Error('Query should not have any side effects');
}
@@ -112,100 +118,20 @@ export class WorkspaceManager {
return result;
}
- public unsubscribe(subscriberId: string) {
- this.subscribers.delete(subscriberId);
+ public unsubscribeQuery(queryId: string) {
+ this.subscribers.delete(queryId);
}
- public async getTransactions(): Promise<Transaction[]> {
- const transactions = await this.database
- .selectFrom('transactions')
- .selectAll()
- .execute();
-
- if (transactions.length === 0) {
- return [];
- }
-
- return transactions.map((row) => {
- return {
- id: row.id,
- workspaceId: this.workspace.id,
- action: row.action as TransactionAction,
- table: row.table as TransactionTable,
- after: row.after,
- before: row.before,
- createdAt: row.created_at,
- };
- });
+ public async executeLocalMutation(mutation: LocalMutation) {
+ await this.mutationManager.executeLocalMutation(mutation);
}
- public async acknowledgeTransaction(transactionId: string) {
- await this.database
- .deleteFrom('transactions')
- .where('id', '=', transactionId)
- .execute();
+ public async executeServerMutation(mutation: ServerMutation) {
+ await this.mutationManager.executeServerMutation(mutation);
}
- public async applyUpdate(update: Update) {
- if (update.type === 'node_sync') {
- await this.handleNodeSync(update);
- }
- }
-
- private async handleNodeSync(update: Update) {
- const node = JSON.parse(JSON.parse(update.content)) as Node;
- const existingNode = await this.database
- .selectFrom('nodes')
- .selectAll()
- .where('id', '=', node.id)
- .executeTakeFirst();
-
- if (!existingNode) {
- const query = this.database
- .insertInto('nodes')
- .values({
- id: node.id,
- type: node.type,
- parent_id: node.parentId,
- workspace_id: this.workspace.id,
- index: node.index,
- content: node.content as any,
- attrs: node.attrs as any,
- created_at: node.createdAt,
- created_by: node.createdBy,
- updated_at: node.updatedAt,
- updated_by: node.updatedBy,
- version_id: node.versionId,
- server_created_at: node.serverCreatedAt,
- server_updated_at: node.serverUpdatedAt,
- server_version_id: node.serverVersionId,
- })
- .compile();
- console.log('query', query);
-
- await this.execute(query);
- } else {
- //TODO: check if the local version has changes before updating
- const query = this.database
- .updateTable('nodes')
- .set({
- type: node.type,
- parent_id: node.parentId,
- index: node.index,
- content: node.content as any,
- attrs: node.attrs as any,
- updated_at: node.updatedAt,
- updated_by: node.updatedBy,
- version_id: node.versionId,
- server_created_at: node.serverCreatedAt,
- server_updated_at: node.serverUpdatedAt,
- server_version_id: node.serverVersionId,
- })
- .where('id', '=', node.id)
- .compile();
-
- await this.execute(query);
- }
+ public async sendMutations() {
+ await this.mutationManager.sendMutations();
}
private async migrate() {
diff --git a/desktop/src/hooks/use-conversation.tsx b/desktop/src/hooks/use-conversation.tsx
index 9036c71b..603f4bdc 100644
--- a/desktop/src/hooks/use-conversation.tsx
+++ b/desktop/src/hooks/use-conversation.tsx
@@ -1,4 +1,4 @@
-import { Node } from '@/types/nodes';
+import { Node, NodeBlock } from '@/types/nodes';
import { useWorkspace } from '@/contexts/workspace';
import { JSONContent } from '@tiptap/core';
import { NeuronId } from '@/lib/id';
@@ -9,6 +9,7 @@ import { sql } from 'kysely';
import { NodesTableSchema } from '@/data/schemas/workspace';
import { hashCode } from '@/lib/utils';
import { MessageNode } from '@/types/messages';
+import { LocalCreateNodesMutation } from '@/types/mutations';
interface useConversationResult {
isLoading: boolean;
@@ -19,15 +20,6 @@ interface useConversationResult {
createMessage: (content: JSONContent) => void;
}
-interface CreateNodeInput {
- id: string;
- parentId: string;
- type: string;
- attrs: Record<string, any>;
- content?: JSONContent[];
- index?: string | null;
-}
-
export const useConversation = (
conversationId: string,
): useConversationResult => {
@@ -53,7 +45,7 @@ export const useConversation = (
`.compile(workspace.schema);
const queryId = queryKey[0];
- return await workspace.executeQueryAndSubscribe(queryId, query);
+ return await workspace.queryAndSubscribe(queryId, query);
},
});
@@ -77,33 +69,27 @@ export const useConversation = (
.compile();
const queryId = queryKey[0];
- return await workspace.executeQueryAndSubscribe(queryId, query);
+ return await workspace.queryAndSubscribe(queryId, query);
},
});
const createMessage = async (content: JSONContent) => {
- const inputs = buildMessageCreateInputs(conversationId, content);
- const createMessageQuery = workspace.schema
- .insertInto('nodes')
- .values(
- inputs.map((input) => {
- return {
- id: input.id,
- type: input.type,
- index: input.index,
- parent_id: input.parentId,
- workspace_id: workspace.id,
- created_at: new Date().toISOString(),
- created_by: workspace.userId,
- version_id: NeuronId.generate(NeuronId.Type.Version),
- attrs: input.attrs && JSON.stringify(input.attrs),
- content: input.content && JSON.stringify(input.content),
- };
- }),
- )
- .compile();
+ const mutation: LocalCreateNodesMutation = {
+ type: 'create_nodes',
+ data: {
+ nodes: [],
+ },
+ };
+
+ buildMessageCreateNodes(
+ mutation,
+ workspace.userId,
+ workspace.id,
+ conversationId,
+ content,
+ );
- await workspace.executeQuery(createMessageQuery);
+ await workspace.mutate(mutation);
};
const conversationNodes =
@@ -155,42 +141,32 @@ const buildMessages = (nodes: Node[], authors: Node[]): MessageNode[] => {
});
};
-const buildMessageCreateInputs = (
- conversationId: string,
- content: JSONContent,
-): CreateNodeInput[] => {
- const inputs: CreateNodeInput[] = [];
- buildMessageCreateInput(content, conversationId, inputs);
- return inputs;
-};
-
-const buildMessageCreateInput = (
- content: JSONContent,
+const buildMessageCreateNodes = (
+ mutation: LocalCreateNodesMutation,
+ userId: string,
+ workspaceId: string,
parentId: string,
- queue: CreateNodeInput[],
+ content: JSONContent,
index?: string | null,
-): void => {
- const id = content.attrs?.id ?? NeuronId.generate(NeuronId.Type.Message);
- const input: CreateNodeInput = {
- id: id,
- parentId,
- type: content.type,
- attrs: {},
- index,
- };
- queue.push(input);
+) => {
+ const id =
+ content.attrs?.id ??
+ NeuronId.generate(NeuronId.getIdTypeFromNode(content.type));
+
+ let attrs = content.attrs ? { ...content.attrs } : null;
+ if (attrs) {
+ delete attrs.id;
- if (content.attrs) {
- delete content.attrs.id;
- if (Object.keys(content.attrs).length > 0) {
- input.attrs = content.attrs;
+ if (Object.keys(attrs).length === 0) {
+ attrs = null;
}
}
+ let nodeContent: NodeBlock[] | null = null;
if (LeafNodeTypes.includes(content.type)) {
- input.content = [];
+ nodeContent = [];
for (const child of content.content) {
- input.content.push({
+ nodeContent.push({
type: child.type,
text: child.text,
marks: child.marks?.map((mark) => {
@@ -201,11 +177,37 @@ const buildMessageCreateInput = (
}),
});
}
- } else {
+ }
+
+ if (nodeContent && nodeContent.length === 0) {
+ nodeContent = null;
+ }
+
+ mutation.data.nodes.push({
+ id: id,
+ parentId,
+ workspaceId: workspaceId,
+ type: content.type,
+ attrs: attrs,
+ index: index,
+ content: nodeContent,
+ createdAt: new Date().toISOString(),
+ createdBy: userId,
+ versionId: NeuronId.generate(NeuronId.Type.Version),
+ });
+
+ if (nodeContent == null && content.content && content.content.length > 0) {
let lastIndex: string | null = null;
for (const child of content.content) {
lastIndex = generateNodeIndex(lastIndex, null);
- buildMessageCreateInput(child, id, queue, lastIndex);
+ buildMessageCreateNodes(
+ mutation,
+ userId,
+ workspaceId,
+ id,
+ child,
+ lastIndex,
+ );
}
}
};
diff --git a/desktop/src/lib/constants.ts b/desktop/src/lib/constants.ts
index 160381e6..b2c57b2b 100644
--- a/desktop/src/lib/constants.ts
+++ b/desktop/src/lib/constants.ts
@@ -1,4 +1,5 @@
export const NodeTypes = {
+ User: 'user',
Space: 'space',
Page: 'page',
Channel: 'channel',
@@ -28,4 +29,4 @@ export const RootNodeTypes: string[] = [
NodeTypes.Message,
NodeTypes.Page,
NodeTypes.Channel,
-];
\ No newline at end of file
+];
diff --git a/desktop/src/lib/id.ts b/desktop/src/lib/id.ts
index 7dc0eaf9..6692e516 100644
--- a/desktop/src/lib/id.ts
+++ b/desktop/src/lib/id.ts
@@ -1,4 +1,5 @@
import { monotonicFactory } from 'ulid';
+import { NodeTypes } from '@/lib/constants';
const ulid = monotonicFactory();
@@ -7,13 +8,23 @@ enum IdType {
Workspace = 'wc',
User = 'us',
Version = 've',
- Transaction = 'tr',
+ Mutation = 'mu',
Space = 'sp',
Page = 'pg',
Channel = 'ch',
Node = 'nd',
Message = 'ms',
Subscriber = 'sb',
+ Paragraph = 'pa',
+ Heading = 'he',
+ Blockquote = 'bq',
+ CodeBlock = 'cb',
+ ListItem = 'li',
+ OrderedList = 'ol',
+ BulletList = 'bl',
+ TaskList = 'tl',
+ TaskItem = 'ti',
+ Divider = 'di',
}
export class NeuronId {
@@ -31,22 +42,36 @@ export class NeuronId {
public static getIdTypeFromNode(nodeType: string): IdType {
switch (nodeType) {
- case 'account':
- return IdType.Account;
- case 'workspace':
- return IdType.Workspace;
- case 'user':
+ case NodeTypes.User:
return IdType.User;
- case 'version':
- return IdType.Version;
- case 'transaction':
- return IdType.Transaction;
- case 'space':
+ case NodeTypes.Space:
return IdType.Space;
- case 'page':
+ case NodeTypes.Page:
return IdType.Page;
- case 'channel':
+ case NodeTypes.Channel:
return IdType.Channel;
+ case NodeTypes.Message:
+ return IdType.Message;
+ case NodeTypes.Paragraph:
+ return IdType.Paragraph;
+ case NodeTypes.Heading:
+ return IdType.Heading;
+ case NodeTypes.Blockquote:
+ return IdType.Blockquote;
+ case NodeTypes.BulletList:
+ return IdType.BulletList;
+ case NodeTypes.CodeBlock:
+ return IdType.CodeBlock;
+ case NodeTypes.ListItem:
+ return IdType.ListItem;
+ case NodeTypes.OrderedList:
+ return IdType.OrderedList;
+ case NodeTypes.TaskList:
+ return IdType.TaskList;
+ case NodeTypes.TaskItem:
+ return IdType.TaskItem;
+ case NodeTypes.Divider:
+ return IdType.Divider;
default:
return IdType.Node;
}
diff --git a/desktop/src/main.ts b/desktop/src/main.ts
index 80fd5916..d0fc73a0 100644
--- a/desktop/src/main.ts
+++ b/desktop/src/main.ts
@@ -3,6 +3,7 @@ import path from 'path';
import { eventBus } from '@/lib/event-bus';
import { appManager } from '@/data/app-manager';
import { CompiledQuery } from 'kysely';
+import { LocalMutation } from '@/types/mutations';
let subscriptionId: string | null = null;
@@ -73,6 +74,28 @@ ipcMain.handle('execute-app-query', async (_, query: CompiledQuery) => {
return await appManager.execute(query);
});
+ipcMain.handle(
+ 'execute-workspace-mutation',
+ async (
+ _,
+ accountId: string,
+ workspaceId: string,
+ mutation: LocalMutation,
+ ) => {
+ const accountManager = await appManager.getAccount(accountId);
+ if (!accountManager) {
+ throw new Error(`Account not found: ${accountId}`);
+ }
+
+ const workspaceManager = accountManager.getWorkspace(workspaceId);
+ if (!workspaceManager) {
+ throw new Error(`Workspace not found: ${workspaceId}`);
+ }
+
+ return await workspaceManager.executeLocalMutation(mutation);
+ },
+);
+
ipcMain.handle(
'execute-workspace-query',
async (_, accountId: string, workspaceId: string, query: CompiledQuery) => {
@@ -86,7 +109,7 @@ ipcMain.handle(
throw new Error(`Workspace not found: ${workspaceId}`);
}
- return await workspaceManager.execute(query);
+ return await workspaceManager.executeQuery(query);
},
);
@@ -109,7 +132,7 @@ ipcMain.handle(
throw new Error(`Workspace not found: ${workspaceId}`);
}
- return await workspaceManager.executeAndSubscribe(queryId, query);
+ return await workspaceManager.executeQueryAndSubscribe(queryId, query);
},
);
@@ -126,6 +149,6 @@ ipcMain.handle(
throw new Error(`Workspace not found: ${workspaceId}`);
}
- workspaceManager.unsubscribe(queryId);
+ workspaceManager.unsubscribeQuery(queryId);
},
);
diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts
index 14db054c..3825b98b 100644
--- a/desktop/src/preload.ts
+++ b/desktop/src/preload.ts
@@ -1,6 +1,7 @@
import { contextBridge, ipcRenderer } from 'electron';
import { eventBus, Event } from '@/lib/event-bus';
import { CompiledQuery, QueryResult } from 'kysely';
+import { LocalMutation } from '@/types/mutations';
contextBridge.exposeInMainWorld('neuron', {
init: () => ipcRenderer.invoke('init'),
@@ -8,11 +9,17 @@ contextBridge.exposeInMainWorld('neuron', {
executeAppQuery: <R>(query: CompiledQuery<R>): Promise<QueryResult<R>> =>
ipcRenderer.invoke('execute-app-query', query),
- executeAccountQuery: <R>(
+ executeWorkspaceMutation: (
accountId: string,
- query: CompiledQuery<R>,
- ): Promise<QueryResult<R>> =>
- ipcRenderer.invoke('execute-account-query', accountId, query),
+ workspaceId: string,
+ mutation: LocalMutation,
+ ): Promise<void> =>
+ ipcRenderer.invoke(
+ 'execute-workspace-mutation',
+ accountId,
+ workspaceId,
+ mutation,
+ ),
executeWorkspaceQuery: <R>(
accountId: string,
diff --git a/desktop/src/types/mutations.ts b/desktop/src/types/mutations.ts
new file mode 100644
index 00000000..c3e0224e
--- /dev/null
+++ b/desktop/src/types/mutations.ts
@@ -0,0 +1,121 @@
+import { Node, NodeBlock } from '@/types/nodes';
+
+export type LocalMutation =
+ | LocalCreateNodeMutation
+ | LocalCreateNodesMutation
+ | LocalUpdateNodeMutation
+ | LocalDeleteNodeMutation
+ | LocalDeleteNodesMutation;
+
+export type LocalCreateNodeMutation = {
+ type: 'create_node';
+ data: {
+ node: LocalCreateNodeData;
+ };
+};
+
+export type LocalCreateNodesMutation = {
+ type: 'create_nodes';
+ data: {
+ nodes: LocalCreateNodeData[];
+ };
+};
+
+export type LocalCreateNodeData = {
+ id: string;
+ type: string;
+ parentId: string | null;
+ workspaceId: string;
+ index: string | null;
+ content: NodeBlock[];
+ attrs: Record<string, any>;
+ createdAt: string;
+ createdBy: string;
+ versionId: string;
+};
+
+export type LocalUpdateNodeMutation = {
+ type: 'update_node';
+ data: {
+ id: string;
+ type: string;
+ parentId: string | null;
+ index: string | null;
+ content: NodeBlock[];
+ attrs: Record<string, any>;
+ updatedAt: string;
+ updatedBy: string;
+ versionId: string;
+ };
+};
+
+export type LocalDeleteNodeMutation = {
+ type: 'delete_node';
+ data: {
+ id: string;
+ };
+};
+
+export type LocalDeleteNodesMutation = {
+ type: 'delete_nodes';
+ data: {
+ ids: string[];
+ };
+};
+
+export type ServerMutation =
+ | ServerCreateNodeMutation
+ | ServerCreateNodesMutation
+ | ServerUpdateNodeMutation
+ | ServerDeleteNodeMutation
+ | ServerDeleteNodesMutation;
+
+export type ServerCreateNodeMutation = {
+ id: string;
+ type: 'create_node';
+ workspaceId: string;
+ data: {
+ node: Node;
+ };
+ createdAt: string;
+};
+
+export type ServerCreateNodesMutation = {
+ id: string;
+ type: 'create_nodes';
+ workspaceId: string;
+ data: {
+ nodes: Node[];
+ };
+ createdAt: string;
+};
+
+export type ServerUpdateNodeMutation = {
+ id: string;
+ type: 'update_node';
+ workspaceId: string;
+ data: {
+ node: Node;
+ };
+ createdAt: string;
+};
+
+export type ServerDeleteNodeMutation = {
+ id: string;
+ type: 'delete_node';
+ workspaceId: string;
+ data: {
+ id: string;
+ };
+ createdAt: string;
+};
+
+export type ServerDeleteNodesMutation = {
+ id: string;
+ type: 'delete_nodes';
+ workspaceId: string;
+ data: {
+ ids: string[];
+ };
+ createdAt: string;
+};
diff --git a/desktop/src/types/transactions.ts b/desktop/src/types/transactions.ts
deleted file mode 100644
index f8162fbd..00000000
--- a/desktop/src/types/transactions.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-export type TransactionTable = 'nodes';
-
-export type TransactionAction = 'insert' | 'update' | 'delete';
-
-export type Transaction = {
- id: string;
- workspaceId: string;
- action: TransactionAction;
- table: TransactionTable;
- after?: string | null;
- before?: string | null;
- createdAt: string;
-};
diff --git a/desktop/src/types/window.d.ts b/desktop/src/types/window.d.ts
index 736e43de..2ceeb077 100644
--- a/desktop/src/types/window.d.ts
+++ b/desktop/src/types/window.d.ts
@@ -1,12 +1,19 @@
import { GlobalDatabaseData } from '@/types/global';
import { EventBus } from '@/lib/event-bus';
import { CompiledQuery, QueryResult } from 'kysely';
+import { LocalMutationInput } from '@/types/mutations';
interface NeuronApi {
init: () => Promise<GlobalDatabaseData>;
logout: (accountId: string) => Promise<void>;
executeAppQuery: (query: CompiledQuery<R>) => Promise<QueryResult<R>>;
+ executeWorkspaceMutation: (
+ accountId: string,
+ workspaceId: string,
+ input: LocalMutationInput,
+ ) => Promise<void>;
+
executeWorkspaceQuery: (
accountId: string,
workspaceId: string,
diff --git a/server/prisma/migrations/20240822171526_init_db/migration.sql b/server/prisma/migrations/20240824195639_init_db/migration.sql
similarity index 95%
rename from server/prisma/migrations/20240822171526_init_db/migration.sql
rename to server/prisma/migrations/20240824195639_init_db/migration.sql
index 7e42f109..79259042 100644
--- a/server/prisma/migrations/20240822171526_init_db/migration.sql
+++ b/server/prisma/migrations/20240824195639_init_db/migration.sql
@@ -85,15 +85,16 @@ CREATE TABLE "account_devices" (
);
-- CreateTable
-CREATE TABLE "updates" (
+CREATE TABLE "mutations" (
"id" VARCHAR(30) NOT NULL,
- "workspace_id" VARCHAR(30) NOT NULL,
"type" VARCHAR(30) NOT NULL,
- "content" JSONB,
+ "workspace_id" VARCHAR(30) NOT NULL,
+ "data" JSONB,
"created_at" TIMESTAMPTZ(6) NOT NULL,
+ "device_id" VARCHAR(30),
"devices" TEXT[],
- CONSTRAINT "updates_pkey" PRIMARY KEY ("id")
+ CONSTRAINT "mutations_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
diff --git a/server/prisma/schema.prisma b/server/prisma/schema.prisma
index 96d8b0a5..9ecfa115 100644
--- a/server/prisma/schema.prisma
+++ b/server/prisma/schema.prisma
@@ -92,13 +92,14 @@ model accountDevices {
@@map("account_devices")
}
-model updates {
+model mutations {
id String @id @db.VarChar(30)
- workspaceId String @db.VarChar(30) @map("workspace_id")
type String @db.VarChar(30)
- content Json?
+ workspaceId String @db.VarChar(30) @map("workspace_id")
+ data Json?
createdAt DateTime @db.Timestamptz(6) @map("created_at")
+ deviceId String? @db.VarChar(30) @map("device_id")
devices String[]
- @@map("updates")
+ @@map("mutations")
}
diff --git a/server/src/api.ts b/server/src/api.ts
index d9b29bee..60e6ff69 100644
--- a/server/src/api.ts
+++ b/server/src/api.ts
@@ -5,10 +5,11 @@ import { WebSocketServer } from 'ws';
import { accountsRouter } from '@/routes/accounts';
import { workspacesRouter } from '@/routes/workspaces';
+import { mutationsRouter } from '@/routes/mutations';
import { authMiddleware } from '@/middlewares/auth';
import { sockets } from '@/lib/sockets';
import { SocketMessage } from '@/types/sockets';
-import { producer, TOPIC_NAMES } from '@/data/kafka';
+import { prisma } from '@/data/prisma';
export const initApi = () => {
const app = express();
@@ -23,6 +24,7 @@ export const initApi = () => {
app.use('/v1/accounts', accountsRouter);
app.use('/v1/workspaces', authMiddleware, workspacesRouter);
+ app.use('/v1/mutations', authMiddleware, mutationsRouter);
const server = http.createServer(app);
@@ -43,25 +45,17 @@ export const initApi = () => {
socket.on('message', async (message) => {
const socketMessage: SocketMessage = JSON.parse(message.toString());
- if (socketMessage.type === 'transaction') {
- await producer.send({
- topic: TOPIC_NAMES.TRANSACTIONS,
- messages: [
- {
- key: socketMessage.payload.id,
- value: JSON.stringify(socketMessage.payload),
- },
- ],
- });
+ if (socketMessage.type === 'mutation_ack') {
+ const mutationId = socketMessage.payload.id;
+ if (!mutationId) {
+ return;
+ }
- const ackMessage: SocketMessage = {
- type: 'transaction_ack',
- payload: {
- id: socketMessage.payload.id,
- workspaceId: socketMessage.payload.workspaceId,
- },
- };
- socket.send(JSON.stringify(ackMessage));
+ await prisma.$executeRaw`
+ UPDATE mutations
+ SET devices = array_remove(devices, ${deviceId})
+ WHERE id = ${mutationId};
+ `;
}
});
diff --git a/server/src/consumers/mutation-changes.ts b/server/src/consumers/mutation-changes.ts
new file mode 100644
index 00000000..8fe22786
--- /dev/null
+++ b/server/src/consumers/mutation-changes.ts
@@ -0,0 +1,80 @@
+import { kafka, TOPIC_NAMES, CONSUMER_IDS } from '@/data/kafka';
+import { ChangeMessage, MutationChangeData } from '@/types/changes';
+import { redis, CHANNEL_NAMES } from '@/data/redis';
+import { PostgresOperation } from '@/lib/constants';
+import { prisma } from '@/data/prisma';
+
+export const initMutationChangesConsumer = async () => {
+ const consumer = kafka.consumer({ groupId: CONSUMER_IDS.MUTATION_CHANGES });
+
+ await consumer.connect();
+ await consumer.subscribe({ topic: TOPIC_NAMES.MUTATION_CHANGES });
+
+ await consumer.run({
+ eachMessage: async ({ message }) => {
+ if (!message || !message.value) {
+ return;
+ }
+
+ const change = JSON.parse(
+ message.value.toString(),
+ ) as ChangeMessage<MutationChangeData>;
+
+ await handleMutationChange(change);
+ },
+ });
+};
+
+const handleMutationChange = async (
+ change: ChangeMessage<MutationChangeData>,
+) => {
+ switch (change.op) {
+ case PostgresOperation.CREATE: {
+ await handleMutationCreate(change);
+ break;
+ }
+ case PostgresOperation.UPDATE: {
+ await handleMutationUpdate(change);
+ break;
+ }
+ case PostgresOperation.DELETE: {
+ await handleMutationDelete(change);
+ break;
+ }
+ }
+};
+
+const handleMutationCreate = async (
+ change: ChangeMessage<MutationChangeData>,
+) => {
+ const mutationData = change.after;
+ if (!mutationData) {
+ return;
+ }
+
+ await redis.publish(CHANNEL_NAMES.MUTATIONS, JSON.stringify(mutationData));
+};
+
+const handleMutationUpdate = async (
+ change: ChangeMessage<MutationChangeData>,
+) => {
+ const mutationData = change.after;
+ if (!mutationData) {
+ return;
+ }
+
+ // if all devices have acknowledged the mutation, delete it
+ if (mutationData.devices == null || mutationData.devices.length == 0) {
+ await prisma.mutations.delete({
+ where: {
+ id: mutationData.id,
+ },
+ });
+ }
+};
+
+const handleMutationDelete = async (
+ change: ChangeMessage<MutationChangeData>,
+) => {
+ console.log('Mutation delete:', change.before?.id);
+};
diff --git a/server/src/consumers/mutations.ts b/server/src/consumers/mutations.ts
new file mode 100644
index 00000000..1f2892cb
--- /dev/null
+++ b/server/src/consumers/mutations.ts
@@ -0,0 +1,39 @@
+import { redis, CHANNEL_NAMES } from '@/data/redis';
+import { sockets } from '@/lib/sockets';
+import { SocketMessage } from '@/types/sockets';
+import { MutationChangeData } from '@/types/changes';
+import { ServerMutation } from '@/types/mutations';
+
+export const initMutationsSubscriber = async () => {
+ const subscriber = redis.duplicate();
+ await subscriber.connect();
+ await subscriber.subscribe(CHANNEL_NAMES.MUTATIONS, handleMessage);
+};
+
+const handleMessage = async (message: string) => {
+ const mutationData = JSON.parse(message) as MutationChangeData;
+ if (!mutationData.devices || !mutationData.devices.length) {
+ return;
+ }
+
+ const mutation: ServerMutation = {
+ id: mutationData.id,
+ type: mutationData.type as any,
+ workspaceId: mutationData.workspace_id,
+ data: JSON.parse(mutationData.data),
+ createdAt: mutationData.created_at,
+ };
+
+ for (const deviceId of mutationData.devices) {
+ const socket = sockets.getSocket(deviceId);
+ if (!socket) {
+ return;
+ }
+
+ const socketMessage: SocketMessage = {
+ type: 'mutation',
+ payload: mutation,
+ };
+ socket.send(JSON.stringify(socketMessage));
+ }
+};
diff --git a/server/src/consumers/node-changes.ts b/server/src/consumers/node-changes.ts
deleted file mode 100644
index 360f85d9..00000000
--- a/server/src/consumers/node-changes.ts
+++ /dev/null
@@ -1,90 +0,0 @@
-import { kafka, TOPIC_NAMES, CONSUMER_IDS } from '@/data/kafka';
-import { ChangeMessage, NodeChangeData } from '@/types/changes';
-import { prisma } from '@/data/prisma';
-import { NeuronId } from '@/lib/id';
-import { Node } from '@/types/nodes';
-
-export const initNodeChangesConsumer = async () => {
- const consumer = kafka.consumer({ groupId: CONSUMER_IDS.NODE_CHANGES });
-
- await consumer.connect();
- await consumer.subscribe({ topic: TOPIC_NAMES.NODE_CHANGES });
-
- await consumer.run({
- eachMessage: async ({ message }) => {
- if (!message || !message.value) {
- return;
- }
-
- const change = JSON.parse(
- message.value.toString(),
- ) as ChangeMessage<NodeChangeData>;
-
- await handleNodeChange(change);
- },
- });
-};
-
-const handleNodeChange = async (change: ChangeMessage<NodeChangeData>) => {
- const changeData = change.after;
- if (!changeData) {
- return;
- }
-
- const workspaceAccounts = await prisma.workspaceAccounts.findMany({
- where: {
- workspaceId: changeData.workspace_id,
- },
- });
-
- if (workspaceAccounts.length === 0) {
- return;
- }
-
- const accountIds = workspaceAccounts.map((account) => account.accountId);
- const accountDevices = await prisma.accountDevices.findMany({
- where: {
- accountId: {
- in: accountIds,
- },
- },
- });
-
- if (accountDevices.length === 0) {
- return;
- }
-
- const deviceIds = accountDevices.map((device) => device.id);
- const node: Node = {
- id: changeData.id,
- workspaceId: changeData.workspace_id,
- parentId: changeData.parent_id,
- type: changeData.type,
- index: changeData.index,
- attrs: changeData.attrs ? JSON.parse(changeData.attrs) : null,
- content: changeData.content ? JSON.parse(changeData.content) : null,
- createdAt: new Date(changeData.created_at),
- createdBy: changeData.created_by,
- updatedAt: changeData.updated_at ? new Date(changeData.updated_at) : null,
- updatedBy: changeData.updated_by,
- versionId: changeData.version_id,
- serverCreatedAt: new Date(changeData.server_created_at),
- serverUpdatedAt: changeData.server_updated_at
- ? new Date(changeData.server_updated_at)
- : null,
- serverVersionId: changeData.server_version_id,
- };
-
- await prisma.updates.create({
- data: {
- id: NeuronId.generate(NeuronId.Type.Update),
- workspaceId: changeData.workspace_id,
- devices: deviceIds,
- type: 'node_sync',
- content: JSON.stringify(node),
- createdAt: new Date(),
- },
- });
-};
-
-const handelNodeDelete = async (id: string) => {};
diff --git a/server/src/consumers/transactions.ts b/server/src/consumers/transactions.ts
deleted file mode 100644
index 20224d14..00000000
--- a/server/src/consumers/transactions.ts
+++ /dev/null
@@ -1,152 +0,0 @@
-import { kafka, TOPIC_NAMES, CONSUMER_IDS } from '@/data/kafka';
-import { NodeTransactionData, Transaction } from '@/types/transactions';
-import { prisma } from '@/data/prisma';
-import { Prisma } from '@prisma/client';
-
-export const initTransactionsConsumer = async () => {
- const consumer = kafka.consumer({ groupId: CONSUMER_IDS.TRANSACTIONS });
- await consumer.connect();
-
- await consumer.subscribe({ topic: TOPIC_NAMES.TRANSACTIONS });
-
- await consumer.run({
- eachMessage: async ({ message }) => {
- if (message.value === null) return;
-
- try {
- const transaction: Transaction = JSON.parse(message.value.toString());
- await handleTransaction(transaction);
- } catch (error) {
- console.error('Error processing transaction:', error);
- }
- },
- });
-};
-
-const handleTransaction = async (transaction: Transaction) => {
- switch (transaction.table) {
- case 'nodes':
- await handleNodeTransaction(transaction);
- break;
- }
-};
-
-const handleNodeTransaction = async (transaction: Transaction) => {
- switch (transaction.action) {
- case 'insert':
- await handleInsertNodeTransaction(transaction);
- break;
- case 'update':
- await handleUpdateNodeTransaction(transaction);
- break;
- case 'delete':
- await handleDeleteNodeTransaction(transaction);
- break;
- }
-};
-
-const handleInsertNodeTransaction = async (transaction: Transaction) => {
- if (!transaction.after) {
- return;
- }
-
- const input = JSON.parse(transaction.after) as NodeTransactionData;
- const data: Prisma.nodesUncheckedCreateInput = {
- id: input.id,
- parentId: input.parent_id,
- workspaceId: input.workspace_id,
- type: input.type,
- index: input.index,
- createdAt: input.created_at,
- createdBy: input.created_by,
- versionId: input.version_id,
- serverCreatedAt: new Date(),
- serverVersionId: input.version_id,
- };
-
- if (input.attrs !== undefined && input.attrs !== null) {
- data.attrs = JSON.parse(input.attrs);
- }
-
- if (input.content !== undefined && input.content !== null) {
- data.content = JSON.parse(input.content);
- }
-
- await prisma.nodes.create({
- data: data,
- });
-};
-
-const handleUpdateNodeTransaction = async (transaction: Transaction) => {
- if (!transaction.after) {
- return;
- }
-
- const input = JSON.parse(transaction.after) as NodeTransactionData;
- const existingNode = await prisma.nodes.findUnique({
- where: {
- id: input.id,
- },
- });
-
- if (!existingNode) {
- return;
- }
-
- const data: Prisma.nodesUncheckedUpdateInput = {
- parentId: input.parent_id,
- updatedAt: input.updated_at,
- updatedBy: input.updated_by,
- versionId: input.version_id,
- serverUpdatedAt: new Date(),
- serverVersionId: input.version_id,
- };
-
- if (input.attrs !== undefined) {
- if (input.attrs === null) {
- data.attrs = Prisma.JsonNull;
- } else {
- const existingAttrs =
- existingNode.attrs != null
- ? (existingNode.attrs as Record<string, any>)
- : {};
-
- const updatedAttrs = JSON.parse(input.attrs) as Record<string, any>;
- const newAttrs = {
- ...existingAttrs,
- ...updatedAttrs,
- };
-
- const hasAttrs = Object.keys(newAttrs).length > 0;
- data.attrs = hasAttrs ? newAttrs : Prisma.JsonNull;
- }
- }
-
- if (input.content !== undefined) {
- if (input.content === null) {
- data.content = Prisma.JsonNull;
- } else {
- data.content = input.content;
- }
- }
-
- await prisma.nodes.update({
- where: {
- id: existingNode.id,
- },
- data: data,
- });
-};
-
-const handleDeleteNodeTransaction = async (transaction: Transaction) => {
- if (!transaction.before) {
- return;
- }
-
- const input = JSON.parse(transaction.before) as NodeTransactionData;
- await prisma.nodes.delete({
- where: {
- id: input.id,
- },
- });
-};
diff --git a/server/src/consumers/update-changes.ts b/server/src/consumers/update-changes.ts
deleted file mode 100644
index 4a6e1da8..00000000
--- a/server/src/consumers/update-changes.ts
+++ /dev/null
@@ -1,46 +0,0 @@
-import { kafka, TOPIC_NAMES, CONSUMER_IDS } from '@/data/kafka';
-import { ChangeMessage, UpdateChangeData } from '@/types/changes';
-import { redis, CHANNEL_NAMES } from '@/data/redis';
-import { Update } from '@/types/updates';
-
-export const initUpdateChangesConsumer = async () => {
- const consumer = kafka.consumer({ groupId: CONSUMER_IDS.UPDATE_CHANGES });
-
- await consumer.connect();
- await consumer.subscribe({ topic: TOPIC_NAMES.UPDATE_CHANGES });
-
- await consumer.run({
- eachMessage: async ({ message }) => {
- if (!message || !message.value) {
- return;
- }
-
- const change = JSON.parse(
- message.value.toString(),
- ) as ChangeMessage<UpdateChangeData>;
-
- await handleUpdateChange(change);
- },
- });
-};
-
-const handleUpdateChange = async (change: ChangeMessage<UpdateChangeData>) => {
- const changeData = change.after;
- if (!changeData) {
- return;
- }
-
- const devices = changeData.devices;
- for (const deviceId of devices) {
- const update: Update = {
- id: changeData.id,
- deviceId: deviceId,
- type: changeData.type,
- content: changeData.content,
- workspaceId: changeData.workspace_id,
- createdAt: changeData.created_at,
- };
-
- await redis.publish(CHANNEL_NAMES.UPDATES, JSON.stringify(update));
- }
-};
diff --git a/server/src/consumers/updates.ts b/server/src/consumers/updates.ts
deleted file mode 100644
index f73c0e97..00000000
--- a/server/src/consumers/updates.ts
+++ /dev/null
@@ -1,24 +0,0 @@
-import { redis, CHANNEL_NAMES } from '@/data/redis';
-import { sockets } from '@/lib/sockets';
-import { Update } from '@/types/updates';
-import { SocketMessage } from '@/types/sockets';
-
-export const initUpdatesSubscriber = async () => {
- const subscriber = redis.duplicate();
- await subscriber.connect();
- await subscriber.subscribe(CHANNEL_NAMES.UPDATES, handleMessage);
-};
-
-const handleMessage = async (message: string) => {
- const update = JSON.parse(message) as Update;
- const socket = sockets.getSocket(update.deviceId);
- if (!socket) {
- return;
- }
-
- const socketMessage: SocketMessage = {
- type: 'update',
- payload: update,
- };
- socket.send(JSON.stringify(socketMessage));
-};
diff --git a/server/src/data/kafka.ts b/server/src/data/kafka.ts
index 41982211..64962df4 100644
--- a/server/src/data/kafka.ts
+++ b/server/src/data/kafka.ts
@@ -21,24 +21,19 @@ export const kafka = new Kafka({
export const producer = kafka.producer();
export const TOPIC_NAMES = {
- TRANSACTIONS:
- process.env.KAFKA_TRANSACTIONS_TOPIC_NAME ?? 'neuron_transactions',
NODE_CHANGES:
process.env.KAFKA_NODE_CHANGES_TOPIC_NAME ?? 'neuron_node_changes',
- UPDATE_CHANGES:
- process.env.KAFKA_UPDATE_CHANGES_TOPIC_NAME ?? 'neuron_update_changes',
+ MUTATION_CHANGES:
+ process.env.KAFKA_MUTATION_CHANGES_TOPIC_NAME ?? 'neuron_mutation_changes',
};
export const CONSUMER_IDS = {
- TRANSACTIONS:
- process.env.KAFKA_TRANSACTIONS_CONSUMER_ID ??
- 'neuron_transactions_consumer',
NODE_CHANGES:
process.env.KAFKA_NODE_CHANGES_CONSUMER_ID ??
'neuron_node_changes_consumer',
- UPDATE_CHANGES:
- process.env.KAFKA_UPDATE_CHANGES_CONSUMER_ID ??
- 'neuron_update_changes_consumer',
+ MUTATION_CHANGES:
+ process.env.KAFKA_MUTATION_CHANGES_CONSUMER_ID ??
+ 'neuron_mutation_changes_consumer',
};
const connectProducer = async () => {
diff --git a/server/src/data/redis.ts b/server/src/data/redis.ts
index e0873f47..9c1748aa 100644
--- a/server/src/data/redis.ts
+++ b/server/src/data/redis.ts
@@ -14,5 +14,5 @@ export const initRedis = async () => {
};
export const CHANNEL_NAMES = {
- UPDATES: process.env.REDIS_UPDATES_CHANNEL_NAME || 'neuron_updates',
+ MUTATIONS: process.env.REDIS_MUTATIONS_CHANNEL_NAME || 'neuron_mutations',
};
diff --git a/server/src/index.ts b/server/src/index.ts
index 9e5e40d6..c882cbc6 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -1,24 +1,17 @@
import { initApi } from '@/api';
import { initRedis } from '@/data/redis';
-import { initTransactionsConsumer } from '@/consumers/transactions';
-import { initNodeChangesConsumer } from '@/consumers/node-changes';
-import { initUpdateChangesConsumer } from '@/consumers/update-changes';
-import { initUpdatesSubscriber } from '@/consumers/updates';
+import { initMutationChangesConsumer } from '@/consumers/mutation-changes';
+import { initMutationsSubscriber } from '@/consumers/mutations';
initApi();
-initTransactionsConsumer().then(() => {
- console.log('Transactions consumer started');
-});
-initNodeChangesConsumer().then(() => {
- console.log('Node changes consumer started');
-});
-initUpdateChangesConsumer().then(() => {
- console.log('Update changes consumer started');
+
+initMutationChangesConsumer().then(() => {
+ console.log('Mutation changes consumer started');
});
initRedis().then(() => {
console.log('Redis initialized');
- initUpdatesSubscriber().then(() => {
- console.log('Updates subscriber started');
+ initMutationsSubscriber().then(() => {
+ console.log('Mutation subscriber started');
});
});
diff --git a/server/src/lib/constants.ts b/server/src/lib/constants.ts
new file mode 100644
index 00000000..f5baae94
--- /dev/null
+++ b/server/src/lib/constants.ts
@@ -0,0 +1,5 @@
+export const PostgresOperation = {
+ CREATE: 'c',
+ UPDATE: 'u',
+ DELETE: 'd',
+} as const;
diff --git a/server/src/lib/id.ts b/server/src/lib/id.ts
index 36d0b2f3..b6de0a12 100644
--- a/server/src/lib/id.ts
+++ b/server/src/lib/id.ts
@@ -7,7 +7,7 @@ enum IdType {
Workspace = 'wc',
User = 'us',
Version = 've',
- Transaction = 'tr',
+ Mutation = 'mu',
Space = 'sp',
Page = 'pg',
Channel = 'ch',
@@ -40,8 +40,8 @@ export class NeuronId {
return IdType.User;
case 'version':
return IdType.Version;
- case 'transaction':
- return IdType.Transaction;
+ case 'mutation':
+ return IdType.Mutation;
case 'space':
return IdType.Space;
case 'page':
diff --git a/server/src/routes/mutations.ts b/server/src/routes/mutations.ts
new file mode 100644
index 00000000..ada05e91
--- /dev/null
+++ b/server/src/routes/mutations.ts
@@ -0,0 +1,575 @@
+import { NeuronRequest, NeuronResponse } from '@/types/api';
+import { Router } from 'express';
+import {
+ LocalMutation,
+ LocalCreateNodeMutation,
+ LocalCreateNodesMutation,
+ LocalUpdateNodeMutation,
+ LocalDeleteNodeMutation,
+ LocalDeleteNodesMutation,
+ LocalCreateNodeData,
+ ServerMutation,
+ ServerCreateNodeMutation,
+ ServerCreateNodesMutation,
+ ServerUpdateNodeMutation,
+ ServerDeleteNodeMutation,
+ ServerDeleteNodesMutation,
+} from '@/types/mutations';
+import { prisma } from '@/data/prisma';
+import { Node, NodeBlock } from '@/types/nodes';
+import { Prisma } from '@prisma/client';
+import { NeuronId } from '@/lib/id';
+
+export const mutationsRouter = Router();
+
+mutationsRouter.post('/', async (req: NeuronRequest, res: NeuronResponse) => {
+ const localMutation = req.body as LocalMutation;
+
+ switch (localMutation.type) {
+ case 'create_node': {
+ await handleCreateNodeMutation(req, res, localMutation);
+ break;
+ }
+ case 'create_nodes': {
+ await handleCreateNodesMutation(req, res, localMutation);
+ break;
+ }
+ case 'update_node': {
+ await handleUpdateNodeMutation(req, res, localMutation);
+ break;
+ }
+ case 'delete_node': {
+ await handleDeleteNodeMutation(req, res, localMutation);
+ break;
+ }
+ case 'delete_nodes': {
+ await handleDeleteNodesMutation(req, res, localMutation);
+ break;
+ }
+ }
+});
+
+const handleCreateNodeMutation = async (
+ req: NeuronRequest,
+ res: NeuronResponse,
+ localMutation: LocalCreateNodeMutation,
+): Promise<NeuronResponse> => {
+ if (!req.accountId) {
+ return res.status(401).json({ success: false });
+ }
+
+ const existingNode = await prisma.nodes.findUnique({
+ where: {
+ id: localMutation.data.node.id,
+ },
+ });
+
+ if (existingNode) {
+ const serverMutation: ServerCreateNodeMutation = {
+ id: NeuronId.generate(NeuronId.Type.Mutation),
+ type: 'create_node',
+ workspaceId: localMutation.workspaceId,
+ createdAt: new Date().toISOString(),
+ data: {
+ node: {
+ id: existingNode.id,
+ parentId: existingNode.parentId,
+ workspaceId: existingNode.workspaceId,
+ type: existingNode.type,
+ index: existingNode.index,
+ attrs: existingNode.attrs as Record<string, any>,
+ content: existingNode.content as NodeBlock[],
+ createdAt: existingNode.createdAt?.toISOString(),
+ createdBy: existingNode.createdBy,
+ updatedAt: existingNode.updatedAt?.toISOString(),
+ updatedBy: existingNode.updatedBy,
+ versionId: existingNode.versionId,
+ serverCreatedAt: existingNode.serverCreatedAt?.toISOString(),
+ serverUpdatedAt: existingNode.serverUpdatedAt?.toISOString(),
+ serverVersionId: existingNode.serverVersionId,
+ },
+ },
+ };
+ return res.status(200).json(serverMutation);
+ }
+
+ const node = buildNode(localMutation.data.node);
+ const workspaceId = node.workspaceId;
+ const workspaceAccounts = await prisma.workspaceAccounts.findMany({
+ where: {
+ workspaceId,
+ },
+ });
+
+ if (workspaceAccounts.length === 0) {
+ return res.status(401).json({ success: false });
+ }
+
+ const accountIds = workspaceAccounts.map((account) => account.accountId);
+ if (!accountIds.includes(req.accountId)) {
+ return res.status(401).json({ success: false });
+ }
+
+ const accountDevices = await prisma.accountDevices.findMany({
+ where: {
+ accountId: {
+ in: accountIds,
+ },
+ },
+ });
+ const deviceIds = accountDevices.map((device) => device.id);
+
+ const result = await prisma.$transaction(async (tx) => {
+ const row = await tx.nodes.create({
+ data: {
+ ...node,
+ attrs: node.attrs === null ? Prisma.DbNull : node.attrs,
+ content: node.content === null ? Prisma.DbNull : node.content,
+ },
+ });
+
+ if (!row) {
+ throw new Error('Failed to create node');
+ }
+
+ const serverMutation: ServerCreateNodeMutation = {
+ id: NeuronId.generate(NeuronId.Type.Mutation),
+ workspaceId: localMutation.workspaceId,
+ type: 'create_node',
+ createdAt: new Date().toISOString(),
+ data: {
+ node: node,
+ },
+ };
+
+ await prisma.mutations.create({
+ data: {
+ ...serverMutation,
+ devices: deviceIds,
+ deviceId: localMutation.deviceId,
+ },
+ });
+
+ return serverMutation;
+ });
+
+ return res.status(200).json(result);
+};
+
+const handleCreateNodesMutation = async (
+ req: NeuronRequest,
+ res: NeuronResponse,
+ localMutation: LocalCreateNodesMutation,
+): Promise<NeuronResponse> => {
+ if (!req.accountId) {
+ return res.status(401).json({ success: false });
+ }
+
+ const existingNodes = await prisma.nodes.findMany({
+ where: {
+ id: {
+ in: localMutation.data.nodes.map((node) => node.id),
+ },
+ },
+ });
+
+ const existingNodeIds = existingNodes.map((node) => node.id);
+ if (existingNodeIds.length === localMutation.data.nodes.length) {
+ const serverMutation: ServerCreateNodesMutation = {
+ id: NeuronId.generate(NeuronId.Type.Mutation),
+ type: 'create_nodes',
+ workspaceId: localMutation.workspaceId,
+ createdAt: new Date().toISOString(),
+ data: {
+ nodes: existingNodes.map((node) => ({
+ id: node.id,
+ parentId: node.parentId,
+ workspaceId: node.workspaceId,
+ type: node.type,
+ index: node.index,
+ attrs: node.attrs as Record<string, any>,
+ content: node.content as NodeBlock[],
+ createdAt: node.createdAt?.toISOString(),
+ createdBy: node.createdBy,
+ updatedAt: node.updatedAt?.toISOString(),
+ updatedBy: node.updatedBy,
+ versionId: node.versionId,
+ serverCreatedAt: node.serverCreatedAt?.toISOString(),
+ serverUpdatedAt: node.serverUpdatedAt?.toISOString(),
+ serverVersionId: node.serverVersionId,
+ })),
+ },
+ };
+ return res.status(200).json(serverMutation);
+ }
+
+ const nodes = localMutation.data.nodes.map((node) => buildNode(node));
+
+ const nodesToInsert = nodes.filter(
+ (node) => !existingNodeIds.includes(node.id),
+ );
+
+ if (nodes.length === 0) {
+ return res.status(400).json({ success: false });
+ }
+
+ const workspaceId = nodes[0].workspaceId;
+ const workspaceAccounts = await prisma.workspaceAccounts.findMany({
+ where: {
+ workspaceId,
+ },
+ });
+
+ if (workspaceAccounts.length === 0) {
+ return res.status(401).json({ success: false });
+ }
+
+ const accountIds = workspaceAccounts.map((account) => account.accountId);
+ if (!accountIds.includes(req.accountId)) {
+ return res.status(401).json({ success: false });
+ }
+
+ const accountDevices = await prisma.accountDevices.findMany({
+ where: {
+ accountId: {
+ in: accountIds,
+ },
+ },
+ });
+ const deviceIds = accountDevices.map((device) => device.id);
+
+ const result = await prisma.$transaction(async (tx) => {
+ const payload = await tx.nodes.createMany({
+ data: nodesToInsert.map((node) => ({
+ ...node,
+ attrs: node.attrs === null ? Prisma.DbNull : node.attrs,
+ content: node.content === null ? Prisma.DbNull : node.content,
+ })),
+ });
+
+ if (payload.count !== nodes.length) {
+ throw new Error('Failed to create nodes');
+ }
+
+ const serverMutation: ServerCreateNodesMutation = {
+ id: NeuronId.generate(NeuronId.Type.Mutation),
+ type: 'create_nodes',
+ workspaceId: workspaceId,
+ createdAt: new Date().toISOString(),
+ data: {
+ nodes,
+ },
+ };
+
+ await tx.mutations.create({
+ data: {
+ ...serverMutation,
+ devices: deviceIds,
+ deviceId: localMutation.deviceId,
+ },
+ });
+
+ return serverMutation;
+ });
+
+ return res.status(200).json(result);
+};
+
+const handleUpdateNodeMutation = async (
+ req: NeuronRequest,
+ res: NeuronResponse,
+ localMutation: LocalUpdateNodeMutation,
+): Promise<NeuronResponse> => {
+ if (!req.accountId) {
+ return res.status(401).json({ success: false });
+ }
+
+ const id = localMutation.data.id;
+ const existingNode = await prisma.nodes.findUnique({
+ where: {
+ id,
+ },
+ });
+
+ if (!existingNode) {
+ return res.status(404).json({ success: false });
+ }
+
+ const workspaceId = existingNode.workspaceId;
+ const workspaceAccounts = await prisma.workspaceAccounts.findMany({
+ where: {
+ workspaceId,
+ },
+ });
+
+ if (workspaceAccounts.length === 0) {
+ return res.status(401).json({ success: false });
+ }
+
+ const accountIds = workspaceAccounts.map((account) => account.accountId);
+ if (!accountIds.includes(req.accountId)) {
+ return res.status(401).json({ success: false });
+ }
+
+ const accountDevices = await prisma.accountDevices.findMany({
+ where: {
+ accountId: {
+ in: accountIds,
+ },
+ },
+ });
+ const deviceIds = accountDevices.map((device) => device.id);
+
+ const data = localMutation.data;
+ const result = await prisma.$transaction(async (tx) => {
+ const row = await tx.nodes.update({
+ where: {
+ id,
+ },
+ data: {
+ type: data.type,
+ parentId: data.parentId,
+ index: data.index,
+ content: data.content ? data.content : Prisma.DbNull,
+ attrs: data.attrs ? data.attrs : Prisma.DbNull,
+ updatedAt: data.updatedAt,
+ updatedBy: data.updatedBy,
+ versionId: data.versionId,
+ serverUpdatedAt: new Date().toISOString(),
+ serverVersionId: data.versionId,
+ },
+ });
+
+ if (!row) {
+ throw new Error('Failed to update node');
+ }
+
+ const node: Node = {
+ id: row.id,
+ parentId: row.parentId,
+ workspaceId: row.workspaceId,
+ type: row.type,
+ index: row.index,
+ attrs: row.attrs as Record<string, any>,
+ content: row.content as NodeBlock[],
+ createdAt: row.createdAt?.toISOString(),
+ createdBy: row.createdBy,
+ updatedAt: row.updatedAt?.toISOString(),
+ updatedBy: row.updatedBy,
+ versionId: row.versionId,
+ serverCreatedAt: row.serverCreatedAt?.toISOString(),
+ serverUpdatedAt: row.serverUpdatedAt?.toISOString(),
+ serverVersionId: row.serverVersionId,
+ };
+
+ const serverMutation: ServerUpdateNodeMutation = {
+ id: NeuronId.generate(NeuronId.Type.Mutation),
+ type: 'update_node',
+ workspaceId: workspaceId,
+ createdAt: new Date().toISOString(),
+ data: {
+ node: node,
+ },
+ };
+
+ await tx.mutations.create({
+ data: {
+ ...serverMutation,
+ devices: deviceIds,
+ deviceId: localMutation.deviceId,
+ },
+ });
+
+ return serverMutation;
+ });
+
+ return res.status(200).json(result);
+};
+
+const handleDeleteNodeMutation = async (
+ req: NeuronRequest,
+ res: NeuronResponse,
+ localMutation: LocalDeleteNodeMutation,
+): Promise<NeuronResponse> => {
+ if (!req.accountId) {
+ return res.status(401).json({ success: false });
+ }
+
+ const id = localMutation.data.id;
+ const existingNode = await prisma.nodes.findUnique({
+ where: {
+ id,
+ },
+ });
+
+ if (!existingNode) {
+ const serverMutation: ServerMutation = {
+ id: NeuronId.generate(NeuronId.Type.Mutation),
+ type: 'delete_node',
+ workspaceId: localMutation.workspaceId,
+ createdAt: new Date().toISOString(),
+ data: {
+ id,
+ },
+ };
+ return res.status(200).json(serverMutation);
+ }
+
+ const workspaceId = existingNode.workspaceId;
+ const workspaceAccounts = await prisma.workspaceAccounts.findMany({
+ where: {
+ workspaceId,
+ },
+ });
+
+ if (workspaceAccounts.length === 0) {
+ return res.status(401).json({ success: false });
+ }
+
+ const accountIds = workspaceAccounts.map((account) => account.accountId);
+ if (!accountIds.includes(req.accountId)) {
+ return res.status(401).json({ success: false });
+ }
+
+ const accountDevices = await prisma.accountDevices.findMany({
+ where: {
+ accountId: {
+ in: accountIds,
+ },
+ },
+ });
+ const deviceIds = accountDevices.map((device) => device.id);
+
+ const result = await prisma.$transaction(async (tx) => {
+ await tx.nodes.delete({
+ where: {
+ id,
+ },
+ });
+
+ const serverMutation: ServerDeleteNodeMutation = {
+ id: NeuronId.generate(NeuronId.Type.Mutation),
+ type: 'delete_node',
+ workspaceId: workspaceId,
+ createdAt: new Date().toISOString(),
+ data: {
+ id,
+ },
+ };
+
+ await tx.mutations.create({
+ data: {
+ ...serverMutation,
+ devices: deviceIds,
+ deviceId: localMutation.deviceId,
+ },
+ });
+ });
+
+ return res.status(200).json(result);
+};
+
+const handleDeleteNodesMutation = async (
+ req: NeuronRequest,
+ res: NeuronResponse,
+ localMutation: LocalDeleteNodesMutation,
+): Promise<NeuronResponse> => {
+ if (!req.accountId) {
+ return res.status(401).json({ success: false });
+ }
+
+ const ids = localMutation.data.ids;
+ const existingNodes = await prisma.nodes.findMany({
+ where: {
+ id: {
+ in: ids,
+ },
+ },
+ });
+
+ const nodesToDelete = existingNodes.map((node) => node.id);
+ if (nodesToDelete.length === 0) {
+ const serverMutation: ServerDeleteNodesMutation = {
+ id: NeuronId.generate(NeuronId.Type.Mutation),
+ type: 'delete_nodes',
+ workspaceId: localMutation.workspaceId,
+ createdAt: new Date().toISOString(),
+ data: {
+ ids,
+ },
+ };
+ return res.status(200).json(serverMutation);
+ }
+
+ const workspaceId = existingNodes[0].workspaceId;
+ const workspaceAccounts = await prisma.workspaceAccounts.findMany({
+ where: {
+ workspaceId,
+ },
+ });
+
+ if (workspaceAccounts.length === 0) {
+ return res.status(401).json({ success: false });
+ }
+
+ const accountIds = workspaceAccounts.map((account) => account.accountId);
+ if (!accountIds.includes(req.accountId)) {
+ return res.status(401).json({ success: false });
+ }
+
+ const accountDevices = await prisma.accountDevices.findMany({
+ where: {
+ accountId: {
+ in: accountIds,
+ },
+ },
+ });
+ const deviceIds = accountDevices.map((device) => device.id);
+
+ const result = await prisma.$transaction(async (tx) => {
+ await tx.nodes.deleteMany({
+ where: {
+ id: {
+ in: nodesToDelete,
+ },
+ },
+ });
+
+ const serverMutation: ServerDeleteNodesMutation = {
+ id: NeuronId.generate(NeuronId.Type.Mutation),
+ type: 'delete_nodes',
+ workspaceId: workspaceId,
+ createdAt: new Date().toISOString(),
+ data: {
+ ids,
+ },
+ };
+
+ await tx.mutations.create({
+ data: {
+ ...serverMutation,
+ devices: deviceIds,
+ deviceId: localMutation.deviceId,
+ },
+ });
+ });
+
+ return res.status(200).json(result);
+};
+
+const buildNode = (data: LocalCreateNodeData): Node => {
+ return {
+ id: data.id,
+ parentId: data.parentId,
+ workspaceId: data.workspaceId,
+ type: data.type,
+ index: data.index,
+ attrs: data.attrs as Record<string, any>,
+ createdAt: data.createdAt,
+ createdBy: data.createdBy,
+ versionId: data.versionId,
+ content: data.content as NodeBlock[],
+ serverCreatedAt: new Date().toISOString(),
+ serverVersionId: data.versionId,
+ };
+};
diff --git a/server/src/routes/workspaces.ts b/server/src/routes/workspaces.ts
index 971b39de..0c512850 100644
--- a/server/src/routes/workspaces.ts
+++ b/server/src/routes/workspaces.ts
@@ -10,7 +10,7 @@ import {
import { ApiError, NeuronRequest, NeuronResponse } from '@/types/api';
import { NeuronId } from '@/lib/id';
import { prisma } from '@/data/prisma';
-import { Request, Response, Router } from 'express';
+import { Router } from 'express';
import { Node, NodeBlock } from '@/types/nodes';
export const workspacesRouter = Router();
@@ -335,34 +335,37 @@ workspacesRouter.get('/', async (req: NeuronRequest, res: NeuronResponse) => {
return res.status(200).json(outputs);
});
-workspacesRouter.get('/:id/nodes', async (req: Request, res: Response) => {
- const workspaceId = req.params.id as string;
- const from = req.query.from as string;
- const nodes = await getNodesFromDatabase(workspaceId, from);
- const outputs: Node[] = nodes.map((node) => {
- return {
- id: node.id,
- parentId: node.parentId,
- workspaceId: node.workspaceId,
- type: node.type,
- index: node.index,
- attrs: node.attrs as Record<string, any>,
- createdAt: node.createdAt,
- createdBy: node.createdBy,
- versionId: node.versionId,
- content: node.content as NodeBlock[],
- updatedAt: node.updatedAt,
- updatedBy: node.updatedBy,
- serverCreatedAt: node.serverCreatedAt,
- serverUpdatedAt: node.serverUpdatedAt,
- serverVersionId: node.serverVersionId,
- };
- });
+workspacesRouter.get(
+ '/:id/nodes',
+ async (req: NeuronRequest, res: NeuronResponse) => {
+ const workspaceId = req.params.id as string;
+ const from = req.query.from as string;
+ const nodes = await getNodesFromDatabase(workspaceId, from);
+ const outputs: Node[] = nodes.map((node) => {
+ return {
+ id: node.id,
+ parentId: node.parentId,
+ workspaceId: node.workspaceId,
+ type: node.type,
+ index: node.index,
+ attrs: node.attrs as Record<string, any>,
+ createdAt: node.createdAt.toISOString(),
+ createdBy: node.createdBy,
+ versionId: node.versionId,
+ content: node.content as NodeBlock[],
+ updatedAt: node.updatedAt?.toISOString(),
+ updatedBy: node.updatedBy,
+ serverCreatedAt: node.serverCreatedAt.toISOString(),
+ serverUpdatedAt: node.serverUpdatedAt?.toISOString(),
+ serverVersionId: node.serverVersionId,
+ };
+ });
- res.status(200).json({
- nodes: outputs,
- });
-});
+ res.status(200).json({
+ nodes: outputs,
+ });
+ },
+);
const getNodesFromDatabase = async (
workspaceId: string,
diff --git a/server/src/types/changes.ts b/server/src/types/changes.ts
index 637d5500..a227dc03 100644
--- a/server/src/types/changes.ts
+++ b/server/src/types/changes.ts
@@ -43,11 +43,12 @@ export type NodeChangeData = {
server_version_id: string;
};
-export type UpdateChangeData = {
+export type MutationChangeData = {
id: string;
- workspace_id: string;
- devices: string[];
type: string;
- content: string;
+ workspace_id: string;
+ data: string;
created_at: string;
+ device_id: string;
+ devices: string[] | null;
};
diff --git a/server/src/types/mutations.ts b/server/src/types/mutations.ts
new file mode 100644
index 00000000..4248047d
--- /dev/null
+++ b/server/src/types/mutations.ts
@@ -0,0 +1,131 @@
+import { Node, NodeBlock } from '@/types/nodes';
+
+export type LocalMutation =
+ | LocalCreateNodeMutation
+ | LocalCreateNodesMutation
+ | LocalUpdateNodeMutation
+ | LocalDeleteNodeMutation
+ | LocalDeleteNodesMutation;
+
+export type LocalCreateNodeMutation = {
+ type: 'create_node';
+ workspaceId: string;
+ deviceId: string;
+ data: {
+ node: LocalCreateNodeData;
+ };
+};
+
+export type LocalCreateNodesMutation = {
+ type: 'create_nodes';
+ workspaceId: string;
+ deviceId: string;
+ data: {
+ nodes: LocalCreateNodeData[];
+ };
+};
+
+export type LocalCreateNodeData = {
+ id: string;
+ type: string;
+ parentId: string | null;
+ workspaceId: string;
+ index: string | null;
+ content: NodeBlock[];
+ attrs: Record<string, any>;
+ createdAt: string;
+ createdBy: string;
+ versionId: string;
+};
+
+export type LocalUpdateNodeMutation = {
+ type: 'update_node';
+ workspaceId: string;
+ deviceId: string;
+ data: {
+ id: string;
+ type: string;
+ parentId: string | null;
+ index: string | null;
+ content: NodeBlock[];
+ attrs: Record<string, any>;
+ updatedAt: string;
+ updatedBy: string;
+ versionId: string;
+ };
+};
+
+export type LocalDeleteNodeMutation = {
+ type: 'delete_node';
+ workspaceId: string;
+ deviceId: string;
+ data: {
+ id: string;
+ };
+};
+
+export type LocalDeleteNodesMutation = {
+ type: 'delete_nodes';
+ workspaceId: string;
+ deviceId: string;
+ data: {
+ ids: string[];
+ };
+};
+
+export type ServerMutation =
+ | ServerCreateNodeMutation
+ | ServerCreateNodesMutation
+ | ServerUpdateNodeMutation
+ | ServerDeleteNodeMutation
+ | ServerDeleteNodesMutation;
+
+export type ServerCreateNodeMutation = {
+ id: string;
+ type: 'create_node';
+ workspaceId: string;
+ data: {
+ node: Node;
+ };
+ createdAt: string;
+};
+
+export type ServerCreateNodesMutation = {
+ id: string;
+ type: 'create_nodes';
+ workspaceId: string;
+ data: {
+ nodes: Node[];
+ };
+ createdAt: string;
+};
+
+export type ServerUpdateNodeMutation = {
+ id: string;
+ type: 'update_node';
+ workspaceId: string;
+ data: {
+ node: Node;
+ };
+ createdAt: string;
+};
+
+export type ServerDeleteNodeMutation = {
+ id: string;
+ type: 'delete_node';
+ workspaceId: string;
+ data: {
+ id: string;
+ };
+ createdAt: string;
+};
+
+export type ServerDeleteNodesMutation = {
+ id: string;
+ type: 'delete_nodes';
+ workspaceId: string;
+ data: {
+ ids: string[];
+ };
+ createdAt: string;
+};
diff --git a/server/src/types/nodes.ts b/server/src/types/nodes.ts
index d22347b0..71d5dfae 100644
--- a/server/src/types/nodes.ts
+++ b/server/src/types/nodes.ts
@@ -6,13 +6,13 @@ export type Node = {
index: string | null;
attrs?: Record<string, any> | null;
content?: NodeBlock[] | null;
- createdAt: Date;
+ createdAt: string;
createdBy: string;
- updatedAt?: Date | null;
+ updatedAt?: string | null;
updatedBy?: string | null;
versionId: string;
- serverCreatedAt: Date;
- serverUpdatedAt?: Date | null;
+ serverCreatedAt: string;
+ serverUpdatedAt?: string | null;
serverVersionId: string;
};
diff --git a/server/src/types/transactions.ts b/server/src/types/transactions.ts
deleted file mode 100644
index 40807216..00000000
--- a/server/src/types/transactions.ts
+++ /dev/null
@@ -1,31 +0,0 @@
-export type TransactionTable = 'nodes';
-
-export type TransactionAction = 'insert' | 'update' | 'delete';
-
-export type Transaction = {
- id: string;
- workspaceId: string;
- action: TransactionAction;
- table: TransactionTable;
- after?: string | null;
- before?: string | null;
- createdAt: string;
-};
-
-export type NodeTransactionData = {
- id: string;
- workspace_id: string;
- parent_id: string | null;
- type: string;
- index: string | null;
- attrs: string | null;
- content: string | null;
- created_at: string;
- updated_at: string | null;
- created_by: string;
- updated_by: string | null;
- version_id: string;
- server_created_at: string;
- server_updated_at: string | null;
- server_version_id: string | null;
-};
diff --git a/server/src/types/updates.ts b/server/src/types/updates.ts
deleted file mode 100644
index 5795f05a..00000000
--- a/server/src/types/updates.ts
+++ /dev/null
@@ -1,8 +0,0 @@
-export type Update = {
- id: string;
- workspaceId: string;
- deviceId: string;
- type: string;
- content: string;
- createdAt: string;
-};