2014-01-26 22:02:56 +00:00
|
|
|
/**
|
|
|
|
* Module Dependencies.
|
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
var PersistedModel = require('../loopback').PersistedModel
|
2014-01-26 22:02:56 +00:00
|
|
|
, loopback = require('../loopback')
|
|
|
|
, assert = require('assert');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Properties
|
|
|
|
*/
|
|
|
|
|
|
|
|
var properties = {
|
2014-05-10 00:19:32 +00:00
|
|
|
seq: {type: Number},
|
|
|
|
time: {type: Date, default: Date},
|
2014-01-26 22:02:56 +00:00
|
|
|
sourceId: {type: String}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Options
|
|
|
|
*/
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* @class
|
2014-06-05 07:45:09 +00:00
|
|
|
* @inherits {PersistedModel}
|
2014-01-26 22:02:56 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
var Checkpoint = module.exports = PersistedModel.extend('Checkpoint', properties, options);
|
2014-01-26 22:02:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current checkpoint id
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Number} checkpointId The current checkpoint id
|
|
|
|
*/
|
|
|
|
|
|
|
|
Checkpoint.current = function(cb) {
|
2014-05-10 00:19:32 +00:00
|
|
|
var Checkpoint = this;
|
2014-01-26 22:02:56 +00:00
|
|
|
this.find({
|
|
|
|
limit: 1,
|
2014-05-10 00:19:32 +00:00
|
|
|
sort: 'seq DESC'
|
2014-01-28 20:54:41 +00:00
|
|
|
}, function(err, checkpoints) {
|
2014-01-26 22:02:56 +00:00
|
|
|
if(err) return cb(err);
|
2014-05-10 00:19:32 +00:00
|
|
|
var checkpoint = checkpoints[0];
|
|
|
|
if(checkpoint) {
|
|
|
|
cb(null, checkpoint.seq);
|
|
|
|
} else {
|
|
|
|
Checkpoint.create({seq: 0}, function(err, checkpoint) {
|
|
|
|
if(err) return cb(err);
|
|
|
|
cb(null, checkpoint.seq);
|
|
|
|
});
|
|
|
|
}
|
2014-01-26 22:02:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-05-10 00:19:32 +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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|