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
Add indexes for frequent queries in server
Hakan Shehuba6b4bad9842
diff --git a/apps/desktop/src/main/data/app/migrations.ts b/apps/desktop/src/main/data/app/migrations.tsindex e3ebffd8..ff921f33 100644--- a/apps/desktop/src/main/data/app/migrations.ts+++ b/apps/desktop/src/main/data/app/migrations.ts@@ -71,7 +71,6 @@ const createWorkspacesTable: Migration = {.addColumn('name', 'text', (col) => col.notNull()).addColumn('description', 'text').addColumn('avatar', 'text')- .addColumn('version_id', 'text', (col) => col.notNull()).addColumn('role', 'text', (col) => col.notNull()).execute();},diff --git a/apps/desktop/src/main/data/app/schema.ts b/apps/desktop/src/main/data/app/schema.tsindex 1113b513..e8bf1ee6 100644--- a/apps/desktop/src/main/data/app/schema.ts+++ b/apps/desktop/src/main/data/app/schema.ts@@ -37,7 +37,6 @@ interface WorkspaceTable {name: ColumnType<string, string, string>;description: ColumnType<string | null, string | null, string | null>;avatar: ColumnType<string | null, string | null, string | null>;- version_id: ColumnType<string, string, string>;role: ColumnType<WorkspaceRole, WorkspaceRole, WorkspaceRole>;}diff --git a/apps/desktop/src/main/mutations/accounts/email-login.ts b/apps/desktop/src/main/mutations/accounts/email-login.tsindex 214b616d..cd9c6496 100644--- a/apps/desktop/src/main/mutations/accounts/email-login.ts+++ b/apps/desktop/src/main/mutations/accounts/email-login.ts@@ -83,7 +83,6 @@ export class EmailLoginMutationHandlerrole: workspace.user.role,description: workspace.description,user_id: workspace.user.id,- version_id: workspace.versionId,}))).execute();diff --git a/apps/desktop/src/main/mutations/accounts/email-register.ts b/apps/desktop/src/main/mutations/accounts/email-register.tsindex e71d73f7..0a2e2875 100644--- a/apps/desktop/src/main/mutations/accounts/email-register.ts+++ b/apps/desktop/src/main/mutations/accounts/email-register.ts@@ -84,7 +84,6 @@ export class EmailRegisterMutationHandlerrole: workspace.user.role,description: workspace.description,user_id: workspace.user.id,- version_id: workspace.versionId,}))).execute();diff --git a/apps/desktop/src/main/mutations/workspaces/workspace-create.ts b/apps/desktop/src/main/mutations/workspaces/workspace-create.tsindex 56bf4960..3d867be1 100644--- a/apps/desktop/src/main/mutations/workspaces/workspace-create.ts+++ b/apps/desktop/src/main/mutations/workspaces/workspace-create.ts@@ -68,7 +68,6 @@ export class WorkspaceCreateMutationHandleravatar: data.avatar,role: data.user.role,user_id: data.user.id,- version_id: data.versionId,}).onConflict((cb) => cb.doNothing()).executeTakeFirst();@@ -86,7 +85,6 @@ export class WorkspaceCreateMutationHandlerid: createdWorkspace.workspace_id,userId: createdWorkspace.user_id,name: createdWorkspace.name,- versionId: createdWorkspace.version_id,accountId: createdWorkspace.account_id,role: createdWorkspace.role,avatar: createdWorkspace.avatar,diff --git a/apps/desktop/src/main/mutations/workspaces/workspace-update.ts b/apps/desktop/src/main/mutations/workspaces/workspace-update.tsindex 961cef2e..7a028544 100644--- a/apps/desktop/src/main/mutations/workspaces/workspace-update.ts+++ b/apps/desktop/src/main/mutations/workspaces/workspace-update.ts@@ -64,7 +64,6 @@ export class WorkspaceUpdateMutationHandlerdescription: data.description,avatar: data.avatar,role: data.role,- version_id: data.versionId,}).where((eb) =>eb.and([@@ -87,7 +86,6 @@ export class WorkspaceUpdateMutationHandlerid: updatedWorkspace.workspace_id,userId: updatedWorkspace.user_id,name: updatedWorkspace.name,- versionId: updatedWorkspace.version_id,accountId: updatedWorkspace.account_id,role: updatedWorkspace.role,},diff --git a/apps/desktop/src/main/services/account-service.ts b/apps/desktop/src/main/services/account-service.tsindex aebe4682..d375ee24 100644--- a/apps/desktop/src/main/services/account-service.ts+++ b/apps/desktop/src/main/services/account-service.ts@@ -123,7 +123,6 @@ class AccountService {avatar: workspace.avatar,description: workspace.description,role: workspace.user.role,- version_id: workspace.versionId,}).returningAll().executeTakeFirst();@@ -153,7 +152,6 @@ class AccountService {avatar: workspace.avatar,description: workspace.description,role: workspace.user.role,- version_id: workspace.versionId,}).where('user_id', '=', currentWorkspace.user_id).executeTakeFirst();diff --git a/apps/desktop/src/main/utils.ts b/apps/desktop/src/main/utils.tsindex bf4b265a..fcdc970b 100644--- a/apps/desktop/src/main/utils.ts+++ b/apps/desktop/src/main/utils.ts@@ -218,7 +218,6 @@ export const mapWorkspace = (row: SelectWorkspace): Workspace => {return {id: row.workspace_id,name: row.name,- versionId: row.version_id,accountId: row.account_id,role: row.role,userId: row.user_id,diff --git a/apps/desktop/src/shared/types/workspaces.ts b/apps/desktop/src/shared/types/workspaces.tsindex c8e8bb1b..444d10ab 100644--- a/apps/desktop/src/shared/types/workspaces.ts+++ b/apps/desktop/src/shared/types/workspaces.ts@@ -5,7 +5,6 @@ export type Workspace = {name: string;description?: string | null;avatar?: string | null;- versionId: string;accountId: string;role: WorkspaceRole;userId: string;diff --git a/apps/server/src/controllers/client/accounts/account-sync.ts b/apps/server/src/controllers/client/accounts/account-sync.tsindex da10a822..c5bfe485 100644--- a/apps/server/src/controllers/client/accounts/account-sync.ts+++ b/apps/server/src/controllers/client/accounts/account-sync.ts@@ -51,7 +51,6 @@ export const accountSyncHandler = async (workspaceOutputs.push({id: workspace.id,name: workspace.name,- versionId: workspace.version_id,avatar: workspace.avatar,description: workspace.description,user: {diff --git a/apps/server/src/controllers/client/workspaces/files/file-download-get.ts b/apps/server/src/controllers/client/workspaces/files/file-download-get.tsindex a969ccad..1db97e9b 100644--- a/apps/server/src/controllers/client/workspaces/files/file-download-get.ts+++ b/apps/server/src/controllers/client/workspaces/files/file-download-get.ts@@ -3,12 +3,13 @@ import {CreateDownloadOutput,hasEntryRole,ApiErrorCode,+ extractEntryRole,} from '@colanode/core';import { GetObjectCommand } from '@aws-sdk/client-s3';import { getSignedUrl } from '@aws-sdk/s3-request-presigner';import { database } from '@/data/database';-import { fetchEntryRole } from '@/lib/entries';+import { fetchEntry, mapEntry } from '@/lib/entries';import { BUCKET_NAMES, filesStorage } from '@/data/storage';import { ResponseBuilder } from '@/lib/response-builder';@@ -31,7 +32,15 @@ export const fileDownloadGetHandler = async (});}- const role = await fetchEntryRole(file.root_id, res.locals.user.id);+ const root = await fetchEntry(file.root_id);+ if (!root) {+ return ResponseBuilder.badRequest(res, {+ code: ApiErrorCode.RootNotFound,+ message: 'Root not found.',+ });+ }++ const role = extractEntryRole(mapEntry(root), res.locals.user.id);if (role === null || !hasEntryRole(role, 'viewer')) {return ResponseBuilder.forbidden(res, {code: ApiErrorCode.FileNoAccess,diff --git a/apps/server/src/controllers/client/workspaces/workspace-get.ts b/apps/server/src/controllers/client/workspaces/workspace-get.tsindex a5363e02..e871988b 100644--- a/apps/server/src/controllers/client/workspaces/workspace-get.ts+++ b/apps/server/src/controllers/client/workspaces/workspace-get.ts@@ -42,7 +42,6 @@ export const workspaceGetHandler = async (name: workspace.name,description: workspace.description,avatar: workspace.avatar,- versionId: workspace.version_id,user: {id: user.id,accountId: user.account_id,diff --git a/apps/server/src/controllers/client/workspaces/workspace-update.ts b/apps/server/src/controllers/client/workspaces/workspace-update.tsindex aefe01fc..3b67cf38 100644--- a/apps/server/src/controllers/client/workspaces/workspace-update.ts+++ b/apps/server/src/controllers/client/workspaces/workspace-update.ts@@ -54,7 +54,6 @@ export const workspaceUpdateHandler = async (name: updatedWorkspace.name,description: updatedWorkspace.description,avatar: updatedWorkspace.avatar,- versionId: updatedWorkspace.version_id,user: {id: res.locals.user.id,accountId: res.locals.user.account_id,diff --git a/apps/server/src/data/migrations.ts b/apps/server/src/data/migrations.tsindex edbb539a..8b847ed0 100644--- a/apps/server/src/data/migrations.ts+++ b/apps/server/src/data/migrations.ts@@ -40,8 +40,15 @@ const createDevicesTable: Migration = {.addColumn('last_online_at', 'timestamptz').addColumn('last_active_at', 'timestamptz').execute();++ await db.schema+ .createIndex('devices_account_id_idx')+ .on('devices')+ .column('account_id')+ .execute();},down: async (db) => {+ await db.schema.dropIndex('devices_account_id_idx').execute();await db.schema.dropTable('devices').execute();},};@@ -60,7 +67,6 @@ const createWorkspacesTable: Migration = {.addColumn('updated_at', 'timestamptz').addColumn('updated_by', 'varchar(30)').addColumn('status', 'integer', (col) => col.notNull())- .addColumn('version_id', 'varchar(30)', (col) => col.notNull()).execute();},down: async (db) => {@@ -104,6 +110,12 @@ const createUsersTable: Migration = {]).execute();+ await db.schema+ .createIndex('users_workspace_id_version_idx')+ .on('users')+ .columns(['workspace_id', 'version'])+ .execute();+await sql`CREATE OR REPLACE FUNCTION update_user_version() RETURNS TRIGGER AS $$BEGIN@@ -124,6 +136,7 @@ const createUsersTable: Migration = {DROP FUNCTION IF EXISTS update_user_version();`.execute(db);+ await db.schema.dropIndex('users_workspace_id_version_idx').execute();await db.schema.dropTable('users').execute();await sql`DROP SEQUENCE IF EXISTS users_version_sequence`.execute(db);},@@ -186,8 +199,25 @@ const createEntryTransactionsTable: Migration = {.defaultTo(sql`nextval('entry_transactions_version_sequence')`)).execute();++ await db.schema+ .createIndex('entry_transactions_entry_id_idx')+ .on('entry_transactions')+ .column('entry_id')+ .execute();++ await db.schema+ .createIndex('entry_transactions_root_id_version_idx')+ .on('entry_transactions')+ .columns(['root_id', 'version'])+ .execute();},down: async (db) => {+ await db.schema.dropIndex('entry_transactions_entry_id_idx').execute();+ await db.schema+ .dropIndex('entry_transactions_root_id_version_idx')+ .execute();+await db.schema.dropTable('entry_transactions').execute();await sql`DROP SEQUENCE IF EXISTS entry_transactions_version_sequence`.execute(db@@ -227,6 +257,12 @@ const createCollaborationsTable: Migration = {]).execute();+ await db.schema+ .createIndex('collaborations_collaborator_version_idx')+ .on('collaborations')+ .columns(['collaborator_id', 'version'])+ .execute();+await sql`CREATE OR REPLACE FUNCTION update_collaboration_version() RETURNS TRIGGER AS $$BEGIN@@ -247,6 +283,10 @@ const createCollaborationsTable: Migration = {DROP FUNCTION IF EXISTS update_collaboration_version();`.execute(db);+ await db.schema+ .dropIndex('collaborations_collaborator_version_idx')+ .execute();+await db.schema.dropTable('collaborations').execute();await sql`DROP SEQUENCE IF EXISTS collaborations_version_sequence`.execute(db@@ -299,6 +339,12 @@ const createEntryInteractionsTable: Migration = {FOR EACH ROWEXECUTE FUNCTION update_entry_interaction_version();`.execute(db);++ await db.schema+ .createIndex('entry_interactions_root_id_version_idx')+ .on('entry_interactions')+ .columns(['root_id', 'version'])+ .execute();},down: async (db) => {await sql`@@ -306,6 +352,10 @@ const createEntryInteractionsTable: Migration = {DROP FUNCTION IF EXISTS update_entry_interaction_version();`.execute(db);+ await db.schema+ .dropIndex('entry_interactions_root_id_version_idx')+ .execute();+await db.schema.dropTable('entry_interactions').execute();await sql`DROP SEQUENCE IF EXISTS entry_interactions_version_sequence`.execute(db@@ -342,6 +392,12 @@ const createMessagesTable: Migration = {).execute();+ await db.schema+ .createIndex('messages_root_id_version_idx')+ .on('messages')+ .columns(['root_id', 'version'])+ .execute();+await sql`CREATE OR REPLACE FUNCTION update_message_version() RETURNS TRIGGER AS $$BEGIN@@ -362,6 +418,7 @@ const createMessagesTable: Migration = {DROP FUNCTION IF EXISTS update_message_version();`.execute(db);+ await db.schema.dropIndex('messages_root_id_version_idx').execute();await db.schema.dropTable('messages').execute();await sql`DROP SEQUENCE IF EXISTS messages_version_sequence`.execute(db);},@@ -399,6 +456,12 @@ const createMessageReactionsTable: Migration = {]).execute();+ await db.schema+ .createIndex('message_reactions_root_id_version_idx')+ .on('message_reactions')+ .columns(['root_id', 'version'])+ .execute();+await sql`CREATE OR REPLACE FUNCTION update_message_reaction_version() RETURNS TRIGGER AS $$BEGIN@@ -414,6 +477,10 @@ const createMessageReactionsTable: Migration = {DROP FUNCTION IF EXISTS update_message_reaction_version();`.execute(db);+ await db.schema+ .dropIndex('message_reactions_root_id_version_idx')+ .execute();+await db.schema.dropTable('message_reactions').execute();await sql`DROP SEQUENCE IF EXISTS message_reactions_version_sequence`.execute(db@@ -452,6 +519,12 @@ const createMessageInteractionsTable: Migration = {]).execute();+ await db.schema+ .createIndex('message_interactions_root_id_version_idx')+ .on('message_interactions')+ .columns(['root_id', 'version'])+ .execute();+await sql`CREATE OR REPLACE FUNCTION update_message_interaction_version() RETURNS TRIGGER AS $$BEGIN@@ -472,6 +545,10 @@ const createMessageInteractionsTable: Migration = {DROP FUNCTION IF EXISTS update_message_interaction_version();`.execute(db);+ await db.schema+ .dropIndex('message_interactions_root_id_version_idx')+ .execute();+await db.schema.dropTable('message_interactions').execute();await sql`DROP SEQUENCE IF EXISTS message_interactions_version_sequence`.execute(db@@ -503,8 +580,18 @@ const createMessageTombstonesTable: Migration = {.defaultTo(sql`nextval('message_tombstones_version_sequence')`)).execute();++ await db.schema+ .createIndex('message_tombstones_root_id_version_idx')+ .on('message_tombstones')+ .columns(['root_id', 'version'])+ .execute();},down: async (db) => {+ await db.schema+ .dropIndex('message_tombstones_root_id_version_idx')+ .execute();+await db.schema.dropTable('message_tombstones').execute();await sql`DROP SEQUENCE IF EXISTS message_tombstones_version_sequence`.execute(db@@ -546,6 +633,12 @@ const createFilesTable: Migration = {).execute();+ await db.schema+ .createIndex('files_root_id_version_idx')+ .on('files')+ .columns(['root_id', 'version'])+ .execute();+await sql`CREATE OR REPLACE FUNCTION update_file_version() RETURNS TRIGGER AS $$BEGIN@@ -566,6 +659,7 @@ const createFilesTable: Migration = {DROP FUNCTION IF EXISTS update_file_version();`.execute(db);+ await db.schema.dropIndex('files_root_id_version_idx').execute();await db.schema.dropTable('files').execute();await sql`DROP SEQUENCE IF EXISTS files_version_sequence`.execute(db);},@@ -603,6 +697,12 @@ const createFileInteractionsTable: Migration = {]).execute();+ await db.schema+ .createIndex('file_interactions_root_id_version_idx')+ .on('file_interactions')+ .columns(['root_id', 'version'])+ .execute();+await sql`CREATE OR REPLACE FUNCTION update_file_interaction_version() RETURNS TRIGGER AS $$BEGIN@@ -623,6 +723,10 @@ const createFileInteractionsTable: Migration = {DROP FUNCTION IF EXISTS update_file_interaction_version();`.execute(db);+ await db.schema+ .dropIndex('file_interactions_root_id_version_idx')+ .execute();+await db.schema.dropTable('file_interactions').execute();await sql`DROP SEQUENCE IF EXISTS file_interactions_version_sequence`.execute(db@@ -654,8 +758,15 @@ const createFileTombstonesTable: Migration = {.defaultTo(sql`nextval('file_tombstones_version_sequence')`)).execute();++ await db.schema+ .createIndex('file_tombstones_root_id_version_idx')+ .on('file_tombstones')+ .columns(['root_id', 'version'])+ .execute();},down: async (db) => {+ await db.schema.dropIndex('file_tombstones_root_id_version_idx').execute();await db.schema.dropTable('file_tombstones').execute();await sql`DROP SEQUENCE IF EXISTS file_tombstones_version_sequence`.execute(dbdiff --git a/apps/server/src/data/schema.ts b/apps/server/src/data/schema.tsindex 85a65d16..8bcfcdc1 100644--- a/apps/server/src/data/schema.ts+++ b/apps/server/src/data/schema.ts@@ -64,7 +64,6 @@ interface WorkspaceTable {created_by: ColumnType<string, string, never>;updated_by: ColumnType<string | null, string | null, string>;status: ColumnType<number, number, number>;- version_id: ColumnType<string, string, string>;}export type SelectWorkspace = Selectable<WorkspaceTable>;diff --git a/apps/server/src/lib/entries.ts b/apps/server/src/lib/entries.tsindex 156fdede..cbaa5954 100644--- a/apps/server/src/lib/entries.ts+++ b/apps/server/src/lib/entries.ts@@ -1,15 +1,7 @@-import {- extractEntryCollaborators,- extractEntryRole,- Entry,- EntryOutput,- EntryRole,- EntryType,-} from '@colanode/core';+import { Entry, EntryOutput, EntryType } from '@colanode/core';import { database } from '@/data/database';import { SelectEntry } from '@/data/schema';-import { EntryCollaborator } from '@/types/entries';export const mapEntryOutput = (entry: SelectEntry): EntryOutput => {return {@@ -80,37 +72,3 @@ export const fetchEntryDescendants = async (return result.map((row) => row.descendant_id);};--export const fetchEntryCollaborators = async (- entryId: string-): Promise<EntryCollaborator[]> => {- const ancestors = await fetchEntryAncestors(entryId);- const collaboratorsMap = new Map<string, string>();-- for (const ancestor of ancestors) {- const collaborators = extractEntryCollaborators(ancestor.attributes);- for (const [collaboratorId, role] of Object.entries(collaborators)) {- collaboratorsMap.set(collaboratorId, role);- }- }-- return Array.from(collaboratorsMap.entries()).map(- ([collaboratorId, role]) => ({- entryId: entryId,- collaboratorId: collaboratorId,- role: role,- })- );-};--export const fetchEntryRole = async (- entryId: string,- collaboratorId: string-): Promise<EntryRole | null> => {- const ancestors = await fetchEntryAncestors(entryId);- if (ancestors.length === 0) {- return null;- }-- return extractEntryRole(ancestors.map(mapEntry), collaboratorId);-};diff --git a/apps/server/src/services/account-service.ts b/apps/server/src/services/account-service.tsindex 684a3991..dbc0dccd 100644--- a/apps/server/src/services/account-service.ts+++ b/apps/server/src/services/account-service.ts@@ -38,7 +38,6 @@ class AccountService {workspaceOutputs.push({id: workspace.id,name: workspace.name,- versionId: workspace.version_id,avatar: workspace.avatar,description: workspace.description,user: {diff --git a/apps/server/src/services/entry-service.ts b/apps/server/src/services/entry-service.tsindex 2a497dc9..1b742475 100644--- a/apps/server/src/services/entry-service.ts+++ b/apps/server/src/services/entry-service.ts@@ -28,7 +28,7 @@ import {SelectCollaboration,} from '@/data/schema';import { eventBus } from '@/lib/event-bus';-import { fetchEntry, fetchEntryAncestors, mapEntry } from '@/lib/entries';+import { fetchEntry, mapEntry } from '@/lib/entries';import { createLogger } from '@/lib/logger';import {ApplyCreateTransactionInput,@@ -151,14 +151,12 @@ class EntryService {private async tryUpdateEntry(input: UpdateEntryInput): Promise<UpdateResult<UpdateEntryOutput>> {- const ancestorRows = await fetchEntryAncestors(input.entryId);- const ancestors = ancestorRows.map(mapEntry);-- const entry = ancestors.find((ancestor) => ancestor.id === input.entryId);- if (!entry) {+ const entryRow = await fetchEntry(input.entryId);+ if (!entryRow) {return { type: 'error', output: null };}+ const entry = mapEntry(entryRow);const previousTransactions = await database.selectFrom('entry_transactions').selectAll()diff --git a/apps/server/src/services/workspace-service.ts b/apps/server/src/services/workspace-service.tsindex ca626590..9bd441cf 100644--- a/apps/server/src/services/workspace-service.ts+++ b/apps/server/src/services/workspace-service.ts@@ -33,7 +33,6 @@ class WorkspaceService {created_at: date,created_by: account.id,status: WorkspaceStatus.Active,- version_id: generateId(IdType.Version),}).returningAll().executeTakeFirst();@@ -127,7 +126,6 @@ class WorkspaceService {name: workspace.name,description: workspace.description,avatar: workspace.avatar,- versionId: workspace.version_id,user: {id: user.id,accountId: user.account_id,diff --git a/apps/server/src/synchronizers/collaborations.ts b/apps/server/src/synchronizers/collaborations.tsindex 73b73ed1..ce187db9 100644--- a/apps/server/src/synchronizers/collaborations.ts+++ b/apps/server/src/synchronizers/collaborations.ts@@ -43,7 +43,6 @@ export class CollaborationSynchronizer extends BaseSynchronizer<SyncCollaboratioconst collaborations = await database.selectFrom('collaborations').selectAll()- .where('workspace_id', '=', this.user.workspaceId).where('collaborator_id', '=', this.user.userId).where('version', '>', this.cursor).orderBy('version', 'asc')diff --git a/packages/core/src/types/api.ts b/packages/core/src/types/api.tsindex e47b7f52..5c6d791b 100644--- a/packages/core/src/types/api.ts+++ b/packages/core/src/types/api.ts@@ -35,6 +35,7 @@ export enum ApiErrorCode {UserNotFound = 'user_not_found',TokenMissing = 'token_missing',TokenInvalid = 'token_invalid',+ RootNotFound = 'root_not_found',Unauthorized = 'unauthorized',Forbidden = 'forbidden',diff --git a/packages/core/src/types/workspaces.ts b/packages/core/src/types/workspaces.tsindex c5c01101..c70d8195 100644--- a/packages/core/src/types/workspaces.ts+++ b/packages/core/src/types/workspaces.ts@@ -28,7 +28,6 @@ export type WorkspaceOutput = {name: string;description?: string | null;avatar?: string | null;- versionId: string;user: WorkspaceUserOutput;};