How to use @feathersjs/commons - 10 common examples

To help you get started, we’ve selected a few @feathersjs/commons examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github feathersjs-ecosystem / feathers-rethinkdb / lib / index.js View on Github external
_find (params = {}) {
    const paginate = typeof params.paginate !== 'undefined' ? params.paginate : this.paginate;
    // Prepare the special query params.
    const { filters } = filterQuery(params.query || {}, paginate);

    let q = params.rethinkdb || this.createQuery(params.query);
    let countQuery;

    // For pagination, count has to run as a separate query, but without limit.
    if (paginate.default) {
      countQuery = q.count().run();
    }

    // Handle $skip AFTER the count query but BEFORE $limit.
    if (filters.$skip) {
      q = q.skip(filters.$skip);
    }
    // Handle $limit AFTER the count query and $skip.
    if (typeof filters.$limit !== 'undefined') {
      q = q.limit(filters.$limit);
github feathersjs-ecosystem / feathers-knex / lib / index.js View on Github external
_patch (id, raw, params = {}) {
    // Do not allow to patch the id
    const data = _.omit(raw, this.id);
    // 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
    return this._findOrGet(id, Object.assign({}, params, {
      query: _.extend({}, params.query, { $select: [`${this.table}.${this.id}`] })
    })).then(results => {
      const idList = results.map(current => current[this.id]);
      const query = {
        [`${this.table}.${this.id}`]: { $in: idList }
      };
      const q = this.knexify(this.db(params), query);
      const findParams = Object.assign({}, params, {
        query: Object.assign({}, params.query, query)
      });

      return q.update(data).then(() => {
        return this._findOrGet(null, findParams).then(items => {
          if (id !== null) {
            if (items.length === 1) {
              return items[0];
            } else {
github feathersjs-ecosystem / feathers-mongodb / lib / index.js View on Github external
_normalizeId (id, data) {
    if (this.id === '_id') {
      // Default Mongo IDs cannot be updated. The Mongo library handles
      // this automatically.
      return _.omit(data, this.id);
    } else if (id !== null) {
      // If not using the default Mongo _id field set the ID to its
      // previous value. This prevents orphaned documents.
      return Object.assign({}, data, { [this.id]: id });
    } else {
      return data;
    }
  }
github feathersjs-ecosystem / feathers-mongoose / lib / service.js View on Github external
}

    const { query, filters } = this.filterQuery(params);
    const options = Object.assign({
      new: true,
      overwrite: this.overwrite,
      runValidators: true,
      context: 'query',
      setDefaultsOnInsert: true
    }, params.mongoose);

    query.$and = (query.$and || []).concat({ [this.id]: id });

    if (this.id === '_id') {
      // We can not update default mongo ids
      data = _.omit(data, this.id);
    } else {
      // If not using the default Mongo _id field set the id to its
      // previous value. This prevents orphaned documents.
      data = Object.assign({}, data, { [this.id]: id });
    }

    const discriminator = query[this.discriminatorKey] || this.discriminatorKey;
    const model = this.discriminators[discriminator] || this.Model;
    let modelQuery = model.findOneAndUpdate(query, data, options);

    if (filters.$populate) {
      modelQuery = modelQuery.populate(filters.$populate);
    }

    return modelQuery.lean(this.lean).exec()
      .then(result => {
github feathersjs-ecosystem / feathers-knex / lib / index.js View on Github external
_patch (id, raw, params = {}) {
    // Do not allow to patch the id
    const data = _.omit(raw, this.id);
    // 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
    return this._findOrGet(id, Object.assign({}, params, {
      query: _.extend({}, params.query, { $select: [`${this.table}.${this.id}`] })
    })).then(results => {
      const idList = results.map(current => current[this.id]);
      const query = {
        [`${this.table}.${this.id}`]: { $in: idList }
      };
      const q = this.knexify(this.db(params), query);
      const findParams = Object.assign({}, params, {
        query: Object.assign({}, params.query, query)
      });

      return q.update(data).then(() => {
github feathersjs-ecosystem / feathers-sequelize / lib / index.js View on Github external
return ids.then(idList => {
      // Create a new query that re-queries all ids that
      // were originally changed
      const findParams = Object.assign({}, params, Object.assign({}, {
        query: Object.assign({ [this.id]: { $in: idList } }, params.query)
      }));

      return Model.update(_.omit(data, this.id), options)
        .then(() => {
          if (params.$returning !== false) {
            return this._getOrFind(id, findParams);
          } else {
            return Promise.resolve([]);
          }
        });
    }).then(select(params, this.id)).catch(utils.errorHandler);
  }
github feathersjs / feathers / packages / authentication-oauth2 / lib / index.js View on Github external
}

    const authSettings = app.get('auth') || app.get('authentication') || {};

    // Attempt to pull options from the global auth config
    // for this provider.
    const providerSettings = authSettings[name] || {};
    const oauth2Settings = merge({
      idField: `${name}Id`,
      path: `/auth/${name}`,
      __oauth: true
    }, pick(authSettings, ...INCLUDE_KEYS), providerSettings, omit(options, ...EXCLUDE_KEYS));

    // Set callback defaults based on provided path
    oauth2Settings.callbackPath = oauth2Settings.callbackPath || `${oauth2Settings.path}/callback`;
    oauth2Settings.callbackURL = oauth2Settings.callbackURL || makeUrl(oauth2Settings.callbackPath, app);

    if (!oauth2Settings.clientID) {
      throw new Error(`You must provide a 'clientID' in your authentication configuration or pass one explicitly`);
    }

    if (!oauth2Settings.clientSecret) {
      throw new Error(`You must provide a 'clientSecret' in your authentication configuration or pass one explicitly`);
    }

    const Verifier = options.Verifier || DefaultVerifier;
    const formatter = options.formatter || rest.formatter;
    const handler = options.handler || defaultHandler(oauth2Settings);
    const errorHandler = typeof options.errorHandler === 'function' ? options.errorHandler(oauth2Settings) : defaultErrorHandler(oauth2Settings);

    // register OAuth middleware
    debug(`Registering '${name}' Express OAuth middleware`);
github feathersjs / feathers / packages / authentication-oauth1 / lib / index.js View on Github external
const authSettings = app.get('auth') || app.get('authentication') || {};

    // Attempt to pull options from the global auth config
    // for this provider.
    const providerSettings = authSettings[name] || {};
    const oauth1Settings = merge({
      idField: `${name}Id`,
      path: `/auth/${name}`,
      session: true,
      __oauth: true
    }, pick(authSettings, ...INCLUDE_KEYS), providerSettings, omit(options, ...EXCLUDE_KEYS));

    // Set callback defaults based on provided path
    oauth1Settings.callbackPath = oauth1Settings.callbackPath || `${oauth1Settings.path}/callback`;
    oauth1Settings.callbackURL = oauth1Settings.callbackURL || makeUrl(oauth1Settings.callbackPath, app);

    if (!oauth1Settings.consumerKey) {
      throw new Error(`You must provide a 'consumerKey' in your authentication configuration or pass one explicitly`);
    }

    if (!oauth1Settings.consumerSecret) {
      throw new Error(`You must provide a 'consumerSecret' in your authentication configuration or pass one explicitly`);
    }

    const Verifier = options.Verifier || DefaultVerifier;
    const formatter = options.formatter || rest.formatter;
    const handler = options.handler || defaultHandler(oauth1Settings);
    const errorHandler = defaultErrorHandler(oauth1Settings);

    // register OAuth middleware
    debug(`Registering '${name}' Express OAuth middleware`);
github feathersjs / feathers / packages / express / lib / rest / getHandler.js View on Github external
return function (req, res, next) {
      const { query } = req;
      const route = omit(req.params, '__feathersId');

      res.setHeader('Allow', allowedMethods.join(','));

      // Check if the method exists on the service at all. Send 405 (Method not allowed) if not
      if (typeof service[method] !== 'function') {
        debug(`Method '${method}' not allowed on '${req.url}'`);
        res.status(statusCodes.methodNotAllowed);

        return next(new errors.MethodNotAllowed(`Method \`${method}\` is not supported by this endpoint.`));
      }

      // Grab the service parameters. Use req.feathers
      // and set the query to req.query merged with req.params
      const params = Object.assign({
        query, route
      }, req.feathers);
github feathersjs-ecosystem / feathers-knex / lib / index.js View on Github external
return this._find(params).then(page => {
      const items = page.data;
      const { query } = filterQuery(params.query || {});
      const q = this.db(params);

      // build up the knex query out of the query params
      this.knexify(q, query);

      return q.del().then(() => {
        if (id !== null) {
          if (items.length === 1) {
            return items[0];
          } else {
            throw new errors.NotFound(`No record found for id '${id}'`);
          }
        }

        return items;
      });