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

Added virtual properties for all fields defined in the schema #23

Merged
merged 6 commits into from
Nov 12, 2016

Conversation

jfbenckhuijsen
Copy link
Contributor

I've added some logic to create virtual properties for each of the fields in the schema. This way I can access properties directly via MyModel.myProperty instead of MyModel.entityData.myProperty, same as can be done in Mongoose

@sebelga
Copy link
Owner

sebelga commented Nov 5, 2016

It's interesting. Although I don't think we could use it just like this in gstore because properties from the Schema could override internal properties of the Model. I just made a test and I could override "constructor", "gstore", and also a custom method that I had on the Schema (ex: userSchema.method('verifyPassword', verifyPassword)).

Do you know if Mongoose define a set of reserved property names that can not be used for the Models definition?

@jfbenckhuijsen
Copy link
Contributor Author

As for keywords override internal fields, looks like Mongoose also has this issue and has defined some reserved keywords. See http://mongoosejs.com/docs/api.html#schema_Schema.reserved

As for properties overriding custom methods: from a design point of view, it would be very strange to define an object (looking at Models in an object oriented way) having a property and a function with the same name. So I think that should be the developers responsibility to keep them separated (i.e. having both a birthday property and a birthday method om the same object would be strange).

Concerning this patch: I can imagine adding a test to verify whether property overrides one of the reserved words for GStore-node and throw an error in that case. To preserve backwards compatibility we can add an option to the schema or model to enable this functionality an the reserved word checking. Not sure if preserving this kind of backwards compatibility is a hot issue for you.

@sebelga
Copy link
Owner

sebelga commented Nov 8, 2016

Actually I forgot but the same behaviour of reserved was copied from Mongoose (https://github.com/sebelga/gstore-node/blob/master/lib/schema.js#L175). :) So yeah we could add a few more properties on that hashtable ("gstore", "entityData", "constructor" and any other we might think of).

Also could you add some tests and update the README.md? I think the documentation could go under Entity -> Properties. Mention that virtual properties are automatically added and mapped from entityData. (they can both be accessed and set) + a short code sample?

I think we can safely add this new feature and not worry too much about those few new reserved properties that might break some projects. I think it shouldn't affect almost anyone (I hope!). I am in the middle of adding #11 (Promises) where there will be more concern about some breaking change. ;)

@jfbenckhuijsen
Copy link
Contributor Author

Will do, I'll be at a conference in the coming days, so that'll be next week before I can add them.

@sebelga
Copy link
Owner

sebelga commented Nov 9, 2016

Great, no hurry at all. thank you!

@sebelga
Copy link
Owner

sebelga commented Nov 11, 2016

Great stuff! I think you can safely delete the test you added at line 106 as it is covered by the one on L302. Also could you squash this in 1 commit. I know I haven't been doing this much (at all) but from now on I'd like to squash features/fixes from branches in 1 commit before merging to master. :) thanks!

@jfbenckhuijsen
Copy link
Contributor Author

Squash -> NP, was still working on it a bit.
Test -> Your call, I personally like my test small and focussed on testing individual stuff, but thats just a matter of taste ;-)

@jfbenckhuijsen
Copy link
Contributor Author

Test is removed. Squashing will be an issue: I had a merge of your master in between commits, messing up the squashing. Will take that into account for other changes, but won't work for now.

Copy link
Owner

@sebelga sebelga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great thank you. As for the tests yes you're right question of taste. I am now reviewing all the tests 1 by 1 for the Promise support and that's why my brain is probably thinking 0 & 1 in tests related stuff :). Your merge should hopefully be released early next week with Promises support. thanks again!

@sebelga sebelga merged commit 718740a into sebelga:master Nov 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants