Skip to content
This repository has been archived by the owner on Oct 10, 2022. It is now read-only.

Support to remove table prefix from entities when "--removePrefix table_prefix_ " is mentioned #335

Closed
darklightcode opened this issue May 24, 2021 · 2 comments
Labels

Comments

@darklightcode
Copy link

darklightcode commented May 24, 2021

First of all, I've started using this tool recently and it's wonderful, great job.

Would be good an option to remove a table_prefix before generating entities.
I decided to delete my entities files after i changed them alot, my case looks like this:

All good with the following:
TypeORM:

  • entityPrefix = web_

Entities:

  • Account
  • User
  • Session

Tables:

  • web_account
  • web_user
  • web_session

As i said before, i deleted all the entities and used typeorm-model-generator to generate new ones, to my surprise the output was:

Not good, different files:
Entities:

  • WebAccount
  • WebUser
  • WebSession

What is should have been:
Entities:

  • Account
  • User
  • Session

Which turned my problem into another problem since my code was bound to the previous -unprefixed- entities, and this could be fixed with a new option to specify a table prefix and extract ONLY those tables. In the meantime i managed to fix it with a custom naming strategy, but it would be nice to label issue this as an enhancement, and the --removePrefix used should end in ormconfig.json.

Here is the code i used in the strategy:

let entityPrefix = getFromArgv('--removePrefix') || '';

function entityName(oldEntityName, entity) {
    let ep = entityPrefix;
    if (ep && ep.trim().length && oldEntityName.startsWith(ep)) {
        entity.sqlName = entity.sqlName.substr(ep.length);
        entity.columns = entity.columns.map(item => {
            if (item.hasOwnProperty('default') && item.default) {
                item.default = item.default.replace(/"'(.*)'"/gi, '"$1"')
            }
            return item;
        })
        return oldEntityName.substr(ep.length);
    }
    return oldEntityName;
}

$ typeorm-model-generator --namedStrategy=./CustomFile.js --removePrefix web_ ...connectionProps

[email protected]
node v14.17.0

@Kononnable
Copy link
Owner

This is not likely to be implemented - #329.
Custom naming strategy is a proper way to achieve the behavior you want.

@stale
Copy link

stale bot commented Aug 13, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Aug 13, 2021
@stale stale bot closed this as completed Aug 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants