UNPKG

graph-common

Version:
209 lines (146 loc) 8.78 kB
<!DOCTYPE html> <html> <head> <title>node_manager.litcoffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <ul id="jump_to"> <li> <a class="large" href="javascript:void(0);">Jump To &hellip;</a> <a class="small" href="javascript:void(0);">+</a> <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="bootstrap.html"> bootstrap.litcoffee </a> <a class="source" href="configuration_manager.html"> configuration_manager.litcoffee </a> <a class="source" href="echo_router.html"> echo_router.litcoffee </a> <a class="source" href="gql.html"> gql.litcoffee </a> <a class="source" href="graph.html"> graph.litcoffee </a> <a class="source" href="index.html"> index.litcoffee </a> <a class="source" href="mongodb_storage.html"> mongodb_storage.litcoffee </a> <a class="source" href="node_manager.html"> node_manager.litcoffee </a> <a class="source" href="node_schema.html"> node_schema.litcoffee </a> <a class="source" href="query.html"> query.litcoffee </a> <a class="source" href="redirect_router.html"> redirect_router.litcoffee </a> <a class="source" href="router_manager.html"> router_manager.litcoffee </a> <a class="source" href="router_schema.html"> router_schema.litcoffee </a> <a class="source" href="schema_manager.html"> schema_manager.litcoffee </a> <a class="source" href="schema_schema.html"> schema_schema.litcoffee </a> <a class="source" href="static_router.html"> static_router.litcoffee </a> <a class="source" href="storage_manager.html"> storage_manager.litcoffee </a> <a class="source" href="storage_router.html"> storage_router.litcoffee </a> </div> </li> </ul> <ul class="sections"> <li id="title"> <div class="annotation"> <h1>node_manager.litcoffee</h1> </div> </li> <li id="section-1"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-1">&#182;</a> </div> <p>NodeManager takes care of Graph API Nodes and updates Query’s Node information</p> </div> <div class="content"><div class='highlight'><pre>Node = <span class="hljs-literal">null</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NodeManager</span></span> <span class="hljs-attribute">constructor</span>: <span class="hljs-function"><span class="hljs-params">(graph, done)</span> -&gt;</span> <span class="hljs-property">@graph</span> = graph <span class="hljs-property">@database</span> = <span class="hljs-property">@graph</span>.database Node = <span class="hljs-property">@database</span>.model(<span class="hljs-string">'Node'</span>) <span class="hljs-property">@nodes</span> = {} <span class="hljs-property">@load_nodes</span>(done) <span class="hljs-attribute">load_nodes</span>: <span class="hljs-function"><span class="hljs-params">(done)</span> -&gt;</span> self = @ Node.find<span class="hljs-function"><span class="hljs-params">({}, (err, nodes) -&gt; self.add_node node <span class="hljs-keyword">for</span> node <span class="hljs-keyword">in</span> nodes done())</span> <span class="hljs-title">add_node</span>: <span class="hljs-params">(node)</span> -&gt;</span> <span class="hljs-property">@graph</span>.verbose(<span class="hljs-string">'NodeManager&gt; add:'</span>,node.name) <span class="hljs-property">@nodes</span>[node.path] = node <span class="hljs-attribute">create_node</span>: <span class="hljs-function"><span class="hljs-params">(node_data)</span> -&gt;</span> <span class="hljs-property">@graph</span>.verbose(<span class="hljs-string">'NodeManager&gt; create:'</span>, node_data) node = <span class="hljs-keyword">new</span> Node node_data <span class="hljs-property">@add_node</span> node <span class="hljs-attribute">query</span>: <span class="hljs-function"><span class="hljs-params">(query, callback)</span> -&gt;</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">typeof</span> query.node == <span class="hljs-string">'string'</span> parsed = <span class="hljs-built_in">require</span>(<span class="hljs-string">'url'</span>).parse(query.node, <span class="hljs-literal">true</span>) node_path = parsed.pathname node_path.replace(<span class="hljs-regexp">/^\//g</span>, <span class="hljs-string">''</span>).replace(<span class="hljs-regexp">/\/</span>$/g, <span class="hljs-string">''</span>) query.search_query = parsed.query || {} conditions = query.search_query.conditions conditions = JSON.parse(conditions) <span class="hljs-keyword">if</span> conditions <span class="hljs-keyword">and</span> <span class="hljs-keyword">typeof</span> conditions == <span class="hljs-string">'string'</span> query.search_query.conditions = conditions <span class="hljs-keyword">if</span> conditions <span class="hljs-property">@find_node</span><span class="hljs-function"><span class="hljs-params">(node_path, (node) -&gt; query.node = node query.unresolved_pathname = node_path .substr(node.path.length, node_path.length - node.path.length) .replace(<span class="hljs-regexp">/^\//g</span>, <span class="hljs-string">''</span>) .replace(<span class="hljs-regexp">/\/</span>$/g, <span class="hljs-string">''</span>) <span class="hljs-keyword">unless</span> query.unresolved_pathname <span class="hljs-keyword">is</span> <span class="hljs-string">''</span> <span class="hljs-keyword">or</span> <span class="hljs-regexp">/\/</span>/.test(query.unresolved_pathname) query.search_query.conditions ?= {} query.search_query.conditions._id = query.unresolved_pathname query.node_path = node.path query.graph.verbose(<span class="hljs-string">'NodeManager&gt; resolved:'</span>, node.path) )</span> <span class="hljs-title">find_node</span>: <span class="hljs-params">(node_path, callback)</span> -&gt;</span> <span class="hljs-keyword">unless</span> node_path <span class="hljs-keyword">or</span> node_path = <span class="hljs-string">''</span> node_path = <span class="hljs-string">''</span> node = <span class="hljs-property">@nodes</span>[node_path] <span class="hljs-keyword">if</span> node callback(node) <span class="hljs-keyword">return</span> node <span class="hljs-keyword">if</span> <span class="hljs-regexp">/\/</span>/.test(node_path) node_path = node_path.substr(<span class="hljs-number">0</span>, node_path.lastIndexOf(<span class="hljs-string">'/'</span>)) <span class="hljs-keyword">else</span> node_path = <span class="hljs-string">''</span> <span class="hljs-keyword">return</span> <span class="hljs-property">@find_node</span>(node_path, callback) <span class="hljs-built_in">module</span>.<span class="hljs-built_in">exports</span> = NodeManager</pre></div></div> </li> </ul> </div> </body> </html>