v5.5.0 - Feedback wanted on executeQuery
#1052
Replies: 3 comments 3 replies
-
The |
Beta Was this translation helpful? Give feedback.
-
I can see a benefit to having many driver.executeQuery(cypher, params, {
resultTransfomer: neo4j.resultTransformers.firstKey('count'),
}) The first thing that strikes me is that the simplified API is actually more verbose and longer type than the current method. The implementation also adds the complexity of needing to know what a result transformer is, what the methods are and how to import it. // New - 260 chars, plus need to know what a result transformer is
const res1 = driver.executeQuery(cypher, params, {
resultTransformer: neo4j.resultTransformers.mappedResultTransformer({
map: (record) => record.toObject(),
collect: (records) => records,
}),
})
console.log(res1)
// Old - 208 Chars
const session = driver.session()
const res2 = await session.executeRead(
tx => tx.run(cypher, params)
)
console.log(res2.records.map(record => record.toObject()))
await session.close() Personally, I would like to see plain functions that can be easily implemented as top-level options with type support. // 185 chars
const res3 = driver.executeQuery(cypher, params, {
recordTransformer: (record) => record.toObject(), // or mapRecord?
resultTransformer: (records) => records
})
console.log(res3)
// or recordTransformer -> map, resultTransformer -> collect for 179 chars It may also help to have more understandable terms. For example |
Beta Was this translation helpful? Give feedback.
-
The TypeScript implementation also seems a little problematic as the executeQuery<T, ResultTransformerOutput = EagerResult<T>>(
query: Query,
parameters?: any,
config?: QueryConfig<T>
): Promise<EagerResult<T>>; This way the user doesn't need to include the extra type by default in order to type check |
Beta Was this translation helpful? Give feedback.
-
Driver.executeQuery
In the version 5.5.0, we introduce a new experimental API to the driver. This is a simplified API for running queries against a database without needing to get into the detail of sessions and transactions.
The very basic usage example:
This runs a the cypher query and return a
EagerResult
object. This object contains the list of records returned, the list of keys present in the records and the result summary. This return object shape is defined by the result transformerneo4j.resultTransfomers.eagerResultTransformer()
which can be configured as following:What is
ResultTransformer
Result transformer is a async function which receives a
Result
and transform it to a desired output. In plain typescript, this type can be defined as:For the driver user doesn't have to implement their own result transformer, the driver offers two implementations:
function neo4j.resultTransformers.eagerResultTransformer(): ResultTransformer<EagerResult>
which provides the default result transformed used indriver.executeQuery
.function neo4j.resultTransformers.mappedResultTransformer(config: { map, collect }): ResultTransformer<T>
which provides an result transformer with methods to map the records and collect the final result.Using the
mappedResultTransformer
to improve the exampleIn the example bellow, we use an
mappedResultTransformer
to map our record to the final shape used in the for loop.Although the code for printing the result was improved, the shape of the returning object is not quite what we need. We actually want a object with the
answer
, how long it takes for the answer be available and the keys, since we'd like to print the keys.The
collect
can be used for defining a function which will receive the mapped records, the summary and the keys. See:NOTE:
ResultTransformer
can also be used inSession.run
andTransaction.run
.Under the Hood
This API the common pattern used for run queries to neo4j using the driver. This very first example can be re-written as:
Configuration
For configuration see executeQuery api docs and query config api docs
Feedback wanted
This new API is currently marked as experimental.
We're definitely looking for feedback on this feature. Any thoughts you have are gratefully received. Specific questions we would like to ask:
mappedResultTransformer
? Is it any improvement which can be done in its usage?executeQuery
for using sessions and transactions (executeRead/executeWrite) in more complex use cases if you need to?Let us know and we will correct course in the next releases!
Beta Was this translation helpful? Give feedback.
All reactions