jsbin/lib/models/bin.js
Aron Carroll 84d4292d99 Create an index file for all models
Also refactored the model modules to export a factory function rather
than returning a function itself. This seems to read a bit better.
2012-04-30 23:19:08 +01:00

46 lines
1.2 KiB
JavaScript

var crypto = require('crypto');
module.exports.create = function (store) {
var bin = {
load: function (params, fn) {
store.getBin.apply(store, arguments);
},
latest: function (params, fn) {
store.getLatestBin.apply(store, arguments);
},
// Create a new bin.
create: function (data, fn) {
store.generateBinId(function (err, id) {
if (err) {
return fn(err);
}
data.url = id;
data.revision = 1;
data.streamingKey = bin.createStreamingKey(id, data.revision);
store.setBin(data, function (err, id) {
data.id = id;
fn(err || null, err ? undefined : data);
});
});
},
createRevision: function (data, fn) {
data.streamingKey = bin.createStreamingKey(data.url, data.revision);
store.setBin(data, function (err, id) {
data.id = id;
fn(err || null, err ? undefined : data);
});
},
// Create a new revision.
updatePanel: function (panel, data, fn) {
store.setBinPanel.apply(store, arguments);
},
createStreamingKey: function (id, rev) {
var key = "" + id + rev + Math.random();
return crypto.createHash('md5').update(key).digest('hex');
}
};
return bin;
};