adminjs icon indicating copy to clipboard operation
adminjs copied to clipboard

jsonb field not showing up in SHOW/EDIT

Open cyzanfar opened this issue 5 years ago • 11 comments

Describe the bug A clear and concise description of what the bug is.

Installed libraries and their versions

To Reproduce Steps to reproduce the behavior: I am trying to display and enable edit for one of my models that has a JSONB datatype.

instructor model

const Instructor = sequelize.define('Instructor', {
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    submission: {
        type: DataTypes.JSONB,
        allowNull: false
    },
}, {});

resource definition

const adminBro = new AdminBro({
    resources: [{resource: Provider}, {
        resource: Instructor,
        options: {
            properties: {
                submission: {
                    type: 'mixed',
                    show: true,
                    filter: false,
                    edit: true
                },
            },
        }
    }],
})

const router = AdminBroExpress.buildRouter(adminBro)

app.use(adminBro.options.rootPath, router)

Here is the UI result in admin: enter image description here

Expected behavior should display json data and be able to edit it.

cyzanfar avatar Mar 28 '21 20:03 cyzanfar

[email protected]
[email protected]
@admin-bro/sequelize

v2.0.1 is very old - AFAIK it didn't even support JSONB objects/arrays, have you tried updating? If you decide to update, it's best to update to admin-bro@beta until it's promoted to official version. It's stable/safe to use if you are worried since most of the changes are actually bug fixes.

If you update to >=v3, make sure to use @admin-bro/express package instead of admin-bro-expressjs.

dziraf avatar Apr 08 '21 15:04 dziraf

Strange... I'm mistaken about my version here is the version i currently have, still doesn't show me/let me edit json field:

    "admin-bro": {
      "version": "3.4.0",
      "resolved": "https://registry.npmjs.org/admin-bro/-/admin-bro-3.4.0.tgz",
      
    ...
    "@admin-bro/design-system": {
      "version": "1.7.0",
      "resolved": "https://registry.npmjs.org/@admin-bro/design-system/-/design-system-1.7.0.tgz",
    ...
        "@admin-bro/express": {
      "version": "3.1.0",
      "resolved": "https://registry.npmjs.org/@admin-bro/express/-/express-3.1.0.tgz",
    },
    "@admin-bro/sequelize": {
      "version": "1.2.1",
      "resolved": "https://registry.npmjs.org/@admin-bro/sequelize/-/sequelize-1.2.1.tgz",

cyzanfar avatar Apr 16 '21 18:04 cyzanfar

Could you please try updating to v4? JSONB handling was bugged but the fix should be included there. Just run yarn add admin-bro@latest or npm i admin-bro@latest

dziraf avatar Apr 16 '21 18:04 dziraf

However you have to define available keys yourself, i. e.:

properties: {
  submission: {
    type: 'mixed',
  },
  'submission.someField': { type: 'string' },
  'submission.anotherField': { type: 'number' },
},

Currently it isn't possible to add extra keys in the UI so they have to be predefined.

dziraf avatar Apr 16 '21 18:04 dziraf

ok what if they are nested objects?

example of a submission field value:

    'submission': {
        'element': {
            'type': 'button',
            'attr': 'class',
            'value': 'ta-button',
            'event': 'click'
        }
    },

cyzanfar avatar Apr 16 '21 20:04 cyzanfar

submission: { type: 'mixed' },
'submission.element': { type: 'mixed' },
'submission.element.type': { type: 'string' },
'submission.element.attr': { type: 'string; },
...etc

Similarly for arrays, i.e.

items: { type: 'mixed', isArray: true },

If you want to have an array of i. e. { label: 'string', value: 'number' }:

'items.label': { type: 'string' },
'items.value': { type: 'number' },

I've also recently added isDraggable option which works if isArray is true:

items: { type: 'mixed', isArray: true, isDraggable: true },

It allows you to reorder elements in JSONB arrays

dziraf avatar Apr 16 '21 20:04 dziraf

Great, thank you @dziraf! i'll try that

cyzanfar avatar Apr 16 '21 21:04 cyzanfar

It would be interesting to have admin-bro automatically traverse the json and builds the necessary fields. I would say maybe it would be better to just output the json object and make that editable.

cyzanfar avatar Apr 19 '21 03:04 cyzanfar

submission: { type: 'mixed' },
'submission.element': { type: 'mixed' },
'submission.element.type': { type: 'string' },
'submission.element.attr': { type: 'string; },
...etc

Similarly for arrays, i.e.

items: { type: 'mixed', isArray: true },

If you want to have an array of i. e. { label: 'string', value: 'number' }:

'items.label': { type: 'string' },
'items.value': { type: 'number' },

I've also recently added isDraggable option which works if isArray is true:

items: { type: 'mixed', isArray: true, isDraggable: true },

It allows you to reorder elements in JSONB arrays

@dziraf This helped a lot, but can you also elaborate how an Json Array of strings in DB can be configured manually? Fo example: items: ['asd', 'abc', 'cad] so what I understand we can just do items: { type: 'mixed', isArray: true } but how do I define the array property types?

code-x-coffee avatar Jul 02 '21 05:07 code-x-coffee

In my case i don't have predefined structure for json. How can I show this?

imaithal avatar Jan 20 '22 06:01 imaithal

In my case i don't have predefined structure for json. How can I show this?

I am exactly in this situation: a JSON field with an arbitrary structure, and AdminJS won't show it nor allow editing it, and there is no workaround that I could find. Just editing the JSON as a string would suffice, I don't need anything more user-friendly.

One would think that the most common case for a JSON field in a SQL database is to have arbitrary data, because if you have a predefined structure then you would use table columns for them.

haplo avatar Sep 30 '22 12:09 haplo