Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

14.1 KB
Raw Blame
/** Database schemas and the typed ops that change them and their rows; row reads run in `@queries/database-sql`. */
import { analytics } from '@app/lib/analytics';
import { useFeatureFlag } from '@app/lib/analytics/posthog';
import { enableDatabases } from '@core/constant/featureFlags';
import type { DatabaseOp } from '@core/database-sql/generated/types';
import { traceDatabaseOps } from '@core/database-sql/trace';
import { fireAndForget } from '@core/util/fireAndForget';
import { catchToResult, type ResultError, throwOnErr } from '@core/util/result';
import { storageServiceClient } from '@service-storage/client';
import type {
  DatabaseOpsError,
  DatabaseSchemaErrorCode,
} from '@service-storage/databases';
import type { CommittedChange } from '@service-storage/generated/schemas/committedChange';
import type { DatabaseDetail } from '@service-storage/generated/schemas/databaseDetail';
import type { ListedDatabase } from '@service-storage/generated/schemas/listedDatabase';
import type { OpResult } from '@service-storage/generated/schemas/opResult';
import type { UndoOutcome } from '@service-storage/generated/schemas/undoOutcome';
import { useQueries, useQuery } from '@tanstack/solid-query';
import { ResultAsync } from 'neverthrow';
import { type Accessor, createEffect, on } from 'solid-js';
import { match, P } from 'ts-pattern';
import { queryClient } from '../client';
import { databasesKeys } from './keys';

const DATABASE_STALE_TIME = 30 * 1000;

const databaseListQueryOptions = {
  queryKey: databasesKeys.list.queryKey,
  queryFn: (): Promise<ListedDatabase[]> =>
    throwOnErr(() => storageServiceClient.databases.list()),
  staleTime: DATABASE_STALE_TIME,
};

export function useDatabasesQuery() {
  const flag = useFeatureFlag(enableDatabases);
  return useQuery(() => ({
    ...databaseListQueryOptions,
    enabled: flag().enabled,
  }));
}

/** Re-read discovery when its surface opens, including databases created by AI or other clients. */
export function useDatabaseDiscoverySync(isActive: Accessor<boolean>) {
  const flag = useFeatureFlag(enableDatabases);
  createEffect(
    on(
      () => isActive() && flag().enabled,
      (active) => {
        if (!active) return;
        queryClient.invalidateQueries({
          queryKey: databasesKeys.list.queryKey,
        });
      }
    )
  );
}

/** One database's schema; spread it and override what a read needs differently. */
export function databaseDetailQueryOptions(id: string) {
  return {
    queryKey: databasesKeys.detail(id).queryKey,
    queryFn: (): Promise<DatabaseDetail> =>
      throwOnErr(() => storageServiceClient.databases.get({ id })),
    staleTime: DATABASE_STALE_TIME,
  };
}

export function useDatabaseDetailQuery(databaseId: () => string | undefined) {
  return useQuery(() => {
    const id = databaseId();
    return { ...databaseDetailQueryOptions(id ?? ''), enabled: !!id };
  });
}

function liveDatabaseIds(listed: readonly ListedDatabase[]): string[] {
  return listed
    .filter((entry) => entry.database.trashed_at === null)
    .map((entry) => entry.database.id);
}

/** Every database the viewer can reach, in detail, for a statement that may read any of them. */
export function useViewerDatabases(): {
  databases: Accessor<DatabaseDetail[] | undefined>;
  error: Accessor<unknown>;
} {
  const list = useDatabasesQuery();
  const details = useQueries(() => ({
    queries: (list.isSuccess ? liveDatabaseIds(list.data) : []).map(
      databaseDetailQueryOptions
    ),
  }));
  return {
    databases: () =>
      list.isSuccess && details.every((detail) => detail.isSuccess)
        ? details.map((detail) => detail.data)
        : undefined,
    error: () =>
      list.isError
        ? list.error
        : details.find((detail) => detail.isError)?.error,
  };
}

/** A cached read; the query's thrown errors come back as the client's own. */
function fetchCached<Data>(options: {
  queryKey: readonly unknown[];
  queryFn: () => Promise<Data>;
  staleTime: number;
}): ResultAsync<Data, ResultError[]> {
  return new ResultAsync(catchToResult(() => queryClient.fetchQuery(options)));
}

/** Share the initial block load with the mounted view's schema query. */
export function fetchDatabaseDetail(
  id: string
): ResultAsync<DatabaseDetail, ResultError[]> {
  return fetchCached(databaseDetailQueryOptions(id));
}

/** {@link useViewerDatabases}, read once. */
export function fetchViewerDatabases(): ResultAsync<
  DatabaseDetail[],
  ResultError[]
> {
  return fetchCached(databaseListQueryOptions).andThen((listed) =>
    ResultAsync.combine(
      liveDatabaseIds(listed).map((id) =>
        fetchCached(databaseDetailQueryOptions(id))
      )
    )
  );
}

/** A batch the viewer committed in this session: its ops, and the journal's changes, which undo names. */
export type CommittedDatabaseBatch = {
  databaseId: string;
  ops: DatabaseOp[];
  changes: CommittedChange[];
  /** The version the batch left each table it touched at, by table id. */
  tableVersions: Record<string, number>;
};

/** The newest version a batch's results and journaled changes report for each table. */
function committedTableVersions(
  results: OpResult[],
  changes: CommittedChange[]
): Record<string, number> {
  const reported = [
    ...results.flatMap((result) =>
      match(result)
        .with({ kind: 'reorder_tables' }, ({ tables }) => tables)
        .with({ tableVersion: P.number }, ({ table, tableVersion }) => [
          { table, version: tableVersion },
        ])
        .otherwise(() => [])
    ),
    ...changes,
  ];
  const versions: Record<string, number> = {};
  for (const { table, version } of reported)
    versions[table] = Math.max(versions[table] ?? version, version);
  return versions;
}

const committedListeners = new Set<(batch: CommittedDatabaseBatch) => void>();

/** Hear of every batch the viewer commits through {@link applyDatabaseOps}; answers the unsubscribe. */
export function onDatabaseBatchCommitted(
  listener: (batch: CommittedDatabaseBatch) => void
): () => void {
  committedListeners.add(listener);
  return () => committedListeners.delete(listener);
}

/** How many rows a batch inserts, updates and deletes; never cell contents. */
export function countRowWrites(ops: DatabaseOp[]) {
  const rows = { insert: 0, update: 0, delete: 0 };
  for (const op of ops) {
    if (op.kind !== 'rows') continue;
    rows[op.change.kind] +=
      op.change.kind === 'update'
        ? op.change.changes.rows.length
        : op.change.rows.length;
  }
  return {
    insertedRows: rows.insert,
    updatedRows: rows.update,
    deletedRows: rows.delete,
  };
}

/**
 * Reports a batch's row writes as edits. A batch that creates a table or a
 * column fills it in the same request (a type conversion copies every cell
 * into its new column), so its row writes are a schema change, not edits.
 */
function trackRowEdits(databaseId: string, ops: DatabaseOp[]) {
  const createsSchema = ops.some(
    (op) =>
      (op.kind === 'table' || op.kind === 'column') &&
      op.change.kind === 'create'
  );
  if (createsSchema) return;
  const counts = countRowWrites(ops);
  if (counts.insertedRows + counts.updatedRows + counts.deletedRows === 0)
    return;
  analytics.track('database_rows_edited', { databaseId, ...counts });
}

/**
 * Apply ops to one database as the current viewer, together or not at all.
 * Each table `baseVersions` names must still be at that version, or the batch
 * is refused as a `CONFLICT`.
 */
export function applyDatabaseOps(
  databaseId: string,
  ops: DatabaseOp[],
  baseVersions?: Record<string, number>
): ResultAsync<OpResult[], DatabaseOpsError> {
  return traceDatabaseOps(databaseId, ops, () =>
    storageServiceClient.databases
      .applyOps({
        id: databaseId,
        request: baseVersions ? { ops, baseVersions } : { ops },
      })
      .map(async (response) => {
        const tableVersions = committedTableVersions(
          response.results,
          response.changes
        );
        for (const listener of committedListeners)
          listener({
            databaseId,
            ops,
            changes: response.changes,
            tableVersions,
          });
        applyDatabaseTableVersions(databaseId, tableVersions);
        trackRowEdits(databaseId, ops);
        // Schema-only creation waits for data callers need to focus or patch views.
        // A row edit that mints options keeps the write queue moving.
        const createsColumnData = ops.some(
          (op) =>
            op.kind === 'column' &&
            (op.change.kind === 'create' ||
              (op.change.kind === 'add_options' &&
                !ops.some((item) => item.kind === 'rows')))
        );
        if (
          ops.some(
            (op) =>
              op.kind === 'column' ||
              (op.kind === 'table' && op.change.kind !== 'reorder_views') ||
              op.kind === 'reorder_tables'
          )
        ) {
          const global = ops.some(
            (op) =>
              op.kind === 'table' &&
              ['rename', 'delete'].includes(op.change.kind)
          );
          const refresh = queryClient.invalidateQueries(
            {
              queryKey: global
                ? databasesKeys.detail._def
                : databasesKeys.detail(databaseId).queryKey,
              refetchType: createsColumnData ? 'all' : 'active',
            },
            // A failed read must not report an already committed write as failed.
            { throwOnError: false }
          );
          if (createsColumnData) await refresh;
          else fireAndForget(refresh, 'database-mutations');
        }
        return response.results;
      })
      // A refused batch is one error: the first op the service could not apply.
      .mapErr(([refusal]) => {
        fireAndForget(invalidateDatabase(databaseId), 'database-mutations');
        return refusal;
      })
  );
}

