MCPcopy
hub / github.com/tinyplex/tinybase / createCustomSqlitePersister

Function createCustomSqlitePersister

src/persisters/common/database/sqlite.ts:36–144  ·  view source on GitHub ↗
(
  store: PersistedStore<Persist>,
  configOrStoreTableName: DatabasePersisterConfig | string | undefined,
  rawExecuteCommand: DatabaseExecuteCommand,
  addChangeListener: (listener: DatabaseChangeListener) => ListenerHandle,
  delChangeListener: (listenerHandle: ListenerHandle) => void,
  onSqlCommand: ((sql: string, params?: any[]) => void) | undefined,
  onIgnoredError: ((error: any) => void) | undefined,
  destroy: () => void,
  persist: Persist,
  thing: any,
  getThing = 'getDb',
  upsert?: Upsert,
)

Source from the content-addressed store, hash-verified

34import {createTabularPersister} from './tabular.ts';
35
36export const createCustomSqlitePersister = <
37 ListenerHandle,
38 Persist extends Persists = Persists.StoreOnly,
39>(
40 store: PersistedStore<Persist>,
41 configOrStoreTableName: DatabasePersisterConfig | string | undefined,
42 rawExecuteCommand: DatabaseExecuteCommand,
43 addChangeListener: (listener: DatabaseChangeListener) => ListenerHandle,
44 delChangeListener: (listenerHandle: ListenerHandle) => void,
45 onSqlCommand: ((sql: string, params?: any[]) => void) | undefined,
46 onIgnoredError: ((error: any) => void) | undefined,
47 destroy: () => void,
48 persist: Persist,
49 thing: any,
50 getThing = 'getDb',
51 upsert?: Upsert,
52): Persister<Persist> => {
53 let dataVersion: number | null;
54 let schemaVersion: number | null;
55 let totalChanges: number | null;
56
57 const executeCommand = getWrappedCommand(rawExecuteCommand, onSqlCommand);
58
59 const [
60 isJson,
61 autoLoadIntervalSeconds,
62 defaultedConfig,
63 managedTableNamesSet,
64 ] = getConfigStructures(configOrStoreTableName);
65
66 const addPersisterListener = (
67 listener: PersisterListener<Persist>,
68 ): (() => void) => {
69 let interval: number | NodeJS.Timeout;
70
71 const startPolling = () =>
72 (interval = startInterval(
73 () =>
74 tryCatch(async () => {
75 const [{d, s, c}] = (await executeCommand(
76 SELECT +
77 // eslint-disable-next-line max-len
78 ` ${DATA_VERSION} d,${SCHEMA_VERSION} s,TOTAL_CHANGES() c FROM ${PRAGMA}${DATA_VERSION} JOIN ${PRAGMA}${SCHEMA_VERSION}`,
79 )) as [IdObj<number>];
80 if (d != dataVersion || s != schemaVersion || c != totalChanges) {
81 if (!isNullish(dataVersion)) {
82 listener();
83 }
84 dataVersion = d;
85 schemaVersion = s;
86 totalChanges = c;
87 }
88 }),
89 autoLoadIntervalSeconds as number,
90 ));
91
92 const stopPolling = () => {
93 dataVersion = schemaVersion = totalChanges = null;

Callers 10

index.tsFile · 0.90
index.tsFile · 0.90
index.tsFile · 0.90
index.tsFile · 0.90
index.tsFile · 0.90
index.tsFile · 0.90
index.tsFile · 0.90
index.tsFile · 0.90
index.tsFile · 0.90

Calls 6

getWrappedCommandFunction · 0.90
getConfigStructuresFunction · 0.90
collValuesFunction · 0.90
getPlaceholdersFunction · 0.90
isTrueFunction · 0.90
isFalseFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…