Skip to content

Commit

Permalink
Export RecordShape type (#1117)
Browse files Browse the repository at this point in the history
This type is the base type used to define the shape of the Record. Exporting this type enables user to use it for defining custom generic function to work with result and records without have to redefine the shape type.

The type was renamed from `Dict` to `RecordShape` for having a more meaningful name for external consumers.
  • Loading branch information
bigmontz authored Jul 27, 2023
1 parent abae164 commit 865c085
Show file tree
Hide file tree
Showing 22 changed files with 108 additions and 83 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import {
PathSegment,
isPathSegment
} from './graph-types'
import Record from './record'
import Record, { RecordShape } from './record'
import { isPoint, Point } from './spatial-types'
import ResultSummary, {
queryType,
Expand Down Expand Up @@ -259,6 +259,7 @@ export type {
SessionConfig,
QueryConfig,
RoutingControl,
RecordShape,
ResultTransformer,
NotificationCategory,
NotificationSeverityLevel,
Expand Down
18 changes: 9 additions & 9 deletions packages/core/src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@

import { newError } from './error'

type Dict<Key extends PropertyKey = PropertyKey, Value = any> = {
type RecordShape<Key extends PropertyKey = PropertyKey, Value = any> = {
[K in Key]: Value
}

type Visitor<
Entries extends Dict = Dict,
Entries extends RecordShape = RecordShape,
Key extends keyof Entries = keyof Entries
> = MapVisitor<void, Entries, Key>

type MapVisitor<
ReturnType,
Entries extends Dict = Dict,
Entries extends RecordShape = RecordShape,
Key extends keyof Entries = keyof Entries
> = (value: Entries[Key], key: Key, record: Record<Entries>) => ReturnType

function generateFieldLookup<
Entries extends Dict = Dict,
Entries extends RecordShape = RecordShape,
Key extends keyof Entries = keyof Entries,
FieldLookup extends Dict<string, number> = Dict<string, number>
FieldLookup extends RecordShape<string, number> = RecordShape<string, number>
> (keys: Key[]): FieldLookup {
const lookup: Dict<string, number> = {}
const lookup: RecordShape<string, number> = {}
keys.forEach((name, idx) => {
lookup[name as string] = idx
})
Expand All @@ -66,9 +66,9 @@ function generateFieldLookup<
* @access public
*/
class Record<
Entries extends Dict = Dict,
Entries extends RecordShape = RecordShape,
Key extends keyof Entries = keyof Entries,
FieldLookup extends Dict<keyof Entries, number> = Dict<keyof Entries, number>
FieldLookup extends RecordShape<keyof Entries, number> = RecordShape<keyof Entries, number>
> {
keys: Key[]
length: number
Expand Down Expand Up @@ -240,4 +240,4 @@ class Record<
}

export default Record
export type { Dict }
export type { RecordShape }
4 changes: 2 additions & 2 deletions packages/core/src/result-eager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
* limitations under the License.
*/

import Record, { Dict } from './record'
import Record, { RecordShape } from './record'
import ResultSummary from './result-summary'

/**
* Represents the fully streamed result
*/
export default class EagerResult<Entries extends Dict = Dict> {
export default class EagerResult<Entries extends RecordShape = RecordShape> {
keys: string[]
records: Array<Record<Entries>>
summary: ResultSummary
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/result-transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
* limitations under the License.
*/

import Record, { Dict } from './record'
import Record, { RecordShape } from './record'
import Result from './result'
import EagerResult from './result-eager'
import ResultSummary from './result-summary'
import { newError } from './error'

async function createEagerResultFromResult<Entries extends Dict> (result: Result): Promise<EagerResult<Entries>> {
async function createEagerResultFromResult<Entries extends RecordShape> (result: Result): Promise<EagerResult<Entries>> {
const { summary, records } = await result
const keys = await result.keys()
return new EagerResult<Entries>(keys, records, summary)
Expand Down Expand Up @@ -59,7 +59,7 @@ class ResultTransformers {
*
* @returns {ResultTransformer<EagerResult<Entries>>} The result transformer
*/
eagerResultTransformer<Entries extends Dict = Dict>(): ResultTransformer<EagerResult<Entries>> {
eagerResultTransformer<Entries extends RecordShape = RecordShape>(): ResultTransformer<EagerResult<Entries>> {
return createEagerResultFromResult
}

Expand Down
34 changes: 17 additions & 17 deletions packages/core/src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/* eslint-disable @typescript-eslint/promise-function-async */

import ResultSummary from './result-summary'
import Record, { Dict } from './record'
import Record, { RecordShape } from './record'
import { Query, PeekableAsyncIterator } from './types'
import { observer, util, connectionHolder } from './internal'
import { newError, PROTOCOL_ERROR } from './error'
Expand Down Expand Up @@ -56,16 +56,16 @@ const DEFAULT_ON_KEYS = (keys: string[]): void => {}
* The query result is the combination of the {@link ResultSummary} and
* the array {@link Record[]} produced by the query
*/
interface QueryResult<RecordShape extends Dict = Dict> {
records: Array<Record<RecordShape>>
interface QueryResult<R extends RecordShape = RecordShape> {
records: Array<Record<R>>
summary: ResultSummary
}

/**
* Interface to observe updates on the Result which is being produced.
*
*/
interface ResultObserver<RecordShape extends Dict =Dict> {
interface ResultObserver<R extends RecordShape = RecordShape> {
/**
* Receive the keys present on the record whenever this information is available
*
Expand All @@ -77,7 +77,7 @@ interface ResultObserver<RecordShape extends Dict =Dict> {
* Receive the each record present on the {@link @Result}
* @param {Record} record The {@link Record} produced
*/
onNext?: (record: Record<RecordShape>) => void
onNext?: (record: Record<R>) => void

/**
* Called when the result is fully received
Expand All @@ -86,7 +86,7 @@ interface ResultObserver<RecordShape extends Dict =Dict> {
onCompleted?: (summary: ResultSummary) => void

/**
* Called when some error occurs during the result proccess or query execution
* Called when some error occurs during the result processing or query execution
* @param {Error} error The error ocurred
*/
onError?: (error: Error) => void
Expand All @@ -111,7 +111,7 @@ interface QueuedResultObserver extends ResultObserver {
* Alternatively can be consumed lazily using {@link Result#subscribe} function.
* @access public
*/
class Result<RecordShape extends Dict = Dict> implements Promise<QueryResult<RecordShape>> {
class Result<R extends RecordShape = RecordShape> implements Promise<QueryResult<R>> {
private readonly _stack: string | null
private readonly _streamObserverPromise: Promise<observer.ResultStreamObserver>
private _p: Promise<QueryResult> | null
Expand Down Expand Up @@ -212,12 +212,12 @@ class Result<RecordShape extends Dict = Dict> implements Promise<QueryResult<Rec
* @private
* @return {Promise} new Promise.
*/
private _getOrCreatePromise (): Promise<QueryResult<RecordShape>> {
private _getOrCreatePromise (): Promise<QueryResult<R>> {
if (this._p == null) {
this._p = new Promise((resolve, reject) => {
const records: Record[] = []
const records: Array<Record<R>> = []
const observer = {
onNext: (record: Record) => {
onNext: (record: Record<R>) => {
records.push(record)
},
onCompleted: (summary: ResultSummary) => {
Expand All @@ -240,9 +240,9 @@ class Result<RecordShape extends Dict = Dict> implements Promise<QueryResult<Rec
* *Should not be combined with {@link Result#subscribe} or ${@link Result#then} functions.*
*
* @public
* @returns {PeekableAsyncIterator<Record<RecordShape>, ResultSummary>} The async iterator for the Results
* @returns {PeekableAsyncIterator<Record<R>, ResultSummary>} The async iterator for the Results
*/
[Symbol.asyncIterator] (): PeekableAsyncIterator<Record<RecordShape>, ResultSummary> {
[Symbol.asyncIterator] (): PeekableAsyncIterator<Record<R>, ResultSummary> {
if (!this.isOpen()) {
const error = newError('Result is already consumed')
return {
Expand Down Expand Up @@ -345,9 +345,9 @@ class Result<RecordShape extends Dict = Dict> implements Promise<QueryResult<Rec
* @param {function(error: {message:string, code:string})} onRejected - function to be called upon errors.
* @return {Promise} promise.
*/
then<TResult1 = QueryResult<RecordShape>, TResult2 = never>(
then<TResult1 = QueryResult<R>, TResult2 = never>(
onFulfilled?:
| ((value: QueryResult<RecordShape>) => TResult1 | PromiseLike<TResult1>)
| ((value: QueryResult<R>) => TResult1 | PromiseLike<TResult1>)
| null,
onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
): Promise<TResult1 | TResult2> {
Expand All @@ -364,7 +364,7 @@ class Result<RecordShape extends Dict = Dict> implements Promise<QueryResult<Rec
*/
catch <TResult = never>(
onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null
): Promise<QueryResult<RecordShape> | TResult> {
): Promise<QueryResult<R> | TResult> {
return this._getOrCreatePromise().catch(onRejected)
}

Expand All @@ -376,7 +376,7 @@ class Result<RecordShape extends Dict = Dict> implements Promise<QueryResult<Rec
* @return {Promise} promise.
*/
[Symbol.toStringTag]: string
finally (onfinally?: (() => void) | null): Promise<QueryResult<RecordShape>> {
finally (onfinally?: (() => void) | null): Promise<QueryResult<R>> {
return this._getOrCreatePromise().finally(onfinally)
}

Expand All @@ -391,7 +391,7 @@ class Result<RecordShape extends Dict = Dict> implements Promise<QueryResult<Rec
* @param {function(error: {message:string, code:string})} observer.onError - handle errors.
* @return {void}
*/
subscribe (observer: ResultObserver<RecordShape>): void {
subscribe (observer: ResultObserver<R>): void {
this._subscribe(observer)
.catch(() => {})
}
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { NumberOrInteger } from './graph-types'
import TransactionPromise from './transaction-promise'
import ManagedTransaction from './transaction-managed'
import BookmarkManager from './bookmark-manager'
import { Dict } from './record'
import { RecordShape } from './record'
import NotificationFilter from './notification-filter'
import { Logger } from './internal/logger'

Expand Down Expand Up @@ -171,11 +171,11 @@ class Session {
* @param {TransactionConfig} [transactionConfig] - Configuration for the new auto-commit transaction.
* @return {Result} New Result.
*/
run<RecordShape extends Dict = Dict> (
run<R extends RecordShape = RecordShape> (
query: Query,
parameters?: any,
transactionConfig?: TransactionConfig
): Result<RecordShape> {
): Result<R> {
const { validatedQuery, params } = validateQueryAndParameters(
query,
parameters
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/transaction-managed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import Result from './result'
import Transaction from './transaction'
import { Query } from './types'
import { Dict } from './record'
import { RecordShape } from './record'

type Run = (query: Query, parameters?: any) => Result
type Run<R extends RecordShape = RecordShape> = (query: Query, parameters?: any) => Result<R>

/**
* Represents a transaction that is managed by the transaction executor.
Expand Down Expand Up @@ -61,7 +61,7 @@ class ManagedTransaction {
* @param {Object} parameters - Map with parameters to use in query
* @return {Result} New Result
*/
run<RecordShape extends Dict =Dict> (query: Query, parameters?: any): Result<RecordShape> {
run<R extends RecordShape = RecordShape> (query: Query, parameters?: any): Result<R> {
return this._run(query, parameters)
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
import { newError } from './error'
import Result from './result'
import { Query } from './types'
import { Dict } from './record'
import { RecordShape } from './record'
import NotificationFilter from './notification-filter'

/**
Expand Down Expand Up @@ -183,7 +183,7 @@ class Transaction {
* @param {Object} parameters - Map with parameters to use in query
* @return {Result} New Result
*/
run<RecordShape extends Dict = Dict> (query: Query, parameters?: any): Result<RecordShape> {
run<R extends RecordShape = RecordShape> (query: Query, parameters?: any): Result<R> {
const { validatedQuery, params } = validateQueryAndParameters(
query,
parameters
Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import QueryExecutor from '../src/internal/query-executor'
import { ConfiguredCustomResolver } from '../src/internal/resolver'
import { LogLevel } from '../src/types'
import resultTransformers from '../src/result-transformers'
import Record, { Dict } from '../src/record'
import Record, { RecordShape } from '../src/record'
import { validNotificationFilters } from './utils/notification-filters.fixtures'

describe('Driver', () => {
Expand Down Expand Up @@ -543,7 +543,7 @@ describe('Driver', () => {
await expect(output).rejects.toThrow(expectedError)
})

function extendsDefaultWith<T = EagerResult<Dict>> (config: QueryConfig<T>) {
function extendsDefaultWith<T = EagerResult<RecordShape>> (config: QueryConfig<T>) {
return () => {
const defaultConfig = {
resultTransformer: resultTransformers.eagerResultTransformer(),
Expand Down
3 changes: 2 additions & 1 deletion packages/neo4j-driver-deno/lib/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import {
PathSegment,
isPathSegment
} from './graph-types.ts'
import Record from './record.ts'
import Record, { RecordShape } from './record.ts'
import { isPoint, Point } from './spatial-types.ts'
import ResultSummary, {
queryType,
Expand Down Expand Up @@ -259,6 +259,7 @@ export type {
SessionConfig,
QueryConfig,
RoutingControl,
RecordShape,
ResultTransformer,
NotificationCategory,
NotificationSeverityLevel,
Expand Down
18 changes: 9 additions & 9 deletions packages/neo4j-driver-deno/lib/core/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@

import { newError } from './error.ts'

type Dict<Key extends PropertyKey = PropertyKey, Value = any> = {
type RecordShape<Key extends PropertyKey = PropertyKey, Value = any> = {
[K in Key]: Value
}

type Visitor<
Entries extends Dict = Dict,
Entries extends RecordShape = RecordShape,
Key extends keyof Entries = keyof Entries
> = MapVisitor<void, Entries, Key>

type MapVisitor<
ReturnType,
Entries extends Dict = Dict,
Entries extends RecordShape = RecordShape,
Key extends keyof Entries = keyof Entries
> = (value: Entries[Key], key: Key, record: Record<Entries>) => ReturnType

function generateFieldLookup<
Entries extends Dict = Dict,
Entries extends RecordShape = RecordShape,
Key extends keyof Entries = keyof Entries,
FieldLookup extends Dict<string, number> = Dict<string, number>
FieldLookup extends RecordShape<string, number> = RecordShape<string, number>
> (keys: Key[]): FieldLookup {
const lookup: Dict<string, number> = {}
const lookup: RecordShape<string, number> = {}
keys.forEach((name, idx) => {
lookup[name as string] = idx
})
Expand All @@ -66,9 +66,9 @@ function generateFieldLookup<
* @access public
*/
class Record<
Entries extends Dict = Dict,
Entries extends RecordShape = RecordShape,
Key extends keyof Entries = keyof Entries,
FieldLookup extends Dict<keyof Entries, number> = Dict<keyof Entries, number>
FieldLookup extends RecordShape<keyof Entries, number> = RecordShape<keyof Entries, number>
> {
keys: Key[]
length: number
Expand Down Expand Up @@ -240,4 +240,4 @@ class Record<
}

export default Record
export type { Dict }
export type { RecordShape }
4 changes: 2 additions & 2 deletions packages/neo4j-driver-deno/lib/core/result-eager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
* limitations under the License.
*/

import Record, { Dict } from './record.ts'
import Record, { RecordShape } from './record.ts'
import ResultSummary from './result-summary.ts'

/**
* Represents the fully streamed result
*/
export default class EagerResult<Entries extends Dict = Dict> {
export default class EagerResult<Entries extends RecordShape = RecordShape> {
keys: string[]
records: Array<Record<Entries>>
summary: ResultSummary
Expand Down
Loading

0 comments on commit 865c085

Please sign in to comment.