UNPKG

@clickup/ent-framework

Version:

A PostgreSQL graph-database-alike library with microsharding and row-level security

55 lines 2.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PgQueryExists = void 0; const QueryBase_1 = require("../abstract/QueryBase"); const PgRunner_1 = require("./PgRunner"); class PgQueryExists extends QueryBase_1.QueryBase { constructor() { super(...arguments); /** @ignore */ this.RUNNER_CLASS = PgRunnerExists; } } exports.PgQueryExists = PgQueryExists; class PgRunnerExists extends PgRunner_1.PgRunner { constructor(schema, client) { super(schema, client); this.op = "EXISTS"; this.maxBatchSize = 100; this.default = false; // We just need something here. this.builder = this.createWhereBuilder({ prefix: this.fmt("SELECT EXISTS (SELECT true FROM %T "), suffix: this.fmt(")"), }); } key(input) { // Coalesce equal queries. return JSON.stringify(input); } async runSingle(input, annotations) { const sql = this.builder.prefix + this.builder.func(input) + this.builder.suffix; const res = await this.clientQuery(sql, annotations, 1); return !!res[0].exists; } async runBatch(inputs, annotations) { // SELECT EXISTS(SELECT 1 FROM ... WHERE ...) // UNION ALL // SELECT EXISTS(SELECT 1 FROM ... WHERE ...) const sql = [...inputs.values()] .map((input) => this.builder.prefix + this.builder.func(input) + this.builder.suffix) .join("\n UNION ALL\n"); const rows = await this.clientQuery(sql, annotations, inputs.size, // The reasonable assumption is that, if someone uses EXISTS, they always // want the query to match some index. { enable_seqscan: "off" }); const outputs = new Map(); let i = 0; for (const key of inputs.keys()) { outputs.set(key, !!rows[i].exists); i++; } return outputs; } } PgRunnerExists.IS_WRITE = false; //# sourceMappingURL=PgQueryExists.js.map