herbsjs / herbs2knex

Create repositories using Knex to retrieve and store Entities
Other
7 stars 13 forks source link

Be able to change conventions used by the Repository class methods #31

Closed guilhermemiuavortx closed 2 years ago

guilhermemiuavortx commented 3 years ago

Is your feature request related to a problem? Please describe. Herbs2knex uses Snake case as convention for naming database table fields. But not every project uses this convention by default. In the project I'm working we use Camel Case in database table fields. Herbs2knex should be able to change conventions used by the Repository class methods.

I believe that snake case and camel case would be enough at the time.

Describe the solution you'd like

const { Repository } = require('@herbsjs/herbs2knex')
const connection = require('connection')  // Knex initialize instance
const { ProductItem } = require('../domain/entities/productItem')

class YourRepository extends Repository {
    constructor() {
        super({
            entity: ProductItem,
            schema: 'main',
            table: 'product_items',
            ids: ['id'],
            foreignKeys: [{ customerId: String }],
            knex: connection,
            //  Configure the convention
            convention: 'snakeCase' // options available: snakeCase and camelCase 
        })
    }
}
dalssoft commented 3 years ago

Agree with the expected behavior, but we are trying to let devs inject their own convention, not create a new convention (like 'snakeCase').

This PRs are an example: https://github.com/herbsjs/herbs2gql/pull/17 / https://github.com/herbsjs/herbs2gql/pull/18

maikvortx commented 2 years ago

Hello everyone!

I think that we can use a custom convention like this:

const { Repository } = require('@herbsjs/herbs2knex')
const connection = require('connection')  // Knex initialize instance
const { ProductItem } = require('../domain/entities/productItem')

class YourRepository extends Repository {
    constructor() {
        super({
            entity: ProductItem,
            schema: 'main',
            table: 'product_items',
            ids: ['id'],
            foreignKeys: [{ customerId: String }],
            knex: connection,
            //  Configure the convention
            options: { 
                  convention: { inputNameRule: (str) => `snake_case_returned` }
            }
        })
    }
}
dalssoft commented 2 years ago

Looks good.

The only thing I would change is the var name: currently it is toTableFieldName.

The good news is that convetion is already a separated class: https://github.com/herbsjs/herbs2knex/blob/master/src/convention.js

maikvortx commented 2 years ago

Hi guys! I'm making progress on custom conventions for column names.

Soon I will open the PR.

maikvortx commented 2 years ago

Example:

//given
    const anEntity = givenAnEntity()
    const ItemRepository = givenAnRepositoryClass({
      entity: anEntity,
      table,
      schema,
      ids: ['id'],
      knex: connection
    })

    const injection = {
      convention: {
        toTableFieldName: field => camelCaseConvention(field)
      }
    }

    const itemRepo = new ItemRepository(injection)

    //when
    const ret = await itemRepo.find({ where: { stringTest: ['marie'] } })

    //then
    assert.deepStrictEqual(ret[0].toJSON(), {
      id: 10,
      stringTest: 'marie',
      booleanTest: true
    })
maikvortx commented 2 years ago

Hello @jhomarolo! Would be possible to mark this PR with a label in progress?

maikmb commented 2 years ago

Hey guys!

PR #36