Add delete collection

This commit is contained in:
rijkvanzanten
2020-06-30 18:08:03 -04:00
parent 0e5e2db084
commit 49e78d064d
2 changed files with 31 additions and 2 deletions

View File

@@ -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;

View File

@@ -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 }),
]);
};