Blame view

node_modules/loopback-connector/lib/model-key-composer.js 2.21 KB
f7563de62   Palak Handa   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  // Copyright IBM Corp. 2016. All Rights Reserved.
  // Node module: loopback-connector
  // This file is licensed under the MIT License.
  // License text available at https://opensource.org/licenses/MIT
  
  'use strict';
  
  var createPromiseCallback = require('./utils').createPromiseCallback;
  var debug = require('debug')('loopback:connector:model-key-composer');
  var g = require('strong-globalize')();
  
  /**
   * 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'
    var value = encodeURIComponent(modelName) + ':' + key;
  
    setImmediate(function() {
      cb(null, value);
    });
    return cb.promise;
  };
  
  var PARSE_KEY_REGEX = /^([^:]*):(.*)/;
  
  /**
   * 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();
  
    var matchResult = composed.match(PARSE_KEY_REGEX);
    if (matchResult) {
      var result = {
        modelName: matchResult[1],
        key: matchResult[2],
      };
      setImmediate(function() {
        cb(null, result);
      });
    } else {
      debug('Invalid key - missing model-name prefix: %s', composed);
      var err = new Error(g.f(
        'Invalid key %j - missing model-name prefix',
        composed));
      err.code = 'NO_MODEL_PREFIX';
      setImmediate(function() {
        cb(err);
      });
    }
    return cb.promise;
  };