/** A table version a request of this viewer's reported apart from an ops batch, such as an undo. */
export type DatabaseTableAdvanced = {
  databaseId: string;
  tableId: string;
  version: number;
};

const advancedListeners = new Set<(change: DatabaseTableAdvanced) => void>();

/** Hear of every table version an undo or redo of this viewer's moved a table to; answers the unsubscribe. */
export function onDatabaseTableAdvanced(
  listener: (change: DatabaseTableAdvanced) => void
): () => void {
  advancedListeners.add(listener);
  return () => advancedListeners.delete(listener);
}

/**
 * Undo one of the viewer's own changes by its journal id. The outcome says what reverted; the
 * changes it made carry their versions, folded into the cached schema, which is re-read for any
 * schema the undo put back, and announced to the open reads of each table, which read it again.
 */
export function undoDatabaseChange(
  databaseId: string,
  change: number
): ResultAsync<UndoOutcome, ResultError[]> {
  return storageServiceClient.databases
    .undoChange({ id: databaseId, change })
    .map(({ outcome }) => {
      const changes = match(outcome)
        .with(
          { kind: P.union('reverted', 'partial') },
          ({ changes }) => changes
        )
        .otherwise(() => []);
      applyDatabaseTableVersions(
        databaseId,
        Object.fromEntries(
          changes.map(({ table, version }) => [table, version])
        )
      );
      if (changes.length > 0)
        fireAndForget(invalidateDatabase(databaseId), 'database-mutations');
      for (const { table, version } of changes)
        for (const listener of advancedListeners)
          listener({ databaseId, tableId: table, version });
      return outcome;
    });
}

/** Re-read one database's schema; open reads rerun only when their catalog changes. */
export function invalidateDatabase(databaseId: string) {
  return queryClient.invalidateQueries({
    queryKey: databasesKeys.detail(databaseId).queryKey,
  });
}

/**
 * Fold the versions a write reported into the cached schema, sparing a refetch per cell edit.
 * A delayed response never moves a version backwards.
 */
export function applyDatabaseTableVersions(
  databaseId: string,
  newVersions: Record<string, number>
) {
  if (Object.keys(newVersions).length === 0) return;

  queryClient.setQueryData(
    databasesKeys.detail(databaseId).queryKey,
    (previous: DatabaseDetail | undefined): DatabaseDetail | undefined => {
      if (!previous) return previous;
      return {
        ...previous,
        tables: previous.tables.map((table) => {
          const version = newVersions[table.table.id];
          if (version === undefined || version <= table.table.version) {
            return table;
          }
          return { ...table, table: { ...table.table, version } };
        }),
      };
    }
  );
}

/** Create a database; the service gives it a first table with a Name column. */
export function createDatabase(params: {
  name: string;
  /** UI surface the creation originated from, for analytics. */
  source?: string;
}): ResultAsync<string, ResultError<DatabaseSchemaErrorCode>[]> {
  return storageServiceClient.databases
    .create({ name: params.name })
    .map(({ id }) => {
      analytics.track('create_entity', {
        entityType: 'database',
        entityId: id,
        source: params.source,
      });
      // Creation is committed. Refresh discovery without blocking the new editor.
      queryClient.invalidateQueries({
        queryKey: databasesKeys.list.queryKey,
      });
      return id;
    });
}

/** Copy a database into a new one the caller owns, answering the copy's id. */
export function duplicateDatabase(params: {
  id: string;
  name: string;
}): ResultAsync<string, ResultError<DatabaseSchemaErrorCode>[]> {
  return storageServiceClient.databases
    .duplicate({ id: params.id, name: params.name })
    .map(({ id }) => {
      queryClient.invalidateQueries({
        queryKey: databasesKeys.list.queryKey,
      });
      return id;
    });
}

/** Native sharing adapters retain Result so the shared dialog can render failures. */
export function getDatabaseSharePermissions(id: string) {
  return storageServiceClient.databases.getPermissions({ id });
}

export function updateDatabaseSharePermissions(
  params: Parameters<typeof storageServiceClient.databases.updatePermissions>[0]
) {
  return storageServiceClient.databases.updatePermissions(params);
}