jawj / zapatos

Zero-abstraction Postgres for TypeScript: a non-ORM database library
https://jawj.github.io/zapatos/
Other
1.3k stars 46 forks source link

extras adds properties #87

Closed puckey closed 3 years ago

puckey commented 3 years ago

I was refactoring my code to use the new extras functionality as described in https://github.com/jawj/zapatos/issues/80 and noticed that it was returning both the original column name as well as the aliased one:

  const places = await db
    .select('place', db.all, {
      columns: ['id'],
      extras: {
        abc: 'id'
      }
    })
    .run(pool);

returns

[{"id":1,"abc":1}]

while I expected it to return

[{"abc":1}]

I am not sure this functionality offers any upsides this way, since any use case that I can imagine would then mean mapping through the results to copy over just the abc property

jawj commented 3 years ago

Right: this is what I meant in #80 when I said:

(Though you do need to specify the other columns if you don't want to also return all columns under their original names, as well as your renamed ones).

(I added that note in an edit, so you may not have seen it).

To get what you expected, give it an empty columns option instead:

const places = await db
  .select('place', db.all, {
    columns: [],
    extras: {
      abc: 'id'
    }
  })
  .run(pool);  // returns [{"abc":1}]
puckey commented 3 years ago

Ahh understood! Sorry about that : )