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

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

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

this method is experimental. this is a preview

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

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.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

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 first(): ResultTransformer<Record<Entries>|undefined> since 5.22.0 source

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.

Return:

ResultTransformer<Record<Entries>|undefined>

The result transformer

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)

See:

public mapped(config: object): ResultTransformer<T> since 5.22.0 source

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.

NOTE: The config object requires map or/and collect to be valid.

This method is a alias to ResultTransformers#mappedResultTransformer

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

See:

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:

public summary(): ResultTransformer<ResultSummary<T>> source

this method is experimental. This is a preview feature

Creates a ResultTransformer which consumes the result and returns the ResultSummary.

This result transformer is a shortcut to (result) => result.summary().

Return:

ResultTransformer<ResultSummary<T>>

The result transformer

Example:

const summary = await driver.executeQuery('CREATE (p:Person{ name: $name }) RETURN p', { name: 'Person1'}, {
  resultTransformer: neo4j.resultTransformers.summary()
})

See: