2016-08-08 08:15:22 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-08-15 12:55:26 +00:00
|
|
|
var Promise = require('bluebird');
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2016-08-08 08:15:22 +00:00
|
|
|
};
|
2016-08-15 12:55:26 +00:00
|
|
|
|
2017-01-10 08:18:04 +00:00
|
|
|
function givenKeys(Model, keys, cb) {
|
2016-08-15 12:55:26 +00:00
|
|
|
var p = Promise.all(
|
|
|
|
keys.map(function(k) {
|
|
|
|
return Model.set(k, 'value-' + k);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
if (cb) {
|
|
|
|
p = p.then(function(r) { cb(null, r); }, cb);
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
};
|