mirror of
https://github.com/directus/directus.git
synced 2026-02-04 06:35:22 -05:00
Add delete collection
This commit is contained in:
@@ -3,7 +3,7 @@ import asyncHandler from 'express-async-handler';
|
||||
import sanitizeQuery from '../middleware/sanitize-query';
|
||||
import validateQuery from '../middleware/validate-query';
|
||||
import * as CollectionsService from '../services/collections';
|
||||
import { schemaInspector } from '../database';
|
||||
import database, { schemaInspector } from '../database';
|
||||
import { InvalidPayloadException, CollectionNotFoundException } from '../exceptions';
|
||||
import Joi from '@hapi/joi';
|
||||
|
||||
@@ -58,4 +58,16 @@ router.get(
|
||||
})
|
||||
);
|
||||
|
||||
router.delete(
|
||||
'/:collection',
|
||||
asyncHandler(async (req, res) => {
|
||||
if ((await schemaInspector.hasTable(req.params.collection)) === false) {
|
||||
throw new CollectionNotFoundException(req.params.collection);
|
||||
}
|
||||
|
||||
await CollectionsService.deleteCollection(req.params.collection);
|
||||
res.end();
|
||||
})
|
||||
);
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -32,7 +32,7 @@ export const create = async (payload: any) => {
|
||||
});
|
||||
});
|
||||
|
||||
return await ItemsService.createItem('directus_collections', {
|
||||
const collection = await ItemsService.createItem('directus_collections', {
|
||||
collection: payload.collection,
|
||||
hidden: payload.hidden || false,
|
||||
single: payload.single || false,
|
||||
@@ -40,6 +40,10 @@ export const create = async (payload: any) => {
|
||||
note: payload.note || null,
|
||||
translation: payload.translation || null,
|
||||
});
|
||||
|
||||
/** @TODO insert all fields */
|
||||
|
||||
return collection;
|
||||
};
|
||||
|
||||
export const readAll = async (query?: Query) => {
|
||||
@@ -81,3 +85,16 @@ export const readOne = async (collection: string, query?: Query) => {
|
||||
translation: collectionInfo?.translation || null,
|
||||
};
|
||||
};
|
||||
|
||||
export const deleteCollection = async (collection: string) => {
|
||||
await Promise.all([
|
||||
database.schema.dropTable(collection),
|
||||
ItemsService.deleteItem('directus_collections', collection),
|
||||
database.delete().from('directus_fields').where({ collection }),
|
||||
database
|
||||
.delete()
|
||||
.from('directus_relations')
|
||||
.where({ collection_many: collection })
|
||||
.orWhere({ collection_one: collection }),
|
||||
]);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user