Skip to content
This repository has been archived by the owner on Jun 22, 2021. It is now read-only.

Latest commit

 

History

History
174 lines (142 loc) · 6.19 KB

functions.md

File metadata and controls

174 lines (142 loc) · 6.19 KB

Functions

The facade contains common functions for storage and retrieval of entities from a repository.

countEntities

Counts the number of entities that match the filter option.

const { count } = await facade.countEntities({
  filter: { foo: 'bar' },
});

This package contains the count entities tests and the count entities signature for this function.

createEntity

Creates a new entity using the entity option if no entity exists that matches the id option.

import ConflictingEntityError from '@js-entity-repos/core/dist/errors/ConflictingEntityError';

try {
  const { entity } = await facade.createEntity({
    id: 'example_id',
    entity: { id: 'example_id', foo: 'bar' },
  });
} catch (err) {
  if (err instanceof ConflictingEntityError) {
    // An entity with the given id already exists.
  }
  throw err;
}

This package contains the create entity tests and the create entity signature for this function.

getEntities

Retreives a sorted paginated array of entities that match the filter option. Users of this function may want to use the createCursorFromEntity utility function and may need to use the convertPropertyFilter utility function. Implementors of this function will need to use the createCursorFromEntity and createPaginationFilter utility functions to generate and use cursors.

import { backward, forward } from '@js-entity-repos/core/dist/types/PaginationDirection';
import { asc, desc } from '@js-entity-repos/core/dist/types/SortOrder';
import { start } from '@js-entity-repos/core/dist/types/Cursor';

const firstForwardPage = await facade.getEntities({
  filter: { foo: 'demo' },
  sort: { id: asc, bar: desc },
  pagination: { limit: 10, direction: forward, cursor: start },
});
const firstPageEntities = firstForwardPage.entities;
if (firstForwardPage.hasMoreForward) {
  const secondForwardPage = await facade.getEntities({
    filter: { foo: 'demo' },
    sort: { id: asc, bar: desc },
    pagination: { limit: 10, direction: forward, cursor: firstForwardPage.forwardCursor },
  });
  const secondPageEntities = secondForwardPage.entities;
  if (secondForwardPage.hasMoreBackward) {
    const firstPage = await facade.getEntities({
      filter: { foo: 'demo' },
      sort: { id: asc, bar: desc },
      pagination: { limit: 10, direction: backward, cursor: secondForwardPage.backwardCursor },
    });
  }
}

This package contains the get entities tests and the get entities signature for this function.

getEntity

Retrieves a single entity that matches the id and filter options.

import MissingEntityError from '@js-entity-repos/core/dist/errors/MissingEntityError';

try {
  const { entity } = await facade.getEntity({
    id: 'example_id',
    filter: { foo: 'bar' },
  });
} catch (err) {
  if (err instanceof MissingEntityError) {
    // No entity exists that matches the given id and filter options.
  }
  throw err;
}

This package contains the get entity tests and the get entity signature for this function.

patchEntity

For an entity that matches the id and filter options, it changes some of an entity's properties using the patch option.

import MissingEntityError from '@js-entity-repos/core/dist/errors/MissingEntityError';

try {
  const { entity } = await facade.patchEntity({
    id: 'example_id',
    patch: { foo: 'bar' },
    filter: { foo: 'bar' },
  });
} catch (err) {
  if (err instanceof MissingEntityError) {
    // No entity exists that matches the given id and filter options.
  }
  throw err;
}

This package contains the patch entity tests and the patch entity signature for this function.

removeEntities

Removes all entities that match the filter option.

await facade.removeEntities({
  filter: { foo: 'bar' },
});

This package contains the remove entities tests and the remove entities signature for this function.

removeEntity

Removes an entity that matches the id and filter options.

import MissingEntityError from '@js-entity-repos/core/dist/errors/MissingEntityError';

try {
  await facade.removeEntity({
    id: 'example_id',
    filter: { foo: 'bar' },
  });
} catch (err) {
  if (err instanceof MissingEntityError) {
    // No entity exists that matches the given id and filter options.
  }
  throw err;
}

This package contains the remove entity tests and the remove entity signature for this function.

replaceEntity

For an entity that matches the id and filter options, it changes all of an entity's properties using the entity option.

import MissingEntityError from '@js-entity-repos/core/dist/errors/MissingEntityError';

try {
  const { entity } = await facade.replaceEntity({
    id: 'example_id',
    entity: { id: 'example_id', foo: 'bar' },
    filter: { foo: 'bar' },
  });
} catch (err) {
  if (err instanceof MissingEntityError) {
    // No entity exists that matches the given id and filter options.
  }
  throw err;
}

This package contains the replace entity tests and the replace entity signature for this function.