How to use the strapi-utils.models.getNature function in strapi-utils

To help you get started, we’ve selected a few strapi-utils 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 strapi / strapi / packages / strapi-connector-mongoose / lib / mount-models.js View on Github external
const buildRelation = ({ definition, model, instance, attribute, name }) => {
  const { nature, verbose } =
    utilsModels.getNature(attribute, name, undefined, model.toLowerCase()) ||
    {};

  // Build associations key
  utilsModels.defineAssociations(
    model.toLowerCase(),
    definition,
    attribute,
    name
  );

  switch (verbose) {
    case 'hasOne': {
      const ref = attribute.plugin
        ? strapi.plugins[attribute.plugin].models[attribute.model].globalId
        : strapi.models[attribute.model].globalId;
github strapi / strapi / packages / strapi-hook-mongoose / lib / index.js View on Github external
_.forEach(definition.attributes, (details, name) => {
                const verbose = _.get(utilsModels.getNature(details, name, undefined, model.toLowerCase()), 'verbose') || '';

                // Build associations key
                utilsModels.defineAssociations(model.toLowerCase(), definition, details, name);

                if (_.isEmpty(verbose)) {
                  definition.loadedModel[name].type = utils(instance).convertType(details.type);
                }

                switch (verbose) {
                  case 'hasOne': {
                    const ref = details.plugin ? strapi.plugins[details.plugin].models[details.model].globalId : strapi.models[details.model].globalId;

                    definition.loadedModel[name] = {
                      type: instance.Schema.Types.ObjectId,
                      ref
                    };
github strapi / strapi / packages / strapi-hook-bookshelf / lib / index.js View on Github external
_.forEach(definition.attributes, (details, name) => {
              const verbose = _.get(
                utilsModels.getNature(details, name, undefined, model.toLowerCase()),
                'verbose'
              ) || '';

              // Build associations key
              utilsModels.defineAssociations(
                model.toLowerCase(),
                definition,
                details,
                name
              );

              let globalId;
              const globalName = details.model || details.collection || '';

              // Exclude polymorphic association.
              if (globalName !== '*') {
github strapi / strapi / packages / strapi-hook-mongoose / lib / index.js View on Github external
_.forEach(definition.attributes, (details, name) => {
              const verbose = _.get(utilsModels.getNature(details, name, undefined, model.toLowerCase()), 'verbose') || '';

              // Build associations key
              utilsModels.defineAssociations(model.toLowerCase(), definition, details, name);

              if (_.isEmpty(verbose)) {
                definition.loadedModel[name].type = utils(instance).convertType(details.type);
              }

              switch (verbose) {
                case 'hasOne': {
                  const ref = details.plugin ? strapi.plugins[details.plugin].models[details.model].globalId : strapi.models[details.model].globalId;

                  definition.loadedModel[name] = {
                    type: instance.Schema.Types.ObjectId,
                    ref
                  };
github strapi / strapi / packages / strapi-connector-bookshelf / lib / mount-models.js View on Github external
Object.keys(definition.attributes).forEach(name => {
      const details = definition.attributes[name];
      if (details.type !== undefined) {
        return;
      }

      const { nature, verbose } =
        utilsModels.getNature(details, name, undefined, model.toLowerCase()) ||
        {};

      // Build associations key
      utilsModels.defineAssociations(
        model.toLowerCase(),
        definition,
        details,
        name
      );

      let globalId;
      const globalName = details.model || details.collection || '';

      // Exclude polymorphic association.
      if (globalName !== '*') {
        globalId = details.plugin
github strapi / strapi / packages / strapi-generate-migrations / lib / builder / relations.js View on Github external
module.exports = (rootModels, modelName, details, attribute, toDrop, onlyDrop, history) => {
  let tplRelationUp;
  let tplRelationDown;
  let infos = {};
  let oldInfos = {};

  if (!onlyDrop && toDrop) {
    infos = utilsModels.getNature(details, attribute, rootModels);
    oldInfos = utilsModels.getNature(_.get(rootModels[modelName].oldAttributes, attribute), attribute, history);

    const isDifferentVerbose = !(oldInfos.hasOwnProperty('nature') && oldInfos.nature === infos.nature);

    if (isDifferentVerbose) {
      handleRelation(oldInfos, history, modelName, _.get(rootModels[modelName].oldAttributes, attribute), attribute, true, true);
      handleRelation(infos, rootModels, modelName, details, attribute);
    } else {
      handleRelation(infos, rootModels, modelName, details, attribute, true, true);
    }
  } else if (onlyDrop || toDrop) {
    oldInfos = utilsModels.getNature(_.get(rootModels[modelName].oldAttributes, attribute), attribute, history);

    handleRelation(oldInfos, history, modelName, _.get(rootModels[modelName].oldAttributes, attribute), attribute, true, true);
  } else {
    infos = utilsModels.getNature(details, attribute, rootModels);
github strapi / strapi / packages / strapi-generate-migrations / lib / builder / relations.js View on Github external
oldInfos = utilsModels.getNature(_.get(rootModels[modelName].oldAttributes, attribute), attribute, history);

    const isDifferentVerbose = !(oldInfos.hasOwnProperty('nature') && oldInfos.nature === infos.nature);

    if (isDifferentVerbose) {
      handleRelation(oldInfos, history, modelName, _.get(rootModels[modelName].oldAttributes, attribute), attribute, true, true);
      handleRelation(infos, rootModels, modelName, details, attribute);
    } else {
      handleRelation(infos, rootModels, modelName, details, attribute, true, true);
    }
  } else if (onlyDrop || toDrop) {
    oldInfos = utilsModels.getNature(_.get(rootModels[modelName].oldAttributes, attribute), attribute, history);

    handleRelation(oldInfos, history, modelName, _.get(rootModels[modelName].oldAttributes, attribute), attribute, true, true);
  } else {
    infos = utilsModels.getNature(details, attribute, rootModels);

    handleRelation(infos, rootModels, modelName, details, attribute);
  }

  function handleRelation(infos, models, modelName, details, attribute, toDrop, onlyDrop) {
    if (_.isEmpty(_.get(rootModels[modelName].attributes, attribute + '.create'))) {
      _.set(rootModels[modelName].attributes, attribute + '.create', {
        drop: '',
        others: ''
      });
    }

    if (_.isEmpty(_.get(rootModels[modelName].attributes, attribute + '.delete'))) {
      _.set(rootModels[modelName].attributes, attribute + '.delete', {
        drop: '',
        others: ''
github strapi / strapi / packages / strapi-generate-migrations / lib / builder / relations.js View on Github external
let oldInfos = {};

  if (!onlyDrop && toDrop) {
    infos = utilsModels.getNature(details, attribute, rootModels);
    oldInfos = utilsModels.getNature(_.get(rootModels[modelName].oldAttributes, attribute), attribute, history);

    const isDifferentVerbose = !(oldInfos.hasOwnProperty('nature') && oldInfos.nature === infos.nature);

    if (isDifferentVerbose) {
      handleRelation(oldInfos, history, modelName, _.get(rootModels[modelName].oldAttributes, attribute), attribute, true, true);
      handleRelation(infos, rootModels, modelName, details, attribute);
    } else {
      handleRelation(infos, rootModels, modelName, details, attribute, true, true);
    }
  } else if (onlyDrop || toDrop) {
    oldInfos = utilsModels.getNature(_.get(rootModels[modelName].oldAttributes, attribute), attribute, history);

    handleRelation(oldInfos, history, modelName, _.get(rootModels[modelName].oldAttributes, attribute), attribute, true, true);
  } else {
    infos = utilsModels.getNature(details, attribute, rootModels);

    handleRelation(infos, rootModels, modelName, details, attribute);
  }

  function handleRelation(infos, models, modelName, details, attribute, toDrop, onlyDrop) {
    if (_.isEmpty(_.get(rootModels[modelName].attributes, attribute + '.create'))) {
      _.set(rootModels[modelName].attributes, attribute + '.create', {
        drop: '',
        others: ''
      });
    }
github strapi / strapi / packages / strapi-generate-migrations / lib / builder / relations.js View on Github external
module.exports = (rootModels, modelName, details, attribute, toDrop, onlyDrop, history) => {
  let tplRelationUp;
  let tplRelationDown;
  let infos = {};
  let oldInfos = {};

  if (!onlyDrop && toDrop) {
    infos = utilsModels.getNature(details, attribute, rootModels);
    oldInfos = utilsModels.getNature(_.get(rootModels[modelName].oldAttributes, attribute), attribute, history);

    const isDifferentVerbose = !(oldInfos.hasOwnProperty('nature') && oldInfos.nature === infos.nature);

    if (isDifferentVerbose) {
      handleRelation(oldInfos, history, modelName, _.get(rootModels[modelName].oldAttributes, attribute), attribute, true, true);
      handleRelation(infos, rootModels, modelName, details, attribute);
    } else {
      handleRelation(infos, rootModels, modelName, details, attribute, true, true);
    }
  } else if (onlyDrop || toDrop) {
    oldInfos = utilsModels.getNature(_.get(rootModels[modelName].oldAttributes, attribute), attribute, history);

    handleRelation(oldInfos, history, modelName, _.get(rootModels[modelName].oldAttributes, attribute), attribute, true, true);
  } else {
    infos = utilsModels.getNature(details, attribute, rootModels);