dresende / node-orm2

Object Relational Mapping
http://github.com/dresende/node-orm2
MIT License
3.07k stars 376 forks source link

Named Parameters With JSON #496

Open toaster33 opened 10 years ago

toaster33 commented 10 years ago

I recently switched from Sequelize to ORM2 mostly because of transaction issues but one nice feature besides offering more database types was the use of JSON objects for named parameters instead of ORM2's current usage of an array list.

As far as using ORM2, I got it up and running in no time and the switch was quite easy and it works as expected.

dxg commented 10 years ago

Are we talking about db.driver.execQuery("SELECT ?? ...", ['id'], ...), or are we talking about something else? Can you provide an example? Thanks.

toaster33 commented 10 years ago

Yes, sorry, I should of provided an example.

I am referring to db.driver.execQuery("SELECT column FROM table WHERE columnA = ?", ['someValue'], ...);

Suggestion would be to do something like:

var jsonValues = { aNamedParameter : "someValue"};
db.driver.execQuery(
    "SELECT column FROM table WHERE columnA = :aNamedParameter",
    jsonValues, ...
);

Then users would not need to worry about the order of the array list.

Also if something was in the json object that did not map then everything should still work:

var jsonValues = { 
    aNamedParameter : "someValue",
    bNamedParameter : "anotherValue"
};
sundagy commented 10 years ago

It will be great feature!

dxg commented 10 years ago

That does look pretty good; I recently wrote a rather large query with a nested select and keeping track of all the parameters was rather tricky.

I'll leave this open for a bit to see if anyone has any other ideas regarding the DSL, though :blah looks pretty good. This is also easy to implement which makes me happy.

serkanserttop commented 10 years ago

Having implemented the exact same thing before, I gave a try. I only checked with mysql, more test coverage would be useful. https://github.com/dresende/node-orm2/pull/531

serkanserttop commented 10 years ago

I screwed up git rebase earlier, and decided to remove the repo and re-fork the project. I think for that reason 2 more old commits are shown above but the relevant one starts with 9895189. It looks a bit bizarre, so I wanted to make sure it does not confuse people.