How to use the strapi-utils.models.defineAssociations 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-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
                  };
                  break;
                }
                case 'hasMany': {
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 !== '*') {
                globalId = details.plugin ?
                  _.get(strapi.plugins,`${details.plugin}.models.${globalName.toLowerCase()}.globalId`):
                  _.get(strapi.models, `${globalName.toLowerCase()}.globalId`);
              }
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;

      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
          ? _.get(
              strapi.plugins,
              `${details.plugin}.models.${globalName.toLowerCase()}.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
                    };
                    break;
                  }
                  case 'hasMany': {