Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

2.1 KB
Raw Blame
import { createIndexedDBLocalDatabaseAdapter } from './indexedDBAdapter';
import type {
  LocalDatabaseAdapter,
  LocalDatabaseBatchOperation,
  LocalDatabaseKeyQuery,
} from './types';

export type {
  LocalDatabaseAdapter,
  LocalDatabaseBatchOperation,
  LocalDatabaseCollectionInfo,
  LocalDatabaseEntry,
  LocalDatabaseKeyGroup,
  LocalDatabaseKeyPage,
  LocalDatabaseKeyQuery,
} from './types';

let activeAdapter: LocalDatabaseAdapter = createIndexedDBLocalDatabaseAdapter();

/**
 * Replace the local database implementation before application initialization.
 * Electron can register a SQLite IPC adapter from its renderer entry point.
 */
export const registerLocalDatabaseAdapter = (adapter: LocalDatabaseAdapter): (() => void) => {
  const previousAdapter = activeAdapter;
  activeAdapter = adapter;

  return () => {
    if (activeAdapter === adapter) activeAdapter = previousAdapter;
  };
};

/** Runtime-neutral local database used by persistence repositories. */
export const localDatabase = {
  batch: (operations: LocalDatabaseBatchOperation[]): Promise<void> =>
    activeAdapter.batch(operations),
  delete: (collection: string, key: string): Promise<void> => activeAdapter.delete(collection, key),
  deleteByPrefix: (collection: string, prefix: string): Promise<void> =>
    activeAdapter.deleteByPrefix(collection, prefix),
  entriesByPrefix: <T>(collection: string, prefix: string) =>
    activeAdapter.entriesByPrefix<T>(collection, prefix),
  get: <T>(collection: string, key: string): Promise<T | undefined> =>
    activeAdapter.get<T>(collection, key),
  initialize: (): Promise<void> => activeAdapter.initialize(),
  keyGroupsByPrefix: (collection: string, prefix: string, delimiter: string) =>
    activeAdapter.keyGroupsByPrefix(collection, prefix, delimiter),
  keysByPrefix: (collection: string, prefix: string, query?: LocalDatabaseKeyQuery) =>
    activeAdapter.keysByPrefix(collection, prefix, query),
  listCollections: () => activeAdapter.listCollections(),
  set: (collection: string, key: string, value: unknown): Promise<void> =>
    activeAdapter.set(collection, key, value),
};