Skip to content

Add generic types for Node#labels, Relationship#type and UnboundRelationship#type #1014

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 1 commit into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions packages/core/src/graph-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ function hasIdentifierProperty (obj: any, property: string): boolean {
/**
* Class for Node Type.
*/
class Node<T extends NumberOrInteger = Integer, P extends Properties = Properties> {
class Node<T extends NumberOrInteger = Integer, P extends Properties = Properties, Label extends string = string> {
identity: T
labels: string[]
labels: Label[]
properties: P
elementId: string
/**
Expand All @@ -60,7 +60,7 @@ class Node<T extends NumberOrInteger = Integer, P extends Properties = Propertie
* @param {Properties} properties - Map with node properties
* @param {string} elementId - Node element identifier
*/
constructor (identity: T, labels: string[], properties: P, elementId?: string) {
constructor (identity: T, labels: Label[], properties: P, elementId?: string) {
/**
* Identity of the node.
* @type {NumberOrInteger}
Expand Down Expand Up @@ -124,11 +124,11 @@ function isNode (obj: object): obj is Node {
/**
* Class for Relationship Type.
*/
class Relationship<T extends NumberOrInteger = Integer, P extends Properties = Properties> {
class Relationship<T extends NumberOrInteger = Integer, P extends Properties = Properties, Type extends string = string> {
identity: T
start: T
end: T
type: string
type: Type
properties: P
elementId: string
startNodeElementId: string
Expand All @@ -147,7 +147,7 @@ class Relationship<T extends NumberOrInteger = Integer, P extends Properties = P
* @param {string} endNodeElementId - End Node element identifier
*/
constructor (
identity: T, start: T, end: T, type: string, properties: P,
identity: T, start: T, end: T, type: Type, properties: P,
elementId?: string, startNodeElementId?: string, endNodeElementId?: string
) {
/**
Expand Down Expand Up @@ -236,9 +236,9 @@ function isRelationship (obj: object): obj is Relationship {
* Class for UnboundRelationship Type.
* @access private
*/
class UnboundRelationship<T extends NumberOrInteger = Integer, P extends Properties = Properties> {
class UnboundRelationship<T extends NumberOrInteger = Integer, P extends Properties = Properties, Type extends string = string> {
identity: T
type: string
type: Type
properties: P
elementId: string

Expand All @@ -250,7 +250,7 @@ class UnboundRelationship<T extends NumberOrInteger = Integer, P extends Propert
* @param {Properties} properties - Map with relationship properties
* @param {string} elementId - Relationship element identifier
*/
constructor (identity: T, type: string, properties: any, elementId?: string) {
constructor (identity: T, type: Type, properties: any, elementId?: string) {
/**
* Identity of the relationship.
* @type {NumberOrInteger}
Expand Down
40 changes: 40 additions & 0 deletions packages/core/test/graph-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ describe('Node', () => {
expect(isNode(nonNode)).toBe(false)
})

test('should type mapping labels', () => {
type PersonLabels = 'Person' | 'Actor'
const labels: PersonLabels[] = ['Actor', 'Person']
type Person = Node<number, {}, PersonLabels>

const p: Person = new Node(1, labels, {})

const receivedLabels: PersonLabels[] = p.labels

expect(receivedLabels).toEqual(labels)

// @ts-expect-error
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _: 'Movie'|Array<'TvShow'> = p.labels
})

function validNodes (): any[] {
return [
[new Node(1, ['label'], {}, 'elementId')],
Expand Down Expand Up @@ -191,6 +207,18 @@ describe('Relationship', () => {
expect(isRelationship(nonRelationship)).toBe(false)
})

test('should type mapping relationship type', () => {
type ActedIn = Relationship<number, { [key in string]: any }, 'ACTED_IN'>
const a: ActedIn = new Relationship(1, 1, 2, 'ACTED_IN', {})

const receivedType: 'ACTED_IN' = a.type
expect(receivedType).toEqual('ACTED_IN')

// @ts-expect-error
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _: 'DIRECTED' = a.type
})

function validRelationships (): any[] {
return [
[new Relationship(1, 2, 3, 'Rel', {}, 'elementId', 'startNodeElementId', 'endNodeElementId')],
Expand Down Expand Up @@ -306,6 +334,18 @@ describe('UnboundRelationship', () => {
)
})

test('should type mapping relationship type', () => {
type ActedIn = UnboundRelationship<number, { [key in string]: any }, 'ACTED_IN'>
const a: ActedIn = new UnboundRelationship(1, 'ACTED_IN', {})

const receivedType: 'ACTED_IN' = a.type
expect(receivedType).toEqual('ACTED_IN')

// @ts-expect-error
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _: 'DIRECTED' = a.type
})

function validUnboundRelationships (): any[] {
return [
[new UnboundRelationship(1, 'Rel', {}, 'elementId')],
Expand Down
18 changes: 9 additions & 9 deletions packages/neo4j-driver-deno/lib/core/graph-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ function hasIdentifierProperty (obj: any, property: string): boolean {
/**
* Class for Node Type.
*/
class Node<T extends NumberOrInteger = Integer, P extends Properties = Properties> {
class Node<T extends NumberOrInteger = Integer, P extends Properties = Properties, Label extends string = string> {
identity: T
labels: string[]
labels: Label[]
properties: P
elementId: string
/**
Expand All @@ -60,7 +60,7 @@ class Node<T extends NumberOrInteger = Integer, P extends Properties = Propertie
* @param {Properties} properties - Map with node properties
* @param {string} elementId - Node element identifier
*/
constructor (identity: T, labels: string[], properties: P, elementId?: string) {
constructor (identity: T, labels: Label[], properties: P, elementId?: string) {
/**
* Identity of the node.
* @type {NumberOrInteger}
Expand Down Expand Up @@ -124,11 +124,11 @@ function isNode (obj: object): obj is Node {
/**
* Class for Relationship Type.
*/
class Relationship<T extends NumberOrInteger = Integer, P extends Properties = Properties> {
class Relationship<T extends NumberOrInteger = Integer, P extends Properties = Properties, Type extends string = string> {
identity: T
start: T
end: T
type: string
type: Type
properties: P
elementId: string
startNodeElementId: string
Expand All @@ -147,7 +147,7 @@ class Relationship<T extends NumberOrInteger = Integer, P extends Properties = P
* @param {string} endNodeElementId - End Node element identifier
*/
constructor (
identity: T, start: T, end: T, type: string, properties: P,
identity: T, start: T, end: T, type: Type, properties: P,
elementId?: string, startNodeElementId?: string, endNodeElementId?: string
) {
/**
Expand Down Expand Up @@ -236,9 +236,9 @@ function isRelationship (obj: object): obj is Relationship {
* Class for UnboundRelationship Type.
* @access private
*/
class UnboundRelationship<T extends NumberOrInteger = Integer, P extends Properties = Properties> {
class UnboundRelationship<T extends NumberOrInteger = Integer, P extends Properties = Properties, Type extends string = string> {
identity: T
type: string
type: Type
properties: P
elementId: string

Expand All @@ -250,7 +250,7 @@ class UnboundRelationship<T extends NumberOrInteger = Integer, P extends Propert
* @param {Properties} properties - Map with relationship properties
* @param {string} elementId - Relationship element identifier
*/
constructor (identity: T, type: string, properties: any, elementId?: string) {
constructor (identity: T, type: Type, properties: any, elementId?: string) {
/**
* Identity of the relationship.
* @type {NumberOrInteger}
Expand Down