Field Types

Status: Draft

Page Types provide a way to influence read and write behaviour. Let's say the database column that stores your json data is of type text. Within the field type of json the value can be turned into json directly after it's converted from the row to the record. Likewise, the other way around the data will be stringified upon write.


Create a new file in the @/plugins/custom/field-types directory,

import defineFieldType from '@/lib/field-type/defineFieldType.js'

export default defineFieldType({
    id: 'json',
    dataType: 'text',
    read: (value) => {
        // Transforms the string value received from the database into a JSON object.
        // This is typically used when retrieving data from the database,
        // where the data is stored in a string format but needs to be converted
        // back into a JSON object for manipulation or display in the application.
        return JSON.parse(value)
    },
    write: (value) => {
        // Converts a JSON object into a string format suitable for storage in the database.
        // This is used when sending data to the database, converting the JSON object
        // from the application into a string format that can be stored efficiently.
        return JSON.stringify(value)
    }
})

Then within the @/plugins/custom/field-types/index.js

import json from './json'

export default ([
    json
])

The field types are registered within @/plugins/custom/register.js

import definePlugin from '@/lib/module/definePlugin'
import fieldTypes from './field-types'

export default definePlugin({
    fieldTypes
})