mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
2.0 KiB
2.0 KiB
description, contributors
| description | contributors |
|---|---|
| Learn about the ItemsService in Directus and how to utilize them when building extensions. | Esther Agbaje |
Accessing Items
The ItemsService provides access to perform operations on items in a collection. It requires a collection and a schema
to operate.
export default defineEndpoint((router, context) => {
const { services, getSchema } = context;
const { ItemsService } = services;
router.get('/', async (req, res) => {
const itemsService = new ItemsService('collection_name', {
schema: await getSchema(),
accountability: req.accountability
});
// Your route handler logic
});
});
Create an Item
router.post('/', async (req, res) => {
const itemsService = new ItemsService('collection_name', {
schema: await getSchema(),
accountability: req.accountability
});
const data = await itemsService.createOne({
title: 'Hello world!',
body: 'This is our first article',
});
res.json(data);
});
Get an Item
router.get('/', async (req, res) => {
const itemsService = new ItemsService('collection_name', {
schema: await getSchema(),
accountability: req.accountability
});
const data = await itemsService.readOne('item_id');
res.json(data);
});
Update an Item
router.patch('/', async (req, res) => {
const itemsService = new ItemsService('collection_name', {
schema: await getSchema(),
accountability: req.accountability
});
const data = await itemsService.updateOne('item_id', {
title: "An updated title"
});
res.json(data);
});
Delete an Item
router.delete('/', async (req, res) => {
const itemsService = new ItemsService('collection_name', {
schema: await getSchema(),
accountability: req.accountability
});
const data = await itemsService.deleteOne('item_id');
res.json(data);
});
::: tip Explore ItemsService In-Depth
Refer to the full list of methods in our codebase.
:::