Skip to content

Adds optional generic parameter to Node and Relationship #780

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 3 commits into from
Oct 14, 2021
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
29 changes: 15 additions & 14 deletions core/src/graph-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { stringify } from './json'

type StandardDate = Date
type NumberOrInteger = number | Integer | bigint
type Properties = { [key: string]: any }

const IDENTIFIER_PROPERTY_ATTRIBUTES = {
value: true,
Expand All @@ -43,18 +44,18 @@ function hasIdentifierProperty(obj: any, property: string): boolean {
/**
* Class for Node Type.
*/
class Node<T extends NumberOrInteger = Integer> {
class Node<T extends NumberOrInteger = Integer, P extends Properties = Properties> {
identity: T
labels: string[]
properties: any
properties: P
/**
* @constructor
* @protected
* @param {Integer|number} identity - Unique identity
* @param {Array<string>} labels - Array for all labels
* @param {Object} properties - Map with node properties
* @param {Properties} properties - Map with node properties
*/
constructor(identity: T, labels: string[], properties: any) {
constructor(identity: T, labels: string[], properties: P) {
/**
* Identity of the node.
* @type {Integer|number}
Expand All @@ -67,7 +68,7 @@ class Node<T extends NumberOrInteger = Integer> {
this.labels = labels
/**
* Properties of the node.
* @type {Object}
* @type {Properties}
*/
this.properties = properties
}
Expand Down Expand Up @@ -112,12 +113,12 @@ function isNode(obj: object): boolean {
/**
* Class for Relationship Type.
*/
class Relationship<T extends NumberOrInteger = Integer> {
class Relationship<T extends NumberOrInteger = Integer, P extends Properties = Properties> {
identity: T
start: T
end: T
type: string
properties: any
properties: P

/**
* @constructor
Expand All @@ -126,9 +127,9 @@ class Relationship<T extends NumberOrInteger = Integer> {
* @param {Integer|number} start - Identity of start Node
* @param {Integer|number} end - Identity of end Node
* @param {string} type - Relationship type
* @param {Object} properties - Map with relationship properties
* @param {Properties} properties - Map with relationship properties
*/
constructor(identity: T, start: T, end: T, type: string, properties: any) {
constructor(identity: T, start: T, end: T, type: string, properties: P) {
/**
* Identity of the relationship.
* @type {Integer|number}
Expand All @@ -151,7 +152,7 @@ class Relationship<T extends NumberOrInteger = Integer> {
this.type = type
/**
* Properties of the relationship.
* @type {Object}
* @type {Properties}
*/
this.properties = properties
}
Expand Down Expand Up @@ -194,17 +195,17 @@ function isRelationship(obj: object): boolean {
* Class for UnboundRelationship Type.
* @access private
*/
class UnboundRelationship<T extends NumberOrInteger = Integer> {
class UnboundRelationship<T extends NumberOrInteger = Integer, P extends Properties = Properties> {
identity: T
type: string
properties: any
properties: P

/**
* @constructor
* @protected
* @param {Integer|number} identity - Unique identity
* @param {string} type - Relationship type
* @param {Object} properties - Map with relationship properties
* @param {Properties} properties - Map with relationship properties
*/
constructor(identity: T, type: string, properties: any) {
/**
Expand All @@ -219,7 +220,7 @@ class UnboundRelationship<T extends NumberOrInteger = Integer> {
this.type = type
/**
* Properties of the relationship.
* @type {Object}
* @type {Properties}
*/
this.properties = properties
}
Expand Down
29 changes: 29 additions & 0 deletions test/types/graph-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ const node2: Node<number> = new Node(2, ['Person', 'Employee'], {
})
const node2Id: number = node2.identity

type NodeProps = { name: string }
const node3: Node<number, NodeProps> = new Node(2, ['Person', 'Employee'], {
name: 'Alice'
})
const node3Props: NodeProps = node3.properties
const node3PropertyName: string = node3.properties.name

const rel1: Relationship = new Relationship(int(1), int(2), int(3), 'KNOWS', {
since: 12345
})
Expand Down Expand Up @@ -86,6 +93,28 @@ const rel6Start: number = rel6.start
const rel6End: number = rel6.end
const isRel6: boolean = rel6 instanceof UnboundRelationship

type RelationshipProps = { since: number }
const rel7: Relationship<number, RelationshipProps> = new Relationship(
2,
3,
4,
'KNOWS',
{
since: 12345
}
)
const rel7Props: RelationshipProps = rel7.properties
const rel7PropertySince: number = rel7.properties.since

const rel8: UnboundRelationship<
number,
RelationshipProps
> = new UnboundRelationship(5, 'KNOWS', {
since: 12345
})
const rel8Props: RelationshipProps = rel8.properties
const rel8PropertySince: number = rel8.properties.since

const pathSegment1: PathSegment = new PathSegment(node1, rel1, node1)
const pathSegment1Start: Node = pathSegment1.start
const pathSegment1Rel: Relationship = pathSegment1.relationship
Expand Down