Skip to content

Commit

Permalink
Add sessionParams to QueryBuilder and QueryRunner (#78)
Browse files Browse the repository at this point in the history
- Add `sessionParams` property to `QueryBuilder`.
- `QueryRunner` uses it when running queries or when calling
`queryBuilder.run()`.
- `QueryBuilder` uses it when calling `getRunnable` in its `run` method.
- Pass the `sessionParams` of the Neogma instance to `QueryRunner`.
  • Loading branch information
themetalfleece authored Sep 5, 2023
1 parent f7f483e commit c306f75
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion documentation/md/docs/QueryBuilder/Overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions documentation/md/docs/QueryRunner/Overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
},
});
```

Expand Down
3 changes: 3 additions & 0 deletions src/Neogma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export class Neogma {
this.queryRunner = new QueryRunner({
driver: this.driver,
logger: options?.logger,
sessionParams: {
database: this.database,
},
});
}

Expand Down
1 change: 1 addition & 0 deletions src/Queries/QueryBuilder/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,7 @@ export class QueryBuilder {
);
},
queryRunner.getDriver(),
queryRunner.sessionParams,
);
}

Expand Down
8 changes: 7 additions & 1 deletion src/Queries/QueryRunner/QueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -272,7 +277,7 @@ export class QueryRunner {
statement: string,
/** parameters for the query */
parameters?: Record<string, any>,
/** 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<QueryResult> {
return getRunnable(
Expand All @@ -299,6 +304,7 @@ export class QueryRunner {
return session.run(trimmedStatement, parameters);
},
this.driver,
this.sessionParams,
);
}

Expand Down

0 comments on commit c306f75

Please sign in to comment.