An objection models generator
npm install objection-gen-cli



Objection model generator
This package allow you to generate objection model class.
Like this one:
``
import { Model } from 'objection';
export class User extends Model {
public id: number;
public firstName?: string;
static get tableName() {
return 'users';
}
static get relationMappings() {
const { Product } = require('./product');
return {
children: {
relation: Model.HasManyRelation,
modelClass: User,
join: {
from: 'users.id',
to: 'users.parent_id'
}
},
parent: {
relation: Model.HasOneRelation,
modelClass: User,
join: {
from: 'users.parent_id',
to: 'users.id'
}
},
userProducts: {
relation: Model.ManyToManyRelation,
modelClass: Product,
join: {
from: 'users.id',
through: {
from: 'user_product.user_id',
to: 'user_product.product_id'
},
to: 'products.id'
}
},
};
}
}
`
``
yarn add -D objection-gen-cli
Or:
``
npm install -D objection-gen-cli
``
npx objection-cli
Or (after install):
```
yarn run objection-cli
This script will generate a prompt that will guide you in building your models.
Note: The models generated should have a readable look. I dont plan to allow to configure the syntax (quote, commas ...). Your linter should take care of that.
All main methods should be exposed so you can use them, but at the moment it's not really customisable.
- Testing
- Shortcut for id field generation
- Model update during prompts
- Add cli argument to avoid questions (folder name, extention, id ...)
- Add relations to properties (need to resolve circular deps first)
- Load existing models from the folder to allow relations to them (might be tricky)
- Generate a basic knex migration file (might be tricky)
- Remove remove all dependencies but prompts
If you have any idea of things you would like to add, feel free to fill an issue !
I hope you enjoy it :)
Thanks a lot for making objection guys, it's an awesome tool !