-
Notifications
You must be signed in to change notification settings - Fork 149
Add generics and type mapping to Result, Session.run and Transaction.run #1010
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The new generic typing allow mapping the records of these running queries to type-safe Record. Given the following Person and Friendship definitions. ```typescript interface Person { age: Integer name: string } interface Friendship { since: Integer } interface PersonAndFriendship { p: Node<number, Person> f: Relationship<number, Friendship> } ``` The new type-mapping allow safe access the properties of query which return `Record<Person>` ```typescript const { records } = await session.run<Person>('MATCH (p:Person) RETURN p.name, p.age') for (const person of records) { const age: Integer = person.get('age') const name: string = person.get('name') // @ts-expect-error const nameInt: Integer = person.get('name') } ``` The type-mapping can be also extended for `Node` and `Relationship`. ```typescript const { records } = await session.run<PersonAndFriendship>('MATCH (p:Person)-[f:Friendship]-() RETURN p, f') for (const r of records) { const person = r.get('p') const age: Integer = person.properties.age const name: string = person.properties.name // @ts-expect-error const nameInt: Integer = person.properties.name // @ts-expect-error const err: string = person.properties.err const friendship = r.get('f') const since: Integer = friendship.properties.since // @ts-expect-error const sinceString: string = friendship.properties.since // @ts-expect-error const err2: string = friendship.properties.err } ``` The usage in combination with `executeRead` and `executeWrite` is also possibile. ``` const { records } = await session.executeRead(tx => tx.run<Person>('MATCH (p:Person) RETURN p.name, p.age')) for (const person of records) { const age: Integer = person.get('age') const name: string = person.get('name') // @ts-expect-error const nameInt: Integer = person.get('name') } ```
robsdedude
approved these changes
Oct 26, 2022
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
bigmontz
added a commit
to bigmontz/neo4j-javascript-driver
that referenced
this pull request
Nov 1, 2022
…run (neo4j#1010) The new generic typing allow mapping the records of these running queries to type-mapped Record. Given the following Person and Friendship definitions. ```typescript interface Person { age: Integer name: string } interface Friendship { since: Integer } interface PersonAndFriendship { p: Node<number, Person> f: Relationship<number, Friendship> } ``` The new type-mapping allow safe access the properties of query which return `Record<Person>` ```typescript const { records } = await session.run<Person>('MATCH (p:Person) RETURN p.name as name, p.age as age') for (const person of records) { const age: Integer = person.get('age') const name: string = person.get('name') // @ts-expect-error const nameInt: Integer = person.get('name') } ``` The type-mapping can be also extended for `Node` and `Relationship`. ```typescript const { records } = await session.run<PersonAndFriendship>('MATCH (p:Person)-[f:Friendship]-() RETURN p, f') for (const r of records) { const person = r.get('p') const age: Integer = person.properties.age const name: string = person.properties.name // @ts-expect-error const nameInt: Integer = person.properties.name // @ts-expect-error const err: string = person.properties.err const friendship = r.get('f') const since: Integer = friendship.properties.since // @ts-expect-error const sinceString: string = friendship.properties.since // @ts-expect-error const err2: string = friendship.properties.err } ``` The usage in combination with `executeRead` and `executeWrite` is also possible. ```typescript const { records } = await session.executeRead(tx => tx.run<Person>('MATCH (p:Person) RETURN p.name as name, p.age as age')) for (const person of records) { const age: Integer = person.get('age') const name: string = person.get('name') // @ts-expect-error const nameInt: Integer = person.get('name') } ``` With some async iterator usage: ```typescript const personList = await session.executeRead(async (tx) => { const result = tx.run<Person>('MATCH (p:Person) RETURN p.name as name, p.age as age') const objects: Person[] = [] // iterate while streaming the objects for await (const record of result) { objects.push(record.toObject()) } return objects }) for (const person of personList) { const age: Integer = person.age const name: string = person.name // @ts-expect-error const nameInt: Integer = person.name // @ts-expect-error const nome: string = person.nome } ```⚠️ This type definitions are not asserted in runtime. Thus mismatched type records coming from the database will not trigger type errors.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The new generic typing allow mapping the records of these running queries to type-mapped Record.
Given the following Person and Friendship definitions.
The new type-mapping allow safe access the properties of query which return
Record<Person>
The type-mapping can be also extended for
Node
andRelationship
.The usage in combination with
executeRead
andexecuteWrite
is also possible.With some async iterator usage: