Skip to content

Instantly share code, notes, and snippets.

@cdaringe
Created September 1, 2015 21:07
Show Gist options
  • Save cdaringe/a4a5b346039cd8309d36 to your computer and use it in GitHub Desktop.
Save cdaringe/a4a5b346039cd8309d36 to your computer and use it in GitHub Desktop.
coinstac-analysis-model
// @package analysis.js
// @note analysis Model
// note, using es5 for server compatibility (not that it has to run on the server, but we run `node file.js` to debug and test)
var _ = require('lodash');
var Model = require('ampersand-model');
var PouchDocument = require('./pouch-document'); // &-Model with _id & _rev props
var isoDateMixin = require('ampersand-state-mixin-datatype-iso-date');
var md5 = require('md5');
// alternatively could do Model.extend(PouchDocument, { ... our new vals ... });
var Analysis = PouchDocument.extend(isoDateMixin, {
props: {
// _id & _rev specified from PouchDocument
consortiumId: {
type: 'string',
required: true
},
sha: ['string', true], // string, required
complete: ['iso-date', true], // forces dates in ISO 8601 long string
result: ['any', true] // ? different analysis may need different formats
},
derived: {
_id: {
// id should be a unique value representing this file in this consoritum
deps: ['sha', 'consortiumId'],
fn: function() { // note, Model _caches_ derived attributes :)
return md5(this.sha + this.consortiumId)
}
}
},
/**
* Extend serialize to get derived attrs on the cloned obj
**/
serialize: function() {
var tempSerialized = Model.prototype.serialize.call(this);
_.extend(tempSerialized, {
_id: this._id
});
return tempSerialized;
}
});
module.exports = Analysis;
var a1;
try {
a1 = new Analysis({
_someInvalidProp: 123,
_id: 23 // shouldnt be a number,
});
console.dir(a1.serialize());
} catch(err) {
console.error(err.message);
// pass
}
var a2 = new Analysis({
consortiumId: 'abc',
sha: '123',
// complete: 'Tue Sep 01 2015 14:01:56 GMT-0700', // makes a biiig fuss if not in ISO 8601 format
complete: require('moment')().format(),
result: { bananas: 'oranges' },
_rev: 'asdf923-sd-2'
});
console.dir(a2.serialize());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment