You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Typescript provides concept of declaration merging. From the documentation: “declaration merging” means that the compiler merges two separate declarations declared with the same name into a single definition.
This technique is often used to provide various utility functions in enums as we are used to from other languages (like Java and similar).
and then we define namespace with same name as enum and with utility function from():
exportnamespaceDay{exportfunctionfrom(day: string): Day{if(day==='sun'||day==='su'||day==='sunday'){returnDay.SUNDAY;}// ... rest of the codethrownewError(`Unknown enum ${day}`);}}
this two declarations are merged. In most cases we access enum values like this:
constday=Day.MODAY;
but since we also defined from() in namespace with same name as enum is, we can reference it by referencing enum:
Issue is that in such case Typeorm does not create SQL statements properly when we e.g. generate a migration.
Expected Behavior
In case when enum is created in the database (example is for postgresql) we expect that only actual enum values are provided without enum functions.
See: AS ENUM('sunday', 'monday', ... it contains only 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'.
exportclasstest1620891256583implementsMigrationInterface{name='test1620891256583'publicasyncup(queryRunner: QueryRunner): Promise<void>{awaitqueryRunner.query(`CREATE TYPE "testDatabaseSchema"."day_enum_test_day_enum" AS ENUM('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday')`);awaitqueryRunner.query(`CREATE TABLE "testDatabaseSchema"."day_enum_test" ("id" character varying NOT NULL, "day" "testDatabaseSchema"."day_enum_test_day_enum" NOT NULL DEFAULT 'sunday', CONSTRAINT "PK_599b919752788408ef71afd73dd" PRIMARY KEY ("id"))`);}publicasyncdown(queryRunner: QueryRunner): Promise<void>{awaitqueryRunner.query(`DROP TABLE "testDatabaseSchema"."day_enum_test"`);awaitqueryRunner.query(`DROP TYPE "testDatabaseSchema"."day_enum_test_day_enum"`);}}
Actual Behavior
Unfortunately in our case because function is part of enum, it is included in generated SQL.
See: AS ENUM('sunday', 'monday', ... it contains also from() implemetation (function body was converted to string). This is causing later in some cases syntax error on the DB side while running migration or enum type in the DB contains what is should not.
exportclasstest1620891209562implementsMigrationInterface{name='test1620891209562'publicasyncup(queryRunner: QueryRunner): Promise<void>{awaitqueryRunner.query(`CREATE TYPE "testDatabaseSchema"."day_enum_test_day_enum" AS ENUM('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'function from(day) { if (day === ''sun' || day === 'su' || day === 'sunday') { return Day.SUNDAY; } throw new Error(\`Unknown enum ${day}\`); }')`);awaitqueryRunner.query(`CREATE TABLE "testDatabaseSchema"."day_enum_test" ("id" character varying NOT NULL, "day" "testDatabaseSchema"."day_enum_test_day_enum" NOT NULL DEFAULT 'sunday', CONSTRAINT "PK_599b919752788408ef71afd73dd" PRIMARY KEY ("id"))`);}publicasyncdown(queryRunner: QueryRunner): Promise<void>{awaitqueryRunner.query(`DROP TABLE "testDatabaseSchema"."day_enum_test"`);awaitqueryRunner.query(`DROP TYPE "testDatabaseSchema"."day_enum_test_day_enum"`);}}
Steps to Reproduce
Create day.ts and copy into it enum and namespace code from above.
Create day-enum-test.entity.ts and copy into it entity code from above.
Make sure you have ormconfig setup for postgres.
Run migration typeorm migration:generate --name test.
Check content of generated {TIMESTAMP}-test.ts file.
My Environment
Dependency
Version
Node.js version
v12.20.2
Typescript version
v4.2.3
TypeORM version
0.2.32
Additional Context
Relevant Database Driver(s)
aurora-data-api
aurora-data-api-pg
better-sqlite3
cockroachdb
cordova
expo
mongodb
mysql
nativescript
oracle
postgres
react-native
sap
sqlite
sqlite-abstract
sqljs
sqlserver
Are you willing to resolve this issue by submitting a Pull Request?
Yes, I have the time, and I know how to start.
Yes, I have the time, but I don't know how to start. I would need guidance.
No, I don't have the time, although I believe I could do it if I had the time...
No, I don't have the time and I wouldn't even know how to start.
The text was updated successfully, but these errors were encountered:
Issue Description
Typescript provides concept of declaration merging. From the documentation: “declaration merging” means that the compiler merges two separate declarations declared with the same name into a single definition.
This technique is often used to provide various utility functions in enums as we are used to from other languages (like Java and similar).
For example if we have following enum:
and then we define namespace with same name as enum and with utility function
from()
:this two declarations are merged. In most cases we access enum values like this:
but since we also defined
from()
in namespace with same name as enum is, we can reference it by referencing enum:If we then for example define following entity:
Issue is that in such case Typeorm does not create SQL statements properly when we e.g. generate a migration.
Expected Behavior
In case when enum is created in the database (example is for postgresql) we expect that only actual enum values are provided without enum functions.
See:
AS ENUM('sunday', 'monday', ...
it contains only'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'
.Actual Behavior
Unfortunately in our case because function is part of enum, it is included in generated SQL.
See:
AS ENUM('sunday', 'monday', ...
it contains alsofrom()
implemetation (function body was converted to string). This is causing later in some cases syntax error on the DB side while running migration or enum type in the DB contains what is should not.Steps to Reproduce
day.ts
and copy into it enum and namespace code from above.day-enum-test.entity.ts
and copy into it entity code from above.typeorm migration:generate --name test
.{TIMESTAMP}-test.ts
file.My Environment
Additional Context
Relevant Database Driver(s)
aurora-data-api
aurora-data-api-pg
better-sqlite3
cockroachdb
cordova
expo
mongodb
mysql
nativescript
oracle
postgres
react-native
sap
sqlite
sqlite-abstract
sqljs
sqlserver
Are you willing to resolve this issue by submitting a Pull Request?
The text was updated successfully, but these errors were encountered: