loopback-datasource-juggler/test/kvao/_helpers.js

37 lines
882 B
JavaScript
Raw Normal View History

'use strict';
2018-12-07 16:13:48 +00:00
const Promise = require('bluebird');
exports.givenCacheItem = givenCacheItem;
exports.givenKeys = givenKeys;
exports.givenModel = givenModel;
function givenCacheItem(dataSourceFactory) {
const modelProperties = {
key: String,
value: 'Any',
};
return givenModel(dataSourceFactory, 'CacheItem', modelProperties);
2018-12-07 16:13:48 +00:00
}
function givenModel(dataSourceFactory, modelName,
2018-06-12 07:13:32 +00:00
modelProperties, options) {
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
}
function givenKeys(Model, keys, cb) {
2018-12-07 16:13:48 +00:00
let 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;
2018-12-07 16:13:48 +00:00
}