ResultTransformers
Defines the object which holds the common ResultTransformer used with Driver#executeQuery.
Method Summary
Public Methods | ||
public |
eager(): ResultTransformer<EagerResult<Entries>> this method is experimental. this is a preview
Creates a ResultTransformer which transforms Result to EagerResult by consuming the whole stream. |
since 5.22.0 |
public |
eagerResultTransformer(): ResultTransformer<EagerResult<Entries>> Creates a ResultTransformer which transforms Result to EagerResult by consuming the whole stream. |
|
public |
first(): ResultTransformer<Record<Entries>|undefined> this method is experimental. This is a preview feature.
Creates a ResultTransformer which collects the first record Record of Result and discard the rest of the records, if existent. |
since 5.22.0 |
public |
mapped(config: object): ResultTransformer<T> this method is experimental. This is a preview feature
Creates a ResultTransformer which maps the Record in the result and collects it along with the ResultSummary and Result#keys. |
since 5.22.0 |
public |
mappedResultTransformer(config: object): ResultTransformer<T> Creates a ResultTransformer which maps the Record in the result and collects it along with the ResultSummary and Result#keys. |
|
public |
this method is experimental. This is a preview feature
Creates a ResultTransformer which consumes the result and returns the ResultSummary. |
Public Methods
public eager(): ResultTransformer<EagerResult<Entries>> since 5.22.0 source
Creates a ResultTransformer which transforms Result to EagerResult by consuming the whole stream.
This is the default implementation used in Driver#executeQuery and a alias to resultTransformers.eagerResultTransformer
Example:
// This:
const { keys, records, summary } = await driver.executeQuery('CREATE (p:Person{ name: $name }) RETURN p', { name: 'Person1'}, {
resultTransformer: neo4j.resultTransformers.eager()
})
// is equivalent to:
const { keys, records, summary } = await driver.executeQuery('CREATE (p:Person{ name: $name }) RETURN p', { name: 'Person1'})
public eagerResultTransformer(): ResultTransformer<EagerResult<Entries>> source
Creates a ResultTransformer which transforms Result to EagerResult by consuming the whole stream.
This is the default implementation used in Driver#executeQuery
Example:
// This:
const { keys, records, summary } = await driver.executeQuery('CREATE (p:Person{ name: $name }) RETURN p', { name: 'Person1'}, {
resultTransformer: neo4j.resultTransformers.eagerResultTransformer()
})
// is equivalent to:
const { keys, records, summary } = await driver.executeQuery('CREATE (p:Person{ name: $name }) RETURN p', { name: 'Person1'})
public first(): ResultTransformer<Record<Entries>|undefined> since 5.22.0 source
Creates a ResultTransformer which collects the first record Record of Result and discard the rest of the records, if existent.
Example:
// Using in executeQuery
const maybeFirstRecord = await driver.executeQuery('MATCH (p:Person{ age: $age }) RETURN p.name as name', { age: 25 }, {
resultTransformer: neo4j.resultTransformers.first()
})
// Using in other results
const record = await neo4j.resultTransformers.first()(result)
public mapped(config: object): ResultTransformer<T> since 5.22.0 source
Creates a ResultTransformer which maps the Record in the result and collects it along with the ResultSummary and Result#keys.
NOTE: The config object requires map or/and collect to be valid.
This method is a alias to ResultTransformers#mappedResultTransformer
Params:
Name | Type | Attribute | Description |
config | object | The result transformer configuration |
|
config.map | function(record: Record): R |
|
Method called for mapping each record |
config.collect | function(records: R[], summary: ResultSummary, keys: string[]): T |
|
Method called for mapping the result data to the transformer output. |
Example:
// Mapping the records
const { keys, records, summary } = await driver.executeQuery('MATCH (p:Person{ age: $age }) RETURN p.name as name', { age: 25 }, {
resultTransformer: neo4j.resultTransformers.mapped({
map(record) {
return record.get('name')
}
})
})
records.forEach(name => console.log(`${name} has 25`))
// Mapping records and collect result
const names = await driver.executeQuery('MATCH (p:Person{ age: $age }) RETURN p.name as name', { age: 25 }, {
resultTransformer: neo4j.resultTransformers.mapped({
map(record) {
return record.get('name')
},
collect(records, summary, keys) {
return records
}
})
})
names.forEach(name => console.log(`${name} has 25`))
// The transformer can be defined one and used everywhere
const getRecordsAsObjects = neo4j.resultTransformers.mapped({
map(record) {
return record.toObject()
},
collect(objects) {
return objects
}
})
// The usage in a driver.executeQuery
const objects = await driver.executeQuery('MATCH (p:Person{ age: $age }) RETURN p.name as name', { age: 25 }, {
resultTransformer: getRecordsAsObjects
})
objects.forEach(object => console.log(`${object.name} has 25`))
// The usage in session.executeRead
const objects = await session.executeRead(tx => getRecordsAsObjects(tx.run('MATCH (p:Person{ age: $age }) RETURN p.name as name')))
objects.forEach(object => console.log(`${object.name} has 25`))
public mappedResultTransformer(config: object): ResultTransformer<T> source
Creates a ResultTransformer which maps the Record in the result and collects it along with the ResultSummary and Result#keys.
NOTE: The config object requires map or/and collect to be valid.
Params:
Name | Type | Attribute | Description |
config | object | The result transformer configuration |
|
config.map | function(record: Record): R |
|
Method called for mapping each record |
config.collect | function(records: R[], summary: ResultSummary, keys: string[]): T |
|
Method called for mapping the result data to the transformer output. |
Example:
// Mapping the records
const { keys, records, summary } = await driver.executeQuery('MATCH (p:Person{ age: $age }) RETURN p.name as name', { age: 25 }, {
resultTransformer: neo4j.resultTransformers.mappedResultTransformer({
map(record) {
return record.get('name')
}
})
})
records.forEach(name => console.log(`${name} has 25`))
// Mapping records and collect result
const names = await driver.executeQuery('MATCH (p:Person{ age: $age }) RETURN p.name as name', { age: 25 }, {
resultTransformer: neo4j.resultTransformers.mappedResultTransformer({
map(record) {
return record.get('name')
},
collect(records, summary, keys) {
return records
}
})
})
names.forEach(name => console.log(`${name} has 25`))
// The transformer can be defined one and used everywhere
const getRecordsAsObjects = neo4j.resultTransformers.mappedResultTransformer({
map(record) {
return record.toObject()
},
collect(objects) {
return objects
}
})
// The usage in a driver.executeQuery
const objects = await driver.executeQuery('MATCH (p:Person{ age: $age }) RETURN p.name as name', { age: 25 }, {
resultTransformer: getRecordsAsObjects
})
objects.forEach(object => console.log(`${object.name} has 25`))
// The usage in session.executeRead
const objects = await session.executeRead(tx => getRecordsAsObjects(tx.run('MATCH (p:Person{ age: $age }) RETURN p.name as name')))
objects.forEach(object => console.log(`${object.name} has 25`))
public summary(): ResultTransformer<ResultSummary<T>> source
Creates a ResultTransformer which consumes the result and returns the ResultSummary.
This result transformer is a shortcut to (result) => result.summary()
.
Example:
const summary = await driver.executeQuery('CREATE (p:Person{ name: $name }) RETURN p', { name: 'Person1'}, {
resultTransformer: neo4j.resultTransformers.summary()
})