UNPKG

@jakub.knejzlik/ts-query-server

Version:

TypeScript library for executing SQL queries built with ts-query across diverse server environments.

47 lines (46 loc) 1.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.QueryRouter = void 0; class QueryRouter { constructor(defaultClient) { this.defaultClient = defaultClient; this.clientsMap = {}; } addClient(client, sourceName) { if (this.clientsMap[sourceName]) { throw new Error(`Client with source name ${sourceName} already exists`); } this.clientsMap[sourceName] = client; } async executeQueries(queries, datasource) { if (!datasource) { const sources = this.getSources(queries); if (sources.length > 1) { throw new Error(`Queries from multiple sources are not supported: ${sources.join(", ")}`); } if (sources.length === 1) { datasource = sources[0]; } } const client = datasource ? this.clientsMap[datasource] : this.defaultClient; if (!client) { throw new Error(`Client with source name ${datasource} not found`); } return client.executeQueries(queries); } getSources(queries) { const sources = new Set(); for (const query of queries) { for (const table of query.getTableNames()) { const parts = table.split("@"); if (parts.length > 1) { sources.add(parts[0]); } } } return Array.from(sources); } } exports.QueryRouter = QueryRouter;