2019-04-18 21:32:38 +00:00
|
|
|
// Copyright IBM Corp. 2016,2019. All Rights Reserved.
|
2016-10-19 10:55:35 +00:00
|
|
|
// Node module: loopback-connector
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2019-08-02 13:50:51 +00:00
|
|
|
const createPromiseCallback = require('./utils').createPromiseCallback;
|
|
|
|
const debug = require('debug')('loopback:connector:model-key-composer');
|
|
|
|
const g = require('strong-globalize')();
|
2016-10-19 10:55:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a single key string from a tuple (modelName, key).
|
|
|
|
*
|
|
|
|
* This method is typically used by KeyValue connectors to build a single
|
|
|
|
* key string for a given modelName+key tuple.
|
|
|
|
*
|
|
|
|
* @param {String} modelName
|
|
|
|
* @param {String} key
|
|
|
|
* @callback {Function} cb The callback to receive the composed value.
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {String} composedKey
|
|
|
|
* @promise
|
|
|
|
*/
|
|
|
|
exports.compose = function composeKeyFromModelNameAndKey(modelName, key, cb) {
|
|
|
|
cb = cb || createPromiseCallback();
|
|
|
|
|
|
|
|
// Escape model name to prevent collision
|
|
|
|
// 'model' + 'foo:bar' --vs-- 'model:foo' + 'bar'
|
2019-08-02 13:50:51 +00:00
|
|
|
const value = encodeURIComponent(modelName) + ':' + key;
|
2016-10-19 10:55:35 +00:00
|
|
|
|
|
|
|
setImmediate(function() {
|
|
|
|
cb(null, value);
|
|
|
|
});
|
|
|
|
return cb.promise;
|
|
|
|
};
|
|
|
|
|
2019-08-02 13:50:51 +00:00
|
|
|
const PARSE_KEY_REGEX = /^([^:]*):(.*)/;
|
2016-10-19 10:55:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a composed key string into a tuple (modelName, key).
|
|
|
|
*
|
|
|
|
* This method is typically used by KeyValue connectors to parse a composed
|
|
|
|
* key string returned by SCAN/ITERATE method back to the expected
|
|
|
|
* modelName+tuple key.
|
|
|
|
*
|
|
|
|
* @param {String} composed The composed key as returned by `composeKey`
|
|
|
|
* @callback {Function} cb The callback to receive the parsed result.
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Object} result The result with properties `modelName` and `key`.
|
|
|
|
* @promise
|
|
|
|
*/
|
|
|
|
exports.parse = function(composed, cb) {
|
|
|
|
cb = cb || createPromiseCallback();
|
|
|
|
|
2019-08-02 13:50:51 +00:00
|
|
|
const matchResult = composed.match(PARSE_KEY_REGEX);
|
2016-10-19 10:55:35 +00:00
|
|
|
if (matchResult) {
|
2019-08-02 13:50:51 +00:00
|
|
|
const result = {
|
2016-10-19 10:55:35 +00:00
|
|
|
modelName: matchResult[1],
|
|
|
|
key: matchResult[2],
|
|
|
|
};
|
|
|
|
setImmediate(function() {
|
|
|
|
cb(null, result);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
debug('Invalid key - missing model-name prefix: %s', composed);
|
2019-08-02 13:50:51 +00:00
|
|
|
const err = new Error(g.f(
|
2016-10-19 10:55:35 +00:00
|
|
|
'Invalid key %j - missing model-name prefix',
|
2019-11-01 12:45:20 +00:00
|
|
|
composed,
|
2019-08-02 13:50:51 +00:00
|
|
|
));
|
2016-10-19 10:55:35 +00:00
|
|
|
err.code = 'NO_MODEL_PREFIX';
|
|
|
|
setImmediate(function() {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return cb.promise;
|
|
|
|
};
|