Skip to content

Commit

Permalink
update readme (#1249)
Browse files Browse the repository at this point in the history
Replaces deprecated function use, and moves session.run usage examples down to not be the top example.
  • Loading branch information
MaxAake authored Feb 5, 2025
1 parent 2031e43 commit ae705f7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 75 deletions.
148 changes: 74 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,76 +188,6 @@ var rxSession = driver.rxSession({
})
```

### Executing Queries

#### Consuming Records with Streaming API

```javascript
// Run a Cypher statement, reading the result in a streaming manner as records arrive:
session
.run('MERGE (alice:Person {name : $nameParam}) RETURN alice.name AS name', {
nameParam: 'Alice'
})
.subscribe({
onKeys: keys => {
console.log(keys)
},
onNext: record => {
console.log(record.get('name'))
},
onCompleted: () => {
session.close() // returns a Promise
},
onError: error => {
console.log(error)
}
})
```

Subscriber API allows following combinations of `onKeys`, `onNext`, `onCompleted` and `onError` callback invocations:

- zero or one `onKeys`,
- zero or more `onNext` followed by `onCompleted` when operation was successful. `onError` will not be invoked in this case
- zero or more `onNext` followed by `onError` when operation failed. Callback `onError` might be invoked after couple `onNext` invocations because records are streamed lazily by the database. `onCompleted` will not be invoked in this case.

#### Consuming Records with Promise API

```javascript
// the Promise way, where the complete result is collected before we act on it:
session
.run('MERGE (james:Person {name : $nameParam}) RETURN james.name AS name', {
nameParam: 'James'
})
.then(result => {
result.records.forEach(record => {
console.log(record.get('name'))
})
})
.catch(error => {
console.log(error)
})
.then(() => session.close())
```

#### Consuming Records with Reactive API

```javascript
rxSession
.run('MERGE (james:Person {name: $nameParam}) RETURN james.name AS name', {
nameParam: 'Bob'
})
.records()
.pipe(
map(record => record.get('name')),
concatWith(rxSession.close())
)
.subscribe({
next: data => console.log(data),
complete: () => console.log('completed'),
error: err => console.log(err)
})
```

### Transaction functions

```javascript
Expand All @@ -276,7 +206,7 @@ neo4j.driver('neo4j://localhost', neo4j.auth.basic('neo4j', 'password'), {
// It is possible to execute read transactions that will benefit from automatic
// retries on both single instance ('bolt' URI scheme) and Causal Cluster
// ('neo4j' URI scheme) and will get automatic load balancing in cluster deployments
var readTxResultPromise = session.readTransaction(txc => {
var readTxResultPromise = session.executeRead(txc => {
// used transaction will be committed automatically, no need for explicit commit/rollback

var result = txc.run('MATCH (person:Person) RETURN person.name AS name')
Expand All @@ -300,7 +230,7 @@ readTxResultPromise

```javascript
rxSession
.readTransaction(txc =>
.executeRead(txc =>
txc
.run('MATCH (person:Person) RETURN person.name AS name')
.records()
Expand All @@ -318,7 +248,7 @@ rxSession
```javascript
// It is possible to execute write transactions that will benefit from automatic retries
// on both single instance ('bolt' URI scheme) and Causal Cluster ('neo4j' URI scheme)
var writeTxResultPromise = session.writeTransaction(async txc => {
var writeTxResultPromise = session.executeWrite(async txc => {
// used transaction will be committed automatically, no need for explicit commit/rollback

var result = await txc.run(
Expand All @@ -344,7 +274,7 @@ writeTxResultPromise

```javascript
rxSession
.writeTransaction(txc =>
.executeWrite(txc =>
txc
.run("MERGE (alice:Person {name: 'James'}) RETURN alice.name AS name")
.records()
Expand All @@ -357,6 +287,76 @@ rxSession
})
```

### Consuming Records

#### Consuming Records with Streaming API

```javascript
// Run a Cypher statement, reading the result in a streaming manner as records arrive:
session
.run('MERGE (alice:Person {name : $nameParam}) RETURN alice.name AS name', {
nameParam: 'Alice'
})
.subscribe({
onKeys: keys => {
console.log(keys)
},
onNext: record => {
console.log(record.get('name'))
},
onCompleted: () => {
session.close() // returns a Promise
},
onError: error => {
console.log(error)
}
})
```

Subscriber API allows following combinations of `onKeys`, `onNext`, `onCompleted` and `onError` callback invocations:

- zero or one `onKeys`,
- zero or more `onNext` followed by `onCompleted` when operation was successful. `onError` will not be invoked in this case
- zero or more `onNext` followed by `onError` when operation failed. Callback `onError` might be invoked after couple `onNext` invocations because records are streamed lazily by the database. `onCompleted` will not be invoked in this case.

#### Consuming Records with Promise API

```javascript
// the Promise way, where the complete result is collected before we act on it:
session
.run('MERGE (james:Person {name : $nameParam}) RETURN james.name AS name', {
nameParam: 'James'
})
.then(result => {
result.records.forEach(record => {
console.log(record.get('name'))
})
})
.catch(error => {
console.log(error)
})
.then(() => session.close())
```

#### Consuming Records with Reactive API

```javascript
rxSession
.run('MERGE (james:Person {name: $nameParam}) RETURN james.name AS name', {
nameParam: 'Bob'
})
.records()
.pipe(
map(record => record.get('name')),
concatWith(rxSession.close())
)
.subscribe({
next: data => console.log(data),
complete: () => console.log('completed'),
error: err => console.log(err)
})
```

### Explicit Transactions

#### With Async Session
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
"typescript": "^4.9.5"
},
"lint-staged": {
"*.{js,md,html}": [
"*.{js,html}": [
"npm run standard",
"git add"
],
"*.md": [
"git add"
],
"packages/core/**/*.ts": [
"npm run ts-standard::core",
"git add"
Expand Down

0 comments on commit ae705f7

Please sign in to comment.