Open-source and local-first Slack and Notion alternative that puts you in control of your data
TypeScript chatchat-applicationcrdteditorelectronknowledge-baselocal-firstnotionnotion-alternativerealtime-collaborationself-hostedslacksqliteteam-collaborationwikiyjs
Fix seed script with the new changes
Hakan Shehubca7eb6ef872
diff --git a/apps/desktop/src/main/services/node-service.ts b/apps/desktop/src/main/services/node-service.tsindex 2adf519f..33c9837d 100644--- a/apps/desktop/src/main/services/node-service.ts+++ b/apps/desktop/src/main/services/node-service.ts@@ -23,7 +23,6 @@ import {SelectNode,SelectTransaction,} from '@/main/data/workspace/schema';-import { interactionService } from '@/main/services/interaction-service';import { fetchNodeAncestors, mapNode, mapTransaction } from '@/main/utils';import { eventBus } from '@/shared/lib/event-bus';@@ -903,11 +902,6 @@ class NodeService {userId,node: mapNode(createdNode),});-- await interactionService.setInteraction(userId, createdNode.id, {- attribute: 'lastReceivedTransactionId',- value: transaction.id,- });}private async applyServerUpdateTransaction(@@ -1053,11 +1047,6 @@ class NodeService {userId,node: mapNode(updatedNode),});-- await interactionService.setInteraction(userId, updatedNode.id, {- attribute: 'lastReceivedTransactionId',- value: transaction.id,- });}private async applyServerDeleteTransaction(diff --git a/apps/server/src/controllers/client/workspaces/users/user-create.ts b/apps/server/src/controllers/client/workspaces/users/user-create.tsindex 84bc759b..6924f2c2 100644--- a/apps/server/src/controllers/client/workspaces/users/user-create.ts+++ b/apps/server/src/controllers/client/workspaces/users/user-create.ts@@ -10,7 +10,6 @@ import {import { database } from '@/data/database';import { ApiError } from '@/types/api';-import { fetchNode } from '@/lib/nodes';import { getNameFromEmail } from '@/lib/utils';import { SelectUser } from '@/data/schema';import { eventBus } from '@/lib/event-bus';@@ -47,15 +46,6 @@ export const userCreateHandler = async (return;}- const workspaceNodeRow = await fetchNode(workspaceId);- if (!workspaceNodeRow) {- res.status(500).json({- code: ApiError.InternalServerError,- message: 'Something went wrong.',- });- return;- }-const results: UserInviteResult[] = [];for (const email of input.emails) {let account = await databasediff --git a/apps/server/src/synchronizers/collaborations.ts b/apps/server/src/synchronizers/collaborations.tsindex aeea534e..9a36fed8 100644--- a/apps/server/src/synchronizers/collaborations.ts+++ b/apps/server/src/synchronizers/collaborations.ts@@ -44,6 +44,7 @@ export class CollaborationSynchronizer extends BaseSynchronizer<SyncCollaboratio.selectFrom('collaborations').selectAll().where('workspace_id', '=', this.user.workspaceId)+ .where('collaborator_id', '=', this.user.userId).where('version', '>', this.cursor).orderBy('version', 'asc').limit(50)diff --git a/packages/core/src/types/mutations.ts b/packages/core/src/types/mutations.tsindex d1181cea..17f23b0a 100644--- a/packages/core/src/types/mutations.ts+++ b/packages/core/src/types/mutations.ts@@ -1,5 +1,5 @@import { FileType } from './files';-import { MessageType } from './messages';+import { MessageContent, MessageType } from './messages';import {LocalCreateTransaction,LocalDeleteTransaction,@@ -64,7 +64,7 @@ export type CreateMessageMutationData = {type: MessageType;nodeId: string;parentId: string;- content: unknown;+ content: MessageContent;rootId: string;createdAt: string;};diff --git a/scripts/src/seed/index.ts b/scripts/src/seed/index.tsindex 2da33d1f..23572de9 100644--- a/scripts/src/seed/index.ts+++ b/scripts/src/seed/index.ts@@ -127,26 +127,26 @@ const inviteAccountsToWorkspace = async ();};-const sendTransactions = async (user: User, workspaceId: string) => {- const url = `${SERVER_DOMAIN}/client/v1/workspaces/${workspaceId}/transactions`;+const sendMutations = async (user: User, workspaceId: string) => {+ const url = `${SERVER_DOMAIN}/client/v1/workspaces/${workspaceId}/mutations`;const batchSize = 100;- const totalBatches = Math.ceil(user.transactions.length / batchSize);+ const totalBatches = Math.ceil(user.mutations.length / batchSize);let currentBatch = 1;// Create a copy of the transactions array to modify- const remainingTransactions = [...user.transactions];+ const remainingMutations = [...user.mutations];- while (remainingTransactions.length > 0) {- const batch = remainingTransactions.splice(0, batchSize);+ while (remainingMutations.length > 0) {+ const batch = remainingMutations.splice(0, batchSize);console.log(- `Sending batch ${currentBatch} of ${totalBatches} transactions for user ${user.login.account.email}`+ `Sending batch ${currentBatch} of ${totalBatches} mutations for user ${user.login.account.email}`);await axios.post(url,{- transactions: batch,+ mutations: batch,},{headers: { Authorization: `Bearer ${user.login.token}` },@@ -184,7 +184,7 @@ const seed = async () => {users.push({login: mainAccount,userId: workspace.user.id,- transactions: [],+ mutations: [],});const otherAccounts = fakerAccounts.slice(1);@@ -203,7 +203,7 @@ const seed = async () => {users.push({login: account,userId: accountWorkspace.user.id,- transactions: [],+ mutations: [],});}@@ -211,10 +211,10 @@ const seed = async () => {const nodeGenerator = new NodeGenerator(workspace.id, users);nodeGenerator.generate();- console.log('Sending transactions');+ console.log('Sending mutations');for (const user of users) {- console.log('Sending transactions for user', user.login.account.email);- await sendTransactions(user, workspace.id);+ console.log('Sending mutations for user', user.login.account.email);+ await sendMutations(user, workspace.id);}console.log('Done');diff --git a/scripts/src/seed/node-generator.ts b/scripts/src/seed/node-generator.tsindex 28676e73..532ca909 100644--- a/scripts/src/seed/node-generator.ts+++ b/scripts/src/seed/node-generator.ts@@ -2,7 +2,6 @@ import {Block,generateId,IdType,- LocalTransaction,NodeAttributes,NodeRole,registry,@@ -13,6 +12,9 @@ import {DatabaseAttributes,FieldValue,ViewFilterAttributes,+ ApplyCreateTransactionMutation,+ LocalCreateTransaction,+ CreateMessageMutation,} from '@colanode/core';import { encodeState, YDoc } from '@colanode/crdt';import { faker } from '@faker-js/faker';@@ -41,9 +43,14 @@ export class NodeGenerator {'The general space','01je8kh1yekmqd9643naenqf1rem');- this.buildPage('Welcome', spaceId, '01je8kh2018tqrrxqsc6af71zfem');- this.buildPage('Resources', spaceId, '01je8kh202et2bagv8phg219cbem');- this.buildPage('Guide', spaceId, '01je8kh202et2bagv8phg219cdem');+ this.buildPage(spaceId, 'Welcome', spaceId, '01je8kh2018tqrrxqsc6af71zfem');+ this.buildPage(+ spaceId,+ 'Resources',+ spaceId,+ '01je8kh202et2bagv8phg219cbem'+ );+ this.buildPage(spaceId, 'Guide', spaceId, '01je8kh202et2bagv8phg219cdem');this.buildChannel('Announcements', spaceId, '01je8kh1zvyrbp8fgt59t2cy8pem');}@@ -55,8 +62,8 @@ export class NodeGenerator {);this.buildChannel('Discussions', spaceId, '01je8kh1j5m7v8bk06ara5txq5em');this.buildChannel('Alerts', spaceId, '01je8kh1yv79yfr5dsvy19g696em');- this.buildPage('Roadmap', spaceId, '01je8kh1yyqmcxbdf67bgsc0wqem');- this.buildTasksDatabase(spaceId);+ this.buildPage(spaceId, 'Roadmap', spaceId, '01je8kh1yyqmcxbdf67bgsc0wqem');+ this.buildTasksDatabase(spaceId, spaceId);}private buildBusinessSpace() {@@ -65,9 +72,9 @@ export class NodeGenerator {'The business space','01je8kh2055hxb5k4g0sr5vj6gem');- this.buildPage('Notes', spaceId, '01je8kh2055hxb5k4g0sr5vj6aem');- this.buildClientsDatabase(spaceId);- this.buildMeetingsDatabase(spaceId);+ this.buildPage(spaceId, 'Notes', spaceId, '01je8kh2055hxb5k4g0sr5vj6aem');+ this.buildClientsDatabase(spaceId, spaceId);+ this.buildMeetingsDatabase(spaceId, spaceId);}private buildChats() {@@ -95,12 +102,20 @@ export class NodeGenerator {const user = this.getMainUser();const createTransaction = this.buildCreateTransaction(+ spaceId,spaceId,user.userId,spaceAttributes);- user.transactions.push(createTransaction);+ const mutation: ApplyCreateTransactionMutation = {+ type: 'apply_create_transaction',+ id: generateId(IdType.Mutation),+ data: createTransaction,+ createdAt: new Date().toISOString(),+ };++ user.mutations.push(mutation);return spaceId;}@@ -116,13 +131,26 @@ export class NodeGenerator {const user = this.getMainUser();const createTransaction = this.buildCreateTransaction(channelId,+ spaceId,user.userId,channelAttributes);- user.transactions.push(createTransaction);+ const mutation: ApplyCreateTransactionMutation = {+ type: 'apply_create_transaction',+ id: generateId(IdType.Mutation),+ data: createTransaction,+ createdAt: new Date().toISOString(),+ };++ user.mutations.push(mutation);- this.buidMessages(channelId, MESSAGES_PER_CONVERSATION, this.users);+ this.buidMessages(+ spaceId,+ channelId,+ MESSAGES_PER_CONVERSATION,+ this.users+ );}private buildChat(user: User) {@@ -138,17 +166,33 @@ export class NodeGenerator {};const createTransaction = this.buildCreateTransaction(+ chatId,chatId,mainUser.userId,chatAttributes);- mainUser.transactions.push(createTransaction);+ const mutation: ApplyCreateTransactionMutation = {+ type: 'apply_create_transaction',+ id: generateId(IdType.Mutation),+ data: createTransaction,+ createdAt: new Date().toISOString(),+ };++ mainUser.mutations.push(mutation);- this.buidMessages(chatId, MESSAGES_PER_CONVERSATION, [mainUser, user]);+ this.buidMessages(chatId, chatId, MESSAGES_PER_CONVERSATION, [+ mainUser,+ user,+ ]);}- private buildPage(name: string, parentId: string, avatar: string) {+ private buildPage(+ rootId: string,+ name: string,+ parentId: string,+ avatar: string+ ) {const pageId = generateId(IdType.Page);const pageAttributes: NodeAttributes = {type: 'page',@@ -160,42 +204,58 @@ export class NodeGenerator {const user = this.getMainUser();const createTransaction = this.buildCreateTransaction(+ rootId,pageId,user.userId,pageAttributes);- user.transactions.push(createTransaction);+ const mutation: ApplyCreateTransactionMutation = {+ type: 'apply_create_transaction',+ id: generateId(IdType.Mutation),+ data: createTransaction,+ createdAt: new Date().toISOString(),+ };++ user.mutations.push(mutation);}- private buidMessages(conversationId: string, count: number, users: User[]) {+ private buidMessages(+ rootId: string,+ conversationId: string,+ count: number,+ users: User[]+ ) {for (let i = 0; i < count; i++) {- this.buildMessage(conversationId, users);+ this.buildMessage(rootId, conversationId, users);}}- private buildMessage(conversationId: string, users: User[]) {+ private buildMessage(rootId: string, conversationId: string, users: User[]) {const messageId = generateId(IdType.Message);+ const user = this.getRandomUser(users);- const messageAttributes: NodeAttributes = {- type: 'message',- content: this.buildMessageContent(messageId),- parentId: conversationId,- subtype: 'standard',- reactions: {},+ const mutation: CreateMessageMutation = {+ type: 'create_message',+ id: generateId(IdType.Mutation),+ data: {+ id: messageId,+ type: 'standard',+ nodeId: conversationId,+ parentId: conversationId,+ rootId,+ content: {+ blocks: this.buildMessageContent(messageId),+ },+ createdAt: new Date().toISOString(),+ },+ createdAt: new Date().toISOString(),};- const user = this.getRandomUser(users);- const createTransaction = this.buildCreateTransaction(- messageId,- user.userId,- messageAttributes- );-- user.transactions.push(createTransaction);+ user.mutations.push(mutation);}- private buildTasksDatabase(parentId: string) {+ private buildTasksDatabase(rootId: string, parentId: string) {const databaseId = generateId(IdType.Database);const newStatusOption: SelectOptionAttributes = {@@ -396,16 +456,29 @@ export class NodeGenerator {const user = this.getMainUser();const createTransaction = this.buildCreateTransaction(databaseId,+ rootId,user.userId,databaseAttributes);- user.transactions.push(createTransaction);+ const mutation: ApplyCreateTransactionMutation = {+ type: 'apply_create_transaction',+ id: generateId(IdType.Mutation),+ data: createTransaction,+ createdAt: new Date().toISOString(),+ };++ user.mutations.push(mutation);- this.buildRecords(databaseId, databaseAttributes, RECORDS_PER_DATABASE);+ this.buildRecords(+ rootId,+ databaseId,+ databaseAttributes,+ RECORDS_PER_DATABASE+ );}- private buildClientsDatabase(parentId: string) {+ private buildClientsDatabase(rootId: string, parentId: string) {const databaseId = generateId(IdType.Database);const newLeadStatusOption: SelectOptionAttributes = {@@ -638,16 +711,29 @@ export class NodeGenerator {const user = this.getMainUser();const createTransaction = this.buildCreateTransaction(databaseId,+ rootId,user.userId,databaseAttributes);- user.transactions.push(createTransaction);+ const mutation: ApplyCreateTransactionMutation = {+ type: 'apply_create_transaction',+ id: generateId(IdType.Mutation),+ data: createTransaction,+ createdAt: new Date().toISOString(),+ };++ user.mutations.push(mutation);- this.buildRecords(databaseId, databaseAttributes, RECORDS_PER_DATABASE);+ this.buildRecords(+ rootId,+ databaseId,+ databaseAttributes,+ RECORDS_PER_DATABASE+ );}- private buildMeetingsDatabase(parentId: string) {+ private buildMeetingsDatabase(rootId: string, parentId: string) {const databaseId = generateId(IdType.Database);const techTagSelectOption: SelectOptionAttributes = {@@ -766,26 +852,41 @@ export class NodeGenerator {const user = this.getMainUser();const createTransaction = this.buildCreateTransaction(databaseId,+ rootId,user.userId,databaseAttributes);- user.transactions.push(createTransaction);+ const mutation: ApplyCreateTransactionMutation = {+ type: 'apply_create_transaction',+ id: generateId(IdType.Mutation),+ data: createTransaction,+ createdAt: new Date().toISOString(),+ };++ user.mutations.push(mutation);- this.buildRecords(databaseId, databaseAttributes, RECORDS_PER_DATABASE);+ this.buildRecords(+ rootId,+ databaseId,+ databaseAttributes,+ RECORDS_PER_DATABASE+ );}private buildRecords(+ rootId: string,databaseId: string,databaseAttributes: DatabaseAttributes,count: number) {for (let i = 0; i < count; i++) {- this.buildRecord(databaseId, databaseAttributes);+ this.buildRecord(rootId, databaseId, databaseAttributes);}}private buildRecord(+ rootId: string,databaseId: string,databaseAttributes: DatabaseAttributes) {@@ -810,11 +911,19 @@ export class NodeGenerator {const user = this.getRandomUser(this.users);const createTransaction = this.buildCreateTransaction(recordId,+ rootId,user.userId,recordAttributes);- user.transactions.push(createTransaction);+ const mutation: ApplyCreateTransactionMutation = {+ type: 'apply_create_transaction',+ id: generateId(IdType.Mutation),+ data: createTransaction,+ createdAt: new Date().toISOString(),+ };++ user.mutations.push(mutation);}private getRandomUser(users: User[]): User {@@ -831,10 +940,11 @@ export class NodeGenerator {}private buildCreateTransaction(- id: string,+ nodeId: string,+ rootId: string,userId: string,attributes: NodeAttributes- ): LocalTransaction {+ ): LocalCreateTransaction {const ydoc = new YDoc();const model = registry.getModel(attributes.type);@@ -844,8 +954,8 @@ export class NodeGenerator {id: generateId(IdType.Transaction),operation: 'create',data: encodeState(update),- nodeId: id,- nodeType: attributes.type,+ nodeId,+ rootId,createdAt: new Date().toISOString(),createdBy: userId,};@@ -898,7 +1008,7 @@ export class NodeGenerator {} else if (field.type === 'date') {return {type: 'date',- value: faker.date.recent().toISOString(),+ value: faker.date.past().toISOString(),};} else if (field.type === 'email') {return {diff --git a/scripts/src/seed/types.ts b/scripts/src/seed/types.tsindex 56ae1235..7119274c 100644--- a/scripts/src/seed/types.ts+++ b/scripts/src/seed/types.ts@@ -1,4 +1,4 @@-import { LocalTransaction, LoginOutput } from '@colanode/core';+import { LoginOutput, Mutation } from '@colanode/core';export type FakerAccount = {name: string;@@ -10,5 +10,5 @@ export type FakerAccount = {export type User = {login: LoginOutput;userId: string;- transactions: LocalTransaction[];+ mutations: Mutation[];};