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
Make entry parent id nullable and add some indexes in desktop workspace database
Hakan Shehu5863b911da7e
diff --git a/apps/desktop/src/main/data/workspace/migrations.ts b/apps/desktop/src/main/data/workspace/migrations.tsindex c895ef3d..46e6f28e 100644--- a/apps/desktop/src/main/data/workspace/migrations.ts+++ b/apps/desktop/src/main/data/workspace/migrations.ts@@ -37,7 +37,6 @@ const createEntriesTable: Migration = {col.generatedAlwaysAs(sql`json_extract(attributes, '$.parentId')`).stored()- .notNull()).addColumn('root_id', 'text', (col) => col.notNull()).addColumn('attributes', 'text', (col) => col.notNull())@@ -48,9 +47,11 @@ const createEntriesTable: Migration = {.addColumn('transaction_id', 'text', (col) => col.notNull()).execute();- await sql`- CREATE INDEX IF NOT EXISTS "entries_parent_id_type_index" ON "entries" ("parent_id", "type");- `.execute(db);+ await db.schema+ .createIndex('entries_parent_id_type_index')+ .on('entries')+ .columns(['parent_id', 'type'])+ .execute();},down: async (db) => {await db.schema.dropTable('entries').execute();@@ -134,6 +135,12 @@ const createMessagesTable: Migration = {.addColumn('deleted_at', 'text').addColumn('version', 'integer').execute();++ await db.schema+ .createIndex('messages_parent_id_index')+ .on('messages')+ .columns(['parent_id'])+ .execute();},down: async (db) => {await db.schema.dropTable('messages').execute();@@ -207,6 +214,12 @@ const createFilesTable: Migration = {.addColumn('status', 'integer', (col) => col.notNull()).addColumn('version', 'integer').execute();++ await db.schema+ .createIndex('files_parent_id_index')+ .on('files')+ .columns(['parent_id'])+ .execute();},down: async (db) => {await db.schema.dropTable('files').execute();diff --git a/apps/desktop/src/main/data/workspace/schema.ts b/apps/desktop/src/main/data/workspace/schema.tsindex 53ec6cd7..e3763b4c 100644--- a/apps/desktop/src/main/data/workspace/schema.ts+++ b/apps/desktop/src/main/data/workspace/schema.ts@@ -31,7 +31,7 @@ export type UpdateUser = Updateable<UserTable>;interface EntryTable {id: ColumnType<string, string, never>;type: ColumnType<EntryType, never, never>;- parent_id: ColumnType<string, never, never>;+ parent_id: ColumnType<string | null, never, never>;root_id: ColumnType<string, string, never>;attributes: ColumnType<string, string, string>;created_at: ColumnType<string, string, never>;diff --git a/apps/desktop/src/main/mutations/channels/channel-create.ts b/apps/desktop/src/main/mutations/channels/channel-create.tsindex d460e379..0ea54bef 100644--- a/apps/desktop/src/main/mutations/channels/channel-create.ts+++ b/apps/desktop/src/main/mutations/channels/channel-create.ts@@ -40,7 +40,11 @@ export class ChannelCreateMutationHandlerparentId: input.spaceId,};- await entryService.createEntry(input.userId, { id, attributes });+ await entryService.createEntry(input.userId, {+ id,+ attributes,+ parentId: input.spaceId,+ });return {id: id,diff --git a/apps/desktop/src/main/mutations/chats/chat-create.ts b/apps/desktop/src/main/mutations/chats/chat-create.tsindex 0afb2c75..f979c458 100644--- a/apps/desktop/src/main/mutations/chats/chat-create.ts+++ b/apps/desktop/src/main/mutations/chats/chat-create.ts@@ -42,14 +42,17 @@ export class ChatCreateMutationHandlerconst id = generateId(IdType.Chat);const attributes: ChatAttributes = {type: 'chat',- parentId: id,collaborators: {[input.userId]: 'admin',[input.otherUserId]: 'admin',},};- await entryService.createEntry(input.userId, { id, attributes });+ await entryService.createEntry(input.userId, {+ id,+ attributes,+ parentId: null,+ });return {id,diff --git a/apps/desktop/src/main/mutations/databases/database-create.ts b/apps/desktop/src/main/mutations/databases/database-create.tsindex 04cfe422..e1ee297f 100644--- a/apps/desktop/src/main/mutations/databases/database-create.ts+++ b/apps/desktop/src/main/mutations/databases/database-create.ts@@ -53,6 +53,7 @@ export class DatabaseCreateMutationHandlerawait entryService.createEntry(input.userId, {id: databaseId,attributes,+ parentId: input.spaceId,});return {diff --git a/apps/desktop/src/main/mutations/folders/folder-create.ts b/apps/desktop/src/main/mutations/folders/folder-create.tsindex 71175e80..bcbecd76 100644--- a/apps/desktop/src/main/mutations/folders/folder-create.ts+++ b/apps/desktop/src/main/mutations/folders/folder-create.ts@@ -22,7 +22,11 @@ export class FolderCreateMutationHandlercollaborators: {},};- await entryService.createEntry(input.userId, { id, attributes });+ await entryService.createEntry(input.userId, {+ id,+ attributes,+ parentId: input.parentId,+ });return {id: id,diff --git a/apps/desktop/src/main/mutations/pages/page-create.ts b/apps/desktop/src/main/mutations/pages/page-create.tsindex efcbaac9..724a5c91 100644--- a/apps/desktop/src/main/mutations/pages/page-create.ts+++ b/apps/desktop/src/main/mutations/pages/page-create.ts@@ -22,7 +22,11 @@ export class PageCreateMutationHandlercontent: {},};- await entryService.createEntry(input.userId, { id, attributes });+ await entryService.createEntry(input.userId, {+ id,+ attributes,+ parentId: input.parentId,+ });return {id: id,diff --git a/apps/desktop/src/main/mutations/records/record-create.ts b/apps/desktop/src/main/mutations/records/record-create.tsindex b7288f0e..a2af2c4e 100644--- a/apps/desktop/src/main/mutations/records/record-create.ts+++ b/apps/desktop/src/main/mutations/records/record-create.ts@@ -23,7 +23,11 @@ export class RecordCreateMutationHandlercontent: {},};- await entryService.createEntry(input.userId, { id, attributes });+ await entryService.createEntry(input.userId, {+ id,+ attributes,+ parentId: input.databaseId,+ });return {id: id,diff --git a/apps/desktop/src/main/mutations/servers/server-create.ts b/apps/desktop/src/main/mutations/servers/server-create.tsindex 1018c6a1..efd633d7 100644--- a/apps/desktop/src/main/mutations/servers/server-create.ts+++ b/apps/desktop/src/main/mutations/servers/server-create.ts@@ -43,8 +43,12 @@ export class ServerCreateMutationHandlerconst parseDomain = (domain: string): string => {try {- // Try to parse as URL first- const url = new URL(domain.toLowerCase());+ const lowerCaseDomain = domain.toLowerCase();+ const urlString = lowerCaseDomain.startsWith('http')+ ? lowerCaseDomain+ : `http://${lowerCaseDomain}`;++ const url = new URL(urlString);return url.host; // host includes domain + port if present} catch {// If not a valid URL, treat as domain directlydiff --git a/apps/desktop/src/main/mutations/spaces/space-create.ts b/apps/desktop/src/main/mutations/spaces/space-create.tsindex 51868ddf..a0886b8b 100644--- a/apps/desktop/src/main/mutations/spaces/space-create.ts+++ b/apps/desktop/src/main/mutations/spaces/space-create.ts@@ -48,7 +48,6 @@ export class SpaceCreateMutationHandlercollaborators: {[input.userId]: 'admin',},- parentId: spaceId,description: input.description,avatar: null,};@@ -56,6 +55,7 @@ export class SpaceCreateMutationHandlerawait entryService.createEntry(input.userId, {id: spaceId,attributes: spaceAttributes,+ parentId: null,});const pageId = generateId(IdType.Page);@@ -70,6 +70,7 @@ export class SpaceCreateMutationHandlerawait entryService.createEntry(input.userId, {id: pageId,attributes: pageAttributes,+ parentId: spaceId,});const channelId = generateId(IdType.Channel);@@ -82,6 +83,7 @@ export class SpaceCreateMutationHandlerawait entryService.createEntry(input.userId, {id: channelId,attributes: channelAttributes,+ parentId: spaceId,});return {diff --git a/apps/desktop/src/main/queries/chats/chat-list.ts b/apps/desktop/src/main/queries/chats/chat-list.tsindex 139fd847..e174b222 100644--- a/apps/desktop/src/main/queries/chats/chat-list.ts+++ b/apps/desktop/src/main/queries/chats/chat-list.ts@@ -90,6 +90,7 @@ export class ChatListQueryHandler implements QueryHandler<ChatListQueryInput> {const rows = await workspaceDatabase.selectFrom('entries').selectAll()+ .where('parent_id', 'is', null).where('type', '=', 'chat').execute();diff --git a/apps/desktop/src/main/queries/spaces/space-list.ts b/apps/desktop/src/main/queries/spaces/space-list.tsindex f4a5b41d..118d0b16 100644--- a/apps/desktop/src/main/queries/spaces/space-list.ts+++ b/apps/desktop/src/main/queries/spaces/space-list.ts@@ -92,6 +92,7 @@ export class SpaceListQueryHandlerconst rows = await workspaceDatabase.selectFrom('entries').selectAll()+ .where('parent_id', 'is', null).where('type', '=', 'space').execute();diff --git a/apps/desktop/src/main/services/entry-service.ts b/apps/desktop/src/main/services/entry-service.tsindex cba2dca9..8128f90b 100644--- a/apps/desktop/src/main/services/entry-service.ts+++ b/apps/desktop/src/main/services/entry-service.ts@@ -36,6 +36,7 @@ const UPDATE_RETRIES_LIMIT = 20;export type CreateEntryInput = {id: string;attributes: EntryAttributes;+ parentId: string | null;};export type UpdateEntryResult =@@ -60,18 +61,18 @@ class EntryService {}let root: Entry | null = null;- const parent = await fetchEntry(- workspaceDatabase,- input.attributes.parentId- );- if (parent) {- if (parent.id === parent.root_id) {- root = mapEntry(parent);- } else {- const rootRow = await fetchEntry(workspaceDatabase, parent.root_id);- if (rootRow) {- root = mapEntry(rootRow);+ if (input.parentId) {+ const parent = await fetchEntry(workspaceDatabase, input.parentId);++ if (parent) {+ if (parent.id === parent.root_id) {+ root = mapEntry(parent);+ } else {+ const rootRow = await fetchEntry(workspaceDatabase, parent.root_id);+ if (rootRow) {+ root = mapEntry(rootRow);+ }}}}diff --git a/apps/server/src/data/migrations.ts b/apps/server/src/data/migrations.tsindex 8b847ed0..b92bf7ce 100644--- a/apps/server/src/data/migrations.ts+++ b/apps/server/src/data/migrations.ts@@ -154,7 +154,6 @@ const createEntriesTable: Migration = {col.generatedAlwaysAs(sql`(attributes->>'parentId')::VARCHAR(30)`).stored()- .notNull()).addColumn('root_id', 'varchar(30)', (col) => col.notNull()).addColumn('workspace_id', 'varchar(30)', (col) => col.notNull())diff --git a/apps/server/src/data/schema.ts b/apps/server/src/data/schema.tsindex 8bcfcdc1..f69447b7 100644--- a/apps/server/src/data/schema.ts+++ b/apps/server/src/data/schema.ts@@ -95,7 +95,7 @@ export type UpdateUser = Updateable<UserTable>;interface EntryTable {id: ColumnType<string, string, never>;type: ColumnType<EntryType, never, never>;- parent_id: ColumnType<string, never, never>;+ parent_id: ColumnType<string | null, never, never>;root_id: ColumnType<string, string, never>;workspace_id: ColumnType<string, string, never>;attributes: JSONColumnType<EntryAttributes, string | null, string | null>;diff --git a/apps/server/src/services/workspace-service.ts b/apps/server/src/services/workspace-service.tsindex 9bd441cf..efc3bad2 100644--- a/apps/server/src/services/workspace-service.ts+++ b/apps/server/src/services/workspace-service.ts@@ -69,7 +69,6 @@ class WorkspaceService {type: 'space',name: 'Home',description: 'This is your home space.',- parentId: spaceId,collaborators: {[userId]: 'admin',},diff --git a/packages/core/src/registry/chat.ts b/packages/core/src/registry/chat.tsindex 89d5dee5..c5650be0 100644--- a/packages/core/src/registry/chat.ts+++ b/packages/core/src/registry/chat.ts@@ -4,7 +4,6 @@ import { entryRoleEnum } from './core';export const chatAttributesSchema = z.object({type: z.literal('chat'),- parentId: z.string(),collaborators: z.record(z.string(), entryRoleEnum),});diff --git a/packages/core/src/registry/index.ts b/packages/core/src/registry/index.tsindex 1d860c04..7213ec10 100644--- a/packages/core/src/registry/index.ts+++ b/packages/core/src/registry/index.ts@@ -10,7 +10,7 @@ import { SpaceAttributes, spaceAttributesSchema } from './space';type EntryBase = {id: string;- parentId: string;+ parentId: string | null;rootId: string;createdAt: string;createdBy: string;diff --git a/packages/core/src/registry/space.ts b/packages/core/src/registry/space.tsindex 767ba596..325e7764 100644--- a/packages/core/src/registry/space.ts+++ b/packages/core/src/registry/space.ts@@ -5,7 +5,6 @@ import { entryRoleEnum } from './core';export const spaceAttributesSchema = z.object({type: z.literal('space'),name: z.string(),- parentId: z.string(),description: z.string().nullable(),avatar: z.string().nullable().optional(),collaborators: z.record(z.string(), entryRoleEnum),diff --git a/packages/core/src/types/entries.ts b/packages/core/src/types/entries.tsindex 6a21f82e..d4f09a08 100644--- a/packages/core/src/types/entries.ts+++ b/packages/core/src/types/entries.ts@@ -3,7 +3,7 @@ import { EntryAttributes } from '../registry';export type EntryOutput = {id: string;workspaceId: string;- parentId: string;+ parentId: string | null;type: string;attributes: EntryAttributes;state: string;