all files / couch/ couch.js

87.18% Statements 34/39
50% Branches 5/10
80% Functions 12/15
87.18% Lines 34/39
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125                    120× 120×     120× 120×   120×       120×                                   213× 213× 211×         213× 213×                                         119× 119×       119× 119× 119× 119× 119×   119×                 120×       120×       120×             120× 120× 120× 120×                    
import Ember from 'ember';
import { object } from './util/computed';
import { destroyObject } from './util/destroy';
import Changes from './couch/changes/mixin';
import Request from './couch/request/mixin';
 
const {
  computed,
  getOwner
} = Ember;
 
const normalizedUrl = () => {
  return computed('url', function() {
    let url = this.get('url');
    Iif(!url) {
      return url;
    }
    Eif(!url.endsWith('/')) {
      url = `${url}/`;
    }
    return url;
  }).readOnly();
};
 
const session = () => {
  return computed(function() {
    return getOwner(this).factoryFor('couch:session').create({ couch: this });
  }).readOnly();
};
 
export default Ember.Object.extend(
  Changes,
  Request, {
 
  couches: null,
  url: null,
 
  normalizedUrl: normalizedUrl(),
 
  openDatabases: object(),
 
  session: session(),
 
  resolveURL(path) {
    let url = this.get("normalizedUrl");
    if(path) {
      return `${url}${path}`;
    }
    return url;
  },
 
  request(opts={}) {
    opts.url = this.resolveURL(opts.url);
    return this._super(opts);
  },
 
  info() {
    return this.request({
      type: 'get',
      json: true
    });
  },
 
  uuids(count) {
    return this.request({
      type: 'get',
      url: '_uuids',
      qs: {
        count: count || 1
      },
      json: true
    });
  },
 
  createDatabase(name) {
    let couch = this;
    return getOwner(this).factoryFor('couch:database').create({ couch, name });
  },
 
  database(name) {
    let dbs = this.get('openDatabases');
    let db = dbs[name];
    Eif(!db) {
      db = this.createDatabase(name);
      dbs[name] = db;
    }
    return db;
  },
 
  db: computed(function() {
    let couch = this;
    return getOwner(this).factoryFor('couch:databases').create({ couch });
  }).readOnly(),
 
  _destroyRequest() {
    this.get('__request').destroy();
  },
 
  _destroyOpenDatabases() {
    destroyObject(this.get('openDatabases'));
  },
 
  _destroySession() {
    this.get('session').destroy();
  },
 
  createChanges(opts) {
    return getOwner(this).factoryFor('couch:couch-changes').create({ couch: this, opts });
  },
 
  willDestroy() {
    this._destroyRequest();
    this._destroyOpenDatabases();
    this._destroySession();
    this._super();
  },
 
  toStringExtension() {
    return this.get('normalizedUrl');
  }
 
 
});