How to use datx - 10 common examples

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(refs).forEach((key) => {
    data.relationships = data.relationships || { };
    const refIds = getRefId(model, key);
    let rel: IDefinition|Array|undefined;
    if (refIds instanceof Array || isObservableArray(refIds)) {
      rel = (refIds as Array).map((id, index) => {
        const type = getModelType(model[key][index] ? model[key][index] : refs[key].model).toString();

        if (!type) {
          throw error(`The model type can't be retrieved for the reference ${key}`);
        }

        return { id, type };
      });
    } else {
      const type: string = getModelType(model[key] ? model[key] : refs[key].model).toString();

      if (!type) {
        throw error(`The model type can't be retrieved for the reference ${key}`);
      }

      rel = refIds ? { id: refIds, type } : undefined;
    }

    data.relationships[key] = { data: rel || null };
    if (data.attributes) {
      // tslint:disable-next-line:no-dynamic-delete
      delete data.attributes[key];
    }
  });
github infinum / datx / packages / datx-jsonapi / src / decorateCollection.ts View on Github external
refs.forEach((ref: string) => {
        const items = (obj.relationships as IDictionary)[ref].data;
        if (items instanceof Array && items.length < 1) {
          // it's only possible to update items with one ore more refs. Early exit
          return;
        }
        if (items && record) {
          const models: Model|Array|IIdentifier|undefined|null = mapItems(
            items,
            (def: IDefinition) => this.find(def.type, def.id) || def.id,
          );

          const itemType: string = items instanceof Array ? items[0].type : items.type;
          if (ref in record) {
            record[ref] = models;
          } else {
            // @ts-ignore - Ignore until datx is updated
            initModelRef(record, ref, {model: itemType, type: ReferenceType.TO_ONE_OR_MANY}, models);
          }
        }
      });
    }
github infinum / datx / packages / datx-jsonapi / src / decorateCollection.ts View on Github external
if (items instanceof Array && items.length < 1) {
          // it's only possible to update items with one ore more refs. Early exit
          return;
        }
        if (items && record) {
          const models: Model|Array|IIdentifier|undefined|null = mapItems(
            items,
            (def: IDefinition) => this.find(def.type, def.id) || def.id,
          );

          const itemType: string = items instanceof Array ? items[0].type : items.type;
          if (ref in record) {
            record[ref] = models;
          } else {
            // @ts-ignore - Ignore until datx is updated
            initModelRef(record, ref, {model: itemType, type: ReferenceType.TO_ONE_OR_MANY}, models);
          }
        }
      });
    }
github infinum / datx / packages / datx-jsonapi / src / mixin.ts View on Github external
export function jsonapi(
  Base: IModelConstructor|ICollectionConstructor|IViewConstructor,
) {
  if (isModel(Base)) {
    // @ts-ignore
    return decorateModel(Base);
  } else if (isCollection(Base)) {
    // @ts-ignore
    return decorateCollection(Base);
  } else if (isView(Base)) {
    // @ts-ignore
    return decorateView(Base);
  }

  throw new Error('The instance needs to be a model, collection or a view');
}
github infinum / datx / packages / datx-jsonapi / src / mixin.ts View on Github external
export function jsonapi(
  Base: IModelConstructor|ICollectionConstructor|IViewConstructor,
) {
  if (isModel(Base)) {
    // @ts-ignore
    return decorateModel(Base);
  } else if (isCollection(Base)) {
    // @ts-ignore
    return decorateCollection(Base);
  } else if (isView(Base)) {
    // @ts-ignore
    return decorateView(Base);
  }

  throw new Error('The instance needs to be a model, collection or a view');
}
github infinum / datx / packages / datx-jsonapi / src / mixin.ts View on Github external
export function jsonapi(
  Base: IModelConstructor|ICollectionConstructor|IViewConstructor,
) {
  if (isModel(Base)) {
    // @ts-ignore
    return decorateModel(Base);
  } else if (isCollection(Base)) {
    // @ts-ignore
    return decorateCollection(Base);
  } else if (isView(Base)) {
    // @ts-ignore
    return decorateView(Base);
  }

  throw new Error('The instance needs to be a model, collection or a view');
}
github infinum / datx / packages / datx-jsonapi / src / helpers / model.ts View on Github external
export function getModelRefLinks(model: PureModel): IDictionary> {
  return getModelMetaKey(model, MODEL_REF_LINKS_FIELD);
}
github infinum / datx / packages / datx-jsonapi / src / helpers / model.ts View on Github external
function isModelPersisted(model: PureModel): boolean {
  return getModelMetaKey(model, MODEL_PERSISTED_FIELD);
}
github infinum / datx / packages / datx-jsonapi / src / helpers / model.ts View on Github external
export function getModelLinks(model: PureModel): IDictionary {
  return getModelMetaKey(model, MODEL_LINKS_FIELD);
}
github infinum / datx / packages / datx-jsonapi / src / helpers / model.ts View on Github external
export function modelToJsonApi(model: IJsonapiModel): IRecord {
  const staticModel = model.constructor as typeof PureModel;
  const attributes: IDictionary = modelToJSON(model);

  const useAutogenerated: boolean = staticModel['useAutogeneratedIds'];
  const isPersisted = isModelPersisted(model);

  const data: IRecord = {
    attributes,
    id: (isPersisted || useAutogenerated) ? getModelId(model) : undefined,
    type: getModelType(model) as string,
  };

  const refs = getModelMetaKey(model, 'refs');

  Object.keys(refs).forEach((key) => {
    data.relationships = data.relationships || { };
    const refIds = getRefId(model, key);
    let rel: IDefinition|Array|undefined;
    if (refIds instanceof Array || isObservableArray(refIds)) {
      rel = (refIds as Array).map((id, index) => {
        const type = getModelType(model[key][index] ? model[key][index] : refs[key].model).toString();

        if (!type) {
          throw error(`The model type can't be retrieved for the reference ${key}`);
        }

        return { id, type };
      });
    } else {