nodulator
Version:
Complete NodeJS Framework for Restfull APIs
683 lines (444 loc) • 24.1 kB
HTML
<html>
<head>
<title>ClientDB.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 …</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>ClientDB.ls</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
</div>
<div class="content"><div class='highlight'><pre>_ = <span class="hljs-built_in">require</span> <span class="hljs-string">'underscore'</span></pre></div></div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>N = require ‘../../Nodulator’</p>
</div>
<div class="content"><div class='highlight'><pre>tables = {}
rest = <span class="hljs-built_in">require</span> <span class="hljs-string">'rest'</span>
mime = <span class="hljs-built_in">require</span>(<span class="hljs-string">'rest/interceptor/mime'</span>);
Client = rest.wrap(mime)
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RESTClient</span></span>
(@name) ->
Fetch: <span class="hljs-function"><span class="hljs-params">(blob, done)</span> -></span>
Client method: \POST path: \<span class="hljs-regexp">/api/1/</span> + @name + <span class="hljs-string">'/fetch'</span>, headers: {<span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'application/json'</span>}, entity: blob
.<span class="hljs-keyword">then</span> ~> done <span class="hljs-literal">null</span> it.entity
.<span class="hljs-keyword">catch</span> done
Create: <span class="hljs-function"><span class="hljs-params">(blob, done)</span> -></span>
Client method: \POST path: \<span class="hljs-regexp">/api/1/</span> + @name + <span class="hljs-string">'/create'</span>, headers: {<span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'application/json'</span>}, entity: blob
.<span class="hljs-keyword">then</span> ~> done <span class="hljs-literal">null</span> it.entity
.<span class="hljs-keyword">catch</span> done
List: <span class="hljs-function"><span class="hljs-params">(blob, done)</span> -></span>
Client method: \POST path: \<span class="hljs-regexp">/api/1/</span> + @name + <span class="hljs-string">'/list'</span>, headers: {<span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'application/json'</span>}, entity: blob
.<span class="hljs-keyword">then</span> ~> done <span class="hljs-literal">null</span> it.entity
.<span class="hljs-keyword">catch</span> done
Delete: <span class="hljs-function"><span class="hljs-params">(id, blob, done)</span> -></span>
Client method: \POST path: \<span class="hljs-regexp">/api/1/</span> + @name + <span class="hljs-string">'/delete/'</span> + id, headers: {<span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'application/json'</span>}, entity: blob
.<span class="hljs-keyword">then</span> ~> done <span class="hljs-literal">null</span> it.entity
.<span class="hljs-keyword">catch</span> done
Set: <span class="hljs-function"><span class="hljs-params">(id, blob, done)</span> -></span>
Client method: \POST path: \<span class="hljs-regexp">/api/1/</span> + @name + <span class="hljs-string">'/set/'</span> + id, headers: {<span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'application/json'</span>}, entity: blob
.<span class="hljs-keyword">then</span> ~> done <span class="hljs-literal">null</span> it.entity
.<span class="hljs-keyword">catch</span> done</pre></div></div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p>@N.Client = RESTClient</p>
</div>
<div class="content"><div class='highlight'><pre>
dbs = {}
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ClientDB</span></span>
name: <span class="hljs-string">''</span></pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>insertSync: []
deleteSync: []</p>
</div>
<div class="content"><div class='highlight'><pre>
(@resource) ->
dbs[@resource._type] = @
@fetched = []
@collection = []
@client = <span class="hljs-keyword">new</span> RESTClient @resource._type
@SetSockets!
SetSockets: <span class="hljs-function">-></span>
socket.<span class="hljs-literal">on</span> \new_ + @resource._type, @~OnNew
socket.<span class="hljs-literal">on</span> \update_ + @resource._type, @~OnUpdate
socket.<span class="hljs-literal">on</span> \delete_ + @resource._type, @~OnDelete
OnNew: <span class="hljs-function">-></span>
it = @ExtractAssocs it</pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>console.log ‘New’, it</p>
</div>
<div class="content"><div class='highlight'><pre> @collection.push it
@resource._Changed!
@resource.N.bus.emit \new_ + @resource._type, it
OnUpdate: <span class="hljs-function"><span class="hljs-params">(item)</span> -></span>
<span class="hljs-built_in">console</span>.log <span class="hljs-string">'Update'</span> item
item = @ExtractAssocs item</pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>console.log ‘Update’, item
return if item.id not in (@collection |> map (.id))</p>
</div>
<div class="content"><div class='highlight'><pre> idx = @collection |> find-index -> it.id <span class="hljs-keyword">is</span> item.id</pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>console.log idx, @collection.idx</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> idx?</pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>console.log ‘Id ?’, idx, item, @collection[idx]</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-built_in">console</span>.log <span class="hljs-string">'Change'</span> @collection[idx], item
diff = @GetChange @collection[idx], item</pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>console.log ‘diff ?’, diff</p>
</div>
<div class="content"><div class='highlight'><pre> @collection[idx] = item</pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p>@resource._Changed!0</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-built_in">console</span>.log <span class="hljs-string">'diff'</span> diff
<span class="hljs-keyword">if</span> keys diff .length
@resource.N.bus.emit \update_ + @resource._type + \_ + item.id, diff
<span class="hljs-keyword">else</span>
@OnNew item
OnDelete: <span class="hljs-function"><span class="hljs-params">(item)</span> -></span>
item = @ExtractAssocs item</pre></div></div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<p>return if it.id not in @collection |> map (.id)</p>
</div>
<div class="content"><div class='highlight'><pre> @collection = _(@collection).reject (record) -> record.id <span class="hljs-keyword">is</span> item.id
@resource._Changed!
ExtractAssocs: <span class="hljs-function"><span class="hljs-params">(blob)</span> -></span>
@resource._schema.assocs |> map ~>
<span class="hljs-keyword">if</span> blob[it.name]?
dbs[it.type._type].ImportAssocs blob[it.name]
<span class="hljs-keyword">delete</span> blob[it.name]
blob
ImportAssocs: <span class="hljs-function">-></span>
map @~OnUpdate, it
GetChange: <span class="hljs-function"><span class="hljs-params">(base, toCmp)</span>-></span>
res = {}
<span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">of</span> base <span class="hljs-keyword">when</span> toCmp[k] !== v <span class="hljs-keyword">and</span> <span class="hljs-keyword">typeof</span>! v <span class="hljs-keyword">isnt</span> \Object <span class="hljs-keyword">and</span> <span class="hljs-keyword">typeof</span>! v <span class="hljs-keyword">isnt</span> \Array
res[k] = toCmp[k]
res
Select: <span class="hljs-function"><span class="hljs-params">(where, options, done)</span> -></span>
res = map (<span class="hljs-function">-></span> {} import it), _(@collection).where(where)
<span class="hljs-keyword">if</span> options?.limit?
offset = options.offset || <span class="hljs-number">0</span>
res = res[offset til options.limit]
<span class="hljs-keyword">if</span> options?.limit <span class="hljs-keyword">is</span> <span class="hljs-number">1</span>
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> res.length
<span class="hljs-keyword">return</span> @client.Fetch where, (err, data) ~>
<span class="hljs-keyword">return</span> done err <span class="hljs-keyword">if</span> err?
data = @ExtractAssocs data
@collection.push data</pre></div></div>
</li>
<li id="section-12">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-12">¶</a>
</div>
<p>@resource._Changed!</p>
</div>
<div class="content"><div class='highlight'><pre> done <span class="hljs-literal">null</span> [data]
<span class="hljs-keyword">else</span></pre></div></div>
</li>
<li id="section-13">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-13">¶</a>
</div>
<p>console.log ‘BEFORE’ not (@fetched |> find -> console.log it; it === where)</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> (@fetched |> find -> it === where)
@fetched.push where
<span class="hljs-keyword">return</span> @client.List where, (err, data) ~>
<span class="hljs-keyword">return</span> done err <span class="hljs-keyword">if</span> err?
data = map @~ExtractAssocs, data
<span class="hljs-keyword">if</span> data !== res
data
|> each (item) ~>
| item.id <span class="hljs-keyword">in</span> map (.id), @collection => @collection[@collection |> find-index -> it.id <span class="hljs-keyword">is</span> item.id] <<< item
| _ => @collection.push item</pre></div></div>
</li>
<li id="section-14">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-14">¶</a>
</div>
<p>@resource._Changed!</p>
</div>
<div class="content"><div class='highlight'><pre> done <span class="hljs-literal">null</span> data
done <span class="hljs-literal">null</span>, res
Insert: <span class="hljs-function"><span class="hljs-params">(fields, done)</span> -></span>
@client.Create fields, (err, data) ~>
<span class="hljs-keyword">return</span> done err <span class="hljs-keyword">if</span> err?</pre></div></div>
</li>
<li id="section-15">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-15">¶</a>
</div>
<p>@collection.push data
@resource._Changed!</p>
</div>
<div class="content"><div class='highlight'><pre> done <span class="hljs-literal">null</span>, data
Update: <span class="hljs-function"><span class="hljs-params">(fields, where, done)</span> -></span>
@client.Set where.id, fields, (err, data) ~>
<span class="hljs-keyword">return</span> done err <span class="hljs-keyword">if</span> err?
idx = @collection |> find-index -> it.id <span class="hljs-keyword">is</span> where.id</pre></div></div>
</li>
<li id="section-16">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-16">¶</a>
</div>
<p>@collection[idx] <<< data
@resource._Changed!</p>
</div>
<div class="content"><div class='highlight'><pre>
done <span class="hljs-literal">null</span> data</pre></div></div>
</li>
<li id="section-17">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-17">¶</a>
</div>
<p>done null data</p>
</div>
<div class="content"><div class='highlight'><pre>
Delete: <span class="hljs-function"><span class="hljs-params">(where, done)</span> -></span></pre></div></div>
</li>
<li id="section-18">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-18">¶</a>
</div>
<p>idx = @collection |> find-index -> it.id is where.id
save = @collection[idx]</p>
</div>
<div class="content"><div class='highlight'><pre> @collection = _(@collection).reject (item) -> item.id <span class="hljs-keyword">is</span> where.id
@client.Delete where.id, where, (err, data) ~>
<span class="hljs-keyword">return</span> done err <span class="hljs-keyword">if</span> err?</pre></div></div>
</li>
<li id="section-19">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-19">¶</a>
</div>
<p>if err?
@collection.splice idx, 0, save
ChangeWatcher.Invalidate!</p>
</div>
<div class="content"><div class='highlight'><pre> @resource._Changed!
done <span class="hljs-literal">null</span>, <span class="hljs-number">1</span>
<span class="hljs-built_in">module</span>.exports = ClientDB
<span class="hljs-built_in">module</span>.exports.AddTable = <span class="hljs-function"><span class="hljs-params">(name)</span> -></span></pre></div></div>
</li>
<li id="section-20">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-20">¶</a>
</div>
<p>if !(tables[name]?)
tables[name] = []
tables[name]</p>
</div>
</li>
</ul>
</div>
</body>
</html>