forked from Nhogs/nestjs-neo4j
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneo4j.service.ts
119 lines (108 loc) · 2.98 KB
/
neo4j.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import neo4j, {
Driver,
QueryResult,
Result,
RxResult,
RxSession,
RxTransaction,
ServerInfo,
Session,
Transaction,
} from 'neo4j-driver';
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
import { NEO4J_CONFIG, NEO4J_DRIVER } from '../constant';
import { Neo4jConfig, Query, SessionOptions } from '../interface';
import { Neo4jMetadataStorage } from '../storage';
import { TransactionConfig } from 'neo4j-driver-core/types/session';
@Injectable()
/**
* See https://neo4j.com/docs/api/javascript-driver/current/ for details
*/
export class Neo4jService implements OnApplicationShutdown {
constructor(
@Inject(NEO4J_CONFIG) private readonly config: Neo4jConfig,
@Inject(NEO4J_DRIVER) private readonly driver: Driver,
) {}
private _convertSessionOptions(options: SessionOptions) {
return {
database: options?.database || this.config.database,
defaultAccessMode: options?.write
? neo4j.session.WRITE
: neo4j.session.READ,
};
}
getConfig() {
return this.config;
}
/**
* Verifies connectivity of this driver by trying to open a connection with the provided driver options.
*/
verifyConnectivity(options?: { database?: string }): Promise<ServerInfo> {
return this.driver.verifyConnectivity(options);
}
/**
* Regular Session.
* Create a session to run Cypher statements in.
*
* Note: Always make sure to close sessions when you are done using them!
*/
getSession(options?: SessionOptions): Session {
return this.driver.session(this._convertSessionOptions(options));
}
/**
* Reactive session.
* Create a reactive session to run Cypher statements in.
*
* Note: Always make sure to close sessions when you are done using them!
*/
getRxSession(options?: SessionOptions): RxSession {
return this.driver.rxSession(this._convertSessionOptions(options));
}
/**
* Run Cypher query in regular session and close the session after getting results.
*/
run(
query: Query,
sessionOptions?: SessionOptions,
transactionConfig?: TransactionConfig,
): Promise<QueryResult> {
const session = this.getSession(sessionOptions);
return session
.run(query.cypher, query.parameters, transactionConfig)
.finally(async () => {
await session.close();
});
}
/**
* Run Cypher query in reactive session.
*/
rxRun(
query: Query,
sessionOptions?: SessionOptions,
transactionConfig?: TransactionConfig,
): RxResult {
return this.getRxSession(sessionOptions).run(
query.cypher,
query.parameters,
transactionConfig,
);
}
/**
* Returns constraints as runnable Cypher queries defined with decorators on models.
*/
getCypherConstraints(label?: string): string[] {
return Neo4jMetadataStorage.getCypherConstraints(label);
}
onApplicationShutdown() {
return this.driver.close();
}
}
export {
Result,
RxResult,
RxSession,
RxTransaction,
ServerInfo,
Session,
Transaction,
};