-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathneo4j.service.ts
82 lines (63 loc) · 2.49 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
import { Injectable, Inject, OnApplicationShutdown } from '@nestjs/common';
import neo4j, { Driver, int, Transaction, QueryResult } from 'neo4j-driver'
import { Neo4jConnection } from './interfaces/neo4j-connection.interface';
import { NEO4J_OPTIONS, NEO4J_DRIVER } from './neo4j.constants';
import TransactionImpl from 'neo4j-driver-core/lib/transaction'
@Injectable()
export class Neo4jService implements OnApplicationShutdown {
private readonly driver: Driver;
private readonly connection: Neo4jConnection;
constructor(
@Inject(NEO4J_OPTIONS) config: Neo4jConnection,
@Inject(NEO4J_DRIVER) driver: Driver
) {
this.driver = driver
this.connection = config
}
getDriver(): Driver {
return this.driver;
}
getConfig(): Neo4jConnection {
return this.connection;
}
int(value: number) {
return int(value)
}
beginTransaction(database?: string): Transaction {
const session = this.getWriteSession(database)
return session.beginTransaction()
}
getReadSession(database?: string) {
return this.driver.session({
database: database || this.connection.database,
defaultAccessMode: neo4j.session.READ,
})
}
getWriteSession(database?: string) {
return this.driver.session({
database: database || this.connection.database,
defaultAccessMode: neo4j.session.WRITE,
})
}
async read(cypher: string, params?: Record<string, any>, databaseOrTransaction?: string | Transaction): Promise<QueryResult> {
if ( databaseOrTransaction instanceof TransactionImpl ) {
return (<Transaction> databaseOrTransaction).run(cypher, params)
}
const session = this.getReadSession(<string> databaseOrTransaction)
const res = await session.executeRead(tx => tx.run(cypher, params))
await session.close()
return res
}
async write(cypher: string, params?: Record<string, any>, databaseOrTransaction?: string | Transaction): Promise<QueryResult> {
if ( databaseOrTransaction instanceof TransactionImpl ) {
return (<Transaction> databaseOrTransaction).run(cypher, params)
}
const session = this.getWriteSession(<string> databaseOrTransaction)
const res = await session.executeWrite(tx => tx.run(cypher, params))
await session.close()
return res
}
onApplicationShutdown() {
return this.driver.close()
}
}