Nest is the nested data primitive behind many Ingest data objects such as app.config, req.data, req.query, req.post, res.data, and res.errors.
This class lives in @stackpress/lib, not in Ingest itself, but Ingest relies on it heavily enough that it is worth understanding.
import { Nest, nest } from '@stackpress/lib';
const config = nest({
database: {
postgres: {
host: 'localhost',
port: 5432
}
}
});Why it matters in Ingest
Ingest uses the callable form of Nest almost everywhere it needs structured state.
app.config.set('app', { name: 'My App' });
app.get('/users/:id', ({ req, res }) => {
res.setJSON({
id: req.data('id'),
filters: req.query(),
body: req.post(),
config: app.config('app', 'name')
});
});That is why these data objects behave similarly even though they belong to different classes.
Common operations
The most useful methods are:
config('database', 'postgres', 'host'); // callable read
config.get('database', 'postgres', 'port'); // method read
config.set('database', 'postgres', 'port', 5432);
config.has('database', 'postgres', 'host');
config.path('database.postgres.host');
config.entries();The callable form is convenient for reads in handlers, while the methods are better when you need more explicit mutation or inspection.
Where you will see it in Ingest
app.configreq.queryreq.postreq.datares.datares.errors
For example:
app.view.engine = async (filePath, req, res) => {
const props = res.data();
const html = await app.view.render(filePath, {
...req.data(),
props
});
res.setHTML(html);
};Parsing helpers
The same module also provides parsing helpers that Ingest adapters use when building request data.
import {
objectFromQuery,
objectFromFormData,
objectFromJson
} from '@stackpress/lib/Nest';Those helpers are part of how:
- query strings become
req.query - request bodies become
req.post - merged request state becomes
req.data