@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
94 lines • 3.66 kB
JavaScript
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
import { rstripSlash } from "../../utils/slash.js";
import { estimatedRowCount, fetchAllRelationships, fetchColumns, fetchForeignKeys, fetchIndexes, fetchPrimaryKey, listMatviews, listSchemas, listTables, listViews, tableSizeBytes, } from "./client.js";
export async function buildDatabaseJson(accessor) {
const schemas = await listSchemas(accessor, accessor.config.schemas);
const tables = [];
const views = [];
for (const s of schemas) {
for (const t of await listTables(accessor, s)) {
tables.push({
schema: s,
name: t,
row_count_estimate: await estimatedRowCount(accessor, s, t),
size_bytes_estimate: await tableSizeBytes(accessor, s, t),
});
}
for (const v of await listViews(accessor, s)) {
views.push({ schema: s, name: v, kind: 'view' });
}
for (const v of await listMatviews(accessor, s)) {
views.push({ schema: s, name: v, kind: 'materialized' });
}
}
const relationships = await fetchAllRelationships(accessor, schemas);
return {
database: databaseNameFromDsn(accessor.config.dsn),
schemas,
tables,
views,
relationships,
};
}
export async function buildEntitySchemaJson(accessor, schema, name, kind) {
const cols = await fetchColumns(accessor, schema, name);
const pk = await fetchPrimaryKey(accessor, schema, name);
const fks = await fetchForeignKeys(accessor, schema, name);
const idx = await fetchIndexes(accessor, schema, name);
const rows = await estimatedRowCount(accessor, schema, name);
const size = await tableSizeBytes(accessor, schema, name);
const pkSet = new Set(pk);
const fkMap = new Map();
for (const fk of fks) {
const ref = fk.references;
fk.columns.forEach((fromCol, i) => {
const column = ref.columns[i];
if (column === undefined)
return;
fkMap.set(fromCol, {
schema: ref.schema,
table: ref.table,
column,
});
});
}
const annotated = cols.map((col) => {
const out = { ...col };
if (pkSet.has(col.name))
out.primary_key = true;
const ref = fkMap.get(col.name);
if (ref !== undefined)
out.references = ref;
return out;
});
return {
schema,
name,
kind,
columns: annotated,
primary_key: pk,
foreign_keys: fks,
indexes: idx,
row_count_estimate: rows,
size_bytes_estimate: size,
};
}
export function databaseNameFromDsn(dsn) {
const stripped = rstripSlash(dsn);
const lastSegment = stripped.split('/').pop() ?? '';
const beforeQuery = lastSegment.split('?')[0] ?? '';
return beforeQuery !== '' ? beforeQuery : 'postgres';
}
//# sourceMappingURL=_schema_json.js.map