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

Change schema definition format #20

Closed
alazier opened this issue Sep 23, 2015 · 13 comments · Fixed by #123
Closed

Change schema definition format #20

alazier opened this issue Sep 23, 2015 · 13 comments · Fixed by #123
Assignees

Comments

@alazier
Copy link
Contributor

alazier commented Sep 23, 2015

@appden :

I think JS devs would generally prefer to write out regular objects in similar way as the React.PropTypes API, and I think it would make it easier to read code wherever those objects are created. It also would allow devs to reorder the property definitions for cleanliness without worrying about breaking lots of code. We can discuss this more next week – perhaps there are benefits I'm not realizing.

@alazier
Copy link
Contributor Author

alazier commented Sep 23, 2015

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.

@appden
Copy link
Contributor

appden commented Sep 23, 2015

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).

@alazier
Copy link
Contributor Author

alazier commented Sep 23, 2015

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.

@appden
Copy link
Contributor

appden commented Sep 23, 2015

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.

@alazier alazier closed this as completed Oct 29, 2015
@alazier alazier removed the P2 label Oct 29, 2015
@alazier alazier reopened this Oct 29, 2015
@alazier alazier changed the title Explore schema inference from JS objects Change schema definition format Oct 29, 2015
@alazier
Copy link
Contributor Author

alazier commented Nov 2, 2015

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.

@appden
Copy link
Contributor

appden commented Nov 2, 2015

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. 😄

@alazier alazier added P3 and removed P2 labels Nov 3, 2015
@alazier
Copy link
Contributor Author

alazier commented Nov 3, 2015

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

@appden
Copy link
Contributor

appden commented Nov 3, 2015

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

@alazier
Copy link
Contributor Author

alazier commented Nov 3, 2015

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

@appden
Copy link
Contributor

appden commented Nov 3, 2015

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.

@alazier
Copy link
Contributor Author

alazier commented Nov 3, 2015

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

@appden
Copy link
Contributor

appden commented Nov 3, 2015

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.

@alazier
Copy link
Contributor Author

alazier commented Nov 16, 2015

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,

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants