mirror of
https://github.com/directus/directus.git
synced 2026-01-29 15:47:57 -05:00
Add real singleton support to settings
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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<string, any>,
|
||||
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<string, any>, 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);
|
||||
};
|
||||
|
||||
1
src/types/express.d.ts
vendored
1
src/types/express.d.ts
vendored
@@ -12,6 +12,7 @@ declare global {
|
||||
role?: string;
|
||||
collection?: string;
|
||||
sanitizedQuery?: Record<string, any>;
|
||||
single: boolean;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user