Search code examples
node.jsknex.js

How to remove __table from Tabel Knex.js select query


How to remove __table from Tabel Knex.js select query.

The select query returns all data i queried +__table but how can i remove __table.

My select query

table('TestSeriesModes').select('ID', 'Name', 'IsActive', 'IsPhysicalInventory', 'Code').orderBy('ID', 'asc').all()

Returning data

{
    "__table": "TestSeriesModes",
    "ID": 1,
    "Name": "Online",
    "IsActive": true,
    "IsPhysicalInventory": true,
     "Code": "ON"
},
{
    "__table": "TestSeriesModes",
    "ID": 2,
    "Name": "Calling Tablet",
    "IsActive": true,
    "IsPhysicalInventory": true,
    "Code": "CALTAB"
 }

Solution

  • Knex doesn't even have that .all method (at least officially). Looks like you are not using knex directly here. Knex never adds anything like you have described (key called __table) to your resulting rows. Complete reproduction case would be useful, because now the post does not have enough code shown to be able to tell what is wrong with it.

    To answer the query, you can for example use knex directly to get results without that key. Maybe you are using some ORM, which adds it or some other library.

    Other way to drop it would be to filter it out from results afterwards for example by doing:

    const filteredResults = results.map(res => {
        delete res.__table;
    });
    

    Or to use some library like ramda / lodash to filter extra keys from resulting objects.