diff --git a/src/middleware/validate-collection.ts b/src/middleware/validate-collection.ts index aa63c41886..bff8b6794e 100644 --- a/src/middleware/validate-collection.ts +++ b/src/middleware/validate-collection.ts @@ -12,12 +12,20 @@ const validateCollection: RequestHandler = asyncHandler(async (req, res, next) = const exists = await database.schema.hasTable(req.params.collection); - if (exists) { - req.collection = req.params.collection; - return next(); + if (exists === false) { + throw new CollectionNotFoundException(req.params.collection); } - throw new CollectionNotFoundException(req.params.collection); + req.collection = req.params.collection; + + const { single } = await database + .select('single') + .from('directus_collections') + .where({ collection: req.collection }) + .first(); + req.single = single; + + return next(); }); export default validateCollection; diff --git a/src/routes/settings.ts b/src/routes/settings.ts index ac312d74b4..bfab4d2afa 100644 --- a/src/routes/settings.ts +++ b/src/routes/settings.ts @@ -24,15 +24,11 @@ router.patch( sanitizeQuery, validateQuery, asyncHandler(async (req, res) => { - const primaryKey = await SettingsService.updateSettings( - req.params.pk /** @TODO Singleton */, - req.body, - { - ip: req.ip, - userAgent: req.get('user-agent'), - user: req.user, - } - ); + await SettingsService.updateSettings(req.body, { + ip: req.ip, + userAgent: req.get('user-agent'), + user: req.user, + }); const record = await SettingsService.readSettings(req.sanitizedQuery); diff --git a/src/services/settings.ts b/src/services/settings.ts index 32837bce8a..c416c98621 100644 --- a/src/services/settings.ts +++ b/src/services/settings.ts @@ -1,6 +1,7 @@ import { Query } from '../types/query'; import * as ItemsService from './items'; import { Accountability } from '../types'; +import database, { schemaInspector } from '../database'; export const readSettings = async (query: Query) => { const settings = await ItemsService.readItems('directus_settings', { @@ -8,14 +9,28 @@ export const readSettings = async (query: Query) => { limit: 1, }); - return settings[0]; + const settingsObj = settings[0]; + + if (!settingsObj) { + const settingsColumns = await schemaInspector.columnInfo('directus_settings'); + const defaults = {}; + + for (const column of settingsColumns) { + defaults[column.name] = column.default_value; + } + + return defaults; + } + + return settingsObj; }; -export const updateSettings = async ( - pk: string | number, - data: Record, - accountability: Accountability -) => { - /** @NOTE I guess this can technically update _all_ items, as we expect there to only be one */ - return await ItemsService.updateItem('directus_settings', pk, data, accountability); +export const updateSettings = async (data: Record, accountability: Accountability) => { + const record = await database.select('id').from('directus_settings').limit(1).first(); + + if (record) { + return await ItemsService.updateItem('directus_settings', record.id, data, accountability); + } + + return await ItemsService.createItem('directus_settings', data, accountability); }; diff --git a/src/types/express.d.ts b/src/types/express.d.ts index 01ba743a4e..cd00f44ffb 100644 --- a/src/types/express.d.ts +++ b/src/types/express.d.ts @@ -12,6 +12,7 @@ declare global { role?: string; collection?: string; sanitizedQuery?: Record; + single: boolean; } } }