How to use the forest-express.Schemas function in forest-express

To help you get started, we’ve selected a few forest-express 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 ForestAdmin / forest-express-mongoose / src / services / operator-value-parser.js View on Github external
this.perform = (model, key, value) => {
    const schema = Interface.Schemas.schemas[utils.getModelName(model)];
    let parseFct = val => val;
    let ret = null;

    const fieldValues = key.split(':');
    const fieldName = fieldValues[0];
    const subfieldName = fieldValues[1];

    // Mongoose Aggregate don't parse the value automatically.
    let field = _.find(schema.fields, { field: fieldName });

    const isEmbeddedField = !!field.type.fields;
    if (isEmbeddedField) {
      field = _.find(field.type.fields, { field: subfieldName });
    }

    if (field) {
github ForestAdmin / forest-express-sequelize / src / services / has-many-getter.js View on Github external
function HasManyGetter(model, association, opts, params) {
  const queryBuilder = new QueryBuilder(model, opts, params);
  const schema = Interface.Schemas.schemas[association.name];
  const primaryKeyModel = _.keys(model.primaryKeys)[0];

  function getFieldNamesRequested() {
    if (!params.fields || !params.fields[association.name]) { return null; }
    // NOTICE: Force the primaryKey retrieval to store the records properly in
    //         the client.
    const primaryKeyArray = [_.keys(association.primaryKeys)[0]];

    return _.union(primaryKeyArray, params.fields[association.name].split(','));
  }

  const fieldNamesRequested = getFieldNamesRequested();
  const searchBuilder = new SearchBuilder(association, opts, params, fieldNamesRequested);
  const where = searchBuilder.perform(params.associationName);
  const include = queryBuilder.getIncludes(association, fieldNamesRequested);
github ForestAdmin / forest-express-mongoose / src / services / filters-parser.js View on Github external
function FiltersParser(model, timezone, options) {
  const schema = Interface.Schemas.schemas[utils.getModelName(model)];

  const parseInteger = (value) => Number.parseInt(value, 10);
  const parseDate = (value) => new Date(value);
  const parseBoolean = (value) => {
    if (value === 'true') { return true; }
    if (value === 'false') { return false; }
    return typeof value === 'boolean' ? value : null;
  };
  const parseString = (value) => {
    // NOTICE: Check if the value is a real ObjectID. By default, the isValid method returns true
    //         for a random string with length 12 (example: 'Black Friday').
    const { ObjectId } = options.mongoose.Types;
    if (ObjectId.isValid(value) && ObjectId(value).toString() === value) {
      return ObjectId(value);
    }
    return value;
github ForestAdmin / forest-express-sequelize / src / services / search-builder.js View on Github external
_.each(associations, (association) => {
        if (!fieldNamesRequested
          || (fieldNamesRequested.indexOf(association.as) !== -1)) {
          if (['HasOne', 'BelongsTo'].indexOf(association.associationType) > -1) {
            const modelAssociation = association.target;
            const schemaAssociation = Interface.Schemas.schemas[modelAssociation.name];
            const fieldsAssociation = schemaAssociation.fields;

            _.each(fieldsAssociation, (field) => {
              if (field.isVirtual) { return; } // NOTICE: Ignore Smart Fields.
              if (field.integration) { return; } // NOTICE: Ignore integration fields.
              if (field.reference) { return; } // NOTICE: Ignore associations.
              if (field.isSearchable === false) { return; }

              if (hasSearchFields
                && !_.includes(searchAssociationFields, `${association.as}.${field.field}`)) {
                return;
              }

              let condition = {};
              const columnName = field.columnName || field.field;
              const column = opts.sequelize.col(`${association.as}.${columnName}`);
github ForestAdmin / forest-express-mongoose / src / services / has-many-getter.js View on Github external
function HasManyGetter(model, association, opts, params) {
  const OBJECTID_REGEXP = /^[0-9a-fA-F]{24}$/;
  const schema = Interface.Schemas.schemas[utils.getModelName(association)];
  const searchBuilder = new SearchBuilder(association, opts, params);

  function hasPagination() {
    return params.page && params.page.number;
  }

  function getLimit() {
    if (hasPagination()) {
      return parseInt(params.page.number, 10) * params.page.size;
    }
    return 5;
  }

  function getSkip() {
    if (hasPagination()) {
      return (parseInt(params.page.number, 10) - 1) * params.page.size;
github ForestAdmin / forest-express-mongoose / src / services / resource-getter.js View on Github external
function ResourceGetter(model, params) {
  const schema = Interface.Schemas.schemas[utils.getModelName(model)];

  function handlePopulate(query) {
    _.each(schema.fields, (field) => {
      if (field.reference) {
        query.populate(field.field);
      }
    });
  }

  this.perform = () =>
    new P((resolve, reject) => {
      const query = model.findById(params.recordId);

      handlePopulate(query);

      query
github ForestAdmin / forest-express-sequelize / src / services / resource-updater.js View on Github external
function ResourceUpdater(model, params, newRecord) {
  const schema = Interface.Schemas.schemas[model.name];

  this.perform = () => {
    const compositeKeysManager = new CompositeKeysManager(model, schema, newRecord);

    return new ResourceFinder(model, params)
      .perform()
      .then((record) => {
        if (record) {
          _.each(newRecord, (value, attribute) => {
            record[attribute] = value;
          });

          return record.validate()
            .catch((error) => { throw new ErrorHTTP422(error.message); })
            .then(() => record.save());
        }
github ForestAdmin / forest-express-mongoose / src / services / resources-getter.js View on Github external
function ResourcesGetter(model, opts, params) {
  const schema = Interface.Schemas.schemas[utils.getModelName(model)];
  const queryBuilder = new QueryBuilder(model, params, opts);

  let fieldsSearched = null;

  function getSegment() {
    if (schema.segments && params.segment) {
      return _.find(schema.segments, (currentSegment) => currentSegment.name === params.segment);
    }
    return null;
  }

  function getSegmentCondition() {
    const segment = getSegment();
    if (segment && segment.where && typeof segment.where === 'function') {
      return segment.where().then((where) => ({ where }));
    }
github ForestAdmin / forest-express-mongoose / src / services / resource-creator.js View on Github external
function ResourceCreator(Model, params) {
  const schema = Interface.Schemas.schemas[utils.getModelName(Model)];

  function create() {
    return new P((resolve, reject) => {
      if ('_id' in params) { delete params._id; }

      new Model(params)
        .save((err, record) => {
          if (err) { return reject(err); }
          return resolve(record);
        });
    });
  }

  function fetch(record) {
    return new P((resolve, reject) => {
      const query = Model.findById(record.id);
github ForestAdmin / forest-express-sequelize / src / services / resource-creator.js View on Github external
function ResourceCreator(model, params) {
  const schema = Interface.Schemas.schemas[model.name];

  this.perform = function perform() {
    const promises = [];
    const recordCreated = model.build(params);

    if (model.associations) {
      _.forOwn(model.associations, (association, name) => {
        if (association.associationType === 'BelongsTo') {
          promises.push(recordCreated[`set${_.upperFirst(name)}`](params[name], { save: false }));
        }
      });
    }

    return P.all(promises)
      .then(() => recordCreated.validate()
        .catch((error) => {