Skip to content

Commit 433aea0

Browse files
committed
rename rulefactory object, introduce mapping decorators
1 parent 00a68ea commit 433aea0

22 files changed

+551
-132
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: 2 additions & 1 deletion
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",
@@ -40,7 +41,7 @@
4041
"scripts": {
4142
"clean": "lerna clean -y && lerna run clean",
4243
"build": "lerna bootstrap --ci",
43-
"set_version::deno": "lerna run set_version --scope neo4j-driver-deno --stream -- ",
44+
"set_version::deno": "lerna run set_version --scope neo4j-driver-deno --stream -- ",
4445
"build::deno": "lerna run build --scope neo4j-driver-deno --stream -- ",
4546
"build::notci": "lerna bootstrap",
4647
"docs": "lerna run docs --stream --concurrency 1",

packages/core/src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ import ClientCertificate, { clientCertificateProviders, ClientCertificateProvide
103103
import * as internal from './internal' // todo: removed afterwards
104104
import { StandardCase } from './mapping.nameconventions'
105105
import { Rule, Rules, RecordObjectMapping } from './mapping.highlevel'
106-
import { RulesFactories } from './mapping.rulesfactories'
106+
import { rule } from './mapping.rulesfactories'
107+
import mappingDecorators from './mapping.decorators'
107108

108109
/**
109110
* Object containing string constants representing predefined {@link Neo4jError} codes.
@@ -190,7 +191,8 @@ const forExport = {
190191
notificationFilterMinimumSeverityLevel,
191192
clientCertificateProviders,
192193
resolveCertificateProvider,
193-
RulesFactories,
194+
rule,
195+
mappingDecorators,
194196
RecordObjectMapping,
195197
StandardCase
196198
}
@@ -270,7 +272,8 @@ export {
270272
notificationFilterMinimumSeverityLevel,
271273
clientCertificateProviders,
272274
resolveCertificateProvider,
273-
RulesFactories,
275+
rule,
276+
mappingDecorators,
274277
RecordObjectMapping,
275278
StandardCase
276279
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
import { Rule, rulesRegistry } from './mapping.highlevel'
2+
import { rule } from './mapping.rulesfactories'
3+
4+
function booleanProperty (config?: Rule) {
5+
return (_: any, context: any) => {
6+
context.addInitializer(function () {
7+
const constructorName = this.constructor.name
8+
if (rulesRegistry[constructorName] === undefined) {
9+
rulesRegistry[constructorName] = {}
10+
}
11+
const rules = rulesRegistry[constructorName]
12+
rules[context.name] = rule.asBoolean(config)
13+
})
14+
}
15+
}
16+
17+
function stringProperty (config?: Rule) {
18+
return (_: any, context: any) => {
19+
context.addInitializer(function () {
20+
const constructorName = this.constructor.name
21+
if (rulesRegistry[constructorName] === undefined) {
22+
rulesRegistry[constructorName] = {}
23+
}
24+
const rules = rulesRegistry[constructorName]
25+
rules[context.name] = rule.asString(config)
26+
})
27+
}
28+
}
29+
30+
function numberProperty (config?: Rule & { acceptBigInt?: boolean }) {
31+
return (_: any, context: any) => {
32+
context.addInitializer(function () {
33+
const constructorName = this.constructor.name
34+
if (rulesRegistry[constructorName] === undefined) {
35+
rulesRegistry[constructorName] = {}
36+
}
37+
const rules = rulesRegistry[constructorName]
38+
rules[context.name] = rule.asNumber(config)
39+
})
40+
}
41+
}
42+
43+
function bigIntProperty (config?: Rule & { acceptNumber?: boolean }) {
44+
return (_: any, context: any) => {
45+
context.addInitializer(function () {
46+
const constructorName = this.constructor.name
47+
if (rulesRegistry[constructorName] === undefined) {
48+
rulesRegistry[constructorName] = {}
49+
}
50+
const rules = rulesRegistry[constructorName]
51+
rules[context.name] = rule.asBigInt(config)
52+
})
53+
}
54+
}
55+
56+
function nodeProperty (config?: Rule) {
57+
return (_: any, context: any) => {
58+
context.addInitializer(function () {
59+
const constructorName = this.constructor.name
60+
if (rulesRegistry[constructorName] === undefined) {
61+
rulesRegistry[constructorName] = {}
62+
}
63+
const rules = rulesRegistry[constructorName]
64+
rules[context.name] = rule.asNode(config)
65+
})
66+
}
67+
}
68+
69+
function relationshipProperty (config?: Rule) {
70+
return (_: any, context: any) => {
71+
context.addInitializer(function () {
72+
const constructorName = this.constructor.name
73+
if (rulesRegistry[constructorName] === undefined) {
74+
rulesRegistry[constructorName] = {}
75+
}
76+
const rules = rulesRegistry[constructorName]
77+
rules[context.name] = rule.asRelationship(config)
78+
})
79+
}
80+
}
81+
82+
function pathProperty (config?: Rule) {
83+
return (_: any, context: any) => {
84+
context.addInitializer(function () {
85+
const constructorName = this.constructor.name
86+
if (rulesRegistry[constructorName] === undefined) {
87+
rulesRegistry[constructorName] = {}
88+
}
89+
const rules = rulesRegistry[constructorName]
90+
rules[context.name] = rule.asPath(config)
91+
})
92+
}
93+
}
94+
95+
function pointProperty (config?: Rule) {
96+
return (_: any, context: any) => {
97+
context.addInitializer(function () {
98+
const constructorName = this.constructor.name
99+
if (rulesRegistry[constructorName] === undefined) {
100+
rulesRegistry[constructorName] = {}
101+
}
102+
const rules = rulesRegistry[constructorName]
103+
rules[context.name] = rule.asPoint(config)
104+
})
105+
}
106+
}
107+
108+
function durationProperty (config?: Rule & { stringify?: boolean }) {
109+
return (_: any, context: any) => {
110+
context.addInitializer(function () {
111+
const constructorName = this.constructor.name
112+
if (rulesRegistry[constructorName] === undefined) {
113+
rulesRegistry[constructorName] = {}
114+
}
115+
const rules = rulesRegistry[constructorName]
116+
rules[context.name] = rule.asDuration(config)
117+
})
118+
}
119+
}
120+
121+
function listProperty (config?: Rule & { apply?: Rule }) {
122+
return (_: any, context: any) => {
123+
context.addInitializer(function () {
124+
const constructorName = this.constructor.name
125+
if (rulesRegistry[constructorName] === undefined) {
126+
rulesRegistry[constructorName] = {}
127+
}
128+
const rules = rulesRegistry[constructorName]
129+
rules[context.name] = rule.asList({ apply: { ...rules[context.name] }, ...config })
130+
})
131+
}
132+
}
133+
134+
function optionalProperty () {
135+
return (_: any, context: any) => {
136+
context.addInitializer(function () {
137+
const constructorName = this.constructor.name
138+
if (rulesRegistry[constructorName] === undefined) {
139+
rulesRegistry[constructorName] = {}
140+
}
141+
const rules = rulesRegistry[constructorName]
142+
rules[context.name] = { optional: true, ...rules[context.name] }
143+
})
144+
}
145+
}
146+
147+
function mapPropertyFromName (name: string) {
148+
return (_: any, context: any) => {
149+
context.addInitializer(function () {
150+
const constructorName = this.constructor.name
151+
if (rulesRegistry[constructorName] === undefined) {
152+
rulesRegistry[constructorName] = {}
153+
}
154+
const rules = rulesRegistry[constructorName]
155+
rules[context.name] = { from: name, ...rules[context.name] }
156+
})
157+
}
158+
}
159+
160+
function convertPropertyToType (type: any) {
161+
return (_: any, context: any) => {
162+
context.addInitializer(function () {
163+
const constructorName = this.constructor.name
164+
if (rulesRegistry[constructorName] === undefined) {
165+
rulesRegistry[constructorName] = {}
166+
}
167+
const rules = rulesRegistry[constructorName]
168+
rules[context.name] = { convert: (node) => node.as(type), ...rules[context.name] }
169+
})
170+
}
171+
}
172+
173+
const forExport = {
174+
booleanProperty,
175+
stringProperty,
176+
numberProperty,
177+
bigIntProperty,
178+
nodeProperty,
179+
relationshipProperty,
180+
pathProperty,
181+
pointProperty,
182+
durationProperty,
183+
listProperty,
184+
optionalProperty,
185+
mapPropertyFromName,
186+
convertPropertyToType
187+
}
188+
189+
export default forExport

packages/core/src/mapping.highlevel.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ export interface Rule {
3434

3535
export type Rules = Record<string, Rule>
3636

37-
let rulesRegistry: Record<string, Rules> = {}
37+
export let rulesRegistry: Record<string, Rules> = {}
3838

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

4141
function register <T extends {} = Object> (constructor: GenericConstructor<T>, rules: Rules): void {
42-
rulesRegistry[constructor.toString()] = rules
42+
rulesRegistry[constructor.name] = rules
4343
}
4444

4545
function clearMappingRegistry (): void {
@@ -121,9 +121,9 @@ export const RecordObjectMapping = Object.freeze({
121121
*
122122
* //if a type has one odd mapping you can override the translation with the rule
123123
* const personRules = {
124-
* firstName: neo4j.RulesFactories.asString(),
125-
* bornAt: neo4j.RulesFactories.asNumber({ acceptBigInt: true, optional: true })
126-
* weird_name-property: neo4j.RulesFactories.asString({from: 'homeTown'})
124+
* firstName: neo4j.rule.asString(),
125+
* bornAt: neo4j.rule.asNumber({ acceptBigInt: true, optional: true })
126+
* weird_name-property: neo4j.rule.asString({from: 'homeTown'})
127127
* }
128128
* //These rules can then be used by providing them to a hydratedResultsMapper
129129
* record.as<Person>(personRules)
@@ -184,6 +184,5 @@ function getRules<T extends {} = Object> (constructorOrRules: Rules | GenericCon
184184
if (rulesDefined != null) {
185185
return rulesDefined
186186
}
187-
188-
return typeof constructorOrRules !== 'object' ? rulesRegistry[constructorOrRules.toString()] : undefined
187+
return typeof constructorOrRules !== 'object' ? rulesRegistry[constructorOrRules.name] : undefined
189188
}

0 commit comments

Comments
 (0)