How to use the datx-utils.mapItems function in datx-utils

To help you get started, we’ve selected a few datx-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 infinum / datx / packages / datx / src / CompatModel.ts View on Github external
Object.keys(this.static.refs).forEach((prop) => {
      const refs = getModelMetaKey(this, 'refs');
      if (prop in refs) {
        return;
      }
      const ref = this.static.refs[prop];
      const data = mapItems(this[prop] || this.static.defaults[prop], getModelId);
      assignComputed(this, prop);
      if (typeof ref === 'object') {
        // Back reference
        initModelRef(this, prop, {
          model: ref.model,
          property: ref.property,
          type: ReferenceType.TO_ONE_OR_MANY,
        }, data);
      } else {
        // Normal reference
        initModelRef(this, prop, {
          model: ref,
          type: ReferenceType.TO_ONE_OR_MANY,
        }, data);
      }
    });
github infinum / datx / packages / datx / src / helpers / model / fields.ts View on Github external
function getNormalRef(model: PureModel, key: string, refOptions: IReferenceOptions): PureModel|Array|null {
  const value: IIdentifier | Array = storage.getModelDataKey(model, key);
  const collection = getModelCollection(model);
  if (!collection) {
    return null;
  }

  let dataModels = mapItems(value, (id) => id ? collection.findOne(refOptions.model, id) : id);
  if (refOptions.type === ReferenceType.TO_MANY && !(dataModels instanceof Array)) {
    dataModels = [dataModels];
  }
  if (dataModels instanceof Array) {
    const data: IObservableArray = observable.array(dataModels, { deep: false });
    intercept(data, (change: TChange) => partialRefUpdate(model, key, change));

    return data;
  }

  return dataModels;
}
github infinum / datx / packages / datx / src / helpers / model / fields.ts View on Github external
export function updateRef(model: PureModel, key: string, value: TRefValue) {
  const refOptions = storage.getModelReferenceOptions(model, key);

  const check = refOptions.type === ReferenceType.TO_MANY ? value || [] : value;
  const isArray = check instanceof Array || isObservableArray(check);
  validateRef(refOptions, isArray, key);

  const collection = getModelCollection(model);
  startAction(model);

  let ids: IIdentifier|Array|null = mapItems(value, (ref: IIdentifier|PureModel) => {
    if (ref && collection) {
      if (ref instanceof PureModel) {
        const refType = getModelType(ref);
        if (refType !== getModelType(refOptions.model)) {
          endAction(model);
          throw error(WRONG_REF_TYPE);
        }
      }

      let instance = collection.findOne(refOptions.model, ref);
      if (!instance && typeof ref === 'object') {
        instance = collection.add(ref, refOptions.model);
      }
      endAction(model);

      return getModelId(instance || ref);
github infinum / datx / packages / datx-jsonapi / src / helpers / model.ts View on Github external
export function saveRelationship(
  model: T,
  ref: string,
  options?: IRequestOptions,
): Promise {
  const collection = getModelCollection(model) as IJsonapiCollection;
  const link = getLink(model, ref, 'self');
  const href: string = typeof link === 'object' ? link.href : link;

  const ids = getRefId(model, ref);
  const type = getModelType(getModelMetaKey(model, 'refs')[ref].model);
  type ID = IDefinition|Array;
  const data: ID = mapItems(ids, (id) => ({ id, type })) as ID;

  return update(href, { data }, collection, options && options.headers)
    .then(handleResponse(model, ref));
}
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;
      }

datx-utils

DatX lib utils for mixins

MIT
Latest version published 3 years ago

Package Health Score

57 / 100
Full package analysis