Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB Atlas Conditional Schema #1743

Closed
dotjon0 opened this issue Jul 8, 2024 · 7 comments
Closed

MongoDB Atlas Conditional Schema #1743

dotjon0 opened this issue Jul 8, 2024 · 7 comments

Comments

@dotjon0
Copy link

dotjon0 commented Jul 8, 2024

What happened?

May we ask if MongoDB Atlas Conditional Schema https://www.mongodb.com/community/forums/t/validate-schema-with-conditions/121759/2 is supported? We want to basically have a model with 2 x sets of DB schema which 'are identical' except for the 'required fields' in MongoDB Atlas (i.e. the same field may be required and not required in the MongoDB Atlas schema). In this instance we assume Realm Device sync will support this as long as we make the field in Realm as 'not required'.

example MongoDB Atlas schema validation:

let schemaType0Or2 =  {
      required: [ "type"  ], //  some fields are required
      properties: {
            type: {
                bsonType: "string",
            },
            baseClxName: {
                bsonType: "string",
            },
      }
}

let schemaType1 =  {
      required: [ "type", "baseClxName"  ], //  all fields are required
      properties: {
            type: {
                bsonType: "string",
            },
            baseClxName: {
                bsonType: "string",
            },
      }
}

db.createCollection( 
    "testCollection", 
    { validator: 
        { $jsonSchema: 
            { oneOf: [ schemaType0Or2, schemaType1 ] } 
        } 
    } 
)

Example desired Flutter Realm Schema:

class _TestCollection {

  late String bsonType; // all documents have this field as 'required' in MongoDB Atlas schema validation

  String baseClxName; // some documents are 'required', others 'not required' in MongoDB Atlas schema validation

}

Repro steps

na

Version

na

What Atlas Services are you using?

Both Atlas Device Sync and Atlas App Services

What type of application is this?

Flutter Application

Client OS and version

na

Code snippets

na

Stacktrace of the exception/crash you're getting

na

Relevant log output

na
Copy link

sync-by-unito bot commented Jul 8, 2024

➤ PM Bot commented:

Jira ticket: RDART-1068

@nirinchev
Copy link
Member

No, conditional schema is not supported on the server.

@dotjon0
Copy link
Author

dotjon0 commented Jul 10, 2024

No, conditional schema is not supported on the server.

Thanks for coming back @nirinchev. Is this the case even if we make the properties that change 'required / not-required' as 'always not-required' in the Realm schema? If not possible, is this a Realm Flutter limitation or a Realm Core limitation please?

@nirinchev
Copy link
Member

nirinchev commented Jul 10, 2024

It's a combination of limitations - oneOf is not supported as a json schema expression by the server. Then, the type of json schema conditions is not expressible by the dart type system. Finally, the Realm Core database doesn't have conditional schema support either.

(and to clarify the AppServices json schema support is different from the MongoDB server json schema support)

@dotjon0
Copy link
Author

dotjon0 commented Jul 10, 2024

It's a combination of limitations - oneOf is not supported as a json schema expression by the server. Then, the type of json schema conditions is not expressible by the dart type system. Finally, the Realm Core database doesn't have conditional schema support either.

Completely get the complexities of 'conditional schema' and why Device Sync / Realm Flutter does not support it. If we ignore 'conditional schema' for a moment, will the below work:

So if we had the below property schema, Device Sync / Realm schema will not care about the Atlas schema validation, so from Device Sync / Realm schema point of view as the properties are always 'not required' (in Device Sync / Realm) it does not matter if they are 'required' or 'not required' in Atlas?

  • Device Sync / Realm schema - property is always 'not required'
  • Atlas schema validation - property is either 'not required' or 'required'

So the above is not using conditional schema in Device Sync / Realm, just facilitating it via never expecting the property to be required.

(for context, we are not mutating data via Realm / Device Sync - we mutate data via Atlas only and then only use Realm / Device Sync to sync data to local devices from Atlas)

@nirinchev
Copy link
Member

I see - yes, that should work. If a write comes in from Realm/Device Sync that seems to pass the Device Sync schema validation, but fails the Atlas schema validation, a compensating write will be synthesized that reverts it on the client. E.g. if you have a rule where baseClxName is optional only if type is Foo and you insert a Bar without baseClxName, you'd see that change reverted by the server.

@dotjon0
Copy link
Author

dotjon0 commented Jul 10, 2024

Perfect, thats amazing! Thanks @nirinchev

@dotjon0 dotjon0 closed this as completed Jul 10, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants