2014-01-26 22:02:56 +00:00
|
|
|
/**
|
|
|
|
* Module Dependencies.
|
|
|
|
*/
|
|
|
|
|
2014-10-13 09:45:06 +00:00
|
|
|
var assert = require('assert');
|
2014-01-26 22:02:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checkpoint list entry.
|
|
|
|
*
|
|
|
|
* @property id {Number} the sequencial identifier of a checkpoint
|
|
|
|
* @property time {Number} the time when the checkpoint was created
|
|
|
|
* @property sourceId {String} the source identifier
|
2014-10-13 09:45:06 +00:00
|
|
|
*
|
|
|
|
* @class Checkpoint
|
2014-06-05 07:45:09 +00:00
|
|
|
* @inherits {PersistedModel}
|
2014-01-26 22:02:56 +00:00
|
|
|
*/
|
|
|
|
|
2014-10-13 09:45:06 +00:00
|
|
|
module.exports = function(Checkpoint) {
|
|
|
|
|
|
|
|
// Workaround for https://github.com/strongloop/loopback/issues/292
|
|
|
|
Checkpoint.definition.rawProperties.time.default =
|
|
|
|
Checkpoint.definition.properties.time.default = function() {
|
|
|
|
return new Date();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current checkpoint id
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Number} checkpointId The current checkpoint id
|
|
|
|
*/
|
|
|
|
|
|
|
|
Checkpoint.current = function(cb) {
|
|
|
|
var Checkpoint = this;
|
|
|
|
this.find({
|
|
|
|
limit: 1,
|
|
|
|
order: 'seq DESC'
|
|
|
|
}, function(err, checkpoints) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
var checkpoint = checkpoints[0];
|
|
|
|
if (checkpoint) {
|
2014-05-10 00:19:32 +00:00
|
|
|
cb(null, checkpoint.seq);
|
2014-10-13 09:45:06 +00:00
|
|
|
} else {
|
|
|
|
Checkpoint.create({seq: 0}, function(err, checkpoint) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
cb(null, checkpoint.seq);
|
|
|
|
});
|
|
|
|
}
|
2014-05-10 00:19:32 +00:00
|
|
|
});
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-05-10 00:19:32 +00:00
|
|
|
|
2014-10-13 09:45:06 +00:00
|
|
|
Checkpoint.beforeSave = function(next, model) {
|
|
|
|
if (!model.getId() && model.seq === undefined) {
|
|
|
|
model.constructor.current(function(err, seq) {
|
|
|
|
if (err) return next(err);
|
|
|
|
model.seq = seq + 1;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-10-13 09:45:06 +00:00
|
|
|
};
|