Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database and Session params #76

Merged
merged 2 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions documentation/md/docs/Getting-Started.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const neogma = new Neogma(
url: 'bolt://localhost:7687',
username: 'neo4j',
password: 'password',
/* --> (optional) the database to be used by default for sessions */
database: 'myDb',
},
{
/* --> (optional) logs every query that Neogma runs, using the given function */
Expand Down
20 changes: 16 additions & 4 deletions documentation/md/docs/Sessions-and-Transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ await getSession(
await myFindUser(session);
},
/* --> a neo4j driver is needed */
driver
driver,
{
/* --> pass any other params, as you would for the driver's `session` method */
database: "myDb"
}
);
/* --> at this stage, the session will close */
```
Expand Down Expand Up @@ -146,7 +150,11 @@ await getTransaction(
);
},
/* --> a neo4j driver is needed */
driver
driver,
{
/* --> pass any other params, as you would for the driver's `session` method */
database: "myDb"
}
);
/* --> at this stage, the transaction will be committed */
```
Expand All @@ -165,7 +173,11 @@ await getRunnable(
await queryRunner.run('RETURN 1 = 1', {}, session);
},
/* --> a neo4j driver is needed */
driver
driver,
{
/* --> pass any other params, as you would for the driver's `session` method */
database: "myDb"
}
);
```

Expand All @@ -187,7 +199,7 @@ await getRunnable(
driver
);
},
driver
driver,
);
```

Expand Down
34 changes: 31 additions & 3 deletions src/Neogma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface ConnectParamsI {
url: string;
username: string;
password: string;
database?: string;
}

interface ConnectOptionsI extends Config {
Expand All @@ -22,6 +23,7 @@ export class Neogma {
public readonly queryRunner: QueryRunner;
/** a map between each Model's modelName and the Model itself */
public modelsByName: Record<string, NeogmaModel<any, any, any, any>> = {};
public database?: string;

/**
*
Expand All @@ -37,6 +39,8 @@ export class Neogma {
neo4j.auth.basic(username, password),
options,
);

this.database = params.database;
} catch (err) {
throw new NeogmaConnectivityError(err);
}
Expand All @@ -58,21 +62,45 @@ export class Neogma {
public getSession = <T>(
runInSession: Session | null,
callback: (s: Session) => Promise<T>,
/**
* Override the configuration of the session.
* By default, the "database" param used in the constructor is always passed.
*/
sessionConfig?: neo4j_driver.SessionConfig,
): Promise<T> => {
return getSession<T>(runInSession, callback, this.driver);
return getSession<T>(runInSession, callback, this.driver, {
database: this.database,
...sessionConfig,
});
};

public getTransaction = <T>(
runInTransaction: Runnable | null,
callback: (tx: Transaction) => Promise<T>,
/**
* Override the configuration of the session.
* By default, the "database" param used in the constructor is always passed.
*/
sessionConfig?: neo4j_driver.SessionConfig,
): Promise<T> => {
return getTransaction<T>(runInTransaction, callback, this.driver);
return getTransaction<T>(runInTransaction, callback, this.driver, {
database: this.database,
...sessionConfig,
});
};

public getRunnable = <T>(
runInExisting: Runnable | null,
callback: (tx: Runnable) => Promise<T>,
/**
* Override the configuration of the session.
* By default, the "database" param used in the constructor is always passed.
*/
sessionConfig?: neo4j_driver.SessionConfig,
): Promise<T> => {
return getRunnable<T>(runInExisting, callback, this.driver);
return getRunnable<T>(runInExisting, callback, this.driver, {
database: this.database,
...sessionConfig,
});
};
}
10 changes: 7 additions & 3 deletions src/Sessions/Sessions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Driver, Session, Transaction } from 'neo4j-driver';
import { Driver, Session, SessionConfig, Transaction } from 'neo4j-driver';
import { Runnable } from '../Queries/QueryRunner';

const isTransaction = (tx: any): tx is Transaction =>
Expand All @@ -10,12 +10,13 @@ export const getSession = async <T>(
runInSession: Session | null,
callback: (s: Session) => Promise<T>,
driver: Driver,
sessionParams?: SessionConfig,
): Promise<T> => {
if (runInSession) {
return callback(runInSession);
}

const session = driver.session();
const session = driver.session(sessionParams);
try {
const result = await callback(session);
await session.close();
Expand All @@ -32,6 +33,7 @@ export const getTransaction = async <T>(
runInExisting: Runnable | null,
callback: (tx: Transaction) => Promise<T>,
driver: Driver,
sessionParams?: SessionConfig,
): Promise<T> => {
// if it's a transaction, return it with the callback
if (isTransaction(runInExisting)) {
Expand All @@ -53,6 +55,7 @@ export const getTransaction = async <T>(
}
},
driver,
sessionParams,
);
};

Expand All @@ -61,9 +64,10 @@ export const getRunnable = async <T>(
runInExisting: Runnable | null | undefined,
callback: (tx: Runnable) => Promise<T>,
driver: Driver,
sessionParams?: SessionConfig,
): Promise<T> => {
if (runInExisting) {
return callback(runInExisting);
}
return getSession(null, callback, driver);
return getSession(null, callback, driver, sessionParams);
};