Coder Social home page Coder Social logo

Change schema definition format about realm-js HOT 13 CLOSED

realm avatar realm commented on July 30, 2024
Change schema definition format

from realm-js.

Comments (13)

alazier avatar alazier commented on July 30, 2024

We will need a dynamic api similar to the current implementation as this is currently being added to the other bindings. We can explore automatic schema inference but there are some big issues such as support for non-native js types (int, float, data, etc) which may be difficult to specify in an elegant way.

from realm-js.

appden avatar appden commented on July 30, 2024

I'm personally in favor of continuing to be explicit, but losing the order dependency. I like the React.PropTypes API overall, so I think we could model the schema representation to be similar...

PersonObject.schema = {
  name: 'PersonObject',
  properties: {
    name: Realm.Types.string,
    age: Realm.Types.double,
    children: Realm.Types.arrayOf('PersonObject'),
    // Also allow these for setting more options...
    limbs: {type: Realm.Types.int, default: 4},
    career: {type: Realm.Types.string, optional: true},
  }
};

In addition, I think it's more appropriate to have the schema be set as a static property of the constructor rather than on the prototype. This is how React's propTypes works, and won't cause issues with someone creating a schema property on objects. Furthermore, perhaps it would be safer to be even more explicit by instead naming this property realmSchema (just a thought).

from realm-js.

alazier avatar alazier commented on July 30, 2024

Using a map rather than an array of properties looks good to me. We can't set the schema as a static property on Realm as we need to support access to multiple realms with differing schema.

from realm-js.

appden avatar appden commented on July 30, 2024

Sorry, I meant a static property of the constructor of an object rather than on its prototype. For example, inside TestObjects.js the PersonObject class would have schema (or realmSchema) set directly on PersonObject rather than PersonObject.prototype.

from realm-js.

alazier avatar alazier commented on July 30, 2024

So it sounds like we can't do this at the moment as we need to support column ordering in order for sync to work. In the future if the constraint is laxed then we can support the proposed format.

from realm-js.

appden avatar appden commented on July 30, 2024

So my primary issue is with the look and feel of the current API, not necessarily with the columns having intrinsic ordering. This also relates to #112, which is that I want to remove creating objects from arrays of properties.

In JS, the property keys will still have ordering in my proposed new API, meaning iterating over those keys or calling Object.keys will always be in the order as defined. The only issue is that encoding those into JSON makes them lose their ordering (since JSON keys have unspecified ordering), so we'd need to change the RPC serializing and deserializing techniques to retain the ordering over those keys, which is quite easy. 😄

from realm-js.

alazier avatar alazier commented on July 30, 2024

Does JS ensure that keys will retain the ordering in which they are defined?

from realm-js.

appden avatar appden commented on July 30, 2024

Yes it is guaranteed in the language. I actually have all of this working in my sk-schema-api branch.

from realm-js.

alazier avatar alazier commented on July 30, 2024

I don't think this is guaranteed for Objects: http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order

from realm-js.

appden avatar appden commented on July 30, 2024

Yes it is. Older versions of ECMAScript left it unspecified, but all engines have maintained insert order and many libraries and websites have come to depend on it, so ES6 retroactively officially added this to the specification since all engines had this behavior anyways.

from realm-js.

alazier avatar alazier commented on July 30, 2024

From the ES6 spec:
The mechanics and order of enumerating the properties is not specified

from realm-js.

appden avatar appden commented on July 30, 2024

You're right, I was partially mistaken. The spec doesn't require the engine use insertion order for enumerating properties, but it does require that engine to maintain insertion order since it requires Object.getOwnPropertyNames(object) to return property names in insertion order.

from realm-js.

alazier avatar alazier commented on July 30, 2024

Lets finish this this week. I think we need to make the change to the schema format. We shouldn't rip out array support and I don't think it is necessary to refactor all the tests a this time, although if there aren't any conflicts we can keep the current changes. Moving forward we can write new tests using objects rather than arrays if that is preferable,

from realm-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.