-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneo4j-service.ts
54 lines (53 loc) · 1.63 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
import { DOCUMENT } from '@angular/common';
import { Inject, Injectable, OnDestroy } from '@angular/core';
import * as neo4j from 'neo4j-driver';
import { Observable, of } from 'rxjs';
import { catchError, filter, finalize, map } from 'rxjs/operators';
import { environment } from 'src/environments/environment';
import { MessageService } from './message.service';
@Injectable({
providedIn: 'root',
})
export default class Neo4jService implements OnDestroy {
driver: neo4j.Driver;
constructor(
@Inject(DOCUMENT) private document: { location: any },
private messageService: MessageService
) {
// creates connection to neo4j database
this.driver = neo4j.driver(
environment.neo4j.url(this.document.location),
neo4j.auth.basic(
environment.neo4j.username,
environment.neo4j.password
)
);
}
//close connection to the database
ngOnDestroy(): void {
this.driver.close();
}
// methods to execute the specified query with the specified parameters
query(q: string, params: Dict = {}): Observable<neo4j.Record> {
//neo4j session between database and webapp
const rxSession = this.driver.rxSession();
return rxSession
.run(q, params)
.records()
.pipe(
// Catch to display an error message
catchError((e) => {
this.messageService.push({
type: 'error',
text: 'Cant connect to the lume servers. Please try again soon.',
title: 'Connectivity Issues',
});
console.warn(e);
return of(null);
}),
finalize(() => rxSession.close()),
filter((r) => r !== null), // filter out error events
map((r) => r as neo4j.Record) // map all remaining observables to Record
);
}
}