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
Refactor text indexing in desktop
Hakan Shehue9f677088ed6
diff --git a/apps/desktop/src/main/data/workspace/migrations.ts b/apps/desktop/src/main/data/workspace/migrations.tsindex 5eeb2622..a682cd9d 100644--- a/apps/desktop/src/main/data/workspace/migrations.ts+++ b/apps/desktop/src/main/data/workspace/migrations.ts@@ -331,28 +331,15 @@ const createEntryPathsTable: Migration = {},};-const createNodeNamesTable: Migration = {+const createTextsTable: Migration = {up: async (db) => {await sql`- CREATE VIRTUAL TABLE node_names USING fts5(id UNINDEXED, name);+ CREATE VIRTUAL TABLE texts USING fts5(id UNINDEXED, name, text);`.execute(db);},down: async (db) => {await sql`- DROP TABLE IF EXISTS node_names;- `.execute(db);- },-};--const createNodeTextsTable: Migration = {- up: async (db) => {- await sql`- CREATE VIRTUAL TABLE node_texts USING fts5(id UNINDEXED, text);- `.execute(db);- },- down: async (db) => {- await sql`- DROP TABLE IF EXISTS node_texts;+ DROP TABLE IF EXISTS texts;`.execute(db);},};@@ -386,7 +373,6 @@ export const workspaceDatabaseMigrations: Record<string, Migration> = {'00011_create_file_interactions_table': createFileInteractionsTable,'00012_create_mutations_table': createMutationsTable,'00013_create_entry_paths_table': createEntryPathsTable,- '00014_create_node_names_table': createNodeNamesTable,- '00015_create_node_texts_table': createNodeTextsTable,- '00016_create_cursors_table': createCursorsTable,+ '00014_create_texts_table': createTextsTable,+ '00015_create_cursors_table': createCursorsTable,};diff --git a/apps/desktop/src/main/data/workspace/schema.ts b/apps/desktop/src/main/data/workspace/schema.tsindex 0f126ca8..9089e596 100644--- a/apps/desktop/src/main/data/workspace/schema.ts+++ b/apps/desktop/src/main/data/workspace/schema.ts@@ -209,23 +209,15 @@ export type SelectMutation = Selectable<MutationTable>;export type CreateMutation = Insertable<MutationTable>;export type UpdateMutation = Updateable<MutationTable>;-interface NodeNameTable {+interface TextTable {id: ColumnType<string, string, never>;- name: ColumnType<string, string, string>;-}--export type SelectNodeName = Selectable<NodeNameTable>;-export type CreateNodeName = Insertable<NodeNameTable>;-export type UpdateNodeName = Updateable<NodeNameTable>;--interface NodeTextTable {- id: ColumnType<string, string, never>;- text: ColumnType<string, string, string>;+ name: ColumnType<string | null, string | null, string | null>;+ text: ColumnType<string | null, string | null, string | null>;}-export type SelectNodeText = Selectable<NodeTextTable>;-export type CreateNodeText = Insertable<NodeTextTable>;-export type UpdateNodeText = Updateable<NodeTextTable>;+export type SelectText = Selectable<TextTable>;+export type CreateText = Insertable<TextTable>;+export type UpdateText = Updateable<TextTable>;interface CursorTable {key: ColumnType<string, string, never>;@@ -248,7 +240,6 @@ export interface WorkspaceDatabaseSchema {file_states: FileStateTable;file_interactions: FileInteractionTable;mutations: MutationTable;- node_names: NodeNameTable;- node_texts: NodeTextTable;+ texts: TextTable;cursors: CursorTable;}diff --git a/apps/desktop/src/main/queries/users/user-search.ts b/apps/desktop/src/main/queries/users/user-search.tsindex 863031d3..6e596919 100644--- a/apps/desktop/src/main/queries/users/user-search.ts+++ b/apps/desktop/src/main/queries/users/user-search.ts@@ -75,9 +75,9 @@ export class UserSearchQueryHandlerconst query = sql<SelectUser>`SELECT u.*FROM users u- JOIN node_names nn ON u.id = nn.id+ JOIN texts t ON u.id = t.idWHERE u.id != ${input.userId}- AND nn.name MATCH ${input.searchQuery + '*'}+ AND t.name MATCH ${input.searchQuery + '*'}${exclude.length > 0? sql`AND u.id NOT IN (${sql.join(diff --git a/apps/desktop/src/main/services/entry-service.ts b/apps/desktop/src/main/services/entry-service.tsindex a339cbfa..f9026ea5 100644--- a/apps/desktop/src/main/services/entry-service.ts+++ b/apps/desktop/src/main/services/entry-service.ts@@ -120,8 +120,6 @@ class EntryService {const createdAt = new Date().toISOString();const transactionId = generateId(IdType.Transaction);-- const name = model.getName(inputItem.id, inputItem.attributes);const text = model.getText(inputItem.id, inputItem.attributes);const createdEntry = await transaction@@ -181,17 +179,10 @@ class EntryService {createdMutations.push(mutation);- if (name) {- await transaction- .insertInto('node_names')- .values({ id: inputItem.id, name })- .execute();- }-if (text) {await transaction- .insertInto('node_texts')- .values({ id: inputItem.id, text })+ .insertInto('texts')+ .values({ id: inputItem.id, name: text.name, text: text.text }).execute();}}@@ -296,7 +287,6 @@ class EntryService {}const update = ydoc.updateAttributes(model.schema, updatedAttributes);- const name = model.getName(entryId, updatedAttributes);const text = model.getText(entryId, updatedAttributes);const { updatedEntry, createdMutation } = await workspaceDatabase@@ -354,31 +344,14 @@ class EntryService {throw new Error('Failed to create mutation');}- if (name !== undefined) {- await trx- .deleteFrom('node_names')- .where('id', '=', entryId)- .execute();- }-- if (name) {- await trx- .insertInto('node_names')- .values({ id: entryId, name })- .execute();- }-if (text !== undefined) {- await trx- .deleteFrom('node_texts')- .where('id', '=', entryId)- .execute();+ await trx.deleteFrom('texts').where('id', '=', entryId).execute();}if (text) {await trx- .insertInto('node_texts')- .values({ id: entryId, text })+ .insertInto('texts')+ .values({ id: entryId, name: text.name, text: text.text }).execute();}@@ -469,8 +442,7 @@ class EntryService {.where('entry_id', '=', entryId).execute();- await trx.deleteFrom('node_names').where('id', '=', entryId).execute();- await trx.deleteFrom('node_texts').where('id', '=', entryId).execute();+ await trx.deleteFrom('texts').where('id', '=', entryId).execute();const createdTransaction = await trx.insertInto('transactions')@@ -608,7 +580,6 @@ class EntryService {}const attributesJson = JSON.stringify(attributes);- const name = model.getName(entryId, attributes);const text = model.getText(entryId, attributes);const existingEntry = await workspaceDatabase@@ -671,26 +642,14 @@ class EntryService {).execute();- await trx- .deleteFrom('node_names')- .where('id', '=', entryId)- .execute();- await trx- .deleteFrom('node_texts')- .where('id', '=', entryId)- .execute();-- if (name) {- await trx- .insertInto('node_names')- .values({ id: entryId, name })- .execute();+ if (text !== undefined) {+ await trx.deleteFrom('texts').where('id', '=', entryId).execute();}if (text) {await trx- .insertInto('node_texts')- .values({ id: entryId, text })+ .insertInto('texts')+ .values({ id: entryId, name: text.name, text: text.text }).execute();}});@@ -762,20 +721,14 @@ class EntryService {).execute();- await trx.deleteFrom('node_names').where('id', '=', entryId).execute();- await trx.deleteFrom('node_texts').where('id', '=', entryId).execute();-- if (name) {- await trx- .insertInto('node_names')- .values({ id: entryId, name })- .execute();+ if (text !== undefined) {+ await trx.deleteFrom('texts').where('id', '=', entryId).execute();}if (text) {await trx- .insertInto('node_texts')- .values({ id: entryId, text })+ .insertInto('texts')+ .values({ id: entryId, name: text.name, text: text.text }).execute();}});@@ -846,7 +799,6 @@ class EntryService {return;}- const name = model.getName(transaction.entryId, attributes);const text = model.getText(transaction.entryId, attributes);const { createdEntry } = await workspaceDatabase@@ -880,17 +832,14 @@ class EntryService {}).execute();- if (name) {- await trx- .insertInto('node_names')- .values({ id: transaction.entryId, name })- .execute();- }-if (text) {await trx- .insertInto('node_texts')- .values({ id: transaction.entryId, text })+ .insertInto('texts')+ .values({+ id: transaction.entryId,+ name: text.name,+ text: text.text,+ }).execute();}@@ -977,8 +926,6 @@ class EntryService {ydoc.applyUpdate(transaction.data);const attributes = ydoc.getAttributes<EntryAttributes>();-- const name = model.getName(transaction.entryId, attributes);const text = model.getText(transaction.entryId, attributes);const { updatedEntry } = await workspaceDatabase@@ -1011,31 +958,21 @@ class EntryService {}).execute();- if (name !== undefined) {- await trx- .deleteFrom('node_names')- .where('id', '=', transaction.entryId)- .execute();- }-- if (name) {- await trx- .insertInto('node_names')- .values({ id: transaction.entryId, name })- .execute();- }-if (text !== undefined) {await trx- .deleteFrom('node_texts')+ .deleteFrom('texts').where('id', '=', transaction.entryId).execute();}if (text) {await trx- .insertInto('node_texts')- .values({ id: transaction.entryId, text })+ .insertInto('texts')+ .values({+ id: transaction.entryId,+ name: text.name,+ text: text.text,+ }).execute();}@@ -1097,12 +1034,7 @@ class EntryService {.execute();await trx- .deleteFrom('node_names')- .where('id', '=', transaction.entryId)- .execute();-- await trx- .deleteFrom('node_texts')+ .deleteFrom('texts').where('id', '=', transaction.entryId).execute();});diff --git a/apps/desktop/src/main/services/message-service.ts b/apps/desktop/src/main/services/message-service.tsindex 526830c7..979b65ad 100644--- a/apps/desktop/src/main/services/message-service.ts+++ b/apps/desktop/src/main/services/message-service.ts@@ -1,4 +1,5 @@import {+ extractText,SyncMessageData,SyncMessageInteractionData,SyncMessageReactionData,@@ -51,6 +52,20 @@ class MessageService {return;}+ await workspaceDatabase+ .deleteFrom('texts')+ .where('id', '=', message.id)+ .execute();++ await workspaceDatabase+ .insertInto('texts')+ .values({+ id: message.id,+ name: null,+ text: extractText(message.id, message.content.blocks),+ })+ .execute();+eventBus.publish({type: 'message_updated',userId,@@ -83,6 +98,15 @@ class MessageService {return;}+ await workspaceDatabase+ .insertInto('texts')+ .values({+ id: message.id,+ name: null,+ text: extractText(message.id, message.content.blocks),+ })+ .execute();+eventBus.publish({type: 'message_created',userId,diff --git a/apps/desktop/src/main/services/user-service.ts b/apps/desktop/src/main/services/user-service.tsindex 0bbd9b5b..c149a19b 100644--- a/apps/desktop/src/main/services/user-service.ts+++ b/apps/desktop/src/main/services/user-service.ts@@ -37,6 +37,16 @@ class UserService {.where('version', '<', BigInt(user.version))).execute();++ await workspaceDatabase+ .deleteFrom('texts')+ .where('id', '=', user.id)+ .execute();++ await workspaceDatabase+ .insertInto('texts')+ .values({ id: user.id, name: user.name, text: null })+ .execute();}}diff --git a/packages/core/src/registry/channel.ts b/packages/core/src/registry/channel.tsindex f3daeeb1..4a6f7fef 100644--- a/packages/core/src/registry/channel.ts+++ b/packages/core/src/registry/channel.ts@@ -16,15 +16,16 @@ export type ChannelAttributes = z.infer<typeof channelAttributesSchema>;export const channelModel: EntryModel = {type: 'channel',schema: channelAttributesSchema,- getName: (_, attributes) => {+ getText: (id, attributes) => {if (attributes.type !== 'channel') {return undefined;}- return attributes.name;- },- getText: () => {- return undefined;+ return {+ id,+ name: attributes.name,+ text: null,+ };},canCreate: async (context, attributes) => {if (attributes.type !== 'channel') {diff --git a/packages/core/src/registry/chat.ts b/packages/core/src/registry/chat.tsindex 549aad6d..8929781e 100644--- a/packages/core/src/registry/chat.ts+++ b/packages/core/src/registry/chat.ts@@ -13,9 +13,6 @@ export type ChatAttributes = z.infer<typeof chatAttributesSchema>;export const chatModel: EntryModel = {type: 'chat',schema: chatAttributesSchema,- getName: () => {- return undefined;- },getText: () => {return undefined;},diff --git a/packages/core/src/registry/core.ts b/packages/core/src/registry/core.tsindex c0b9b022..a8ddb158 100644--- a/packages/core/src/registry/core.ts+++ b/packages/core/src/registry/core.ts@@ -19,6 +19,12 @@ export const entryRoleEnum = z.enum(['viewer',]);+export type EntryText = {+ id: string;+ name: string | null;+ text: string | null;+};+export class EntryMutationContext {public accountId: string;public workspaceId: string;@@ -72,12 +78,8 @@ export interface EntryModel {attributes: EntryAttributes) => Promise<boolean>;canDelete: (context: EntryMutationContext, entry: Entry) => Promise<boolean>;- getName: (- id: string,- attributes: EntryAttributes- ) => string | null | undefined;getText: (id: string,attributes: EntryAttributes- ) => string | null | undefined;+ ) => EntryText | undefined | null;}diff --git a/packages/core/src/registry/database.ts b/packages/core/src/registry/database.tsindex b2d4091e..50815fdd 100644--- a/packages/core/src/registry/database.ts+++ b/packages/core/src/registry/database.ts@@ -84,15 +84,16 @@ export type ViewType = 'table' | 'board' | 'calendar';export const databaseModel: EntryModel = {type: 'database',schema: databaseAttributesSchema,- getName: (_, attributes) => {+ getText: (id, attributes) => {if (attributes.type !== 'database') {return undefined;}- return attributes.name;- },- getText: () => {- return undefined;+ return {+ id,+ name: attributes.name,+ text: null,+ };},canCreate: async (context, attributes) => {if (attributes.type !== 'database') {diff --git a/packages/core/src/registry/folder.ts b/packages/core/src/registry/folder.tsindex f0ae2904..95486600 100644--- a/packages/core/src/registry/folder.ts+++ b/packages/core/src/registry/folder.ts@@ -16,15 +16,16 @@ export type FolderAttributes = z.infer<typeof folderAttributesSchema>;export const folderModel: EntryModel = {type: 'folder',schema: folderAttributesSchema,- getName: (_, attributes) => {+ getText: (id, attributes) => {if (attributes.type !== 'folder') {return undefined;}- return attributes.name;- },- getText: () => {- return undefined;+ return {+ id,+ name: attributes.name,+ text: null,+ };},canCreate: async (context, attributes) => {if (attributes.type !== 'folder') {diff --git a/packages/core/src/registry/page.ts b/packages/core/src/registry/page.tsindex 343bc8a1..c4ccc948 100644--- a/packages/core/src/registry/page.ts+++ b/packages/core/src/registry/page.ts@@ -20,19 +20,16 @@ export type PageAttributes = z.infer<typeof pageAttributesSchema>;export const pageModel: EntryModel = {type: 'page',schema: pageAttributesSchema,- getName: (_, attributes) => {- if (attributes.type !== 'page') {- return undefined;- }-- return attributes.name;- },getText: (id, attributes) => {if (attributes.type !== 'page') {return undefined;}- return extractText(id, attributes.content);+ return {+ id,+ name: attributes.name,+ text: extractText(id, attributes.content),+ };},canCreate: async (context, attributes) => {if (attributes.type !== 'page') {diff --git a/packages/core/src/registry/record.ts b/packages/core/src/registry/record.tsindex af075489..db789cb8 100644--- a/packages/core/src/registry/record.ts+++ b/packages/core/src/registry/record.ts@@ -21,19 +21,16 @@ export type RecordAttributes = z.infer<typeof recordAttributesSchema>;export const recordModel: EntryModel = {type: 'record',schema: recordAttributesSchema,- getName: (_, attributes) => {- if (attributes.type !== 'record') {- return undefined;- }-- return attributes.name;- },getText: (id, attributes) => {if (attributes.type !== 'record') {return undefined;}- return extractText(id, attributes.content);+ return {+ id,+ name: attributes.name,+ text: extractText(id, attributes.content),+ };},canCreate: async (context, attributes) => {if (attributes.type !== 'record') {diff --git a/packages/core/src/registry/space.ts b/packages/core/src/registry/space.tsindex dac8d8bb..8e8613fa 100644--- a/packages/core/src/registry/space.ts+++ b/packages/core/src/registry/space.ts@@ -16,15 +16,16 @@ export type SpaceAttributes = z.infer<typeof spaceAttributesSchema>;export const spaceModel: EntryModel = {type: 'space',schema: spaceAttributesSchema,- getName: (_, attributes) => {+ getText: (id, attributes) => {if (attributes.type !== 'space') {return undefined;}- return attributes.name;- },- getText: () => {- return undefined;+ return {+ id,+ name: attributes.name,+ text: null,+ };},canCreate: async (context, __) => {if (context.workspaceRole === 'guest' || context.workspaceRole === 'none') {