Skip to content

Commit

Permalink
Comply to ExecuteQuery ADR latest revision
Browse files Browse the repository at this point in the history
  • Loading branch information
fbiville committed Feb 14, 2023
1 parent 15386b7 commit 68ce3ff
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
12 changes: 6 additions & 6 deletions packages/core/src/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ class QueryConfig<T = EagerResult> {
* A BookmarkManager is a piece of software responsible for keeping casual consistency between different pieces of work by sharing bookmarks
* between the them.
*
* By default, it uses the driver's non mutable driver level bookmark manager. See, {@link Driver.queryBookmarkManager}
* By default, it uses the driver's non mutable driver level bookmark manager. See, {@link Driver.defaultExecuteQueryBookmarkManager}
*
* Can be set to null to disable causal chaining.
* @type {BookmarkManager|null}
Expand All @@ -338,7 +338,7 @@ class Driver {
private readonly _createConnectionProvider: CreateConnectionProvider
private _connectionProvider: ConnectionProvider | null
private readonly _createSession: CreateSession
private readonly _queryBookmarkManager: BookmarkManager
private readonly _defaultExecuteQueryBookmarkManager: BookmarkManager
private readonly _queryExecutor: QueryExecutor

/**
Expand Down Expand Up @@ -369,7 +369,7 @@ class Driver {
this._log = log
this._createConnectionProvider = createConnectionProvider
this._createSession = createSession
this._queryBookmarkManager = bookmarkManager()
this._defaultExecuteQueryBookmarkManager = bookmarkManager()
this._queryExecutor = createQueryExecutor(this.session.bind(this))

/**
Expand All @@ -389,8 +389,8 @@ class Driver {
* @type {BookmarkManager}
* @returns {BookmarkManager}
*/
get queryBookmarkManager (): BookmarkManager {
return this._queryBookmarkManager
get defaultExecuteQueryBookmarkManager (): BookmarkManager {
return this._defaultExecuteQueryBookmarkManager
}

/**
Expand Down Expand Up @@ -472,7 +472,7 @@ class Driver {
* @see https://github.com/neo4j/neo4j-javascript-driver/discussions/1052
*/
async executeQuery<T = EagerResult> (query: Query, parameters?: any, config: QueryConfig<T> = {}): Promise<T> {
const bookmarkManager = config.bookmarkManager === null ? undefined : (config.bookmarkManager ?? this.queryBookmarkManager)
const bookmarkManager = config.bookmarkManager === null ? undefined : (config.bookmarkManager ?? this.defaultExecuteQueryBookmarkManager)
const resultTransformer = (config.resultTransformer ?? resultTransformers.eagerResultTransformer()) as ResultTransformer<T>
const routingConfig: string = config.routing ?? routing.WRITERS

Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/result-transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class ResultTransformers {
*/
mappedResultTransformer <
R = Record, T = { records: R[], keys: string[], summary: ResultSummary }
>(config: { map?: (rec: Record) => R, collect?: (records: R[], summary: ResultSummary, keys: string[]) => T }): ResultTransformer<T> {
>(config: { map?: (rec: Record) => R | undefined, collect?: (records: R[], summary: ResultSummary, keys: string[]) => T }): ResultTransformer<T> {
if (config == null || (config.collect == null && config.map == null)) {
throw newError('Requires a map or/and a collect functions.')
}
Expand All @@ -151,7 +151,10 @@ class ResultTransformers {
},
onNext (record: Record) {
if (config.map != null) {
state.records.push(config.map(record))
const mappedRecord = config.map(record)
if (mappedRecord !== undefined) {
state.records.push(mappedRecord)
}
} else {
state.records.push(record as unknown as R)
}
Expand Down
6 changes: 3 additions & 3 deletions packages/core/test/driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ describe('Driver', () => {
expect(eagerResult).toEqual(expected)
expect(spiedExecute).toBeCalledWith({
resultTransformer: resultTransformers.eagerResultTransformer(),
bookmarkManager: driver?.queryBookmarkManager,
bookmarkManager: driver?.defaultExecuteQueryBookmarkManager,
routing: routing.WRITERS,
database: undefined,
impersonatedUser: undefined
Expand All @@ -361,7 +361,7 @@ describe('Driver', () => {
expect(summary).toEqual(expected.summary)
expect(spiedExecute).toBeCalledWith({
resultTransformer: resultTransformers.eagerResultTransformer(),
bookmarkManager: driver?.queryBookmarkManager,
bookmarkManager: driver?.defaultExecuteQueryBookmarkManager,
routing: routing.WRITERS,
database: undefined,
impersonatedUser: undefined
Expand Down Expand Up @@ -485,7 +485,7 @@ describe('Driver', () => {
return () => {
const defaultConfig = {
resultTransformer: resultTransformers.eagerResultTransformer(),
bookmarkManager: driver?.queryBookmarkManager,
bookmarkManager: driver?.defaultExecuteQueryBookmarkManager,
routing: routing.WRITERS,
database: undefined,
impersonatedUser: undefined
Expand Down
22 changes: 22 additions & 0 deletions packages/core/test/result-transformers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,28 @@ describe('resultTransformers', () => {
expect(collect).toHaveBeenCalledWith(rawRecords.map(rec => new Record(keys, rec)), new ResultSummary(query, params, meta), keys)
})

it('should skip the undefined records', async () => {
const {
rawRecords,
result,
keys
} = scenario()
const map = jest.fn((record) => {
const number = record.get('a') as number
return number < 2 ? undefined : number
})

const transform = resultTransformers.mappedResultTransformer({ map })

const { records: as }: { records: number[] } = await transform(result)

expect(as).toEqual(rawRecords[1][0])
expect(map).toHaveBeenCalledTimes(rawRecords.length)
for (const rawRecord of rawRecords) {
expect(map).toHaveBeenCalledWith(new Record(keys, rawRecord))
}
})

it.each([
undefined,
null,
Expand Down

0 comments on commit 68ce3ff

Please sign in to comment.