mirror of
https://github.com/directus/directus.git
synced 2026-02-11 10:26:14 -05:00
Add barebones crud
This commit is contained in:
@@ -1,19 +1,53 @@
|
||||
import express, { RequestHandler } from 'express';
|
||||
import express from 'express';
|
||||
import asyncHandler from 'express-async-handler';
|
||||
import * as itemsService from '../services/items';
|
||||
import { createItem, readItems, readItem, updateItem, deleteItem } from '../services/items';
|
||||
|
||||
const readItems: RequestHandler = asyncHandler(async (req, res) => {
|
||||
const records = await itemsService.readAll(req.params.collection);
|
||||
res.json({
|
||||
data: records,
|
||||
});
|
||||
});
|
||||
const router = express.Router();
|
||||
|
||||
const createItem: RequestHandler = asyncHandler(async (req, res) => {
|
||||
await itemsService.create(req.params.collection, req.body);
|
||||
res.status(200).end();
|
||||
});
|
||||
router.post(
|
||||
'/:collection',
|
||||
asyncHandler(async (req, res) => {
|
||||
await createItem(req.params.collection, req.body);
|
||||
res.status(200).end();
|
||||
})
|
||||
);
|
||||
|
||||
const router = express.Router().get('/:collection', readItems).post('/:collection', createItem);
|
||||
router.get(
|
||||
'/:collection',
|
||||
asyncHandler(async (req, res) => {
|
||||
const records = await readItems(req.params.collection);
|
||||
|
||||
return res.json({
|
||||
data: records,
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
router.get(
|
||||
'/:collection/:pk',
|
||||
asyncHandler(async (req, res) => {
|
||||
const record = await readItem(req.params.collection, req.params.pk);
|
||||
|
||||
return res.json({
|
||||
data: record,
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
router.patch(
|
||||
'/:collection/:pk',
|
||||
asyncHandler(async (req, res) => {
|
||||
await updateItem(req.params.collection, req.params.pk, req.body);
|
||||
return res.status(200).end();
|
||||
})
|
||||
);
|
||||
|
||||
router.delete(
|
||||
'/:collection/:pk',
|
||||
asyncHandler(async (req, res) => {
|
||||
await deleteItem(req.params.collection, req.params.pk);
|
||||
return res.status(200).end();
|
||||
})
|
||||
);
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -1,10 +1,32 @@
|
||||
import database from '../database';
|
||||
import { Query } from '../types/query';
|
||||
|
||||
export const readAll = async (collection: string, query: Query = {}) => {
|
||||
export const createItem = async (
|
||||
collection: string,
|
||||
data: Record<string, any>,
|
||||
query: Query = {}
|
||||
) => {
|
||||
return await database(collection).insert(data);
|
||||
};
|
||||
|
||||
export const readItems = async (collection: string, query: Query = {}) => {
|
||||
return await database.select('*').from(collection);
|
||||
};
|
||||
|
||||
export const create = async (collection: string, data: Record<string, any>, query: Query = {}) => {
|
||||
return await database(collection).insert(data);
|
||||
export const readItem = async (collection: string, pk: number | string, query = {}) => {
|
||||
const records = await database.select('*').from(collection).where({ id: pk });
|
||||
return records[0];
|
||||
};
|
||||
|
||||
export const updateItem = async (
|
||||
collection: string,
|
||||
pk: number | string,
|
||||
data: Record<string, any>,
|
||||
query: Query = {}
|
||||
) => {
|
||||
return await database(collection).update(data).where({ id: pk });
|
||||
};
|
||||
|
||||
export const deleteItem = async (collection: string, pk: number | string) => {
|
||||
return await database(collection).delete().where({ id: pk });
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user