Skip to content

Introduce Result, Record and Graph types mapping #1248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 33 commits into
base: 6.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
5c45b0c
Introduce Result, Record and Graph types mappping
bigmontz Nov 8, 2023
0a93a37
Add global registry
bigmontz Nov 8, 2023
918989f
sync deno
bigmontz Nov 8, 2023
d731611
Merge branch '5.0' into 5.x-high-level-hydration
MaxAake Jan 31, 2025
4e8ac68
conflict resolution fix
MaxAake Jan 31, 2025
d71a19c
Documentation additions, import fixes and result.as fix
MaxAake Mar 10, 2025
cf7521d
deno sync
MaxAake Mar 10, 2025
009dd42
mapper undef check
MaxAake Mar 10, 2025
777c392
deno sync
MaxAake Mar 10, 2025
561456d
initialize mapper and add namemapping
MaxAake Mar 11, 2025
26f4e47
deny sync
MaxAake Mar 11, 2025
7ddb87e
improved name mappers
MaxAake Mar 12, 2025
65d2c56
deno sync
MaxAake Mar 12, 2025
f2c8d42
simplify name translations
MaxAake Mar 14, 2025
3ce02e6
deno sync
MaxAake Mar 14, 2025
281c5cd
name mapping documentation
MaxAake Mar 17, 2025
3116c63
deno sync
MaxAake Mar 17, 2025
11a74c4
example test and fix to convention namecheck
MaxAake Mar 17, 2025
0a83829
license header
MaxAake Mar 17, 2025
6af3a90
unit tests for mapping
MaxAake Mar 18, 2025
b8b037e
integration test expansion
MaxAake Mar 24, 2025
28df189
small doc changes and renaming hydrated
MaxAake Mar 26, 2025
eafc641
remove global exports of mapping functions
MaxAake Mar 26, 2025
b407c47
hydrated renamed in examples testing
MaxAake Mar 28, 2025
5ec4d1f
Refactored Results
MaxAake Apr 1, 2025
fba3a09
deno sync
MaxAake Apr 1, 2025
d97edde
renaming and exporting fix
MaxAake Apr 10, 2025
2d1c162
deno
MaxAake Apr 10, 2025
0c37551
small naming conventions refactor
MaxAake Apr 15, 2025
022b03a
deno sync
MaxAake Apr 15, 2025
9cdcc62
rename the mapping object for clarity
MaxAake Apr 16, 2025
509d020
improved ROM object documentation
MaxAake Apr 22, 2025
00a68ea
deno sync
MaxAake Apr 22, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions packages/core/src/graph-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
import Integer from './integer'
import { stringify } from './json'
import { Rules, GenericConstructor, as } from './mapping.highlevel'

type StandardDate = Date
/**
Expand Down Expand Up @@ -82,6 +83,22 @@ class Node<T extends NumberOrInteger = Integer, P extends Properties = Propertie
this.elementId = _valueOrGetDefault(elementId, () => identity.toString())
}

/**
* Hydrates an object of a given type with the properties of the node
*
* @param {GenericConstructor<T> | Rules} constructorOrRules Contructor for the desired type or {@link Rules} for the hydration
* @param {Rules} [rules] {@link Rules} for the hydration
* @returns {T}
*/
as <T extends {} = Object>(rules: Rules): T
as <T extends {} = Object>(genericConstructor: GenericConstructor<T>): T
as <T extends {} = Object>(genericConstructor: GenericConstructor<T>, rules?: Rules): T
as <T extends {} = Object>(constructorOrRules: GenericConstructor<T> | Rules, rules?: Rules): T {
return as({
get: (key) => this.properties[key]
}, constructorOrRules, rules)
}

/**
* @ignore
*/
Expand Down Expand Up @@ -199,6 +216,22 @@ class Relationship<T extends NumberOrInteger = Integer, P extends Properties = P
this.endNodeElementId = _valueOrGetDefault(endNodeElementId, () => end.toString())
}

/**
* Hydrates an object of a given type with the properties of the relationship
*
* @param {GenericConstructor<T> | Rules} constructorOrRules Contructor for the desired type or {@link Rules} for the hydration
* @param {Rules} [rules] {@link Rules} for the hydration
* @returns {T}
*/
as <T extends {} = Object>(rules: Rules): T
as <T extends {} = Object>(genericConstructor: GenericConstructor<T>): T
as <T extends {} = Object>(genericConstructor: GenericConstructor<T>, rules?: Rules): T
as <T extends {} = Object>(constructorOrRules: GenericConstructor<T> | Rules, rules?: Rules): T {
return as({
get: (key) => this.properties[key]
}, constructorOrRules, rules)
}

/**
* @ignore
*/
Expand Down Expand Up @@ -320,6 +353,22 @@ class UnboundRelationship<T extends NumberOrInteger = Integer, P extends Propert
)
}

/**
* Hydrates an object of a given type with the properties of the relationship
*
* @param {GenericConstructor<T> | Rules} constructorOrRules Contructor for the desired type or {@link Rules} for the hydration
* @param {Rules} [rules] {@link Rules} for the hydration
* @returns {T}
*/
as <T extends {} = Object>(rules: Rules): T
as <T extends {} = Object>(genericConstructor: GenericConstructor<T>): T
as <T extends {} = Object>(genericConstructor: GenericConstructor<T>, rules?: Rules): T
as <T extends {} = Object>(constructorOrRules: GenericConstructor<T> | Rules, rules?: Rules): T {
return as({
get: (key) => this.properties[key]
}, constructorOrRules, rules)
}

/**
* @ignore
*/
Expand Down
20 changes: 16 additions & 4 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ import NotificationFilter, {
notificationFilterMinimumSeverityLevel,
NotificationFilterMinimumSeverityLevel
} from './notification-filter'
import Result, { QueryResult, ResultObserver } from './result'
import Result, { MappedQueryResult, QueryResult, ResultObserver } from './result'
import EagerResult from './result-eager'
import ConnectionProvider, { Releasable } from './connection-provider'
import Connection from './connection'
Expand All @@ -101,6 +101,9 @@ import * as json from './json'
import resultTransformers, { ResultTransformer } from './result-transformers'
import ClientCertificate, { clientCertificateProviders, ClientCertificateProvider, ClientCertificateProviders, RotatingClientCertificateProvider, resolveCertificateProvider } from './client-certificate'
import * as internal from './internal' // todo: removed afterwards
import { StandardCase } from './mapping.nameconventions'
import { Rule, Rules, RecordObjectMapping } from './mapping.highlevel'
import { RulesFactories } from './mapping.rulesfactories'

/**
* Object containing string constants representing predefined {@link Neo4jError} codes.
Expand Down Expand Up @@ -186,7 +189,10 @@ const forExport = {
notificationFilterDisabledClassification,
notificationFilterMinimumSeverityLevel,
clientCertificateProviders,
resolveCertificateProvider
resolveCertificateProvider,
RulesFactories,
RecordObjectMapping,
StandardCase
}

export {
Expand Down Expand Up @@ -263,14 +269,18 @@ export {
notificationFilterDisabledClassification,
notificationFilterMinimumSeverityLevel,
clientCertificateProviders,
resolveCertificateProvider
resolveCertificateProvider,
RulesFactories,
RecordObjectMapping,
StandardCase
}

export type {
StandardDate,
NumberOrInteger,
NotificationPosition,
QueryResult,
MappedQueryResult,
ResultObserver,
TransactionConfig,
BookmarkManager,
Expand All @@ -294,7 +304,9 @@ export type {
ClientCertificate,
ClientCertificateProvider,
ClientCertificateProviders,
RotatingClientCertificateProvider
RotatingClientCertificateProvider,
Rule,
Rules
}

export default forExport
3 changes: 2 additions & 1 deletion packages/core/src/internal/observers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import Record from '../record'
import { GenericResultObserver } from '../result'
import ResultSummary from '../result-summary'

interface StreamObserver {
Expand Down Expand Up @@ -115,7 +116,7 @@ export interface ResultStreamObserver extends StreamObserver {
* @param {function(metadata: Object)} observer.onCompleted - Handle stream tail, the summary.
* @param {function(error: Object)} observer.onError - Handle errors, should always be provided.
*/
subscribe: (observer: ResultObserver) => void
subscribe: (observer: GenericResultObserver<any>) => void
}

export class CompletedObserver implements ResultStreamObserver {
Expand Down
189 changes: 189 additions & 0 deletions packages/core/src/mapping.highlevel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/**
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { newError } from './error'
import { nameConventions, StandardCase } from './mapping.nameconventions'

/**
* constructor function of any class
*/
export type GenericConstructor<T extends {}> = new (...args: any[]) => T

export interface Rule {
optional?: boolean
from?: string
convert?: (recordValue: any, field: string) => any
validate?: (recordValue: any, field: string) => void
}

export type Rules = Record<string, Rule>

let rulesRegistry: Record<string, Rules> = {}

let nameMapping: (name: string) => string = (name) => name

function register <T extends {} = Object> (constructor: GenericConstructor<T>, rules: Rules): void {
rulesRegistry[constructor.toString()] = rules
}

function clearMappingRegistry (): void {
rulesRegistry = {}
}

function translateIdentifiers (translationFunction: (name: string) => string): void {
nameMapping = translationFunction
}

function getCaseTranslator (databaseConvention: string | StandardCase, codeConvention: string | StandardCase): ((name: string) => string) {
const keys = Object.keys(nameConventions)
if (!keys.includes(databaseConvention)) {
throw newError(
`Naming convention ${databaseConvention} is not recognized,
please provide a recognized name convention or manually provide a translation function.`
)
}
if (!keys.includes(codeConvention)) {
throw newError(
`Naming convention ${codeConvention} is not recognized,
please provide a recognized name convention or manually provide a translation function.`
)
}
// @ts-expect-error
return (name: string) => nameConventions[databaseConvention].encode(nameConventions[codeConvention].tokenize(name))
}

export const RecordObjectMapping = Object.freeze({
/**
* Clears all registered type mappings from the record object mapping registry.
* @experimental
*/
clearMappingRegistry,
/**
* Creates a translation frunction from record key names to object property names, for use with the {@link translateIdentifiers} function
*
* Recognized naming conventions are "camelCase", "PascalCase", "snake_case", "kebab-case", "SCREAMING_SNAKE_CASE"
*
* @experimental
* @param {string} databaseConvention The naming convention in use in database result Records
* @param {string} codeConvention The naming convention in use in JavaScript object properties
* @returns {function} translation function
*/
getCaseTranslator,
/**
* Registers a set of {@link Rules} to be used by {@link hydrated} for the provided class when no other rules are specified. This registry exists in global memory, not the driver instance.
*
* @example
* // The following code:
* const summary = await driver.executeQuery('CREATE (p:Person{ name: $name }) RETURN p', { name: 'Person1'}, {
* resultTransformer: neo4j.resultTransformers.hydrated(Person, personClassRules)
* })
*
* can instead be written:
* neo4j.RecordObjectMapping.register(Person, personClassRules)
*
* const summary = await driver.executeQuery('CREATE (p:Person{ name: $name }) RETURN p', { name: 'Person1'}, {
* resultTransformer: neo4j.resultTransformers.hydrated(Person)
* })
*
* @experimental
* @param {GenericConstructor} constructor The constructor function of the class to set rules for
* @param {Rules} rules The rules to set for the provided class
*/
register,
/**
* Sets a default name translation from record keys to object properties.
* If providing a function, provide a function that maps FROM your object properties names TO record key names.
*
* The function getCaseTranslator can be used to provide a prewritten translation function between some common naming conventions.
*
* @example
* //if the keys on records from the database are in ALLCAPS
* RecordObjectMapping.translateIdentifiers((name) => name.toUpperCase())
*
* //if you utilize PacalCase in the database and camelCase in JavaScript code.
* RecordObjectMapping.translateIdentifiers(mapping.getCaseTranslator("PascalCase", "camelCase"))
*
* //if a type has one odd mapping you can override the translation with the rule
* const personRules = {
* firstName: neo4j.RulesFactories.asString(),
* bornAt: neo4j.RulesFactories.asNumber({ acceptBigInt: true, optional: true })
* weird_name-property: neo4j.RulesFactories.asString({from: 'homeTown'})
* }
* //These rules can then be used by providing them to a hydratedResultsMapper
* record.as<Person>(personRules)
* //or by registering them to the mapping registry
* RecordObjectMapping.register(Person, personRules)
*
* @experimental
* @param {function} translationFunction A function translating the names of your JS object property names to record key names
*/
translateIdentifiers
})

interface Gettable { get: <V>(key: string) => V }

export function as <T extends {} = Object> (gettable: Gettable, constructorOrRules: GenericConstructor<T> | Rules, rules?: Rules): T {
const GenericConstructor = typeof constructorOrRules === 'function' ? constructorOrRules : Object
const theRules = getRules(constructorOrRules, rules)
const vistedKeys: string[] = []

const obj = new GenericConstructor()

for (const [key, rule] of Object.entries(theRules ?? {})) {
vistedKeys.push(key)
_apply(gettable, obj, key, rule)
}

for (const key of Object.getOwnPropertyNames(obj)) {
if (!vistedKeys.includes(key)) {
_apply(gettable, obj, key, theRules?.[key])
}
}

return obj as unknown as T
}

function _apply<T extends {}> (gettable: Gettable, obj: T, key: string, rule?: Rule): void {
const mappedkey = nameMapping(key)
const value = gettable.get(rule?.from ?? mappedkey)
const field = `${obj.constructor.name}#${key}`
const processedValue = valueAs(value, field, rule)
// @ts-expect-error
obj[key] = processedValue ?? obj[key]
}

export function valueAs (value: unknown, field: string, rule?: Rule): unknown {
if (rule?.optional === true && value == null) {
return value
}

if (typeof rule?.validate === 'function') {
rule.validate(value, field)
}

return ((rule?.convert) != null) ? rule.convert(value, field) : value
}
function getRules<T extends {} = Object> (constructorOrRules: Rules | GenericConstructor<T>, rules: Rules | undefined): Rules | undefined {
const rulesDefined = typeof constructorOrRules === 'object' ? constructorOrRules : rules
if (rulesDefined != null) {
return rulesDefined
}

return typeof constructorOrRules !== 'object' ? rulesRegistry[constructorOrRules.toString()] : undefined
}
Loading