atomicrecord
Version:
Super lightweight node.js ActiveRecord ORM layer for FoundationDB
365 lines (329 loc) • 10.5 kB
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>
»
<a href='../alphabetical_index.html' title='Index'>
Index
</a>
»
<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('atomicrecord');
var Person = AtomicRecord({
database: 'myDatabaseName',
dataset: 'people',
partition: false,
primaryKey: {
getKeyFields: function () {
return ['timestamp', 'id'];
},
getDirectoryFields: function () {
return ['region'];
}
},
fields: {
firstName: 'f',
lastName: 'l',
gender: 'g',
age: 'a',
region: 'r',
contacts: 'c',
enabled: 'e',
timestamp: 't'
}
});
</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('transaction complete: person saved', person);
}
}
function transaction(tr, callback) {
// create person instance
var person = new Person();
person.firstName = 'John';
person.lastName = 'Smith';
person.gender = 'M'
person.age = 30
person.region = 'USA';
person.contacts = ['+1-212-555-1234', '+1-917-555-6789'];
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 = 'John';
person.lastName = 'Smith';
person.gender = 'M'
person.age = 30
person.region = 'USA';
person.contacts = ['+1-212-555-1234', '+1-917-555-6789'];
person.timestamp = new Date();
person.save(function (err, p) {
if (err) {
console.error(err);
}
else {
console.log('person saved', 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('transaction complete');
}
}
function transaction(tr, callback) {
// create person instances with initializers
var john = new Person({
firstName: 'John',
lastName: 'Smith',
gender: 'M',
age: 30,
region: 'USA',
contacts: ['+1-212-555-1234', '+1-917-555-6789'],
timestamp: new Date()
});
var sarah = new Person({
firstName: 'Sarah',
lastName: 'Jones',
gender: 'F',
age: 25,
region: 'Europe',
contacts: '+44-207-1234-5678',
timestamp: new Date()
});
var people = [john, sarah];
// batch save every 1sec
var queue = new AtomicRecord.Queue(people);
queue.on('saved', function (count) {
console.log('saved', count);
});
queue.on('error', function (err) {
console.error(err);
});
queue.on('recordsaved', function (record) {
console.log('record saved');
console.log('unaliased document', record.toDocument());
console.log();
console.log('aliased document', 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('saved', function (count) {
console.log('saved', count);
});
queue.on('error', function (err) {
console.error(err);
});
queue.on('recordsaved', function (record) {
console.log('record', record);
});
// continuously push records into the queue for processing
for (var i = 0; i < 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('transaction complete')
}
}
function transaction(tr, callback) {
var finder = Person.find({ region: 'USA' });
finder.on('data', function (data) {
for (var i = 0; i < data.length; i++) {
var record = data[i];
console.log('keySize', record.keySize);
console.log('valueSize', record.valueSize);
console.log('record', record);
console.log()
}
});
finder.on('error', function (err) {
console.error('err', err);
});
finder.on('continue', function () {
tr.options.setReadYourWritesDisable();
});
finder.on('end', function () {
console.log('end');
callback(null);
});
finder.execute(tr, 'array');
}
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: 'USA' });
finder.on('data', function (data) {
for (var i = 0; i < data.length; i++) {
var record = data[i];
console.log('keySize', record.keySize);
console.log('valueSize', record.valueSize);
console.log('record', record);
console.log()
}
});
finder.on('error', function (err) {
console.error('err', err);
});
finder.on('end', function () {
console.log('end');
callback(null);
});
finder.execute('array');
</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 <play@frisb.com></p><p>Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), 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 'AS IS', 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
✲
Press H to see the keyboard shortcuts
✲
<a href='http://twitter.com/netzpirat' target='_parent'>@netzpirat</a>
✲
<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>