UNPKG

falkordb

Version:
113 lines (112 loc) 4.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Cluster = void 0; const client_1 = require("@redis/client"); const lodash = require("lodash"); /** * A client that connects to a Redis Cluster. */ class Cluster { #client; constructor(client) { // Convert the single client options to a cluster client options const redisClusterOption = client.options; redisClusterOption.rootNodes = [ client.options, ]; // Remove the URL from the defaults so it won't override the dynamic cluster URLs const defaults = lodash.cloneDeep(client.options); defaults?.url && delete defaults.url; redisClusterOption.defaults = defaults; redisClusterOption.maxCommandRedirections = 100000; client.disconnect(); this.#client = (0, client_1.createCluster)(redisClusterOption); } async getConnection() { const connection = this.#client.nodeClient(this.#client.getRandomNode()); return connection instanceof Promise ? await connection : connection; } async init(falkordb) { await this.#client .on("error", (err) => falkordb.emit("error", err)) // Forward errors .connect(); } // eslint-disable-next-line @typescript-eslint/no-unused-vars async query(graph, query, options, compact = true) { return this.#client.falkordb.query(graph, query, options, compact); } // eslint-disable-next-line @typescript-eslint/no-unused-vars async roQuery(graph, query, options, compact = true) { return this.#client.falkordb.roQuery(graph, query, options, compact); } async delete(graph) { const reply = this.#client.falkordb.delete(graph); return reply.then(() => { }); } async explain(graph, query) { return this.#client.falkordb.explain(graph, query); } async list() { const reply = await Promise.all(this.#client.masters.map(async (master) => { return (await this.#client.nodeClient(master)).falkordb.list(); })); const [result, errors] = [ reply.filter((r) => !(r instanceof Error)).flat(), reply.filter((r) => r instanceof Error), ]; if (errors.length > 0) { console.error("Some nodes failed to respond:", errors); } return result; } async configGet(configKey) { return this.#client.falkordb.configGet(configKey); } async configSet(configKey, value) { const reply = this.#client.falkordb.configSet(configKey, value); return reply.then(() => { }); } async info(section) { return this.#client.falkordb.info(section); } async copy(srcGraph, destGraph) { return this.#client.falkordb.copy(srcGraph, destGraph); } slowLog(graph) { return this.#client.falkordb.slowLog(graph); } async memoryUsage(graph, options) { return this.#client.falkordb.memoryUsage(graph, options); } async constraintCreate(graph, constraintType, entityType, label, ...properties) { const reply = this.#client.falkordb.constraintCreate(graph, constraintType, entityType, label, ...properties); return reply.then(() => { }); } async constraintDrop(graph, constraintType, entityType, label, ...properties) { const reply = this.#client.falkordb.constraintDrop(graph, constraintType, entityType, label, ...properties); return reply.then(() => { }); } async udfLoad(name, script, replace = false) { return this.#client.falkordb.udfLoad(name, script, replace); } async udfList(lib, withCode = false) { return this.#client.falkordb.udfList(lib, withCode); } async udfFlush() { return this.#client.falkordb.udfFlush(); } async udfDelete(lib) { return this.#client.falkordb.udfDelete(lib); } async profile(graph, query) { return this.#client.falkordb.profile(graph, query); } async quit() { return this.disconnect(); } async disconnect() { const reply = this.#client.disconnect(); return reply.then(() => { }); } } exports.Cluster = Cluster;