Prefix existing field typecasting flags with "cast-" (#12141)

* Prefix typecasting field flags

* Fix payload test

* Update flags for system tables and for field creation in app

* Revert cast-hash and cast-uuid

* Rename migrations file to current date
This commit is contained in:
ian
2022-03-22 02:46:48 +08:00
committed by GitHub
parent 716e02f21f
commit e2eb2801c5
17 changed files with 131 additions and 51 deletions

View File

@@ -0,0 +1,78 @@
import { toArray } from '@directus/shared/utils';
import { Knex } from 'knex';
import { isArray } from 'lodash';
export async function up(knex: Knex): Promise<void> {
const fields = await knex
.select<{ id: number; special: string }[]>('id', 'special')
.from('directus_fields')
.whereNotNull('special')
.orWhere('special', '<>', '');
for (const { id, special } of fields) {
let parsedSpecial;
try {
parsedSpecial = toArray(special);
} catch {
continue;
}
if (parsedSpecial && isArray(parsedSpecial)) {
let updateRequired = false;
parsedSpecial = parsedSpecial.map((special) => {
switch (special) {
case 'boolean':
case 'csv':
case 'json':
updateRequired = true;
return 'cast-' + special;
default:
return special;
}
});
if (updateRequired) {
await knex('directus_fields').update({ special: parsedSpecial }).where({ id });
}
}
}
}
export async function down(knex: Knex): Promise<void> {
const fields = await knex
.select<{ id: number; special: string }[]>('id', 'special')
.from('directus_fields')
.whereNotNull('special')
.orWhere('special', '<>', '');
for (const { id, special } of fields) {
let parsedSpecial;
try {
parsedSpecial = toArray(special);
} catch {
continue;
}
if (parsedSpecial && isArray(parsedSpecial)) {
let updateRequired = false;
parsedSpecial = parsedSpecial.map((special) => {
switch (special) {
case 'cast-boolean':
case 'cast-csv':
case 'cast-json':
updateRequired = true;
return special.replace('cast-', '');
default:
return special;
}
});
if (updateRequired) {
await knex('directus_fields').update({ special: parsedSpecial }).where({ id });
}
}
}
}

View File

@@ -42,21 +42,21 @@ fields:
width: full
- field: hidden
special: boolean
special: cast-boolean
interface: boolean
options:
label: $t:field_options.directus_collections.hidden_label
width: half
- field: singleton
special: boolean
special: cast-boolean
interface: boolean
options:
label: $t:singleton_label
width: half
- field: translations
special: json
special: cast-json
interface: list
options:
template: '{{ translation }} ({{ language }})'
@@ -115,7 +115,7 @@ fields:
- field: archive_app_filter
interface: boolean
special: boolean
special: cast-boolean
options:
label: $t:field_options.directus_collections.archive_app_filter
width: half

View File

@@ -17,7 +17,7 @@ fields:
- collection: directus_fields
field: special
hidden: true
special: csv
special: cast-csv
width: half
- collection: directus_fields
@@ -27,7 +27,7 @@ fields:
- collection: directus_fields
field: options
hidden: true
special: json
special: cast-json
width: half
- collection: directus_fields
@@ -37,25 +37,25 @@ fields:
- collection: directus_fields
field: display_options
hidden: true
special: json
special: cast-json
width: half
- collection: directus_fields
field: readonly
hidden: true
special: boolean
special: cast-boolean
width: half
- collection: directus_fields
field: hidden
hidden: true
special: boolean
special: cast-boolean
width: half
- collection: directus_fields
field: required
hidden: true
special: boolean
special: cast-boolean
width: half
- collection: directus_fields
@@ -73,7 +73,7 @@ fields:
- collection: directus_fields
field: translations
hidden: true
special: json
special: cast-json
width: half
- collection: directus_fields
@@ -83,4 +83,4 @@ fields:
- collection: directus_fields
field: conditions
hidden: true
special: json
special: cast-json

View File

@@ -23,7 +23,7 @@ fields:
interface: tags
options:
iconRight: local_offer
special: json
special: cast-json
width: full
display: labels
display_options:
@@ -71,7 +71,7 @@ fields:
- field: metadata
hidden: true
special: json
special: cast-json
- field: type
display: mime-type

View File

@@ -9,13 +9,13 @@ fields:
- field: note
- field: type
- field: show_header
special: boolean
special: cast-boolean
- field: position_x
- field: position_y
- field: width
- field: height
- field: options
special: json
special: cast-json
- field: date_created
special: date-created
- field: user_created

View File

@@ -4,12 +4,12 @@ table: directus_permissions
fields:
- field: permissions
hidden: true
special: json
special: cast-json
width: half
- field: presets
hidden: true
special: json
special: cast-json
width: half
- field: role
@@ -23,11 +23,11 @@ fields:
- field: fields
width: half
special: csv
special: cast-csv
- field: action
width: half
- field: validation
width: half
special: json
special: cast-json

View File

@@ -3,15 +3,15 @@ table: directus_presets
fields:
- field: filter
hidden: true
special: json
special: cast-json
- field: layout_query
hidden: true
special: json
special: cast-json
- field: layout_options
hidden: true
special: json
special: cast-json
- field: role
width: half

View File

@@ -20,7 +20,7 @@ fields:
width: half
- field: one_allowed_collections
special: csv
special: cast-csv
width: half
- field: junction_field

View File

@@ -15,11 +15,11 @@ fields:
- field: data
hidden: true
special: json
special: cast-json
- field: delta
hidden: true
special: json
special: cast-json
- field: parent
width: half

View File

@@ -25,24 +25,24 @@ fields:
- field: app_access
interface: boolean
special: boolean
special: cast-boolean
width: half
- field: admin_access
interface: boolean
special: boolean
special: cast-boolean
width: half
- field: ip_access
interface: tags
options:
placeholder: $t:field_options.directus_roles.ip_access
special: csv
special: cast-csv
width: full
- field: enforce_tfa
interface: boolean
special: boolean
special: cast-boolean
width: half
- field: users

View File

@@ -121,7 +121,7 @@ fields:
- field: module_bar
interface: system-modules
special: json
special: cast-json
- field: security_divider
interface: presentation-divider
@@ -297,7 +297,7 @@ fields:
]
width: full
template: '{{key}}'
special: json
special: cast-json
width: full
- field: map_divider
@@ -322,7 +322,7 @@ fields:
- field: basemaps
interface: list
special: json
special: cast-json
options:
template: '{{name}}'
fields:

