adonisjs / lucid

AdonisJS SQL ORM. Supports PostgreSQL, MySQL, MSSQL, Redshift, SQLite and many more
https://lucid.adonisjs.com/
MIT License
1.03k stars 189 forks source link

Get Interface of columns for a model #907

Closed lubiikpupiik closed 1 year ago

lubiikpupiik commented 1 year ago

Hi,

Is there any way how to get an interface for a model columns? For example, i have this model:

class User extends BaseModel {
  email: string;
  password: string;
}

And i would like to get this model as an interface, but right now the model is extended by the BaseModel, so additionally types such as findBy are added into the interface. Is there any way, how to get only the columns? I was lookin into the types, but i did not find anything.

Use case would be something like: I want to edit some of the columns, so i would have some update method and i want to use the type as a type of an argument for this method:

update(user: Partial<User['Columns']>) {
... My code
}

Thank you and have a nice day!

mastermunj commented 1 year ago

You can try

import { ModelAttributes } from '@ioc:Adonis/Lucid/Orm';
...
...
update(user: ModelAttributes<InstanceType<User>>) {}
lubiikpupiik commented 1 year ago

Thanks! 🎉 Works like a charm