insomnia/app/backend/database/models/stats.js

34 lines
751 B
JavaScript
Raw Normal View History

2016-09-21 20:32:45 +00:00
'use strict';
2016-09-22 01:26:40 +00:00
const db = require('../index');
2016-09-21 20:32:45 +00:00
module.exports.type = 'Stats';
module.exports.prefix = 'sta';
2016-09-21 21:46:42 +00:00
module.exports.slug = 'stats';
module.exports.init = () => db.initModel({
2016-09-21 20:32:45 +00:00
lastLaunch: Date.now(),
lastVersion: null,
launches: 0
});
module.exports.create = (patch = {}) => {
return db.docCreate(module.exports.type, patch);
};
module.exports.update = patch => {
return module.exports.get().then(stats => {
return db.docUpdate(stats, patch);
});
};
module.exports.get = () => {
return db.all(module.exports.type).then(results => {
if (results.length === 0) {
return module.exports.create()
.then(module.exports.get);
} else {
return new Promise(resolve => resolve(results[0]));
}
});
};