Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support enum for DB who don't support it #1414

Closed
antoine-pous opened this issue Jan 6, 2018 · 8 comments
Closed

Support enum for DB who don't support it #1414

antoine-pous opened this issue Jan 6, 2018 · 8 comments

Comments

@antoine-pous
Copy link

antoine-pous commented Jan 6, 2018

Currently SQLite doesn't have ENUM type, but TypeORM can use it's own validator before inserting a value into the DB and emit an error when the value is not in the enum.

enum MyEnum { 
    zero = 0; 
    one = 1;
}

@Entity()
export class Test {
    @Column({enum: MyEnum})
    myVar: number; // Must be 0 or 1
}
@mhombach
Copy link

I'm a big fan of making ORM-Systems really universal or at least as much universal as it can get.
If i create my business-logic within it and i later need to change the database-engine, then there should not be a bunch of stuff i need to change. Sure, i can then optimize everything for the new engine, but it should "work" flawless without changing much stuff...
So i would vote for implementing this as universal type into the ORM and when a database doenst support that column-type, then internally it should just get converted to a string or an integer, which represents that ENUM values. And sure, then it should be validated exactly to match the declared ENUM-Object.

@pleerock
Copy link
Member

@mhombach sorry to disappoint you, but Im biggest anti-fun of universal ORM-systems, simply because:

  • any abstraction come with cost, and cost of making everything universal is too high - you loose to much functionality that each database provides which values higher then having things "universal", because of...
  • being universal is mostly useless, because in practice it does not give anything except of "i later need to change the database-engine". But reality that you are not going to change your database engine almost never. And if you'll have such need ever it will be easier to you to change all places in your code to match new database rather then loosing benefits provided by point 1

But this particular feature request we can implement, because it does not restrict / limit us in anything, its just an addition.

@atheros
Copy link

atheros commented Mar 5, 2018

I once wrote a full app using an orm(not typeorm) using linux + mysql for in-house usage, then my boss came and told me I have to deploy same app to one of our clients. The catch was it was supposed to run on windows with mssql. Obviously it took me some time to get the job done and I would have greatly appreciated if the orm I was using would allow me to abstract things more.

In any case since there is already simple-array and simple-json, why not add simple-enum? Also an option with "useNativeTypeIfAvailable" would be even better.

@pleerock
Copy link
Member

pleerock commented Mar 9, 2018

This issue is opened for the community. Feel free to implement simple-enum type, its easy feature to implement.

@pleerock pleerock added this to the Community milestone Mar 29, 2018
@davidmpaz
Copy link

My 5 cents...

@antoine-pous I came into this issue a couple of day before looking for the same kind of functionality. The outcome of it did not look well so i tried to solve it working around. I am working with cordova and sqljs drivers. So, what I did was this:

// CostType.ts
export enum CostType {
    TOTAL = 'total',
    TAX_TOTAL = 'taxTotal',
    GRANT_TOTAL = 'grantTotal'
}

// CostEntryOrmModel.ts
@Entity('costEntry')
export class CostEntryOrmModel {

    @PrimaryGeneratedColumn()
    identifier: number;

    // note here how the type is specified to be the type of value in enum
    @Column({type: 'varchar'})
    id: CostType;

    // .....
}

Would this work for you? Can you give it a try and report back your feedback?

If I find time this week I can make a pull request with some tests to make a PoC

Cheers

@maxxims
Copy link

maxxims commented Apr 26, 2019

@borremosch could you please provide an example for this implementation?

I am mostly interested for the MSSQL.

export enum CostType { TOTAL = 0, TAX_TOTAL = 1, GRANT_TOTAL = 2 }

export class CostEntryOrmModel {
    @PrimaryGeneratedColumn()
    id: number;
    @Column({type: 'simple-enum'})
    cost: CostType;
}

This is saved as nvarchar and I don't see any CHECK Constraints being added.

@borremosch
Copy link
Contributor

borremosch commented Apr 26, 2019

@maxxims you'll have to add the enum type to the column decorator as well, just like with a regular enum:

@Column({
    type: 'simple-enum',
    enum: CostType
})
cost: CostType;

Hope this solves it

@SYip
Copy link

SYip commented Jan 20, 2020

Should this issue be closed as #3700 was already merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants