Skip to content

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
merged 4 commits into from
Oct 27, 2022

Conversation

bigmontz
Copy link
Contributor

@bigmontz bigmontz commented Oct 25, 2022

The new generic typing allow mapping the records of these running queries to type-mapped Record.

Given the following Person and Friendship definitions.

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>

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.

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.

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:

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.

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')
}
```
Copy link
Member

@robsdedude robsdedude left a comment

Choose a reason for hiding this comment

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

🚀

@bigmontz bigmontz merged commit 6105c26 into neo4j:5.0 Oct 27, 2022
@bigmontz bigmontz deleted the 5.x-better-typing-support branch October 27, 2022 10:57
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants