Skip to content

Introduce Result, Record and Graph types mapping #1159

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 28 additions & 0 deletions packages/core/src/graph-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
import Integer from './integer'
import { stringify } from './json'
import { Rules, GenericConstructor, as } from './mapping.highlevel'

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

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 @@ -201,6 +211,15 @@ class Relationship<T extends NumberOrInteger = Integer, P extends Properties = P
this.endNodeElementId = _valueOrGetDefault(endNodeElementId, () => end.toString())
}

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 @@ -322,6 +341,15 @@ class UnboundRelationship<T extends NumberOrInteger = Integer, P extends Propert
)
}

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
14 changes: 11 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ import * as types from './types'
import * as json from './json'
import resultTransformers, { ResultTransformer } from './result-transformers'
import * as internal from './internal' // todo: removed afterwards
import { Rule, Rules, mapping } from './mapping.highlevel'
import { RulesFactories } from './mapping.rulesfactories'

/**
* Object containing string constants representing predefined {@link Neo4jError} codes.
Expand Down Expand Up @@ -171,7 +173,9 @@ const forExport = {
notificationCategory,
notificationSeverityLevel,
notificationFilterDisabledCategory,
notificationFilterMinimumSeverityLevel
notificationFilterMinimumSeverityLevel,
RulesFactories,
mapping
}

export {
Expand Down Expand Up @@ -240,7 +244,9 @@ export {
notificationCategory,
notificationSeverityLevel,
notificationFilterDisabledCategory,
notificationFilterMinimumSeverityLevel
notificationFilterMinimumSeverityLevel,
RulesFactories,
mapping
}

export type {
Expand All @@ -265,7 +271,9 @@ export type {
NotificationSeverityLevel,
NotificationFilter,
NotificationFilterDisabledCategory,
NotificationFilterMinimumSeverityLevel
NotificationFilterMinimumSeverityLevel,
Rule,
Rules
}

export default forExport
90 changes: 90 additions & 0 deletions packages/core/src/mapping.highlevel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* 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.
*/
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>

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

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

export const mapping = {
register
}

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 value = gettable.get(rule?.from ?? key)
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