How to use the datx.getModelType 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(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 / 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}`);
        }
github infinum / datx / packages / datx-jsonapi / src / helpers / model.ts View on Github external
export function getModelEndpointUrl(model: IJsonapiModel, options?: IRequestOptions): string {
  const queryData = prepareQuery(
    getModelType(model),
    isModelPersisted(model) ? getModelId(model) : undefined,
    undefined,
    options,
    undefined,
    model,
  );

  return queryData.url;
}
github infinum / datx / packages / datx-jsonapi / src / helpers / model.ts View on Github external
return responseObj.then((response) => {
      const related = getModelMetaKey(model, MODEL_RELATED_FIELD);
      const prop = getModelMetaKey(model, MODEL_PROP_FIELD);
      const record = response.data;
      const recordType = record && getModelType(record);
      if (record && recordType !== getModelType(model) && recordType === getModelType(related)) {
        if (prop) {
          related[prop] = record;

          return response;
        }
        setModelMetaKey(related, MODEL_PERSISTED_FIELD, true);

        return response.replaceData(related);
      }

      return response;
    });
  }
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 / Response.ts View on Github external
@action public replaceData(data: T): Response {
    const record: PureModel = this.data as PureModel;
    if (record === data) {
      return this;
    }

    const newId = getModelId(record);
    const type = getModelType(record);

    const viewIndexes = this.views.map((view) => view.list.indexOf(record));

    if (this.__collection) {
      this.__collection.removeOne(type, newId);
      this.__collection.add(data);
    }

    updateModel(data, modelToJSON(record));
    updateModelId(data, newId);

    this.views.forEach((view, index) => {
      if (viewIndexes[index] !== -1) {
        view.list[viewIndexes[index]] = data;
      }
    });
github infinum / datx / packages / datx-jsonapi / src / helpers / model.ts View on Github external
return responseObj.then((response) => {
      const related = getModelMetaKey(model, MODEL_RELATED_FIELD);
      const prop = getModelMetaKey(model, MODEL_PROP_FIELD);
      const record = response.data;
      const recordType = record && getModelType(record);
      if (record && recordType !== getModelType(model) && recordType === getModelType(related)) {
        if (prop) {
          related[prop] = record;

          return response;
        }
        setModelMetaKey(related, MODEL_PERSISTED_FIELD, true);

        return response.replaceData(related);
      }

      return response;
    });
  }