How to use the datx.getModelCollection 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
function getLink(model: PureModel, ref: string, key: string) {
  const collection = getModelCollection(model);
  if (!collection) {
    throw error('The model needs to be in a collection');
  }
  const links = getModelRefLinks(model);
  if (!links || !(ref in links)) {
    throw error(`The reference ${ref} doesn't have any links`);
  }
  const refLinks = links[ref];
  if (!refLinks || !(key in refLinks)) {
    throw error(`Link ${key} doesn't exist on the model`);
  }

  return refLinks[key];
}
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
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;
github infinum / datx / packages / datx-jsonapi / src / helpers / model.ts View on Github external
export function saveModel(model: IJsonapiModel, options?: IRequestOptions): Promise {
  const collection = getModelCollection(model) as IJsonapiCollection;

  const data: IRecord = modelToJsonApi(model);
  const requestMethod = isModelPersisted(model) ? update : create;
  const url = getModelEndpointUrl(model, options);

  return requestMethod(url, { data }, collection, options && options.headers)
    .then(handleResponse(model))
    .then((response) => {
      clearCacheByType(getModelType(model));

      return response;
    });
}
github infinum / datx / packages / datx-jsonapi / src / helpers / model.ts View on Github external
export async function fetchModelRefLink(
  model: PureModel,
  ref: string,
  key: string,
  requestHeaders?: IDictionary,
  options?: IRequestOptions,
): Promise> {
  const collection = getModelCollection(model);
  const link = getLink(model, ref, key);

  return fetchLink(link, collection as IJsonapiCollection, requestHeaders, options);
}
github infinum / datx / packages / datx-jsonapi / src / helpers / model.ts View on Github external
export function removeModel(model: T, options?: IRequestOptions): Promise {
  const collection = getModelCollection(model) as IJsonapiCollection;

  const isPersisted = isModelPersisted(model);
  const url = getModelEndpointUrl(model);

  if (isPersisted) {
    return remove(url, collection, options && options.headers)
      .then((response: Response) => {
        if (response.error) {
          throw response.error;
        }

        setModelPersisted(model, false);

        if (collection) {
          collection.removeOne(model);
        }