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

Function getDatabaseFunctions

test/unit/persisters/common/databases.ts:486–581  ·  view source on GitHub ↗
(
  cmd: (
    db: Database,
    sql: string,
    args?: any[],
  ) => Promise<{[id: string]: any}[]>,
  isPostgres = false,
  jsonValues = false,
)

Source from the content-addressed store, hash-verified

484};
485
486export const getDatabaseFunctions = <Database>(
487 cmd: (
488 db: Database,
489 sql: string,
490 args?: any[],
491 ) => Promise<{[id: string]: any}[]>,
492 isPostgres = false,
493 jsonValues = false,
494): [
495 (db: Database) => Promise<DumpOut>,
496 (db: Database, dump: DumpIn) => Promise<void>,
497] => {
498 const getDatabase = async (db: Database): Promise<DumpOut> => {
499 const dump: DumpOut = {};
500 (
501 await cmd(
502 db,
503 isPostgres
504 ? 'SELECT table_name tn, column_name cn, data_type ty ' +
505 'FROM information_schema.columns ' +
506 `WHERE table_schema='public' ` +
507 'AND table_name NOT LIKE $1 ' +
508 'AND table_name NOT LIKE $2'
509 : 'SELECT t.name tn, c.name cn, LOWER(c.type) ty ' +
510 'FROM pragma_table_list() t, ' +
511 'pragma_table_info(t.name) c ' +
512 `WHERE t.schema='main' AND t.type = 'table' ` +
513 'AND t.name NOT LIKE $1 ' +
514 'AND t.name NOT LIKE $2',
515 ['%sql%', '%electric%'],
516 )
517 ).forEach(({tn, cn, ty}) => {
518 if (!dump[tn]) {
519 dump[tn] = [{}, [{}]];
520 }
521 dump[tn][0][cn] = ty;
522 });
523 await Promise.all(
524 Object.keys(dump).map(async (tn) => {
525 const rows = await cmd(
526 db,
527 'SELECT * FROM ' + escapeId(tn) + ' ORDER BY 1',
528 );
529 rows.forEach((row) => {
530 Object.entries(row).forEach(([column, value], index) => {
531 if (index == 0 || !jsonValues) {
532 row[column] = value;
533 } else {
534 row[column] = JSON.parse(value);
535 }
536 });
537 });
538 dump[tn][1] = [...rows];
539 }),
540 );
541 return dump;
542 };
543

Callers 3

tabular.test.tsFile · 0.90
json.test.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…