UNPKG

falkordb

Version:
89 lines (88 loc) 3.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const events_1 = require("events"); const redis_1 = require("redis"); const graph_1 = require("./graph"); const commands_1 = require("./commands"); const single_1 = require("./clients/single"); const sentinel_1 = require("./clients/sentinel"); const cluster_1 = require("./clients/cluster"); const nullClient_1 = require("./clients/nullClient"); async function clientFactory(client) { const info = await client.info("server"); if (info.includes("redis_mode:sentinel")) { return new sentinel_1.Sentinel(client); } else if (info.includes("redis_mode:cluster")) { return new cluster_1.Cluster(client); } return new single_1.Single(client); } class FalkorDB extends events_1.EventEmitter { #client = new nullClient_1.NullClient(); static async connect(options) { const redisOption = (options ?? {}); // If the URL is provided, and the protocol is `falkor` replaces it with `redis` for the underline redis client // e.g. falkor://localhost:6379 -> redis://localhost:6379 if (redisOption.url && redisOption.url.startsWith('falkor')) { redisOption.url = redisOption.url.replace('falkor', 'redis'); } // Just copy the pool options to the isolation pool options as expected by the redis client if (options?.poolOptions) { redisOption.isolationPoolOptions = options.poolOptions; } redisOption.modules = { falkordb: commands_1.default }; // Create an empty FalkorDB instance for the redisClient on error event to work const falkordb = new FalkorDB(); // Create initial redis single client const redisClient = (0, redis_1.createClient)(redisOption); await redisClient .on('error', err => falkordb.emit('error', err)) // Forward errors .connect(); // Create FalkorDB client wrapper const client = await clientFactory(redisClient); await client.init(falkordb); // Set the client to the FalkorDB instance after it was initialized falkordb.#client = client; return falkordb; } selectGraph(graphId) { return new graph_1.default(this.#client, graphId); } get connection() { return this.#client.getConnection(); } async list() { return this.#client.list(); } async configGet(configKey) { return this.#client.configGet(configKey); } async configSet(configKey, value) { return this.#client.configSet(configKey, value); } async info(section) { return this.#client.info(section); } async udfLoad(name, script, replace = false) { return this.#client.udfLoad(name, script, replace); } async udfList(lib, withCode = false) { return this.#client.udfList(lib, withCode); } async udfFlush() { return this.#client.udfFlush(); } async udfDelete(lib) { return this.#client.udfDelete(lib); } /** * Closes the client. */ async close() { return this.#client.disconnect(); } } exports.default = FalkorDB;