diff --git a/documentation/md/docs/QueryBuilder/Overview.md b/documentation/md/docs/QueryBuilder/Overview.md index f6fdcdd..06e3495 100644 --- a/documentation/md/docs/QueryBuilder/Overview.md +++ b/documentation/md/docs/QueryBuilder/Overview.md @@ -115,7 +115,7 @@ An existing session can be given await new QueryBuilder() .raw('match n return n') .run(queryRunner, session); -``` +``` In order to avoid having to provide the QueryRunner instance on every call, the static `queryRunner` can be set. This can be done as soon as the `Neogma` instance is created, and should be set only once. diff --git a/documentation/md/docs/QueryRunner/Overview.md b/documentation/md/docs/QueryRunner/Overview.md index 3810bd4..c485b7f 100644 --- a/documentation/md/docs/QueryRunner/Overview.md +++ b/documentation/md/docs/QueryRunner/Overview.md @@ -12,6 +12,10 @@ const queryRunner = new QueryRunner({ driver: neogma.driver, /* --> (optional) logs every query that this QueryRunner instance runs, using the given function */ logger: console.log + /* --> (optional) Session config to be used when creating a session to run any query. If a custom session is passed to any method, this param will be ignored. */ + sessionParams: { + database: 'myDb' + }, }); ``` diff --git a/src/Neogma.ts b/src/Neogma.ts index 41dcb61..7b3d526 100644 --- a/src/Neogma.ts +++ b/src/Neogma.ts @@ -48,6 +48,9 @@ export class Neogma { this.queryRunner = new QueryRunner({ driver: this.driver, logger: options?.logger, + sessionParams: { + database: this.database, + }, }); } diff --git a/src/Queries/QueryBuilder/QueryBuilder.ts b/src/Queries/QueryBuilder/QueryBuilder.ts index cb1850d..7543a88 100644 --- a/src/Queries/QueryBuilder/QueryBuilder.ts +++ b/src/Queries/QueryBuilder/QueryBuilder.ts @@ -823,6 +823,7 @@ export class QueryBuilder { ); }, queryRunner.getDriver(), + queryRunner.sessionParams, ); } diff --git a/src/Queries/QueryRunner/QueryRunner.ts b/src/Queries/QueryRunner/QueryRunner.ts index 981f5e4..6902b57 100644 --- a/src/Queries/QueryRunner/QueryRunner.ts +++ b/src/Queries/QueryRunner/QueryRunner.ts @@ -11,6 +11,7 @@ import { LocalDateTime as Neo4jLocalDateTime, LocalTime as Neo4jLocalTime, Duration as Neo4jDuration, + SessionConfig, } from 'neo4j-driver'; import * as uuid from 'uuid'; import { getRunnable } from '../../Sessions'; @@ -68,6 +69,7 @@ export interface CreateRelationshipParamsI { export class QueryRunner { private driver: Driver; + public sessionParams?: SessionConfig; /** whether to log the statements and parameters with the given function */ private logger: | null @@ -76,9 +78,12 @@ export class QueryRunner { constructor(params: { driver: QueryRunner['driver']; logger?: QueryRunner['logger']; + /** these params will be used when creating a new session to run any query */ + sessionParams?: SessionConfig; }) { this.driver = params.driver; this.logger = params?.logger || null; + this.sessionParams = params.sessionParams; } public getDriver(): Driver { @@ -272,7 +277,7 @@ export class QueryRunner { statement: string, /** parameters for the query */ parameters?: Record, - /** the session or transaction for running this query */ + /** the session or transaction for running this query. Passing it will ignore the `sessionParams` passed to the constructor */ existingSession?: Runnable | null, ): Promise { return getRunnable( @@ -299,6 +304,7 @@ export class QueryRunner { return session.run(trimmedStatement, parameters); }, this.driver, + this.sessionParams, ); }