src/index.js
/**
   * `patch` service function for FeathersCassandra.
   * @param id
   * @param data
   * @param params
   */
`patch` service function for FeathersCassandra.
` patch ` service function for FeathersCassandra .
_patch (id, data, params = {}) {
    this.validate(data, 'patch');
    utils.setTimestampFields(data, true, false, this.modelOptions.timestamps);
    utils.setVersionField(data, this.modelOptions.versions);
    const beforeHook = this.Model._properties.schema.before_update;
    const afterHook = this.Model._properties.schema.after_update;
    const hookOptions = utils.getHookOptions(params.query);
    if (beforeHook && beforeHook(params.query, data, hookOptions, id) === false) { throw new errors.BadRequest('Error in before_update lifecycle function'); }
    let { filters, query } = this.filterQuery(params);
    const dataCopy = Object.assign({}, data);
    const mapIds = page => Array.isArray(this.id)
      ? this.id.map(idKey => [...new Set((page.data || page).map(current => current[idKey]))])
      : (page.data || page).map(current => current[this.id]);
    const ids =
      id === null ? this._find(params).then(mapIds) : Promise.resolve([id]);
    if (id !== null) {
      if (Array.isArray(this.id)) {
        query = Object.assign({}, query, this.getIdsQuery(id));
      } else {
        query[this.id] = id;
      }
    }
    const q = this._createQuery('update');
    if (params.query) {
      if (!isNaN(params.query.$ttl)) {
        q.usingTTL(Number(params.query.$ttl));
        delete params.query.$ttl;
      }
      if (params.query.$timestamp) {
        q.usingTimestamp(params.query.$timestamp);
        delete params.query.$timestamp;
      }
    }
    utils.prepareIfCondition(id, query, this.id);
    this.objectify(q, query);
    if (Array.isArray(this.id)) {
      for (const idKey of this.id) {
        delete dataCopy[idKey];
      }
    } else {
      delete dataCopy[this.id];
    }
    return ids
      .then(idList => {
        const selectParam = filters.$select ? { $select: filters.$select } : undefined;
        const findParams = Object.assign({}, params, { query: Object.assign({}, this.getIdsQuery(id, idList), selectParam) });
        utils.prepareData(q, dataCopy, METHODS);
        q.set(dataCopy);
        return utils.exec(q, params)
          .then(() => {
            if (afterHook && afterHook(params.query, data, hookOptions, id) === false) { throw new errors.BadRequest('Error in after_update lifecycle function'); }
            return params.query && params.query.$noSelect
              ? {}
              : this._find(findParams)
                .then(page => {
                  const items = page.data || page;
                  if (id !== null) {
                    if (items.length === 1) {
                      return items[0];
                    } else {
                      throw new errors.NotFound(`No record found for id '${id}'`);
                    }
                  } else if (!items.length) {
                    throw new errors.NotFound(`No record found for id '${id}'`);
                  }
                  return items;
                });
          });
      })
      .catch(errorHandler);
  }
_patch ( id , data , params = { } ) { this . validate ( data , 'patch' ) ; utils . setTimestampFields ( data , true , false , this . modelOptions . timestamps ) ; utils . setVersionField ( data , this . modelOptions . versions ) ; const beforeHook = this . Model . _properties . schema . before_update ; const afterHook = this . Model . _properties . schema . after_update ; const hookOptions = utils . getHookOptions ( params . query ) ; if ( beforeHook && beforeHook ( params . query , data , hookOptions , id ) === false ) { throw new errors . BadRequest ( 'Error in before_update lifecycle function' ) ; } let { filters , query } = this . filterQuery ( params ) ; const dataCopy = Object . assign ( { } , data ) ; const mapIds = page => Array . isArray ( this . id ) ? this . id . map ( idKey => [ ... new Set ( ( page . data || page ) . map ( current => current [ idKey ] ) ) ] ) : ( page . data || page ) . map ( current => current [ this . id ] ) ; const ids = id === null ? this . _find ( params ) . then ( mapIds ) : Promise . resolve ( [ id ] ) ; if ( id !== null ) { if ( Array . isArray ( this . id ) ) { query = Object . assign ( { } , query , this . getIdsQuery ( id ) ) ; } else { query [ this . id ] = id ; } } const q = this . _createQuery ( 'update' ) ; if ( params . query ) { if ( ! isNaN ( params . query . $ttl ) ) { q . usingTTL ( Number ( params . query . $ttl ) ) ; delete params . query . $ttl ; } if ( params . query . $timestamp ) { q . usingTimestamp ( params . query . $timestamp ) ; delete params . query . $timestamp ; } } utils . prepareIfCondition ( id , query , this . id ) ; this . objectify ( q , query ) ; if ( Array . isArray ( this . id ) ) { for ( const idKey of this . id ) { delete dataCopy [ idKey ] ; } } else { delete dataCopy [ this . id ] ; } return ids . then ( idList => { const selectParam = filters . $select ? { $select : filters . $select } : undefined ; const findParams = Object . assign ( { } , params , { query : Object . assign ( { } , this . getIdsQuery ( id , idList ) , selectParam ) } ) ; utils . prepareData ( q , dataCopy , METHODS ) ; q . set ( dataCopy ) ; return utils . exec ( q , params ) . then ( ( ) => { if ( afterHook && afterHook ( params . query , data , hookOptions , id ) === false ) { throw new errors . BadRequest ( 'Error in after_update lifecycle function' ) ; } return params . query && params . query . $noSelect ? { } : this . _find ( findParams ) . then ( page => { const items = page . data || page ; if ( id !== null ) { if ( items . length === 1 ) { return items [ 0 ] ; } else { throw new errors . NotFound ( ` ${ id } ` ) ; } } else if ( ! items . length ) { throw new errors . NotFound ( ` ${ id } ` ) ; } return items ; } ) ; } ) ; } ) . catch ( errorHandler ) ; }
`patch` service function for FeathersCassandra.
` patch ` service function for FeathersCassandra .
// By default we will just query for the one id. For multi patch // we create a list of the ids of all items that will be changed // to re-query them after the update // Create a new query that re-queries all ids that // were originally changed