View File

@@ -50,7 +50,7 @@ fields:
- field: tags
interface: tags
special: json
special: cast-json
width: full
options:
iconRight: local_offer
@@ -93,7 +93,7 @@ fields:
- field: email_notifications
interface: boolean
width: half
special: boolean
special: cast-boolean
- field: admin_divider
interface: presentation-divider

View File

@@ -60,12 +60,12 @@ fields:
interface: boolean
options:
label: $t:fields.directus_webhooks.data_label
special: boolean
special: cast-boolean
width: half
display: boolean
- field: headers
special: json
special: cast-json
interface: list
options:
template: '{{ header }}: {{ value }}'
@@ -105,7 +105,7 @@ fields:
value: update
- text: $t:delete_label
value: delete
special: csv
special: cast-csv
width: full
display: labels
display_options:
@@ -129,7 +129,7 @@ fields:
- field: collections
interface: system-collections
special: csv
special: cast-csv
width: full
display: labels
display_options:

View File

@@ -63,7 +63,7 @@ export class PayloadService {
return value;
},
async boolean({ action, value }) {
async 'cast-boolean'({ action, value }) {
if (action === 'read') {
if (value === true || value === 1 || value === '1') {
return true;
@@ -76,7 +76,7 @@ export class PayloadService {
return value;
},
async json({ action, value }) {
async 'cast-json'({ action, value }) {
if (action === 'read') {
if (typeof value === 'string') {
try {
@@ -117,7 +117,7 @@ export class PayloadService {
if (action === 'update') return new Date();
return value;
},
async csv({ action, value }) {
async 'cast-csv'({ action, value }) {
if (Array.isArray(value) === false && typeof value !== 'string') return;
if (action === 'read' && Array.isArray(value) === false) {

View File

@@ -115,9 +115,9 @@ export default function getLocalType(
const special = field?.special;
if (special) {
if (special.includes('json')) return 'json';
if (special.includes('cast-json')) return 'json';
if (special.includes('hash')) return 'hash';
if (special.includes('csv')) return 'csv';
if (special.includes('cast-csv')) return 'csv';
if (special.includes('uuid') || special.includes('file')) return 'uuid';
if (type?.startsWith('geometry')) {
return (special[0] as Type) || 'geometry';