models: move Checkpoint LDL def into a json file
This commit is contained in:
parent
461ae92c1c
commit
6cbc231fba
|
@ -2,27 +2,7 @@
|
||||||
* Module Dependencies.
|
* Module Dependencies.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var PersistedModel = require('../../lib/loopback').PersistedModel
|
var assert = require('assert');
|
||||||
, loopback = require('../../lib/loopback')
|
|
||||||
, assert = require('assert');
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Properties
|
|
||||||
*/
|
|
||||||
|
|
||||||
var properties = {
|
|
||||||
seq: {type: Number},
|
|
||||||
time: {type: Date, default: Date},
|
|
||||||
sourceId: {type: String}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Options
|
|
||||||
*/
|
|
||||||
|
|
||||||
var options = {
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checkpoint list entry.
|
* Checkpoint list entry.
|
||||||
|
@ -31,47 +11,53 @@ var options = {
|
||||||
* @property time {Number} the time when the checkpoint was created
|
* @property time {Number} the time when the checkpoint was created
|
||||||
* @property sourceId {String} the source identifier
|
* @property sourceId {String} the source identifier
|
||||||
*
|
*
|
||||||
* @class
|
* @class Checkpoint
|
||||||
* @inherits {PersistedModel}
|
* @inherits {PersistedModel}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var Checkpoint = module.exports = PersistedModel.extend('Checkpoint', properties, options);
|
module.exports = function(Checkpoint) {
|
||||||
|
|
||||||
/**
|
// Workaround for https://github.com/strongloop/loopback/issues/292
|
||||||
* Get the current checkpoint id
|
Checkpoint.definition.rawProperties.time.default =
|
||||||
* @callback {Function} callback
|
Checkpoint.definition.properties.time.default = function() {
|
||||||
* @param {Error} err
|
return new Date();
|
||||||
* @param {Number} checkpointId The current checkpoint id
|
};
|
||||||
*/
|
|
||||||
|
|
||||||
Checkpoint.current = function(cb) {
|
/**
|
||||||
var Checkpoint = this;
|
* Get the current checkpoint id
|
||||||
this.find({
|
* @callback {Function} callback
|
||||||
limit: 1,
|
* @param {Error} err
|
||||||
order: 'seq DESC'
|
* @param {Number} checkpointId The current checkpoint id
|
||||||
}, function(err, checkpoints) {
|
*/
|
||||||
if(err) return cb(err);
|
|
||||||
var checkpoint = checkpoints[0];
|
Checkpoint.current = function(cb) {
|
||||||
if(checkpoint) {
|
var Checkpoint = this;
|
||||||
cb(null, checkpoint.seq);
|
this.find({
|
||||||
} else {
|
limit: 1,
|
||||||
Checkpoint.create({seq: 0}, function(err, checkpoint) {
|
order: 'seq DESC'
|
||||||
if(err) return cb(err);
|
}, function(err, checkpoints) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
var checkpoint = checkpoints[0];
|
||||||
|
if (checkpoint) {
|
||||||
cb(null, checkpoint.seq);
|
cb(null, checkpoint.seq);
|
||||||
});
|
} else {
|
||||||
}
|
Checkpoint.create({seq: 0}, function(err, checkpoint) {
|
||||||
});
|
if (err) return cb(err);
|
||||||
}
|
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 {
|
|
||||||
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
{
|
||||||
|
"name": "Checkpoint",
|
||||||
|
"properties": {
|
||||||
|
"seq": {
|
||||||
|
"type": "number"
|
||||||
|
},
|
||||||
|
"time": {
|
||||||
|
"type": "date"
|
||||||
|
},
|
||||||
|
"sourceId": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -34,7 +34,10 @@ module.exports = function(loopback) {
|
||||||
require('../common/models/user.js'));
|
require('../common/models/user.js'));
|
||||||
|
|
||||||
loopback.Change = require('../common/models/change');
|
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
|
* Automatically attach these models to dataSources
|
||||||
|
|
Loading…
Reference in New Issue