copleykj / socialize-base-model

MIT License
14 stars 12 forks source link

Feature Request: find({},{class: "foo"}) #2

Closed awatson1978 closed 8 years ago

awatson1978 commented 9 years ago

Hi there, Taking a bit closer look at incorporating socialize:base-model into our existing apps, and I've been thinking about how to make refactor/upgrade paths for existing applications. Long story short, would it be possible to add an option to the find() and findOne() functions that control whether or not a class or document is returned?

Specifically an if/then around line 64 should do the trick: https://github.com/copleykj/socialize-base-model/blob/master/base-model.js#L64

I'm not entirely sure whether the syntax should be affirmative or negative, but here are a couple ideas:

find({},{noTransform: true})  // opt-out; return the original document, please
find({},{class: true})   // opt-in; please return the default class
find({},{class: "foo"})  // opt-in; return the specified class

If we could add such functionality, then people could drop the socialize:base-model into existing apps without them breaking, and start migrating sections of their app without needing to overhaul the entire thing. The opt-in syntax would be the cleanest upgrade path for new devs; but might break the rest of the socialize packages.

Maybe a global option also?

Book = BaseModel.extendAndSetupCollection("books", {noTransform: true});  // opt-out
Book = BaseModel.extendAndSetupCollection("books", {class: true});  // opt-in generically
Book = BaseModel.extendAndSetupCollection("books", {class: "Book"});  // opt-in specifically

Then people could opt-in by default, and opt-out as needed; or vice-versa, and opt-out by default, and opt-in as needed.

Having this functionality will make it much easier to integrate the socialize package into existing apps!

copleykj commented 9 years ago

So currently you can always pass the {transform:null} to the second parameter of a find or findOne like normal and it will disable returning instances. Adding a second parameter to extendAndSetupCollection would be a very feasible option. A opt out scenario would be ideal as this would not disrupt the current API and not break functionality of any of the other packages in the set.

Tentatively this would look like:

Book = BaseModel.extendAndSetupCollection("books", true);

Further thoughts are welcome.