@@ -52,11 +52,36 @@ class ResultTransformers {
52
52
* const { keys, records, summary } = await driver.executeQuery('CREATE (p:Person{ name: $name }) RETURN p', { name: 'Person1'})
53
53
*
54
54
* @returns {ResultTransformer<EagerResult<Entries>> } The result transformer
55
+ * @alias {@link ResultTransformers#eager }
55
56
*/
56
57
eagerResultTransformer < Entries extends RecordShape = RecordShape > ( ) : ResultTransformer < EagerResult < Entries > > {
57
58
return createEagerResultFromResult
58
59
}
59
60
61
+ /**
62
+ * Creates a {@link ResultTransformer} which transforms {@link Result} to {@link EagerResult}
63
+ * by consuming the whole stream.
64
+ *
65
+ * This is the default implementation used in {@link Driver#executeQuery} and a alias to
66
+ * {@link resultTransformers.eagerResultTransformer}
67
+ *
68
+ * @example
69
+ * // This:
70
+ * const { keys, records, summary } = await driver.executeQuery('CREATE (p:Person{ name: $name }) RETURN p', { name: 'Person1'}, {
71
+ * resultTransformer: neo4j.resultTransformers.eager()
72
+ * })
73
+ * // is equivalent to:
74
+ * const { keys, records, summary } = await driver.executeQuery('CREATE (p:Person{ name: $name }) RETURN p', { name: 'Person1'})
75
+ *
76
+ * @returns {ResultTransformer<EagerResult<Entries>> } The result transformer
77
+ * @experimental this is a preview
78
+ * @since 5.22.0
79
+ * @alias {@link ResultTransformers#eagerResultTransformer }
80
+ */
81
+ eager < Entries extends RecordShape = RecordShape > ( ) : ResultTransformer < EagerResult < Entries > > {
82
+ return createEagerResultFromResult
83
+ }
84
+
60
85
/**
61
86
* Creates a {@link ResultTransformer} which maps the {@link Record} in the result and collects it
62
87
* along with the {@link ResultSummary} and {@link Result#keys}.
@@ -122,41 +147,81 @@ class ResultTransformers {
122
147
mappedResultTransformer <
123
148
R = Record , T = { records : R [ ] , keys : string [ ] , summary : ResultSummary }
124
149
> ( config : { map ?: ( rec : Record ) => R | undefined , collect ?: ( records : R [ ] , summary : ResultSummary , keys : string [ ] ) => T } ) : ResultTransformer < T > {
125
- if ( config == null || ( config . collect == null && config . map == null ) ) {
126
- throw newError ( 'Requires a map or/and a collect functions.' )
127
- }
128
- return async ( result : Result ) => {
129
- return await new Promise ( ( resolve , reject ) => {
130
- const state : { keys : string [ ] , records : R [ ] } = { records : [ ] , keys : [ ] }
131
-
132
- result . subscribe ( {
133
- onKeys ( keys : string [ ] ) {
134
- state . keys = keys
135
- } ,
136
- onNext ( record : Record ) {
137
- if ( config . map != null ) {
138
- const mappedRecord = config . map ( record )
139
- if ( mappedRecord !== undefined ) {
140
- state . records . push ( mappedRecord )
141
- }
142
- } else {
143
- state . records . push ( record as unknown as R )
144
- }
145
- } ,
146
- onCompleted ( summary : ResultSummary ) {
147
- if ( config . collect != null ) {
148
- resolve ( config . collect ( state . records , summary , state . keys ) )
149
- } else {
150
- const obj = { records : state . records , summary, keys : state . keys }
151
- resolve ( obj as unknown as T )
152
- }
153
- } ,
154
- onError ( error : Error ) {
155
- reject ( error )
156
- }
157
- } )
158
- } )
159
- }
150
+ return createMappedResultTransformer ( config )
151
+ }
152
+
153
+ /**
154
+ * Creates a {@link ResultTransformer} which maps the {@link Record} in the result and collects it
155
+ * along with the {@link ResultSummary} and {@link Result#keys}.
156
+ *
157
+ * NOTE: The config object requires map or/and collect to be valid.
158
+ *
159
+ * This method is a alias to {@link ResultTransformers#mappedResultTransformer}
160
+ *
161
+ *
162
+ * @example
163
+ * // Mapping the records
164
+ * const { keys, records, summary } = await driver.executeQuery('MATCH (p:Person{ age: $age }) RETURN p.name as name', { age: 25 }, {
165
+ * resultTransformer: neo4j.resultTransformers.mapped({
166
+ * map(record) {
167
+ * return record.get('name')
168
+ * }
169
+ * })
170
+ * })
171
+ *
172
+ * records.forEach(name => console.log(`${name} has 25`))
173
+ *
174
+ * @example
175
+ * // Mapping records and collect result
176
+ * const names = await driver.executeQuery('MATCH (p:Person{ age: $age }) RETURN p.name as name', { age: 25 }, {
177
+ * resultTransformer: neo4j.resultTransformers.mapped({
178
+ * map(record) {
179
+ * return record.get('name')
180
+ * },
181
+ * collect(records, summary, keys) {
182
+ * return records
183
+ * }
184
+ * })
185
+ * })
186
+ *
187
+ * names.forEach(name => console.log(`${name} has 25`))
188
+ *
189
+ * @example
190
+ * // The transformer can be defined one and used everywhere
191
+ * const getRecordsAsObjects = neo4j.resultTransformers.mapped({
192
+ * map(record) {
193
+ * return record.toObject()
194
+ * },
195
+ * collect(objects) {
196
+ * return objects
197
+ * }
198
+ * })
199
+ *
200
+ * // The usage in a driver.executeQuery
201
+ * const objects = await driver.executeQuery('MATCH (p:Person{ age: $age }) RETURN p.name as name', { age: 25 }, {
202
+ * resultTransformer: getRecordsAsObjects
203
+ * })
204
+ * objects.forEach(object => console.log(`${object.name} has 25`))
205
+ *
206
+ *
207
+ * // The usage in session.executeRead
208
+ * const objects = await session.executeRead(tx => getRecordsAsObjects(tx.run('MATCH (p:Person{ age: $age }) RETURN p.name as name')))
209
+ * objects.forEach(object => console.log(`${object.name} has 25`))
210
+ *
211
+ * @param {object } config The result transformer configuration
212
+ * @param {function(record:Record):R } [config.map=function(record) { return record }] Method called for mapping each record
213
+ * @param {function(records:R[], summary:ResultSummary, keys:string[]):T } [config.collect=function(records, summary, keys) { return { records, summary, keys }}] Method called for mapping
214
+ * the result data to the transformer output.
215
+ * @returns {ResultTransformer<T> } The result transformer
216
+ * @experimental This is a preview feature
217
+ * @alias {@link ResultTransformers#mappedResultTransformer }
218
+ * @since 5.22.0
219
+ * @see {@link Driver#executeQuery }
220
+ */
221
+ mapped <
222
+ R = Record , T = { records : R [ ] , keys : string [ ] , summary : ResultSummary }
223
+ > ( config : { map ?: ( rec : Record ) => R | undefined , collect ?: ( records : R [ ] , summary : ResultSummary , keys : string [ ] ) => T } ) : ResultTransformer < T > {
224
+ return createMappedResultTransformer ( config )
160
225
}
161
226
162
227
/**
@@ -222,6 +287,44 @@ async function createEagerResultFromResult<Entries extends RecordShape> (result:
222
287
return new EagerResult < Entries > ( keys , records , summary )
223
288
}
224
289
290
+ function createMappedResultTransformer < R = Record , T = { records : R [ ] , keys : string [ ] , summary : ResultSummary } > ( config : { map ?: ( rec : Record ) => R | undefined , collect ?: ( records : R [ ] , summary : ResultSummary , keys : string [ ] ) => T } ) : ResultTransformer < T > {
291
+ if ( config == null || ( config . collect == null && config . map == null ) ) {
292
+ throw newError ( 'Requires a map or/and a collect functions.' )
293
+ }
294
+ return async ( result : Result ) => {
295
+ return await new Promise ( ( resolve , reject ) => {
296
+ const state : { keys : string [ ] , records : R [ ] } = { records : [ ] , keys : [ ] }
297
+
298
+ result . subscribe ( {
299
+ onKeys ( keys : string [ ] ) {
300
+ state . keys = keys
301
+ } ,
302
+ onNext ( record : Record ) {
303
+ if ( config . map != null ) {
304
+ const mappedRecord = config . map ( record )
305
+ if ( mappedRecord !== undefined ) {
306
+ state . records . push ( mappedRecord )
307
+ }
308
+ } else {
309
+ state . records . push ( record as unknown as R )
310
+ }
311
+ } ,
312
+ onCompleted ( summary : ResultSummary ) {
313
+ if ( config . collect != null ) {
314
+ resolve ( config . collect ( state . records , summary , state . keys ) )
315
+ } else {
316
+ const obj = { records : state . records , summary, keys : state . keys }
317
+ resolve ( obj as unknown as T )
318
+ }
319
+ } ,
320
+ onError ( error : Error ) {
321
+ reject ( error )
322
+ }
323
+ } )
324
+ } )
325
+ }
326
+ }
327
+
225
328
async function first < Entries extends RecordShape > ( result : Result ) : Promise < Record < Entries > | undefined > {
226
329
// The async iterator is not used in the for await fashion
227
330
// because the transpiler is generating a code which
0 commit comments