How to use the datx.ReferenceType.TO_ONE function in datx

To help you get started, we’ve selected a few datx 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 infinum / datx / packages / datx-jsonapi / src / helpers / model.ts View on Github external
Object.keys(data.relationships).forEach((key) => {
      const ref = (data.relationships as IDictionary)[key];
      if (ref && 'data' in ref && ref.data) {
        if ((!(ref.data instanceof Array) || ref.data.length > 0)) {
          rawData[key] = mapItems(ref.data, (item: IDefinition) => item.id);
          if (!classRefs || !(key in classRefs)) {
            refs[key] = {
              model: ref.data instanceof Array ? ref.data[0].type : ref.data.type,
              type: ref.data instanceof Array ? ReferenceType.TO_MANY : ReferenceType.TO_ONE,
            };
          }
        } else {
          rawData[key] = [];
        }
      }
      if (ref && 'links' in ref) {
        refLinks[key] = ref.links;
      }
      if (ref && 'meta' in ref) {
        refMeta[key] = ref.meta;
      }

      rawData[META_FIELD].fields.push(...Object.keys(refs));
    });