How to use the datx.getModelMetaKey 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
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 {
github infinum / datx / packages / datx-jsonapi / src / helpers / model.ts View on Github external
export async function fetchModelLink(
  model: PureModel,
  key: string,
  requestHeaders?: IDictionary,
  options?: IRequestOptions,
): Promise> {
  const collection = getModelCollection(model);
  const links = getModelLinks(model);
  if (!links || !(key in links)) {
    throw error(`Link ${key} doesn't exist on the model`);
  }
  const link = links[key];
  const responseObj = fetchLink(link, collection as IJsonapiCollection, requestHeaders, options);

  if (getModelMetaKey(model, MODEL_QUEUE_FIELD)) {
    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);
      }
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;
    });
  }