UNPKG

atomicrecord

Version:

Super lightweight node.js ActiveRecord ORM layer for FoundationDB

365 lines (329 loc) 10.5 kB
<!DOCTYPE html> <html> <head> <meta charset='UTF-8'> <title>Documentation</title> <script src='../javascript/application.js'></script> <script src='../javascript/search.js'></script> <link rel='stylesheet' href='../stylesheets/application.css' type='text/css'> <script> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'undefined']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> </head> <body> <div id='base' data-path='../'></div> <div id='header'> <div id='menu'> <a href='../extra/README.md.html' title='AtomicRecord'> AtomicRecord </a> &raquo; <a href='../alphabetical_index.html' title='Index'> Index </a> &raquo; <span class='title'>README.md</span> </div> </div> <div id='content'> <nav class='toc'> <p class='title'> <a class='hide_toc' href='#'> <strong>Table of Contents</strong> </a> <small> (<a class='float_toc' href='#'>left</a>) </small> </p> </nav> <div id='filecontents'> <h1 id="atomicrecord">AtomicRecord</h1><p>AtomicRecord is a super lightweight node.js ActiveRecord ORM layer for FoundationDB.</p><p>This module is still early alpha and work is in progress. All contributions are welcome.</p><h2 id="example-usage">Example Usage</h2> <h4 id="create-atomicrecord-class">Create AtomicRecord Class</h4> <pre><code class="lang-js">var AtomicRecord = require(&#39;atomicrecord&#39;); var Person = AtomicRecord({ database: &#39;myDatabaseName&#39;, dataset: &#39;people&#39;, partition: false, primaryKey: { getKeyFields: function () { return [&#39;timestamp&#39;, &#39;id&#39;]; }, getDirectoryFields: function () { return [&#39;region&#39;]; } }, fields: { firstName: &#39;f&#39;, lastName: &#39;l&#39;, gender: &#39;g&#39;, age: &#39;a&#39;, region: &#39;r&#39;, contacts: &#39;c&#39;, enabled: &#39;e&#39;, timestamp: &#39;t&#39; } }); </code></pre> <h4 id="transactional-save">Transactional Save</h4> <pre><code class="lang-js">function complete(err, person) { if (err) { console.error(err); } else { console.log(&#39;transaction complete: person saved&#39;, person); } } function transaction(tr, callback) { // create person instance var person = new Person(); person.firstName = &#39;John&#39;; person.lastName = &#39;Smith&#39;; person.gender = &#39;M&#39; person.age = 30 person.region = &#39;USA&#39;; person.contacts = [&#39;+1-212-555-1234&#39;, &#39;+1-917-555-6789&#39;]; person.timestamp = new Date(); person.save(tr, callback); } Person.doTransaction(transaction, complete); </code></pre> <h4 id="non-transactional-save">Non-Transactional Save</h4> <pre><code class="lang-js">// create person instance var person = new Person(); person.firstName = &#39;John&#39;; person.lastName = &#39;Smith&#39;; person.gender = &#39;M&#39; person.age = 30 person.region = &#39;USA&#39;; person.contacts = [&#39;+1-212-555-1234&#39;, &#39;+1-917-555-6789&#39;]; person.timestamp = new Date(); person.save(function (err, p) { if (err) { console.error(err); } else { console.log(&#39;person saved&#39;, p); } }); </code></pre> <h4 id="transactional-atomicqueue-batch-save">Transactional AtomicQueue Batch Save</h4> <pre><code class="lang-js">function complete(err) { if (err) { console.error(err); } else { console.log(&#39;transaction complete&#39;); } } function transaction(tr, callback) { // create person instances with initializers var john = new Person({ firstName: &#39;John&#39;, lastName: &#39;Smith&#39;, gender: &#39;M&#39;, age: 30, region: &#39;USA&#39;, contacts: [&#39;+1-212-555-1234&#39;, &#39;+1-917-555-6789&#39;], timestamp: new Date() }); var sarah = new Person({ firstName: &#39;Sarah&#39;, lastName: &#39;Jones&#39;, gender: &#39;F&#39;, age: 25, region: &#39;Europe&#39;, contacts: &#39;+44-207-1234-5678&#39;, timestamp: new Date() }); var people = [john, sarah]; // batch save every 1sec var queue = new AtomicRecord.Queue(people); queue.on(&#39;saved&#39;, function (count) { console.log(&#39;saved&#39;, count); }); queue.on(&#39;error&#39;, function (err) { console.error(err); }); queue.on(&#39;recordsaved&#39;, function (record) { console.log(&#39;record saved&#39;); console.log(&#39;unaliased document&#39;, record.toDocument()); console.log(); console.log(&#39;aliased document&#39;, record.toDocument(true)); console.log(); }); queue.save(tr, callback); } Person.doTransaction(transaction, complete); </code></pre> <h4 id="non-transactional-atomicqueue-batch-save">Non-Transactional AtomicQueue Batch Save</h4> <pre><code class="lang-js">// batch save every 1sec var queue = new AtomicRecord.Queue(people, { batchSaveDelay: 1000 }); queue.on(&#39;saved&#39;, function (count) { console.log(&#39;saved&#39;, count); }); queue.on(&#39;error&#39;, function (err) { console.error(err); }); queue.on(&#39;recordsaved&#39;, function (record) { console.log(&#39;record&#39;, record); }); // continuously push records into the queue for processing for (var i = 0; i &lt; 10000; i++) { queue.add(people[i]); // people is a pseudo-array } </code></pre> <h4 id="transactional-find">Transactional Find</h4> <pre><code class="lang-js">function complete(err) { if (err) { console.error(err); } else { console.log(&#39;transaction complete&#39;) } } function transaction(tr, callback) { var finder = Person.find({ region: &#39;USA&#39; }); finder.on(&#39;data&#39;, function (data) { for (var i = 0; i &lt; data.length; i++) { var record = data[i]; console.log(&#39;keySize&#39;, record.keySize); console.log(&#39;valueSize&#39;, record.valueSize); console.log(&#39;record&#39;, record); console.log() } }); finder.on(&#39;error&#39;, function (err) { console.error(&#39;err&#39;, err); }); finder.on(&#39;continue&#39;, function () { tr.options.setReadYourWritesDisable(); }); finder.on(&#39;end&#39;, function () { console.log(&#39;end&#39;); callback(null); }); finder.execute(tr, &#39;array&#39;); } Person.doTransaction(transaction, complete); </code></pre> <h4 id="non-transactional-find">Non-Transactional Find</h4> <pre><code class="lang-js">var finder = Person.find({ region: &#39;USA&#39; }); finder.on(&#39;data&#39;, function (data) { for (var i = 0; i &lt; data.length; i++) { var record = data[i]; console.log(&#39;keySize&#39;, record.keySize); console.log(&#39;valueSize&#39;, record.valueSize); console.log(&#39;record&#39;, record); console.log() } }); finder.on(&#39;error&#39;, function (err) { console.error(&#39;err&#39;, err); }); finder.on(&#39;end&#39;, function () { console.log(&#39;end&#39;); callback(null); }); finder.execute(&#39;array&#39;); </code></pre> <h2 id="roadmap">Roadmap</h2><p>More comments and elaborate on README</p><p>Finalize Indexing</p><p>Polish the Finder class and iteratorTypes</p><p>Relational data</p><h2 id="installation">Installation</h2> <pre><code>cd node_modules git clone git@github.com:frisb/atomicrecord.git cd atomicrecord npm install </code></pre><h2 id="license">License</h2><p>(The MIT License)</p><p>Copyright (c) frisB.com &lt;play@frisb.com&gt;</p><p>Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the &#39;Software&#39;), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:</p><p>The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.</p><p>THE SOFTWARE IS PROVIDED &#39;AS IS&#39;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p><p><a href="https://github.com/igrigorik/ga-beacon"><img src="https://ga-beacon.appspot.com/UA-40562957-12/atomicrecord/readme" alt="Analytics"></a></p> </div> </div> <div id='footer'> January 20, 15 12:49:54 by <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> Codo </a> 2.0.9 &#10034; Press H to see the keyboard shortcuts &#10034; <a href='http://twitter.com/netzpirat' target='_parent'>@netzpirat</a> &#10034; <a href='http://twitter.com/_inossidabile' target='_parent'>@_inossidabile</a> </div> <iframe id='search_frame'></iframe> <div id='fuzzySearch'> <input type='text'> <ol></ol> </div> <div id='help'> <p> Quickly fuzzy find classes, mixins, methods, file: </p> <ul> <li> <span>T</span> Open fuzzy finder dialog </li> </ul> <p> Control the navigation frame: </p> <ul> <li> <span>L</span> Toggle list view </li> <li> <span>C</span> Show class list </li> <li> <span>I</span> Show mixin list </li> <li> <span>F</span> Show file list </li> <li> <span>M</span> Show method list </li> <li> <span>E</span> Show extras list </li> </ul> <p> You can focus and blur the search input: </p> <ul> <li> <span>S</span> Focus search input </li> <li> <span>Esc</span> Blur search input </li> </ul> </div> </body> </html>