UNPKG

nosql-constraints

Version:

Helpers to manage constrants (i.e. cascade delete) in a NoSQL database

41 lines (40 loc) 1.71 kB
import _ from 'lodash'; /** * Constraints class, supporting fast access to the constraints */ export class Constraints { constraintsGraph; constraintsMap; constructor(constraintsGraph, constraintsMap) { this.constraintsGraph = constraintsGraph; this.constraintsMap = constraintsMap; } getDirectConstraints(containerId, refDocType, cascadeDelete) { // Find vertices that match the refDocType and return the direct constraints (first element of each path) const constraints = []; const constraintEdgesSet = new Set(); for (const [vertex, paths] of this.constraintsMap.entries()) { if (containerId === vertex.vertex.containerId && _.isMatch(refDocType ?? {}, vertex.vertex.refDocType ?? {})) { for (const path of paths) { if (path.length > 0) { const edgeId = `${path[0].fromId} -> ${path[0].toId}`; if (!constraintEdgesSet.has(edgeId) && (cascadeDelete === undefined || (path[0].constraint.cascadeDelete ?? false) === cascadeDelete)) { constraintEdgesSet.add(edgeId); constraints.push(path[0]); } } } } } return constraints; } getDirectCascadeDeleteConstraints(containerId, refDocType) { return this.getDirectConstraints(containerId, refDocType, true); } getDirectNoCascadeDeleteConstraints(containerId, refDocType) { return this.getDirectConstraints(containerId, refDocType, false); } }