UNPKG

@struktoai/mirage-core

Version:

A Unified Virtual Filesystem For AI Agents

89 lines • 3.83 kB
// ========= 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 { EntityKind, SYSTEM_DATABASES } from "./types.js"; import { compareCodePoints } from "../../utils/sort.js"; export async function listDatabases(accessor) { return accessor.listings.get('listDatabases', async () => { const all = await accessor.driver.listDatabases(); let dbs = all.filter((d) => !SYSTEM_DATABASES.has(d)); const allow = accessor.config.databases; if (allow !== null && allow.length > 0) { const allowSet = new Set(allow); dbs = dbs.filter((d) => allowSet.has(d)); } return [...dbs].sort(compareCodePoints); }); } export async function listCollections(accessor, database, kind = null) { const key = `listCollections:${database}:${kind ?? ''}`; return accessor.listings.get(key, async () => { const cols = await accessor.driver.listCollections(database, kind); return [...cols].sort(compareCodePoints); }); } export async function databaseExists(accessor, database) { const dbs = await listDatabases(accessor); return dbs.includes(database); } export async function entityExists(accessor, database, name, kind = null) { if (!(await databaseExists(accessor, database))) return false; const names = await listCollections(accessor, database, kind); return names.includes(name); } export async function findDocuments(accessor, database, collection, filter = {}, options = {}) { const cap = accessor.config.maxDocLimit; const requested = options.limit ?? cap; const limit = Math.min(requested, cap); return accessor.driver.findDocuments(database, collection, filter, { ...options, limit, }); } export function iterDocuments(accessor, database, collection, options = {}) { return accessor.driver.iterDocuments(database, collection, options); } export function findSnapshot(accessor, database, collection, options) { return accessor.driver.findSnapshot(database, collection, options); } export function iterInserts(accessor, database, collection, after = null) { return accessor.driver.iterInserts(database, collection, after); } export async function countDocuments(accessor, database, collection, filter = {}) { return accessor.driver.countDocuments(database, collection, filter); } export async function isView(accessor, database, collection) { const specs = await accessor.driver.listCollectionsDetailed(database, { name: collection, }); for (const spec of specs) return spec.type === EntityKind.VIEW; return false; } export async function getIndexes(accessor, database, collection) { if (await isView(accessor, database, collection)) return []; return accessor.driver.listIndexes(database, collection); } export async function getValidator(accessor, database, collection) { const specs = await accessor.driver.listCollectionsDetailed(database, { name: collection, }); for (const spec of specs) { const validator = spec.options?.validator; return validator?.$jsonSchema ?? null; } return null; } //# sourceMappingURL=client.js.map