ResultTransformers
Defines the object which holds the common ResultTransformer used with Driver#executeQuery.
Method Summary
Public Methods | ||
public |
eagerResultTransformer(): ResultTransformer<EagerResult<Entries>> Creates a ResultTransformer which transforms Result to EagerResult by consuming the whole stream. |
|
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 Methods
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 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`))