Skip to content

Commit

Permalink
Merge pull request #138 from FalkorDB/fix_connection
Browse files Browse the repository at this point in the history
Fix get connection
  • Loading branch information
gkorland authored Sep 10, 2024
2 parents 4a395b5 + 025d35c commit f07a02c
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "falkordb",
"version": "6.2.0",
"version": "6.2.1",
"description": "A FalkorDB javascript library",
"license": "MIT",
"main": "./dist/index.js",
Expand Down
12 changes: 12 additions & 0 deletions src/clients/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,31 @@ import { RedisCommandArgument } from "@redis/client/dist/lib/commands"
import { QueryOptions } from "../commands"
import { ConstraintType, EntityType, GraphReply } from "../graph"
import FalkorDB from "../falkordb"
import { SingleGraphConnection } from "./single"

// A generic client interface for Redis clients
export interface Client {

init(falkordb: FalkorDB): Promise<void>

list(): Promise<Array<string>>

configGet(configKey: string): Promise<(string | number)[] | (string | number)[][]>

configSet(configKey: string, value: number | string): Promise<void>

info(section?: string): Promise<(string | string[])[]>

query<T>(graph: string, query: RedisCommandArgument,options?: QueryOptions): Promise<any>

profile<T>(graph: string, query: RedisCommandArgument): Promise<any>

roQuery<T>(graph: string, query: RedisCommandArgument, options?: QueryOptions): Promise<any>

copy<T>(srcGraph: string, destGraph: string): Promise<any>

delete(graph: string): Promise<void>

explain(graph: string, query: string): Promise<any>

slowLog(graph: string) : Promise<{
Expand All @@ -33,4 +43,6 @@ export interface Client {
label: string, ...properties: string[]) : Promise<void>

quit(): Promise<void>

getConnection(): Promise<SingleGraphConnection>;
}
5 changes: 5 additions & 0 deletions src/clients/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export class Cluster implements Client {
this.#client = createCluster<{ falkordb: typeof commands }, RedisFunctions, RedisScripts>(redisClusterOption)
}

async getConnection() {
const connection = this.#client.nodeClient(this.#client.getRandomNode());
return connection instanceof Promise ? await connection : connection;
}

async init(falkordb: FalkorDB) {
await this.#client
.on('error', err => falkordb.emit('error', err)) // Forward errors
Expand Down
5 changes: 5 additions & 0 deletions src/clients/nullClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { QueryOptions } from "../commands";
import FalkorDB from "../falkordb";
import { ConstraintType, EntityType } from "../graph";
import { Client } from "./client";
import { SingleGraphConnection } from "./single";

/**
* The `NullClient` class is a placeholder implementation of the `Client` interface.
Expand All @@ -16,6 +17,10 @@ import { Client } from "./client";
*
*/
export class NullClient implements Client {

getConnection(): Promise<SingleGraphConnection> {
throw new Error("Method not implemented.");
}
init(falkordb: FalkorDB): Promise<void> {
throw new Error("Method not implemented.");
}
Expand Down
4 changes: 4 additions & 0 deletions src/clients/single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,8 @@ export class Single implements Client {
const reply = this.client.quit();
return reply.then(() => {})
}

async getConnection(){
return this.client;
}
}
2 changes: 1 addition & 1 deletion src/falkordb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export default class FalkorDB extends EventEmitter {
}

public get connection() {
return this.#client;
return this.#client.getConnection();
}

async list() {
Expand Down

0 comments on commit f07a02c

Please sign in to comment.