diff --git a/common/models/checkpoint.js b/common/models/checkpoint.js index 9ac1f5b5..ed57de53 100644 --- a/common/models/checkpoint.js +++ b/common/models/checkpoint.js @@ -2,27 +2,7 @@ * Module Dependencies. */ -var PersistedModel = require('../../lib/loopback').PersistedModel - , loopback = require('../../lib/loopback') - , assert = require('assert'); - -/** - * Properties - */ - -var properties = { - seq: {type: Number}, - time: {type: Date, default: Date}, - sourceId: {type: String} -}; - -/** - * Options - */ - -var options = { - -}; +var assert = require('assert'); /** * Checkpoint list entry. @@ -30,48 +10,54 @@ var options = { * @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 + * + * @class Checkpoint * @inherits {PersistedModel} */ -var Checkpoint = module.exports = PersistedModel.extend('Checkpoint', properties, options); +module.exports = function(Checkpoint) { -/** - * Get the current checkpoint id - * @callback {Function} callback - * @param {Error} err - * @param {Number} checkpointId The current checkpoint id - */ + // Workaround for https://github.com/strongloop/loopback/issues/292 + Checkpoint.definition.rawProperties.time.default = + Checkpoint.definition.properties.time.default = function() { + return new Date(); + }; -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) { - cb(null, checkpoint.seq); - } else { - Checkpoint.create({seq: 0}, function(err, checkpoint) { - if(err) return cb(err); + /** + * 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) { cb(null, checkpoint.seq); - }); - } - }); -} - -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 { + Checkpoint.create({seq: 0}, function(err, checkpoint) { + if (err) return cb(err); + cb(null, checkpoint.seq); + }); + } }); - } else { - next(); } -} + 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(); + } + } +}; diff --git a/common/models/checkpoint.json b/common/models/checkpoint.json new file mode 100644 index 00000000..d557ffb5 --- /dev/null +++ b/common/models/checkpoint.json @@ -0,0 +1,14 @@ +{ + "name": "Checkpoint", + "properties": { + "seq": { + "type": "number" + }, + "time": { + "type": "date" + }, + "sourceId": { + "type": "string" + } + } +} diff --git a/lib/builtin-models.js b/lib/builtin-models.js index 88677a9e..a6175e9f 100644 --- a/lib/builtin-models.js +++ b/lib/builtin-models.js @@ -34,7 +34,10 @@ module.exports = function(loopback) { require('../common/models/user.js')); loopback.Change = require('../common/models/change'); - loopback.Checkpoint = require('../common/models/checkpoint'); + + loopback.Checkpoint = createModel( + require('../common/models/checkpoint.json'), + require('../common/models/checkpoint.js')); /*! * Automatically attach these models to dataSources