UNPKG

graph-common

Version:
95 lines (83 loc) 2.88 kB
(function() { var Node, NodeManager; Node = null; NodeManager = (function() { function NodeManager(graph, done) { this.graph = graph; this.database = this.graph.database; Node = this.database.model('Node'); this.nodes = {}; this.load_nodes(done); } NodeManager.prototype.load_nodes = function(done) { var self; self = this; return Node.find({}, function(err, nodes) { var node, _i, _len; for (_i = 0, _len = nodes.length; _i < _len; _i++) { node = nodes[_i]; self.add_node(node); } return done(); }); }; NodeManager.prototype.add_node = function(node) { this.graph.verbose('NodeManager> add:', node.name); return this.nodes[node.path] = node; }; NodeManager.prototype.create_node = function(node_data) { var node; this.graph.verbose('NodeManager> create:', node_data); node = new Node(node_data); return this.add_node(node); }; NodeManager.prototype.query = function(query, callback) { var conditions, node_path, parsed; if (typeof query.node === 'string') { parsed = require('url').parse(query.node, true); node_path = parsed.pathname; node_path.replace(/^\//g, '').replace(/\/$/g, ''); query.search_query = parsed.query || {}; conditions = query.search_query.conditions; if (conditions && typeof conditions === 'string') { conditions = JSON.parse(conditions); } if (conditions) { query.search_query.conditions = conditions; } return this.find_node(node_path, function(node) { var _base; query.node = node; query.unresolved_pathname = node_path.substr(node.path.length, node_path.length - node.path.length).replace(/^\//g, '').replace(/\/$/g, ''); if (!(query.unresolved_pathname === '' || /\//.test(query.unresolved_pathname))) { if ((_base = query.search_query).conditions == null) { _base.conditions = {}; } query.search_query.conditions._id = query.unresolved_pathname; } query.node_path = node.path; return query.graph.verbose('NodeManager> resolved:', node.path); }); } }; NodeManager.prototype.find_node = function(node_path, callback) { var node; if (!(node_path || (node_path = ''))) { node_path = ''; } node = this.nodes[node_path]; if (node) { callback(node); return node; } if (/\//.test(node_path)) { node_path = node_path.substr(0, node_path.lastIndexOf('/')); } else { node_path = ''; } return this.find_node(node_path, callback); }; return NodeManager; })(); module.exports = NodeManager; }).call(this);