Search code examples
javascriptreactjsreact-nativeuser-interfacerealm

How add in schema a List of object(child) optional Javascript(React-Native)


Thank you very much in advance.
I am developing a quiz application that has two types of answer forms
one with multiple choice options
and another open question input, then in the realm schema I made the following configuration

Option schema(optional)

const OptionSchema = {
  name: 'Option',
  primaryKey: 'id',
  properties: {
    id: {type: 'int', indexed: true},
    description: 'string?',
    imageSound: 'string?',
    correct: 'bool',
    marked: 'bool?',
    play: 'bool?',
  },
};

export default OptionSchema;

Question Schema

const QuestionSchema = {
  name: 'Question',
  primaryKey: 'id',
  properties: {
    id: {type: 'int', indexed: true},
    questionForm: 'string',
    optionForm: 'string',
    typeAnswer: 'string',
    description: 'string',
    correctAnswerDescription: 'string?',
    correctAnswerDescriptionId: 'string?',
    imageSound: 'string?',
    options: 'Option?[]',
  },
};
export default QuestionSchema;

The Question

I was wondering how do I leave the options property: 'Option []' optiocinal in schema because it is not getting reading the documentation there are the following cases:
for properties

displayName: 'string?', // optional property
birthday:    {type: 'date', optional: true}, // optional property

for list

testScores: 'double?[]'

Attempts

trying both ways in my case
first try

options:    {type: 'Option[]', optional: true},

second try

options:'Option?[]'

in neither case did it work would you have a solution?


Solution

  • An option that does not solve the problem but may help temporarily would be:

    add a null record and all registration that is not registered you point null as a child. example

    Schema Option

    const OptionSchema = {
      name: 'Option',
      primaryKey: 'id',
      properties: {
        id: {type: 'int', indexed: true, optional: true},
        description: {type: 'string', optional: true},
        imageSound: {type: 'string', optional: true},
        correct: {type: 'bool', optional: true},
        marked: {type: 'bool', optional: true},
        play: {type: 'bool', optional: true},
      },
    };
    
    export default OptionSchema;
    

    Schema Question

    const QuestionSchema = {
      name: 'Question',
      primaryKey: 'id',
      properties: {
        id: {type: 'int', indexed: true},
        questionForm: 'string',
        optionForm: 'string',
        typeAnswer: 'string',
        description: 'string',
        correctAnswerDescription: {type: 'string', optional: true},
        correctAnswerDescriptionId: {type: 'int', optional: true},
        imageSound: {type: 'string', optional: true},
        options: 'Option[]',
      },
    };
    export default QuestionSchema;
    

    Images - Schema Option(children)

    enter image description here

    Images - Schema Question(father)

    enter image description here

    thank you all I hope I helped until the problem has a solution.