github.com/outbrain/consul@v1.4.5/ui-v2/app/services/repository/kv.js (about)

     1  import RepositoryService from 'consul-ui/services/repository';
     2  import { Promise } from 'rsvp';
     3  import isFolder from 'consul-ui/utils/isFolder';
     4  import { get, set } from '@ember/object';
     5  import { PRIMARY_KEY } from 'consul-ui/models/kv';
     6  
     7  const modelName = 'kv';
     8  export default RepositoryService.extend({
     9    getModelName: function() {
    10      return modelName;
    11    },
    12    getPrimaryKey: function() {
    13      return PRIMARY_KEY;
    14    },
    15    // this one gives you the full object so key,values and meta
    16    findBySlug: function(key, dc) {
    17      if (isFolder(key)) {
    18        const id = JSON.stringify([dc, key]);
    19        let item = get(this, 'store').peekRecord(this.getModelName(), id);
    20        if (!item) {
    21          item = this.create();
    22          set(item, 'Key', key);
    23          set(item, 'Datacenter', dc);
    24        }
    25        return Promise.resolve(item);
    26      }
    27      return get(this, 'store').queryRecord(this.getModelName(), {
    28        id: key,
    29        dc: dc,
    30      });
    31    },
    32    // this one only gives you keys
    33    // https://www.consul.io/api/kv.html
    34    findAllBySlug: function(key, dc) {
    35      if (key === '/') {
    36        key = '';
    37      }
    38      return this.get('store')
    39        .query(this.getModelName(), {
    40          id: key,
    41          dc: dc,
    42          separator: '/',
    43        })
    44        .then(function(items) {
    45          return items.filter(function(item) {
    46            return key !== get(item, 'Key');
    47          });
    48        })
    49        .catch(e => {
    50          if (e.errors && e.errors[0] && e.errors[0].status == '404') {
    51            const id = JSON.stringify([dc, key]);
    52            const record = get(this, 'store').peekRecord(this.getModelName(), id);
    53            if (record) {
    54              record.destroyRecord();
    55            }
    56          }
    57          throw e;
    58        });
    59    },
    60  });