Skip to content

Commit 627e9d1

Browse files
committed
Add type definitions for reactive API
1 parent 5dbb41d commit 627e9d1

13 files changed

+457
-13
lines changed

gulpfile.babel.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ gulp.task('run-ts-declaration-tests', function (done) {
201201
target: 'es6',
202202
noImplicitAny: true,
203203
noImplicitReturns: true,
204-
strictNullChecks: true
204+
strictNullChecks: true,
205+
moduleResolution: 'node',
206+
types: []
205207
})
206208
)
207209
.pipe(gulp.dest('build/test/types'))

test/types/driver.test.ts

+46-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ import { Parameters } from '../../types/statement-runner'
3030
import Session from '../../types/session'
3131
import { Neo4jError } from '../../types/error'
3232
import { ServerInfo } from '../../types/result-summary'
33+
import RxSession from '../../types/session-rx'
34+
import { concat, map, catchError } from 'rxjs/operators'
35+
import { throwError } from 'rxjs'
3336

3437
const dummy: any = null
3538

@@ -88,16 +91,54 @@ const session7: Session = driver.session({
8891
bookmarks: 'bookmark2'
8992
})
9093

91-
session1.run('RETURN 1').then(result => {
92-
session1.close()
93-
result.records.forEach(record => {
94-
console.log(record)
94+
session1
95+
.run('RETURN 1')
96+
.then(result => {
97+
result.records.forEach(record => {
98+
console.log(record)
99+
})
95100
})
96-
})
101+
.then(() => session1.close())
97102

98103
const close: void = driver.close()
99104

100105
driver.verifyConnectivity().then((serverInfo: ServerInfo) => {
101106
console.log(serverInfo.version)
102107
console.log(serverInfo.address)
103108
})
109+
110+
const rxSession1: RxSession = driver.rxSession()
111+
const rxSession2: RxSession = driver.rxSession({ defaultAccessMode: READ })
112+
const rxSession3: RxSession = driver.rxSession({ defaultAccessMode: 'READ' })
113+
const rxSession4: RxSession = driver.rxSession({ defaultAccessMode: WRITE })
114+
const rxSession5: RxSession = driver.rxSession({ defaultAccessMode: 'WRITE' })
115+
const rxSession6: RxSession = driver.rxSession({
116+
defaultAccessMode: READ,
117+
bookmarks: 'bookmark1'
118+
})
119+
const rxSession7: RxSession = driver.rxSession({
120+
defaultAccessMode: READ,
121+
bookmarks: ['bookmark1', 'bookmark2']
122+
})
123+
const rxSession8: RxSession = driver.rxSession({
124+
defaultAccessMode: WRITE,
125+
bookmarks: 'bookmark1'
126+
})
127+
const rxSession9: RxSession = driver.rxSession({
128+
defaultAccessMode: WRITE,
129+
bookmarks: ['bookmark1', 'bookmark2']
130+
})
131+
132+
rxSession1
133+
.run('RETURN 1')
134+
.records()
135+
.pipe(
136+
map(r => r.get(0)),
137+
concat(rxSession1.close()),
138+
catchError(err => rxSession1.close().pipe(concat(throwError(err))))
139+
)
140+
.subscribe({
141+
next: data => console.log(data),
142+
complete: () => console.log('completed'),
143+
error: error => console.log(error)
144+
})

test/types/result-rx.test.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright (c) 2002-2019 "Neo4j,"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import RxResult from '../../types/result-rx'
21+
import Record from '../../types/record'
22+
import ResultSummary from '../../types/result-summary'
23+
import { Observer } from 'rxjs'
24+
25+
const dummy: any = null
26+
27+
const res: RxResult = dummy
28+
29+
res.keys().subscribe({
30+
next: value => console.log(`keys: ${value}`),
31+
complete: () => console.log('keys complete'),
32+
error: error => console.log(`keys error: ${error}`)
33+
})
34+
35+
res.records().subscribe({
36+
next: value => console.log(`record: ${value}`),
37+
complete: () => console.log('records complete'),
38+
error: error => console.log(`records error: ${error}`)
39+
})
40+
41+
res.summary().subscribe({
42+
next: value => console.log(`summary: ${value}`),
43+
complete: () => console.log('summary complete'),
44+
error: error => console.log(`summary error: ${error}`)
45+
})

test/types/result.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,10 @@ res.subscribe({
5050
onError: (error: Error) => console.log(error),
5151
onCompleted: (summary: ResultSummary) => console.log(summary)
5252
})
53+
54+
res.subscribe({
55+
onKeys: (keys: string[]) => console.log(keys),
56+
onNext: (record: Record) => console.log(record),
57+
onError: (error: Error) => console.log(error),
58+
onCompleted: (summary: ResultSummary) => console.log(summary)
59+
})

test/types/session-rx.test.ts

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/**
2+
* Copyright (c) 2002-2019 "Neo4j,"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import RxSession from '../../types/session-rx'
21+
import { TransactionConfig } from '../../types/session'
22+
import RxTransaction from '../../types/transaction-rx'
23+
import Record from '../../types/record'
24+
import RxResult from '../../types/result-rx'
25+
import ResultSummary from '../../types/result-summary'
26+
import Integer from '../../types/integer'
27+
import { Observable, of, Observer, throwError } from 'rxjs'
28+
import { concat, finalize, catchError } from 'rxjs/operators'
29+
30+
const dummy: any = null
31+
const intValue: Integer = Integer.fromInt(42)
32+
33+
const keysObserver: Observer<string[]> = {
34+
next: value => console.log(`keys: ${value}`),
35+
complete: () => console.log('keys complete'),
36+
error: error => console.log(`keys error: ${error}`)
37+
}
38+
39+
const recordsObserver: Observer<Record> = {
40+
next: value => console.log(`record: ${value}`),
41+
complete: () => console.log('records complete'),
42+
error: error => console.log(`records error: ${error}`)
43+
}
44+
45+
const summaryObserver: Observer<ResultSummary> = {
46+
next: value => console.log(`summary: ${value}`),
47+
complete: () => console.log('summary complete'),
48+
error: error => console.log(`summary error: ${error}`)
49+
}
50+
51+
const rxSession: RxSession = dummy
52+
53+
const txConfig1: TransactionConfig = {}
54+
const txConfig2: TransactionConfig = { timeout: 5000 }
55+
const txConfig3: TransactionConfig = { timeout: intValue }
56+
const txConfig4: TransactionConfig = { metadata: {} }
57+
const txConfig5: TransactionConfig = {
58+
metadata: {
59+
key1: 'value1',
60+
key2: 5,
61+
key3: { a: 'a', b: 'b' },
62+
key4: [1, 2, 3]
63+
}
64+
}
65+
const txConfig6: TransactionConfig = {
66+
timeout: 2000,
67+
metadata: { key1: 'value1', key2: 2 }
68+
}
69+
const txConfig7: TransactionConfig = {
70+
timeout: intValue,
71+
metadata: { key1: 'value1', key2: 2 }
72+
}
73+
74+
const tx1: Observable<RxTransaction> = rxSession.beginTransaction()
75+
const bookmark: null | string = <null>rxSession.lastBookmark()
76+
77+
const observable1: Observable<number> = rxSession.readTransaction(
78+
(tx: RxTransaction) => {
79+
return of(10)
80+
}
81+
)
82+
83+
const observable2: Observable<string> = rxSession.readTransaction(
84+
(tx: RxTransaction) => {
85+
return of('42')
86+
}
87+
)
88+
89+
const observable3: Observable<number> = rxSession.writeTransaction(
90+
(tx: RxTransaction) => {
91+
return of(10)
92+
}
93+
)
94+
95+
const observable4: Observable<string> = rxSession.writeTransaction(
96+
(tx: RxTransaction) => {
97+
return of('42')
98+
}
99+
)
100+
101+
const close1: Observable<void> = rxSession.close()
102+
const close2: Observable<void> = rxSession
103+
.close()
104+
.pipe(finalize(() => 'session closed'))
105+
106+
const result1: RxResult = rxSession.run('RETURN 1')
107+
result1.keys().subscribe(keysObserver)
108+
result1.records().subscribe(recordsObserver)
109+
result1
110+
.summary()
111+
.pipe(
112+
concat(close1),
113+
catchError(err => close1.pipe(concat(throwError(err))))
114+
)
115+
.subscribe(summaryObserver)
116+
117+
const result2: RxResult = rxSession.run('RETURN $value', { value: '42' })
118+
result2.keys().subscribe(keysObserver)
119+
result2.records().subscribe(recordsObserver)
120+
result2
121+
.summary()
122+
.pipe(
123+
concat(close1),
124+
catchError(err => close1.pipe(concat(throwError(err))))
125+
)
126+
.subscribe(summaryObserver)
127+
128+
const result3: RxResult = rxSession.run(
129+
'RETURN $value',
130+
{ value: '42' },
131+
txConfig1
132+
)
133+
result3.keys().subscribe(keysObserver)
134+
result3.records().subscribe(recordsObserver)
135+
result3
136+
.summary()
137+
.pipe(
138+
concat(close1),
139+
catchError(err => close1.pipe(concat(throwError(err))))
140+
)
141+
.subscribe(summaryObserver)
142+
143+
const tx2: Observable<RxTransaction> = rxSession.beginTransaction(txConfig2)
144+
const observable5: Observable<string> = rxSession.readTransaction(
145+
(tx: RxTransaction) => of(''),
146+
txConfig3
147+
)
148+
const observable6: Observable<number> = rxSession.writeTransaction(
149+
(tx: RxTransaction) => of(42),
150+
txConfig4
151+
)

test/types/transaction-rx.test.ts

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Copyright (c) 2002-2019 "Neo4j,"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import RxTransaction from '../../types/transaction-rx'
21+
import Record from '../../types/record'
22+
import RxResult from '../../types/result-rx'
23+
import ResultSummary from '../../types/result-summary'
24+
import { Observable, of, Observer, throwError } from 'rxjs'
25+
import { concat, finalize, catchError } from 'rxjs/operators'
26+
27+
const dummy: any = null
28+
29+
const stringObserver: Observer<string> = {
30+
next: value => console.log(value),
31+
complete: () => console.log('complete'),
32+
error: error => console.log(`error: ${error}`)
33+
}
34+
35+
const keysObserver: Observer<string[]> = {
36+
next: value => console.log(`keys: ${value}`),
37+
complete: () => console.log('keys complete'),
38+
error: error => console.log(`keys error: ${error}`)
39+
}
40+
41+
const recordsObserver: Observer<Record> = {
42+
next: value => console.log(`record: ${value}`),
43+
complete: () => console.log('records complete'),
44+
error: error => console.log(`records error: ${error}`)
45+
}
46+
47+
const summaryObserver: Observer<ResultSummary> = {
48+
next: value => console.log(`summary: ${value}`),
49+
complete: () => console.log('summary complete'),
50+
error: error => console.log(`summary error: ${error}`)
51+
}
52+
53+
const tx: RxTransaction = dummy
54+
55+
const result1: RxResult = tx.run('RETURN 1')
56+
result1.keys().subscribe(keysObserver)
57+
result1.records().subscribe(recordsObserver)
58+
result1.summary().subscribe(summaryObserver)
59+
60+
const result2: RxResult = tx.run('RETURN $value', { value: '42' })
61+
result2.keys().subscribe(keysObserver)
62+
result2.records().subscribe(recordsObserver)
63+
result2.summary().subscribe(summaryObserver)
64+
65+
tx.commit()
66+
.pipe(concat(of('committed')))
67+
.subscribe(stringObserver)
68+
69+
tx.rollback()
70+
.pipe(concat(of('rolled back')))
71+
.subscribe(stringObserver)

tsconfig.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"module": "es6",
44
"target": "es6",
55
"noImplicitAny": true,
6-
"noImplicitReturns": true
6+
"noImplicitReturns": true,
7+
"moduleResolution": "node",
8+
"types": []
79
}
810
}

types/driver.d.ts

+11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
import Session from './session'
21+
import RxSession from './session-rx'
2122
import { Parameters } from './statement-runner'
2223
import { Neo4jError } from './error'
2324
import { ServerInfo } from './result-summary'
@@ -72,6 +73,16 @@ declare interface Driver {
7273
database?: string
7374
}): Session
7475

76+
rxSession({
77+
defaultAccessMode,
78+
bookmarks,
79+
database
80+
}?: {
81+
defaultAccessMode?: SessionMode
82+
bookmarks?: string | string[]
83+
database?: string
84+
}): RxSession
85+
7586
close(): void
7687

7788
verifyConnectivity(): Promise<ServerInfo>

0 commit comments

Comments
 (0)