columnInfo from a Lucid object

Is it possible to get a list of table fields from Lucid. I know that columnInfo from knex will be available on the Database object but I need to access the information from Lucid.

Thanks
Simon

For me it seems not feasible. At least I can not find anything in this direction in the official documentation. But I can tell you for sure. Hopefully you get confirmation from Lanz or Virk

It does seem that the a Lucid object has access to the underlying Knex methods. If I could get access to the table name related to the model I should then be able to call columnsInfo

1 Like

You mean you want to get the column info from the Model directly right? I believe the following code should work

await Model.query().columnInfo()

@virk Thank you for the response. I did already try that I thought I must have something wrong because I was getting the error rows.map is not a function

This is the command I use

await Customer.query().columnInfo().fetch()

But then I checked out the code that was causing the problem (see below) and it turns out that Lucid is passing the result to _mapRowsToInstances as an object rather than an Array.

node_modules\@adonisjs\lucid\src\Lucid\QueryBuilder\index.js
   *
   * @private
   */
  _mapRowsToInstances (rows) {
    
    return rows.map((row) => this._mapRowToInstance(row))
  }

  /**
   * Maps a single row to model instance
   *
                

This the result that is being passed to _mapRowsToInstances

{ id:
   { defaultValue: null,
     type: 'int',
     maxLength: null,
     nullable: false },
  company:
   { defaultValue: null,
     type: 'varchar',
     maxLength: 50,
     nullable: true },
  last_name:
   { defaultValue: null,
     type: 'varchar',
     maxLength: 50,
     nullable: true },
  first_name:
   { defaultValue: null,
     type: 'varchar',
     maxLength: 50,
     nullable: true },
   ...
}

Is there anything I can add to the command to make it skip _mapRowsToInstances and just return the Object?

Hi @virk ,

Any update on if this is an error in Adonis that needs to be fixed or if I am doing something wrong.

If it is an error, can I submit a PR so that if _mapRowsToInstances does not receive an array it just returns what it receives.

1 Like

@virk Hi, is there any plans to fix this bug?, I have another requirement to get hold of columnInfo() and again I will have to do some work arounds because this does not work.

1 Like