All files / src/controllers document.js

96% Statements 72/75
29.63% Branches 8/27
94.29% Functions 33/35
96% Lines 72/75

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262  1x 1x               139x       2x             2x   2x 2x       2x               2x   2x 2x       2x               2x   2x 2x       2x             2x   2x 2x       2x             2x   2x 2x                               2x             2x   2x 2x       2x             2x   2x 2x       2x             2x   2x 2x       2x             2x   2x 2x       2x             2x   2x 2x       2x             2x   2x 2x       2x             2x   2x 2x       2x               2x   2x 2x       6x             6x 24x 24x     6x 6x 3x     6x 6x       3x                 3x 3x   3x 3x       1x           1x       1x  
const
  BaseController = require('./base'),
  DocumentSearchResult = require('./searchResult/document');
 
class DocumentController extends BaseController {
 
  /**
   * @param {Kuzzle} kuzzle
   */
  constructor (kuzzle) {
    super(kuzzle, 'document');
  }
 
  count (index, collection, body, options = {}) {
    const request = {
      index,
      collection,
      body,
      action: 'count',
      includeTrash: options.includeTrash
    };
    delete options.includeTrash;
 
    return this.query(request, options)
      .then(response => response.result.count);
  }
 
  create (index, collection, document, _id = null, options = {}) {
    const request = {
      index,
      collection,
      _id,
      body: document,
      action: 'create',
      refresh: options.refresh
    };
    delete options.refresh;
 
    return this.query(request, options)
      .then(response => response.result);
  }
 
  createOrReplace (index, collection, _id, body, options = {}) {
    const request = {
      index,
      collection,
      _id,
      body,
      action: 'createOrReplace',
      refresh: options.refresh
    };
    delete options.refresh;
 
    return this.query(request, options)
      .then(response => response.result);
  }
 
  delete (index, collection, _id, options = {}) {
    const request = {
      index,
      collection,
      _id,
      action: 'delete',
      refresh: options.refresh
    };
    delete options.refresh;
 
    return this.query(request, options)
      .then(response => response.result._id);
  }
 
  deleteByQuery(index, collection, body = {}, options = {}) {
    const request = {
      index,
      collection,
      body,
      action: 'deleteByQuery',
      refresh: options.refresh
    };
    delete options.refresh;
 
    return this.query(request, options)
      .then(response => response.result.ids);
  }
 
  exists (index, collection, _id, options = {}) {
    const request = {
      index,
      collection,
      _id,
      action: 'exists'
    };
 
    return this.query(request, options)
      .then(response => response.result);
  }
 
  get (index, collection, _id, options = {}) {
    const request = {
      index,
      collection,
      _id,
      action: 'get',
      includeTrash: options.includeTrash
    };
    delete options.includeTrash;
 
    return this.query(request, options)
      .then(response => response.result);
  }
 
  mCreate (index, collection, documents, options = {}) {
    const request = {
      index,
      collection,
      body: {documents},
      action: 'mCreate',
      refresh: options.refresh
    };
    delete options.refresh;
 
    return this.query(request, options)
      .then(response => response.result);
  }
 
  mCreateOrReplace (index, collection, documents, options = {}) {
    const request = {
      index,
      collection,
      body: {documents},
      action: 'mCreateOrReplace',
      refresh: options.refresh
    };
    delete options.refresh;
 
    return this.query(request, options)
      .then(response => response.result);
  }
 
  mDelete (index, collection, ids, options = {}) {
    const request = {
      index,
      collection,
      body: {ids},
      action: 'mDelete',
      refresh: options.refresh
    };
    delete options.refresh;
 
    return this.query(request, options)
      .then(response => response.result);
  }
 
  mGet (index, collection, ids, options = {}) {
    const request = {
      index,
      collection,
      body: {ids},
      action: 'mGet',
      includeTrash: options.includeTrash
    };
    delete options.includeTrash;
 
    return this.query(request, options)
      .then(response => response.result);
  }
 
  mReplace (index, collection, documents, options = {}) {
    const request = {
      index,
      collection,
      body: {documents},
      action: 'mReplace',
      refresh: options.refresh
    };
    delete options.refresh;
 
    return this.query(request, options)
      .then(response => response.result);
  }
 
  mUpdate (index, collection, documents, options = {}) {
    const request = {
      index,
      collection,
      body: {documents},
      action: 'mUpdate',
      refresh: options.refresh
    };
    delete options.refresh;
 
    return this.query(request, options)
      .then(response => response.result);
  }
 
  replace (index, collection, _id, body, options = {}) {
    const request = {
      index,
      collection,
      _id,
      body,
      action: 'replace',
      refresh: options.refresh
    };
    delete options.refresh;
 
    return this.query(request, options)
      .then(response => response.result);
  }
 
  search (index, collection, body = {}, options = {}) {
    const request = {
      index,
      collection,
      body,
      action: 'search',
    };
 
    for (const opt of ['from', 'size', 'scroll', 'includeTrash']) {
      request[opt] = options[opt];
      delete options[opt];
    }
 
    request.size = request.size || 10;
    if (!request.scroll && !request.body.sort && !request.from) {
      request.from = 0;
    }
 
    return this.query(request, options)
      .then(response => new DocumentSearchResult(this.kuzzle, request, options, response.result));
  }
 
  update (index, collection, _id, body, options = {}) {
    const request = {
      index,
      collection,
      _id,
      body,
      action: 'update',
      refresh: options.refresh,
      retryOnConflict: options.retryOnConflict
    };
    delete options.refresh;
    delete options.retryOnConflict;
 
    return this.query(request, options)
      .then(response => response.result);
  }
 
  validate (index, collection, body, options = {}) {
    return this.query({
      index,
      collection,
      body,
      action: 'validate'
    }, options)
      .then(response => response.result);
  }
}
 
module.exports = DocumentController;