Home Reference Source
public class | source

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.

since 5.22.0
public
this method was deprecated. This is deprecated as of 6.0, use drop-in replacement {@link ResultTransformers#eager} instead.

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

public

first(): ResultTransformer<Record<Entries>|undefined>

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
this method is experimental. Part of the Record Object Mapping preview feature

Creates a ResultTransformer which maps each record of the result to a hydrated object of a provided type and/or according to provided rules.

public

mapped(config: object): ResultTransformer<T>

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
this method was deprecated. This is deprecated as of 6.0, use drop-in replacement {@link ResultTransformers#mapped} instead.

Creates a ResultTransformer which maps the Record in the result and collects it along with the ResultSummary and Result#keys.

public

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

since 5.22.0

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

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

this method was deprecated. This is deprecated as of 6.0, use drop-in replacement {@link ResultTransformers#eager} instead.

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

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 hydrated(): ResultTransformer<ResultSummary<T>> source

this method is experimental. Part of the Record Object Mapping preview feature

Creates a ResultTransformer which maps each record of the result to a hydrated object of a provided type and/or according to provided rules.

Return:

ResultTransformer<ResultSummary<T>>

The result transformer

Example:


class Person {
  constructor (name) {
    this.name = name
}

const personRules: Rules = {
   name: neo4j.rule.asString()
}

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

// Alternatively, the rules can be registered in the mapping registry.
// This registry exists in global memory and will persist even between driver instances.

neo4j.RecordObjectMapping.register(Person, PersonRules)

// after registering the rule the transformer will follow them when mapping to the provided type
const summary = await driver.executeQuery('CREATE (p:Person{ name: $name }) RETURN p', { name: 'Person1'}, {
  resultTransformer: neo4j.resultTransformers.hydrated(Person)
})

// A hydrated can be used without providing or registering Rules beforehand, but in such case the mapping will be done without any type validation

See:

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.

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

this method was deprecated. This is deprecated as of 6.0, use drop-in replacement {@link ResultTransformers#mapped} instead.

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>> since 5.22.0 source

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: