src/index.js
/**
   * `update` service function for FeathersCassandra.
   * @param id
   * @param data
   * @param params
   */
`update` service function for FeathersCassandra.
` update ` service function for FeathersCassandra .
_update (id, data, params = {}) {
    const update = (id, data, params, oldData) => {
      const fields = Object.keys(oldData || this.fields);
      const createdAtField = this.modelOptions.timestamps && this.modelOptions.timestamps.createdAt;
      let newObject = {};
      for (const key of fields) {
        if (data[key] === undefined) {
          if (!createdAtField || key !== createdAtField) {
            newObject[key] = null;
          }
        } else {
          newObject[key] = data[key];
        }
      }
      if (Array.isArray(this.id)) {
        for (const idKey of this.id) {
          delete newObject[idKey];
        }
      } else {
        delete newObject[this.id];
      }
      const q = this._createQuery('update');
      const idsQuery = this.getIdsQuery(id);
      if (params.query && !isNaN(params.query.$ttl)) {
        q.usingTTL(Number(params.query.$ttl));
        delete params.query.$ttl;
      }
      if (params.query && params.query.$timestamp) {
        q.usingTimestamp(params.query.$timestamp);
        delete params.query.$timestamp;
      }
      utils.prepareData(q, newObject, METHODS);
      q.set(newObject);
      Object.keys(idsQuery).forEach(key => {
        q.where(key, '=', idsQuery[key]);
      });
      if (!oldData) {
        const query = this.filterQuery(params).query;
        utils.prepareIfCondition(id, query, this.id);
        this.objectify(q, query);
      }
      return utils.exec(q, params)
        .then(() => {
          if (createdAtField && !newObject[createdAtField] && oldData) { newObject[createdAtField] = oldData[createdAtField]; }
          if (Array.isArray(this.id)) {
            newObject = Object.assign({}, newObject, this.getIdsQuery(id));
          } else {
            newObject[this.id] = id;
          }
          if (oldData) {
            utils.prepareUpdateResult(data, oldData, newObject, this.fields);
          }
          if (params.query && params.query.$select) {
            const selectedFields = {};
            for (const field of params.query.$select) { selectedFields[field] = newObject[field]; }
            return selectedFields;
          }
          return newObject;
        })
        .catch(errorHandler);
    };
    this.validate(data, 'update');
    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'); }
    if (params.query && params.query.$noSelect) {
      delete params.query.$noSelect;
      return update(id, data, params)
        .then(data => {
          if (afterHook && afterHook(params.query, data, hookOptions, id) === false) { throw new errors.BadRequest('Error in after_update lifecycle function'); }
          return data;
        });
    }
    return this._get(id, params)
      .then(oldData => {
        return update(id, data, params, oldData)
          .then(data => {
            if (afterHook && afterHook(params.query, data, hookOptions, id) === false) { throw new errors.BadRequest('Error in after_update lifecycle function'); }
            return data;
          });
      });
  }
_update ( id , data , params = { } ) { const update = ( id , data , params , oldData ) => { const fields = Object . keys ( oldData || this . fields ) ; const createdAtField = this . modelOptions . timestamps && this . modelOptions . timestamps . createdAt ; let newObject = { } ; for ( const key of fields ) { if ( data [ key ] === undefined ) { if ( ! createdAtField || key !== createdAtField ) { newObject [ key ] = null ; } } else { newObject [ key ] = data [ key ] ; } } if ( Array . isArray ( this . id ) ) { for ( const idKey of this . id ) { delete newObject [ idKey ] ; } } else { delete newObject [ this . id ] ; } const q = this . _createQuery ( 'update' ) ; const idsQuery = this . getIdsQuery ( id ) ; if ( params . query && ! isNaN ( params . query . $ttl ) ) { q . usingTTL ( Number ( params . query . $ttl ) ) ; delete params . query . $ttl ; } if ( params . query && params . query . $timestamp ) { q . usingTimestamp ( params . query . $timestamp ) ; delete params . query . $timestamp ; } utils . prepareData ( q , newObject , METHODS ) ; q . set ( newObject ) ; Object . keys ( idsQuery ) . forEach ( key => { q . where ( key , '=' , idsQuery [ key ] ) ; } ) ; if ( ! oldData ) { const query = this . filterQuery ( params ) . query ; utils . prepareIfCondition ( id , query , this . id ) ; this . objectify ( q , query ) ; } return utils . exec ( q , params ) . then ( ( ) => { if ( createdAtField && ! newObject [ createdAtField ] && oldData ) { newObject [ createdAtField ] = oldData [ createdAtField ] ; } if ( Array . isArray ( this . id ) ) { newObject = Object . assign ( { } , newObject , this . getIdsQuery ( id ) ) ; } else { newObject [ this . id ] = id ; } if ( oldData ) { utils . prepareUpdateResult ( data , oldData , newObject , this . fields ) ; } if ( params . query && params . query . $select ) { const selectedFields = { } ; for ( const field of params . query . $select ) { selectedFields [ field ] = newObject [ field ] ; } return selectedFields ; } return newObject ; } ) . catch ( errorHandler ) ; } ; this . validate ( data , 'update' ) ; 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' ) ; } if ( params . query && params . query . $noSelect ) { delete params . query . $noSelect ; return update ( id , data , params ) . then ( data => { if ( afterHook && afterHook ( params . query , data , hookOptions , id ) === false ) { throw new errors . BadRequest ( 'Error in after_update lifecycle function' ) ; } return data ; } ) ; } return this . _get ( id , params ) . then ( oldData => { return update ( id , data , params , oldData ) . then ( data => { if ( afterHook && afterHook ( params . query , data , hookOptions , id ) === false ) { throw new errors . BadRequest ( 'Error in after_update lifecycle function' ) ; } return data ; } ) ; } ) ; }
`update` service function for FeathersCassandra.
` update ` service function for FeathersCassandra .
// Set missing fields to null // Delete id field so we don't update it // Restore the createdAt field so we can return it to the client // Restore the id field so we can return it to the client