mirror of
https://github.com/directus/directus.git
synced 2026-01-29 22:57:55 -05:00
Add read fields endpoint
This commit is contained in:
@@ -16,6 +16,7 @@ import authRouter from './routes/auth';
|
||||
import collectionsRouter from './routes/collections';
|
||||
import collectionPresetsRouter from './routes/collection-presets';
|
||||
import extensionsRouter from './routes/extensions';
|
||||
import fieldsRouter from './routes/fields';
|
||||
import filesRouter from './routes/files';
|
||||
import foldersRouter from './routes/folders';
|
||||
import itemsRouter from './routes/items';
|
||||
@@ -43,6 +44,7 @@ const app = express()
|
||||
.use('/collections', collectionsRouter)
|
||||
.use('/collection_presets', collectionPresetsRouter)
|
||||
.use('/extensions', extensionsRouter)
|
||||
.use('/fields', fieldsRouter)
|
||||
.use('/files', filesRouter)
|
||||
.use('/folders', foldersRouter)
|
||||
.use('/items', itemsRouter)
|
||||
|
||||
11
src/exceptions/field-not-found.ts
Normal file
11
src/exceptions/field-not-found.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { BaseException } from './base';
|
||||
|
||||
export class FieldNotFoundException extends BaseException {
|
||||
constructor(collection: string, field: string) {
|
||||
super(
|
||||
`Field "${field}" in collection "${collection}" doesn't exist.`,
|
||||
404,
|
||||
'FIELD_NOT_FOUND'
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
export * from './base';
|
||||
export * from './collection-not-found';
|
||||
export * from './field-not-found';
|
||||
export * from './invalid-credentials';
|
||||
export * from './invalid-payload';
|
||||
export * from './invalid-query';
|
||||
export * from './item-limit';
|
||||
export * from './route-not-found';
|
||||
export * from './item-not-found';
|
||||
export * from './collection-not-found';
|
||||
export * from './route-not-found';
|
||||
|
||||
Submodule src/knex-schema-inspector updated: 5b60c08c32...7afe3b04d1
39
src/routes/fields.ts
Normal file
39
src/routes/fields.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { Router } from 'express';
|
||||
import asyncHandler from 'express-async-handler';
|
||||
import * as FieldsService from '../services/fields';
|
||||
import validateCollection from '../middleware/validate-collection';
|
||||
import { schemaInspector } from '../database';
|
||||
import { FieldNotFoundException } from '../exceptions';
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get(
|
||||
'/',
|
||||
asyncHandler(async (req, res) => {
|
||||
const fields = await FieldsService.readAll();
|
||||
return res.json({ data: fields });
|
||||
})
|
||||
);
|
||||
|
||||
router.get(
|
||||
'/:collection',
|
||||
validateCollection,
|
||||
asyncHandler(async (req, res) => {
|
||||
const fields = await FieldsService.readAll(req.collection);
|
||||
return res.json({ data: fields });
|
||||
})
|
||||
);
|
||||
|
||||
router.get(
|
||||
'/:collection/:field',
|
||||
validateCollection,
|
||||
asyncHandler(async (req, res) => {
|
||||
const exists = await schemaInspector.hasColumn(req.collection, req.params.field);
|
||||
if (exists === false) throw new FieldNotFoundException(req.collection, req.params.field);
|
||||
|
||||
const field = await FieldsService.readOne(req.collection, req.params.field);
|
||||
return res.json({ data: field });
|
||||
})
|
||||
);
|
||||
|
||||
export default router;
|
||||
@@ -11,6 +11,8 @@ export const create = async (payload: any) => {
|
||||
table.comment(payload.note);
|
||||
}
|
||||
|
||||
/** @todo move this into fields service */
|
||||
|
||||
payload.fields?.forEach((field: any) => {
|
||||
let column: ColumnBuilder;
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import database, { schemaInspector } from '../database';
|
||||
|
||||
export const readAll = async (collection?: string) => {
|
||||
const fieldsQuery = database.select('*').from('directus_fields');
|
||||
|
||||
if (collection) {
|
||||
fieldsQuery.where({ collection });
|
||||
}
|
||||
|
||||
const [columns, fields] = await Promise.all([
|
||||
schemaInspector.columnInfo(collection),
|
||||
fieldsQuery,
|
||||
]);
|
||||
|
||||
return columns.map((column) => {
|
||||
const field = fields.find(
|
||||
(field) => field.field === column.name && field.collection === column.table
|
||||
);
|
||||
|
||||
/** @TODO
|
||||
* return field defaults if field doesn't exist in directus_fields
|
||||
*/
|
||||
|
||||
const data = {
|
||||
...column,
|
||||
...field,
|
||||
collection: column.table,
|
||||
field: column.name,
|
||||
};
|
||||
|
||||
delete data.table;
|
||||
delete data.name;
|
||||
|
||||
return data;
|
||||
});
|
||||
};
|
||||
|
||||
export const readOne = async (collection: string, field: string) => {
|
||||
const [column, fieldInfo] = await Promise.all([
|
||||
schemaInspector.columnInfo(collection, field),
|
||||
database.select('*').from('directus_fields').where({ collection, field }).first(),
|
||||
]);
|
||||
|
||||
/** @TODO
|
||||
* return field defaults if field doesn't exist in directus_fields
|
||||
*/
|
||||
|
||||
const data = {
|
||||
...column,
|
||||
...fieldInfo,
|
||||
collection: column.table,
|
||||
field: column.name,
|
||||
};
|
||||
|
||||
delete data.table;
|
||||
delete data.name;
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user