Visitar URL original
Expand Record Object Mapping to allow Parameter Mapping by MaxAake · Pull Request #1362 · neo4j/neo4j-javascript-driver · GitHub
Skip to content

Expand Record Object Mapping to allow Parameter Mapping - #1362

Merged
MaxAake merged 51 commits into
6.xfrom
parameter-mapping
Apr 29, 2026
Merged

MaxAake merged 51 commits into
6.xfrom
parameter-mapping

Conversation

@MaxAake

@MaxAake MaxAake commented Nov 19, 2025 •

Copy link
Copy Markdown
Contributor

closes DRIVERS-107

This PR provides support for typechecking and automatic conversion of parameters, as well as using the object mapping registry to register mapping strategies for classes. This lets users directly pass objects even with problematic properties that can not be sent over bolt (like functions), by automatically converting them or by omitting them from the mapping strategy.

This is the 2nd half of the Record Object Mapping feature, which is also marked stabilized and taken out of preview in this PR.

Examples

class Obj {
    constructor (obj) {
        this.string = obj?.string ?? 'hi'
        this.number = obj?.number ?? 1
        this.bigint = obj?.bigint ?? BigInt(1)
        this.date = obj?.date ?? "2024-01-01"
        this.localDate = obj?.localDate ?? "2024-01-01"
        this.dateTime = obj?.dateTime ?? new neo4j.DateTime(1, 1, 1, 1, 1, 1, 1, 0)._toUTC()
        this.localDateTime = obj?.localDateTime ?? new neo4j.LocalDateTime(1, 1, 1, 1, 1, 1, 1).toString()
        this.duration = obj?.duration ?? "P1DT5.00007S"
        this.time = obj?.time ?? "10:11:12.13Z"
        this.localTime = obj?.localTime ?? "10:11:12.0001"
        this.list = obj?.list ?? ["hi"]
        this.function = () => "function string" // bolt cannot send functions
        this.node = new neo4j.Node("123", [], {}) // nor can it send nodes
    }
}

const session = driver.session()
const rules = {
    number: neo4j.rule.asNumber(),
    string: neo4j.rule.asString(),
    bigint: neo4j.rule.asBigInt({acceptNumber: true}),
    date: neo4j.rule.asDate({stringify: true}), // this will ensure date is stored as a Date in the DB and retrieved as a string 
    localDate: neo4j.rule.asDate({stringify: true}),
    dateTime: neo4j.rule.asDateTime({stringify: true}),
    localDateTime: neo4j.rule.asLocalDateTime({stringify: true}),
    duration: neo4j.rule.asDuration({stringify: true}),
    time: neo4j.rule.asTime({from: "dob", stringify: true}),
    localTime: neo4j.rule.asLocalTime({stringify: true}),
    list: neo4j.rule.asList({ apply: neo4j.rule.asString() }),
} // not including function and node here will make the driver skip sending them. They could alternatively be converted to a bolt-compatible type.

neo4j.RecordObjectMapping.register(Obj, rules)

// This allows us to use camelCase for our object properties and snake_case for the properties on our node in the DB
neo4j.RecordObjectMapping.translateIdentifiers(neo4j.RecordObjectMapping.getCaseTranslator("snake_case", "camelCase"))

session.run(
  'MERGE (n {string: $string, number: $number, bigint: $bigint, date: $date, local_date: $local_date, date_time: $date_time, local_date_time: $local_date_time, duration: $duration, dob: $dob, local_time: $local_time, list: $list}) RETURN n',
  new Obj(),
  {}
).as({n: {convert: (n) => new Obj(n.as(rules))}})
.then((res) => {
    console.log(res.records[0])
    session.close()
    driver.close()
})

@MaxAake
MaxAake marked this pull request as ready for review January 29, 2026 14:33
@robsdedude
robsdedude self-requested a review January 30, 2026 10:03

@robsdedude robsdedude left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review part 1. I'll continue next week with the review. I just want to submit my comments to make sure they don't get lost.

Comment thread packages/core/src/internal/util.ts Resolved
Comment thread packages/core/src/graph-types.ts Resolved
Comment thread packages/core/src/graph-types.ts Resolved
Comment thread packages/core/src/mapping.highlevel.ts Resolved
Comment thread packages/core/src/mapping.highlevel.ts Resolved
Comment thread packages/core/src/session.ts Resolved
Comment thread packages/core/src/mapping.highlevel.ts Resolved
Comment thread packages/core/src/mapping.highlevel.ts Resolved
Comment thread packages/core/src/mapping.highlevel.ts Resolved
Comment thread packages/core/src/mapping.highlevel.ts Resolved
Comment thread packages/core/src/mapping.highlevel.ts Resolved
Comment thread packages/core/src/mapping.highlevel.ts Resolved
Comment thread packages/core/src/temporal-types.ts Resolved
Comment thread packages/core/src/temporal-types.ts Resolved
@MaxAake

MaxAake commented Feb 13, 2026

Copy link
Copy Markdown
Contributor Author

Consider making the optional parameter on the rules also allow for the record to totally lack that key, rather than only allow it to be undefined.

Comment thread packages/core/src/temporal-types.ts Resolved
Comment thread packages/core/src/temporal-types.ts Resolved
Comment thread packages/core/src/temporal-types.ts Resolved
Comment thread packages/core/src/temporal-types.ts Resolved
Comment thread packages/core/src/temporal-types.ts Resolved
Comment thread packages/core/src/temporal-types.ts Resolved
Comment thread packages/core/src/temporal-types.ts Resolved
@MaxAake
MaxAake requested a review from robsdedude March 25, 2026 13:20
Comment thread packages/core/src/mapping.highlevel.ts Resolved
Comment thread packages/core/src/mapping.highlevel.ts Resolved
Comment thread packages/core/src/internal/util.ts Resolved
Comment thread packages/core/src/temporal-types.ts Resolved
@robsdedude
robsdedude self-requested a review April 15, 2026 15:20
Comment thread packages/core/src/temporal-types.ts Resolved
Comment thread packages/core/src/temporal-types.ts Resolved
Comment thread packages/core/src/mapping.highlevel.ts Resolved
Comment thread packages/core/src/session.ts Resolved
Comment thread packages/core/src/temporal-types.ts Resolved
Comment thread packages/core/src/mapping.decorators.ts Resolved
Comment thread packages/core/src/mapping.decorators.ts Resolved
Comment thread packages/core/src/mapping.highlevel.ts Resolved
Comment thread packages/core/src/temporal-types.ts Resolved
Comment thread packages/core/src/temporal-types.ts Resolved
@MaxAake
MaxAake requested a review from robsdedude April 28, 2026 14:49

@robsdedude robsdedude left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗨️

🗨️
🗨️ 🗨️ 🗨️   
🗨️   
...  
🗨️  
🗨️   
🗨️   
🗨️   
🗨️   
🗨️ ... 🗨️ 🗨️ 🗨️ 🗨️ ...  
🗨️   
🗨️ ... 🗨️ 🗨️ 🗨️ 🗨️ ... 🗨️ 🗨️ ...🗨️ 🗨️ 🗨️ 🗨️ 🗨️ ...🗨️ ...🗨️ 🗨️ ...🗨️ 🗨️ 🗨️ 🗨️ ...🗨️ ❗  

👷 🚥 🛠️
🗨️
...
👷
✅ 🏁
💸 💰 🥳 ⛄ 🤑

@MaxAake
MaxAake merged commit 2caa88a into 6.x Apr 29, 2026
25 of 45 checks passed
@MaxAake
MaxAake deleted the parameter-mapping branch April 29, 2026 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants