Skip to content

Commit 4c30bfb

Browse files
authored
Preview: Introduce Result, Record and Graph types mapping (#1248)
1 parent 5c324a2 commit 4c30bfb

31 files changed

+3068
-140
lines changed

package-lock.json

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"private": true,
44
"type": "module",
55
"devDependencies": {
6+
"@types/node": "^24.0.10",
67
"husky": "^8.0.3",
78
"lerna": "^4.0.0",
89
"lint-staged": "^14.0.0",

packages/core/src/graph-types.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
import Integer from './integer'
1818
import { stringify } from './json'
19+
import { Rules, GenericConstructor, as } from './mapping.highlevel'
1920

2021
type StandardDate = Date
2122
/**
@@ -82,6 +83,24 @@ class Node<T extends NumberOrInteger = Integer, P extends Properties = Propertie
8283
this.elementId = _valueOrGetDefault(elementId, () => identity.toString())
8384
}
8485

86+
/**
87+
* Hydrates an object of a given type with the properties of the node
88+
*
89+
* @param {GenericConstructor<T> | Rules} constructorOrRules Constructor for the desired type or {@link Rules} for the hydration
90+
* @param {Rules} [rules] {@link Rules} for the hydration
91+
* @returns {T}
92+
*
93+
* @experimental Part of the Record Object Mapping preview feature
94+
*/
95+
as <T extends {} = Object>(rules: Rules): T
96+
as <T extends {} = Object>(genericConstructor: GenericConstructor<T>): T
97+
as <T extends {} = Object>(genericConstructor: GenericConstructor<T>, rules?: Rules): T
98+
as <T extends {} = Object>(constructorOrRules: GenericConstructor<T> | Rules, rules?: Rules): T {
99+
return as({
100+
get: (key) => this.properties[key]
101+
}, constructorOrRules, rules)
102+
}
103+
85104
/**
86105
* @ignore
87106
*/
@@ -199,6 +218,24 @@ class Relationship<T extends NumberOrInteger = Integer, P extends Properties = P
199218
this.endNodeElementId = _valueOrGetDefault(endNodeElementId, () => end.toString())
200219
}
201220

221+
/**
222+
* Hydrates an object of a given type with the properties of the relationship
223+
*
224+
* @param {GenericConstructor<T> | Rules} constructorOrRules Constructor for the desired type or {@link Rules} for the hydration
225+
* @param {Rules} [rules] {@link Rules} for the hydration
226+
* @returns {T}
227+
*
228+
* @experimental Part of the Record Object Mapping preview feature
229+
*/
230+
as <T extends {} = Object>(rules: Rules): T
231+
as <T extends {} = Object>(genericConstructor: GenericConstructor<T>): T
232+
as <T extends {} = Object>(genericConstructor: GenericConstructor<T>, rules?: Rules): T
233+
as <T extends {} = Object>(constructorOrRules: GenericConstructor<T> | Rules, rules?: Rules): T {
234+
return as({
235+
get: (key) => this.properties[key]
236+
}, constructorOrRules, rules)
237+
}
238+
202239
/**
203240
* @ignore
204241
*/
@@ -320,6 +357,24 @@ class UnboundRelationship<T extends NumberOrInteger = Integer, P extends Propert
320357
)
321358
}
322359

360+
/**
361+
* Hydrates an object of a given type with the properties of the relationship
362+
*
363+
* @param {GenericConstructor<T> | Rules} constructorOrRules Constructor for the desired type or {@link Rules} for the hydration
364+
* @param {Rules} [rules] {@link Rules} for the hydration
365+
* @returns {T}
366+
*
367+
* @experimental Part of the Record Object Mapping preview feature
368+
*/
369+
as <T extends {} = Object>(rules: Rules): T
370+
as <T extends {} = Object>(genericConstructor: GenericConstructor<T>): T
371+
as <T extends {} = Object>(genericConstructor: GenericConstructor<T>, rules?: Rules): T
372+
as <T extends {} = Object>(constructorOrRules: GenericConstructor<T> | Rules, rules?: Rules): T {
373+
return as({
374+
get: (key) => this.properties[key]
375+
}, constructorOrRules, rules)
376+
}
377+
323378
/**
324379
* @ignore
325380
*/

packages/core/src/index.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ import NotificationFilter, {
8383
notificationFilterMinimumSeverityLevel,
8484
NotificationFilterMinimumSeverityLevel
8585
} from './notification-filter'
86-
import Result, { QueryResult, ResultObserver } from './result'
86+
import Result, { MappedQueryResult, QueryResult, ResultObserver } from './result'
8787
import EagerResult from './result-eager'
8888
import ConnectionProvider, { Releasable } from './connection-provider'
8989
import Connection from './connection'
@@ -103,6 +103,10 @@ import resultTransformers, { ResultTransformer } from './result-transformers'
103103
import ClientCertificate, { clientCertificateProviders, ClientCertificateProvider, ClientCertificateProviders, RotatingClientCertificateProvider, resolveCertificateProvider } from './client-certificate'
104104
import * as internal from './internal' // todo: removed afterwards
105105
import Vector, { VectorType, vector, isVector } from './vector'
106+
import { StandardCase } from './mapping.nameconventions'
107+
import { Rule, Rules, RecordObjectMapping } from './mapping.highlevel'
108+
import { rule } from './mapping.rulesfactories'
109+
import mappingDecorators from './mapping.decorators'
106110
import UnsupportedType, { isUnsupportedType } from './unsupported-type'
107111

108112
/**
@@ -191,6 +195,10 @@ const forExport = {
191195
notificationFilterMinimumSeverityLevel,
192196
clientCertificateProviders,
193197
resolveCertificateProvider,
198+
rule,
199+
mappingDecorators,
200+
RecordObjectMapping,
201+
StandardCase,
194202
UnsupportedType,
195203
isUnsupportedType,
196204
isVector,
@@ -275,16 +283,21 @@ export {
275283
resolveCertificateProvider,
276284
isVector,
277285
Vector,
286+
vector,
287+
rule,
288+
mappingDecorators,
289+
RecordObjectMapping,
290+
StandardCase,
278291
UnsupportedType,
279292
isUnsupportedType,
280-
vector
281293
}
282294

283295
export type {
284296
StandardDate,
285297
NumberOrInteger,
286298
NotificationPosition,
287299
QueryResult,
300+
MappedQueryResult,
288301
ResultObserver,
289302
TransactionConfig,
290303
BookmarkManager,
@@ -309,7 +322,9 @@ export type {
309322
ClientCertificateProvider,
310323
ClientCertificateProviders,
311324
RotatingClientCertificateProvider,
312-
VectorType
325+
VectorType,
326+
Rule,
327+
Rules
313328
}
314329

315330
export default forExport

packages/core/src/internal/observers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
import Record from '../record'
19+
import { GenericResultObserver } from '../result'
1920
import ResultSummary from '../result-summary'
2021

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

121122
export class CompletedObserver implements ResultStreamObserver {

0 commit comments

Comments
 (0)