PhilWaldmann / openrecord

Make ORMs great again!
https://openrecord.js.org
MIT License
486 stars 38 forks source link

Problem with static filter method when creating with array argument #81

Closed arthurfranca closed 4 years ago

arthurfranca commented 5 years ago

Although undocumented, i've been using this.staticMethods.methodName at the definition scope to create a static method when using a funcion to define a model. It seems to be the right way when not using a class to define it, am i correct?. Mostly i use them for raw sql queries using this.raw(...).then({ rows } => ...} or for setting constants like this.staticMethods.TEST = 'test' even though i'm not sure if it's the right place.

Possibly a bug emerged when i did this.staticMethods.filter = function (q) { ... } and then Model.create([{}, {}]). It seems related to this line.

I guess filter can't be used as a static method and i should just pick another name or was it an unexpected behavior?

PhilWaldmann commented 5 years ago

Hey, sorry for the delay - holidays.

A collection is derived from Array, so overwriting filter() or push(), ect... will conflict with existing methods. Currently there is no mechanism to check for this unintended overwrites.

What raw SQL queries do you need to write? Maybe scopes are better suited (internally it will create a static method, like you did).

arthurfranca commented 5 years ago

Select ... from (select ...) i.e. select from (subquery). I thought regular scope wouldn't work, as i need to return the result from this.raw(), and i think scope always returns the query chain.

I've renamed to this.staticMethods.search thx.

PhilWaldmann commented 5 years ago

Ohh okay. I'll usually use VIEWS so solve this kind of problem. I'll try to implement this feature in V3.