UNPKG

nodulator

Version:

Complete NodeJS Framework for Restfull APIs

487 lines (335 loc) 16 kB
<!DOCTYPE html> <html> <head> <title>Resource.ls</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_wrapper"> <div id="jump_page"> <a class="source" href="Nodulator.html"> Nodulator.ls </a> <a class="source" href="ClientDB.html"> ClientDB.ls </a> <a class="source" href="index.html"> index.ls </a> <a class="source" href="Resource.html"> Resource.ls </a> <a class="source" href="Bus.html"> Bus.ls </a> <a class="source" href="Cache.html"> Cache.ls </a> <a class="source" href="ChangeWatcher.html"> ChangeWatcher.ls </a> <a class="source" href="Debug.html"> Debug.ls </a> <a class="source" href="Wrappers.html"> Wrappers.ls </a> <a class="source" href="Nodulator.html"> Nodulator.ls </a> <a class="source" href="Resource.html"> Resource.ls </a> <a class="source" href="Schema.html"> Schema.ls </a> <a class="source" href="NModule.html"> NModule.ls </a> <a class="source" href="AccountResource.html"> AccountResource.ls </a> <a class="source" href="User.html"> User.ls </a> <a class="source" href="index.html"> index.ls </a> <a class="source" href="Nodulator-Angular.html"> Nodulator-Angular.ls </a> <a class="source" href="TaskDirective.html"> TaskDirective.ls </a> <a class="source" href="index.html"> index.ls </a> <a class="source" href="TaskService.html"> TaskService.ls </a> <a class="source" href="main.html"> main.ls </a> <a class="source" href="index.html"> index.ls </a> <a class="source" href="index.html"> index.ls </a> <a class="source" href="Task.html"> Task.ls </a> <a class="source" href="index.html"> index.ls </a> <a class="source" href="index.html"> index.ls </a> <a class="source" href="index.html"> index.ls </a> <a class="source" href="index.html"> index.ls </a> <a class="source" href="index.html"> index.ls </a> <a class="source" href="DOM.html"> DOM.ls </a> <a class="source" href="View.html"> View.ls </a> <a class="source" href="index.html"> index.ls </a> <a class="source" href="Nodulator.html"> Nodulator.ls </a> <a class="source" href="Mongo.html"> Mongo.ls </a> <a class="source" href="Mysql.html"> Mysql.ls </a> <a class="source" href="SqlMem.html"> SqlMem.ls </a> <a class="source" href="index.html"> index.ls </a> <a class="source" href="Resource.html"> Resource.ls </a> <a class="source" href="Request.html"> Request.ls </a> <a class="source" href="Route.html"> Route.ls </a> <a class="source" href="index.html"> index.ls </a> </div> </div> </li> </ul> <ul class="sections"> <li id="title"> <div class="annotation"> <h1>Resource.ls</h1> </div> </li> <li id="section-1"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-1">&#182;</a> </div> </div> <div class="content"><div class='highlight'><pre><span class="hljs-built_in">require</span>! { <span class="hljs-string">'../Nodulator'</span>: N \../../Common/Helpers/ChangeWatcher \../../Common/Schema \prelude-ls \../../Common/Helpers/Debug \./Connectors : DB async underscore: __ validator: Validator hacktiv: Hacktiv polyparams: ParamWraper } cache = <span class="hljs-literal">null</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> cache? cache := <span class="hljs-built_in">require</span>(\../../Common/Helpers/Cache)(N.config) Wrappers = <span class="hljs-literal">null</span> N.Validator = Validator <span class="hljs-built_in">module</span>.exports = <span class="hljs-function"><span class="hljs-params">(config, routes, name, N)</span> -&gt;</span> debug-resource = <span class="hljs-literal">null</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ResourceServer</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">require</span>(\../../<span class="hljs-title">Common</span>/<span class="hljs-title">Resource</span>)(<span class="hljs-title">config</span>, <span class="hljs-title">routes</span>, <span class="hljs-title">name</span>, <span class="hljs-title">N</span>)</span> @N = N debug-resource = @debug-resource</pre></div></div> </li> <li id="section-2"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>Private Instance Methods</p> </div> </li> <li id="section-3"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>Save without wrap</p> </div> <div class="content"><div class='highlight'><pre> _SaveUnwrapped: <span class="hljs-function"><span class="hljs-params">(config, done)</span> -&gt;</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> done? done = config config = @_config serie = @Serialize! @_schema.Validate serie, (err) ~&gt; exists = @id? debug-resource.Log <span class="hljs-string">"Saving <span class="hljs-subst">#{JSON.stringify serie}</span>"</span> <span class="hljs-keyword">switch</span> | err? =&gt; done err | _ =&gt; @_table.Save serie, config, (err, instance) ~&gt; | err? =&gt; done err | _ =&gt; <span class="hljs-keyword">if</span> !exists @id = instance.id N.bus.emit \new_ + name, @ <span class="hljs-keyword">else</span> N.bus.emit \update_ + name, @ ChangeWatcher.Invalidate! debug-resource.Log <span class="hljs-string">"Saved {id: <span class="hljs-subst">#{@id}</span>}"</span> done <span class="hljs-literal">null</span>, @ @</pre></div></div> </li> <li id="section-4"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Delete without wrap</p> </div> <div class="content"><div class='highlight'><pre> _DeleteUnwrapped: <span class="hljs-function"><span class="hljs-params">(done)</span> -&gt;</span> debug-resource.Log <span class="hljs-string">"Deleting {id: <span class="hljs-subst">#{@id}</span>}"</span> @_table.Delete @id, (err, affected) ~&gt; <span class="hljs-keyword">switch</span> | err? =&gt; done err | _ =&gt; cache.Delete @_type + <span class="hljs-string">'Fetch'</span> + @id, ~&gt; @id = <span class="hljs-literal">undefined</span> N.bus.emit \delete_ + name, @ ChangeWatcher.Invalidate() debug-resource.Log <span class="hljs-string">"Deleted {id: <span class="hljs-subst">#{@id}</span>}"</span> done <span class="hljs-literal">null</span>, @ @</pre></div></div> </li> <li id="section-5"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>Public Class Methods</p> </div> </li> <li id="section-6"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>Create without wraps</p> </div> <div class="content"><div class='highlight'><pre> @_CreateUnwrapped = @_WrapParams <span class="hljs-keyword">do</span> * \Object : optional: <span class="hljs-literal">true</span> * \Array : optional: <span class="hljs-literal">true</span> * \Object : optional: <span class="hljs-literal">true</span> * \Function * \Number : optional: <span class="hljs-literal">true</span> (arg, args, config, done, _depth = @config?.maxDepth) -&gt; <span class="hljs-keyword">if</span> args? @debug-resource.Log <span class="hljs-string">"Creating from array: <span class="hljs-subst">#{args.length}</span> entries"</span> @_HandleArrayArg arg || args || {}, (blob, done) ~&gt; async.mapSeries obj-to-pairs(blob), (pair, done) ~&gt; <span class="hljs-keyword">if</span> pair<span class="hljs-number">.0</span> <span class="hljs-keyword">in</span> (@_schema.assocs |&gt; map (.foreign)) <span class="hljs-keyword">and</span> pair<span class="hljs-number">.1</span>?._promise pair<span class="hljs-number">.1</span>.Then -&gt; done <span class="hljs-literal">null</span> [pair<span class="hljs-number">.0</span>, it.id] pair<span class="hljs-number">.1</span>.Catch done <span class="hljs-keyword">else</span> done <span class="hljs-literal">null</span>, pair , (err, results) ~&gt; <span class="hljs-keyword">return</span> done err <span class="hljs-keyword">if</span> err? blob = pairs-to-obj results @debug-resource.Log <span class="hljs-string">"Creating <span class="hljs-subst">#{JSON.stringify blob}</span>"</span> @resource._Deserialize blob, (err, instance) ~&gt; | err? =&gt; done err | _ =&gt; c = {} <span class="hljs-keyword">if</span> config?.db? @_table.AddDriver config c = config <span class="hljs-keyword">else</span> c = @config instance._SaveUnwrapped c, (err, instance) ~&gt; | err? =&gt; done err | _ =&gt; <span class="hljs-keyword">if</span> instance._schema.assocs.length @_schema.FetchAssoc instance, (err, blob) ~&gt; | err? =&gt; done err | _ =&gt; instance import blob @debug-resource.Log <span class="hljs-string">"Created {id: <span class="hljs-subst">#{instance.id}</span>}"</span> done <span class="hljs-literal">null</span> instance , _depth <span class="hljs-keyword">else</span> @debug-resource.Log <span class="hljs-string">"Created {id: <span class="hljs-subst">#{instance.id}</span>}"</span> done <span class="hljs-literal">null</span> instance , _depth , done</pre></div></div> </li> <li id="section-7"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>Private Init process</p> </div> </li> <li id="section-8"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p>Initialisation @Init = (@config = @config, extendArgs) -&gt; if @INITED return @</p> <p> super …</p> <p> if @_routes? @routes = new @_routes(@, @config)</p> <p> @</p> </div> <div class="content"><div class='highlight'><pre> ResourceServer.DB = DB ResourceServer._PrepareResource(config, routes, name)</pre></div></div> </li> </ul> </div> </body> </html>