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

Mixed support - API #3389

Closed
sync-by-unito bot opened this issue Nov 5, 2020 · 2 comments
Closed

Mixed support - API #3389

sync-by-unito bot opened this issue Nov 5, 2020 · 2 comments
Assignees

Comments

@sync-by-unito
Copy link

sync-by-unito bot commented Nov 5, 2020

Introduction

For JavaScript developers, a mixed type is a natural concept due to JavaScript’s type system.

Schema

We can add a new type to the support by the object schema. The name mixed captures the concept "property of different kinds" well.

const Person = {
  name: "Person",
  properties: {
    stuff: { type: "mixed" }, 
    stuffList1: { type: "list", objectType: "mixed" },
    stuffList2: "mixed[]",
  }
};

Additional requirements for mixed properties:

  • cannot be marked optional since null is always a permitted value.
  • can be indexed
  • cannot be a primary key

Creating objects with mixed

Creating an object with a properties of mixed type should be natural for a JavaScript developer:

realm.write(() => {
  realm.create("Person", {
    stuff: 42,
    stuffList1: [ 
      42, 
      "Everything"
    ]
  });
});

Only the following types are supported: int, bool, float, double, string, data, date, objectId, uuid, decimal128 and links. Realm.create() will validate input accordingly.

Accessors

Due to the flexibility of JavaScript’s type system, we can return properties without much hassle.

var person;
realm.write(() => {
  person = realm.create("Person", {
    stuff: 42,
    stuffList1: [ 
      42, 
      "Everything" 
    ]
  });
});

person.stuffList1.forEach(item => {
  console.log(typeof(item));
  // outputs two lines: 'number', 'string'
});

Moreover, updating a property is supported:

var person;
realm.write(() => {
  person = realm.create("Person", {
    stuff: 42,
    stuffList1: [ 
      42, 
      "Everything" 
    ]
  });
});

realm.write(() => {
  person.stuff = "42";
});

Serialization

It is important to support JSON serialization.

var person;
realm.write(() => {
  person = realm.create("Person", {
    stuff: 42,
    stuffList1: [ 
      42, 
      "Everything"
    ]
  });
});

console.log(JSON.stringify(person));
// { stuff: 42, stuffList1: [ 42, "Everything" ] }
@kraenhansen
Copy link
Member

kraenhansen commented Nov 17, 2020

When reading the scope, it seems to me that it won't be possible to store anything else than primitives in a property of Mixed type:

It should be possible to define a property of a class to be of Mixed type. This means that the property can hold values of all the primitive types (Int, Bool, Float, Double, String, Binary, Timestamp, ObjectId, UUID, Decimal128 and links). Lst must be supported too.

I suggest updating the description above to exclude the { name: "Arthur", harmless: "mostly" } example.

@kneth
Copy link
Contributor

kneth commented Nov 22, 2020

@kraenhansen I have removed it. According to the scope, links are supported, and we need to find a way to capture it. Using { name: "Arthur", harmless: "mostly" } was a clumsy example.

@sync-by-unito sync-by-unito bot assigned cesarvr and unassigned kraenhansen Dec 14, 2020
@sync-by-unito sync-by-unito bot closed this as completed Jan 25, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants