JSON:API
What you'll learn
- Using Foscia to interact with a JSON:API
Setup
Please follow the getting started guide to set up your JSON:API action factory.
Usage
Eager loading relations
Foscia supports
eager loading relations through include
.
Internally, it will use the JSON:API relationships inclusion features.
import { query, all, include } from '@foscia/core';
const posts = await action().run(query(Post), include('author'), all());
Filtering requests
You can filter your request using the
filterBy
enhancer. This function
both supports key-value or object parameters.
import { query, all } from '@foscia/core';
import { filterBy } from '@foscia/jsonapi';
const posts = await action().run(
query(Post),
// Key-value pair.
filterBy('published', true),
// Object.
filterBy({ published: true }),
all(),
);
Sorting results
You can sort results using multiple enhancers:
sortBy
,
sortByAsc
and
sortByDesc
. sortBy
supports
both object and arrays parameters and will apply ascending sorting by default.
sortByAsc
and sortByDesc
supports variadic keys parameter.
import { query, all } from '@foscia/core';
import { sortBy, sortByAsc } from '@foscia/jsonapi';
const posts = await action().run(
query(Post),
// Ascending sorting.
sortBy(['publishedAt', 'createdAt']),
// Custom sorting.
sortBy(['publishedAt', 'createdAt'], ['desc', 'asc']),
sortBy({ publishedAt: 'desc', createdAt: 'asc' }),
// Variadic keys.
sortByAsc('publishedAt', 'createdAt'),
all(),
);