Skip to content

Commit 9c30c13

Browse files
authored
Adds optional generic parameter to Node and Relationship (#780)
adds optional generic parameter to Node and Relationship
1 parent d8c60c5 commit 9c30c13

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

core/src/graph-types.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { stringify } from './json'
2121

2222
type StandardDate = Date
2323
type NumberOrInteger = number | Integer | bigint
24+
type Properties = { [key: string]: any }
2425

2526
const IDENTIFIER_PROPERTY_ATTRIBUTES = {
2627
value: true,
@@ -43,18 +44,18 @@ function hasIdentifierProperty(obj: any, property: string): boolean {
4344
/**
4445
* Class for Node Type.
4546
*/
46-
class Node<T extends NumberOrInteger = Integer> {
47+
class Node<T extends NumberOrInteger = Integer, P extends Properties = Properties> {
4748
identity: T
4849
labels: string[]
49-
properties: any
50+
properties: P
5051
/**
5152
* @constructor
5253
* @protected
5354
* @param {Integer|number} identity - Unique identity
5455
* @param {Array<string>} labels - Array for all labels
55-
* @param {Object} properties - Map with node properties
56+
* @param {Properties} properties - Map with node properties
5657
*/
57-
constructor(identity: T, labels: string[], properties: any) {
58+
constructor(identity: T, labels: string[], properties: P) {
5859
/**
5960
* Identity of the node.
6061
* @type {Integer|number}
@@ -67,7 +68,7 @@ class Node<T extends NumberOrInteger = Integer> {
6768
this.labels = labels
6869
/**
6970
* Properties of the node.
70-
* @type {Object}
71+
* @type {Properties}
7172
*/
7273
this.properties = properties
7374
}
@@ -112,12 +113,12 @@ function isNode(obj: object): obj is Node {
112113
/**
113114
* Class for Relationship Type.
114115
*/
115-
class Relationship<T extends NumberOrInteger = Integer> {
116+
class Relationship<T extends NumberOrInteger = Integer, P extends Properties = Properties> {
116117
identity: T
117118
start: T
118119
end: T
119120
type: string
120-
properties: any
121+
properties: P
121122

122123
/**
123124
* @constructor
@@ -126,9 +127,9 @@ class Relationship<T extends NumberOrInteger = Integer> {
126127
* @param {Integer|number} start - Identity of start Node
127128
* @param {Integer|number} end - Identity of end Node
128129
* @param {string} type - Relationship type
129-
* @param {Object} properties - Map with relationship properties
130+
* @param {Properties} properties - Map with relationship properties
130131
*/
131-
constructor(identity: T, start: T, end: T, type: string, properties: any) {
132+
constructor(identity: T, start: T, end: T, type: string, properties: P) {
132133
/**
133134
* Identity of the relationship.
134135
* @type {Integer|number}
@@ -151,7 +152,7 @@ class Relationship<T extends NumberOrInteger = Integer> {
151152
this.type = type
152153
/**
153154
* Properties of the relationship.
154-
* @type {Object}
155+
* @type {Properties}
155156
*/
156157
this.properties = properties
157158
}
@@ -194,17 +195,17 @@ function isRelationship(obj: object): obj is Relationship {
194195
* Class for UnboundRelationship Type.
195196
* @access private
196197
*/
197-
class UnboundRelationship<T extends NumberOrInteger = Integer> {
198+
class UnboundRelationship<T extends NumberOrInteger = Integer, P extends Properties = Properties> {
198199
identity: T
199200
type: string
200-
properties: any
201+
properties: P
201202

202203
/**
203204
* @constructor
204205
* @protected
205206
* @param {Integer|number} identity - Unique identity
206207
* @param {string} type - Relationship type
207-
* @param {Object} properties - Map with relationship properties
208+
* @param {Properties} properties - Map with relationship properties
208209
*/
209210
constructor(identity: T, type: string, properties: any) {
210211
/**
@@ -219,7 +220,7 @@ class UnboundRelationship<T extends NumberOrInteger = Integer> {
219220
this.type = type
220221
/**
221222
* Properties of the relationship.
222-
* @type {Object}
223+
* @type {Properties}
223224
*/
224225
this.properties = properties
225226
}

test/types/graph-types.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ const node2: Node<number> = new Node(2, ['Person', 'Employee'], {
5050
})
5151
const node2Id: number = node2.identity
5252

53+
type NodeProps = { name: string }
54+
const node3: Node<number, NodeProps> = new Node(2, ['Person', 'Employee'], {
55+
name: 'Alice'
56+
})
57+
const node3Props: NodeProps = node3.properties
58+
const node3PropertyName: string = node3.properties.name
59+
5360
const rel1: Relationship = new Relationship(int(1), int(2), int(3), 'KNOWS', {
5461
since: 12345
5562
})
@@ -99,6 +106,28 @@ const rel6Start: number = rel6.start
99106
const rel6End: number = rel6.end
100107
const isRel6: boolean = rel6 instanceof UnboundRelationship
101108

109+
type RelationshipProps = { since: number }
110+
const rel7: Relationship<number, RelationshipProps> = new Relationship(
111+
2,
112+
3,
113+
4,
114+
'KNOWS',
115+
{
116+
since: 12345
117+
}
118+
)
119+
const rel7Props: RelationshipProps = rel7.properties
120+
const rel7PropertySince: number = rel7.properties.since
121+
122+
const rel8: UnboundRelationship<
123+
number,
124+
RelationshipProps
125+
> = new UnboundRelationship(5, 'KNOWS', {
126+
since: 12345
127+
})
128+
const rel8Props: RelationshipProps = rel8.properties
129+
const rel8PropertySince: number = rel8.properties.since
130+
102131
const pathSegment1: PathSegment = new PathSegment(node1, rel1, node1)
103132
const pathSegment1Start: Node = pathSegment1.start
104133
const pathSegment1Rel: Relationship = pathSegment1.relationship

0 commit comments

Comments
 (0)