Home Reference Source
public class | source

ResultTransformers

You can directly use an instance of this class. resultTransformers

Defines the object which holds the common ResultTransformer used with Driver#executeQuery.

Method Summary

Public Methods
public

Creates a ResultTransformer which transforms Result to EagerResult by consuming the whole stream.

public

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

Return:

ResultTransformer<EagerResult<Entries>>

The result transformer

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:

NameTypeAttributeDescription
config object

The result transformer configuration

config.map function(record: Record): R
  • optional
  • default: function(record) { return record }

Method called for mapping each record

config.collect function(records: R[], summary: ResultSummary, keys: string[]): T
  • optional
  • default: function(records, summary, keys) { return { records, summary, keys }}

Method called for mapping the result data to the transformer output.

Return:

ResultTransformer<T>

The result transformer

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`))

See: