2016-08-08 08:15:22 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-07 16:13:48 +00:00
|
|
|
const Promise = require('bluebird');
|
2016-08-15 12:55:26 +00:00
|
|
|
|
2017-01-10 08:18:04 +00:00
|
|
|
exports.givenCacheItem = givenCacheItem;
|
|
|
|
exports.givenKeys = givenKeys;
|
|
|
|
exports.givenModel = givenModel;
|
|
|
|
|
|
|
|
function givenCacheItem(dataSourceFactory) {
|
|
|
|
const modelProperties = {
|
2016-08-08 08:15:22 +00:00
|
|
|
key: String,
|
2017-01-10 08:18:04 +00:00
|
|
|
value: 'Any',
|
|
|
|
};
|
|
|
|
return givenModel(dataSourceFactory, 'CacheItem', modelProperties);
|
2018-12-07 16:13:48 +00:00
|
|
|
}
|
2017-01-10 08:18:04 +00:00
|
|
|
|
|
|
|
function givenModel(dataSourceFactory, modelName,
|
2018-06-12 07:13:32 +00:00
|
|
|
modelProperties, options) {
|
2017-01-10 08:18:04 +00:00
|
|
|
const dataSource = dataSourceFactory();
|
|
|
|
const Model = dataSource.createModel(modelName, modelProperties);
|
|
|
|
const p = 'deleteAll' in dataSource.connector ?
|
|
|
|
Model.deleteAll() : Promise.resolve();
|
|
|
|
return p.then(() => Model);
|
2018-12-07 16:13:48 +00:00
|
|
|
}
|
2016-08-15 12:55:26 +00:00
|
|
|
|
2017-01-10 08:18:04 +00:00
|
|
|
function givenKeys(Model, keys, cb) {
|
2018-12-07 16:13:48 +00:00
|
|
|
let p = Promise.all(
|
2016-08-15 12:55:26 +00:00
|
|
|
keys.map(function(k) {
|
|
|
|
return Model.set(k, 'value-' + k);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
if (cb) {
|
|
|
|
p = p.then(function(r) { cb(null, r); }, cb);
|
|
|
|
}
|
|
|
|
return p;
|
2018-12-07 16:13:48 +00:00
|
|
|
}
|