question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to create custom field type that imitates Object (JSON) behavior

See original GitHub issue

Using v5 KeystoneJS

Aforementioned, I’ve come across a scenario which requires me to create an Object of Integer type fields as:

    fields: {
        user: {
            type: Relationship,
            ref: 'User'
        },
        parameters: {
           focus: {type: Integer, defaultValue: 0},
           smoothness: {type: Integer, defaultValue: 0},
           legal: {type: Integer, defaultValue: 0},
           braking: {type: Integer, defaultValue: 0},
           distraction: {type: Integer, defaultValue: 0},
           rash_driving: {type: Integer, defaultValue: 0},
        },
    },

For now, we are resorting on creating a Relationship between the models (yes, we had to break this object into a whole model of its own!).

Now, I found this issue however since sit only imitates Text behaviour, it didn’t work out for us. I’ve been trying to understand the official documentation as well but can’t seem to work it out as well due to lack of any examples online.

Can anyone help?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
molombycommented, Feb 3, 2020

I’ve just seen this issue. It touches on some interesting conversations we’ve been having lately around encoding standard field types within JSON.

Basically the idea is that, once the JSON DB adapter (#1177) is done, field types will have specific JSON encode/decode functionality baked in. This should let Keystone create “JSON-ready” object from any list item. If we also borrow some machinery from the JSON field type (#315) it opens up some interesting possibilities, like:

  • Storing a subset (or multiple subsets) of lists fields in a JSON blob, rather than regular DB columns
  • Nesting fields (as per the original question)
  • Storing a “history” of item changes in a JSON array, within the item record
  • Importing/exporting to JSON

The first point in particular is interesting I think as it would let us app developers do Mongo-like prototyping of their list schemas, then easily transition to a proper relational schema once their models had settled down. Keystone could do this transparently and maybe by default – so if, for example, you added a field to a list without adding a column to the relevant table, your values would be stored in a _otherFields JSON column until you updated your DB structure.

0reactions
sambhav2612commented, Nov 24, 2019

@Vultraz actually there’s more to this model than I wrote, it has more fields and we need distinction between these parameters from all other fields in the model henceforth we went with the 2 level hierarchy

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with JSON data in Google Standard SQL | BigQuery
Given a JSON type in BigQuery, you can access the fields in a JSON expression by using the field access operator. The following...
Read more >
Creating a new field type | Blockly - Google Developers
The custom field's constructor is called during the source block initialization regardless of whether the source block is defined in JSON or JavaScript....
Read more >
JSON.stringify() - JavaScript - MDN Web Docs
A function that alters the behavior of the stringification process, or an array of strings and numbers that specifies properties of value to...
Read more >
Create a Custom Address Field - Salesforce Help
In your Salesforce org, click · Click the Object Manager tab. · From the Object Manager page, select the object to which you...
Read more >
Custom Object (JSON) - Accentuate Custom Fields
Custom Object (JSON) · Defining a field as a Custom object allows you to enter field values as raw JSON. · you can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found