2019-05-08 15:45:37 +00:00
|
|
|
// Copyright IBM Corp. 2016,2018. All Rights Reserved.
|
|
|
|
// Node module: loopback-datasource-juggler
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-08-08 08:15:22 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-01-10 08:18:04 +00:00
|
|
|
exports.givenCacheItem = givenCacheItem;
|
|
|
|
exports.givenKeys = givenKeys;
|
|
|
|
exports.givenModel = givenModel;
|
2018-10-05 07:30:02 +00:00
|
|
|
exports.delay = delay;
|
2017-01-10 08:18:04 +00:00
|
|
|
|
|
|
|
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 14:43:40 +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 14:43:40 +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 14:54:29 +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 14:43:40 +00:00
|
|
|
}
|
2018-10-05 07:30:02 +00:00
|
|
|
|
|
|
|
function delay(ms) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(resolve, ms);
|
|
|
|
});
|
|
|
|
}
|