staticql
Version:
Type-safe query engine for static content including Markdown, YAML, JSON, and more.
54 lines (53 loc) • 2.1 kB
JavaScript
import { SourceLoader } from "./SourceLoader.js";
import { Indexer } from "./Indexer.js";
import { QueryBuilder } from "./QueryBuilder.js";
import { defaultValidator } from "./validator/defaultValidator.js";
import { ConsoleLogger } from "./logger/ConsoleLogger.js";
/**
* The core class for querying structured static content.
*/
export class StaticQL {
constructor(config, repository, sourceConfigResolver, options = {}) {
this.config = config;
this.repository = repository;
this.sourceConfigResolver = sourceConfigResolver;
this.options = options;
this.validator = this.options.validator ?? defaultValidator;
this.logger = this.options.logger ?? new ConsoleLogger("info");
}
/**
* Creates a type-safe QueryBuilder for the specified source.
*
* @param sourceName - Name of the source.
* @returns A new QueryBuilder instance.
*/
from(sourceName) {
const sourceLoader = new SourceLoader(this.repository, this.sourceConfigResolver, this.validator);
const indexer = new Indexer(sourceLoader, this.repository, this.sourceConfigResolver, this.logger);
return new QueryBuilder(sourceName, sourceLoader, indexer, this.sourceConfigResolver, this.logger);
}
/**
* Saves index files for all sources
* to the configured output directory.
*
* @param customIndexers - Optional custom indexer callbacks for _custom indexes.
* @throws If writing to the storage fails.
*/
async saveIndexes(customIndexers) {
await this.getIndexer(customIndexers).save();
}
/**
* Returns the configuration object used by StaticQL.
*/
getConfig() {
return this.config;
}
/**
* Returns an Indexer instance.
* Useful for incremental index updates.
*/
getIndexer(customIndexers) {
const sourceLoader = new SourceLoader(this.repository, this.sourceConfigResolver, this.validator);
return new Indexer(sourceLoader, this.repository, this.sourceConfigResolver, this.logger, customIndexers);
}
}