sidorares / node-mysql2

:zap: fast mysqljs/mysql compatible mysql driver for node.js
https://sidorares.github.io/node-mysql2/
MIT License
4.07k stars 619 forks source link

Dot notation extension #1049

Open f8k8 opened 5 years ago

f8k8 commented 5 years ago

We've added (albeit in an older fork of the library) an option to allow the library to process . in field names into objects. For example: SELECT table1.id, table2.fieldA as `foo.fieldA`, table2.fieldB as `foo.fieldB`, table1.anotherField Would normally select the values into columns named:

id
anotherField
foo.fieldA
foo.fieldB

With the option enabled, the returned rows have the object format:

{
  id,
  anotherField,
  foo: {
    fieldA,
    fieldB
  }
}

Would there be any interest in having this as a feature in the library? If so, we can tidy it up / rebase to the latest version and create a pull request.

sidorares commented 5 years ago

hm, might be useful. This is similar to nestTables flag, right? https://github.com/mysqljs/mysql#joins-with-overlapping-column-names

Usually I'm trying to resist new api as much as possible because it's much harder to remove later so don't expect quick decision on this

Would it be easy to integrate to mysqljs/mysql if they want it as well?

f8k8 commented 5 years ago

It is similar to the nestTables flag, but gives you a bit more control over how your final object is laid out. The current implementation is either / or - I.e. you can't enable both nestTables and processDots, as they'd probably conflict.

I've just had a quick look through mysqljs/mysql and it looks like it should be pretty straightforward to add. The feature is implemented in a similar way to nestTables - in the parser it pre-allocates all the fields in the object then just converts fields with . in them (e.g. field.sub1.sub2) into this[field][sub1][sub2].