diff --git a/docs/docs/decorators.md b/docs/docs/decorators.md index e2a295771215..dceedbbd3a95 100644 --- a/docs/docs/decorators.md +++ b/docs/docs/decorators.md @@ -13,6 +13,7 @@ abstract base classes. |-----------|------|----------|-------------| | `tableName` | `string` | yes | Override default collection/table name. | | `collection` | `string` | yes | Alias for `tableName`. | +| `comment` | `string` | yes | Specify comment to table **(SQL only)** | | `customRepository` | `() => EntityRepository` | yes | Set custom repository class. | > You can also use `@Repository()` decorator instead of `customRepository` parameter. @@ -42,6 +43,7 @@ extend the `@Property()` decorator, so you can also use its parameters there. | `unique` | `boolean` | yes | Set column as unique for [Schema Generator](schema-generator.md).. **(SQL only)** | | `nullable` | `boolean` | yes | Set column as nullable for [Schema Generator](schema-generator.md).. **(SQL only)** | | `unsigned` | `boolean` | yes | Set column as unsigned for [Schema Generator](schema-generator.md).. **(SQL only)** | +| `comment` | `string` | yes | Specify comment of column for [Schema Generator](schema-generator.md).. **(SQL only)** | | `version` | `boolean` | yes | Set to true to enable [Optimistic Locking](transactions.md#optimistic-locking). **(SQL only)** | > You can use property initializers as usual. diff --git a/packages/core/src/decorators/Entity.ts b/packages/core/src/decorators/Entity.ts index af9fb9827cca..eb026892f9ad 100644 --- a/packages/core/src/decorators/Entity.ts +++ b/packages/core/src/decorators/Entity.ts @@ -20,5 +20,6 @@ export type EntityOptions = { discriminatorColumn?: string; discriminatorMap?: Dictionary; discriminatorValue?: string; + comment?: string; customRepository?: () => Constructor>; }; diff --git a/packages/core/src/decorators/Property.ts b/packages/core/src/decorators/Property.ts index 00bf6dff5920..cd0cb7a01ebd 100644 --- a/packages/core/src/decorators/Property.ts +++ b/packages/core/src/decorators/Property.ts @@ -55,6 +55,7 @@ export type PropertyOptions = { lazy?: boolean; primary?: boolean; serializedPrimaryKey?: boolean; + comment?: string; }; export interface ReferenceOptions extends PropertyOptions { diff --git a/packages/core/src/typings.ts b/packages/core/src/typings.ts index 5d6af508dc8a..6272ce720d89 100644 --- a/packages/core/src/typings.ts +++ b/packages/core/src/typings.ts @@ -151,6 +151,7 @@ export interface EntityProperty = any> { inverseJoinColumns: string[]; referencedColumnNames: string[]; referencedTableName: string; + comment?: string; } export interface EntityMetadata = any> { @@ -180,6 +181,7 @@ export interface EntityMetadata = any> { class: Constructor; abstract: boolean; useCache: boolean; + comment?: string; } export interface ISchemaGenerator { diff --git a/packages/knex/src/schema/SchemaGenerator.ts b/packages/knex/src/schema/SchemaGenerator.ts index 9f9309432db7..087a37a653c7 100644 --- a/packages/knex/src/schema/SchemaGenerator.ts +++ b/packages/knex/src/schema/SchemaGenerator.ts @@ -185,6 +185,9 @@ export class SchemaGenerator { const constraintName = meta.collection.includes('.') ? meta.collection.split('.').pop()! + '_pkey' : undefined; table.primary(Utils.flatten(meta.primaryKeys.map(prop => meta.properties[prop].fieldNames)), constraintName); } + if (meta.comment) { + table.comment(meta.comment); + } const createIndex = (index: { name?: string | boolean; properties: string | string[]; type?: string }, unique: boolean) => { const properties = Utils.flatten(Utils.asArray(index.properties).map(prop => meta.properties[prop].fieldNames)); @@ -382,6 +385,7 @@ export class SchemaGenerator { Utils.runIfNotEmpty(() => col.index(indexName), index); Utils.runIfNotEmpty(() => col.unique(uniqueName), prop.unique); Utils.runIfNotEmpty(() => col.defaultTo(prop.defaultRaw ? this.knex.raw(prop.defaultRaw) : null), !sameDefault); + Utils.runIfNotEmpty(() => col.comment(prop.comment!), prop.comment); return col; } diff --git a/tests/__snapshots__/SchemaGenerator.test.ts.snap b/tests/__snapshots__/SchemaGenerator.test.ts.snap index e8f2d9f32c77..2aa4b66d1fcb 100644 --- a/tests/__snapshots__/SchemaGenerator.test.ts.snap +++ b/tests/__snapshots__/SchemaGenerator.test.ts.snap @@ -104,7 +104,7 @@ alter table \`author2_following\` add index \`author2_following_author2_1_id_ind alter table \`author2_following\` add index \`author2_following_author2_2_id_index\`(\`author2_2_id\`); alter table \`author2_following\` add primary key \`author2_following_pkey\`(\`author2_1_id\`, \`author2_2_id\`); -create table \`address2\` (\`author_id\` int(11) unsigned not null, \`value\` varchar(255) not null) default character set utf8mb4 engine = InnoDB; +create table \`address2\` (\`author_id\` int(11) unsigned not null, \`value\` varchar(255) not null comment \'This is address property\') default character set utf8mb4 engine = InnoDB comment = \'This is address table\'; alter table \`address2\` add primary key \`address2_pkey\`(\`author_id\`); alter table \`address2\` add index \`address2_author_id_index\`(\`author_id\`); alter table \`address2\` add unique \`address2_author_id_unique\`(\`author_id\`); @@ -317,7 +317,7 @@ alter table \`author2_following\` add index \`author2_following_author2_1_id_ind alter table \`author2_following\` add index \`author2_following_author2_2_id_index\`(\`author2_2_id\`); alter table \`author2_following\` add primary key \`author2_following_pkey\`(\`author2_1_id\`, \`author2_2_id\`); -create table \`address2\` (\`author_id\` int(11) unsigned not null, \`value\` varchar(255) not null) default character set utf8mb4 engine = InnoDB; +create table \`address2\` (\`author_id\` int(11) unsigned not null, \`value\` varchar(255) not null comment \'This is address property\') default character set utf8mb4 engine = InnoDB comment = \'This is address table\'; alter table \`address2\` add primary key \`address2_pkey\`(\`author_id\`); alter table \`address2\` add index \`address2_author_id_index\`(\`author_id\`); alter table \`address2\` add unique \`address2_author_id_unique\`(\`author_id\`); @@ -421,6 +421,8 @@ create index \\"author2_name_age_index\\" on \\"author2\\" (\\"name\\", \\"age\\ alter table \\"author2\\" add constraint \\"author2_name_email_unique\\" unique (\\"name\\", \\"email\\"); create table \\"address2\\" (\\"author_id\\" int4 not null, \\"value\\" varchar(255) not null); +comment on table \\"address2\\" is 'This is address table'; +comment on column \\"address2\\".\\"value\\" is 'This is address property'; alter table \\"address2\\" add constraint \\"address2_pkey\\" primary key (\\"author_id\\"); alter table \\"address2\\" add constraint \\"address2_author_id_unique\\" unique (\\"author_id\\"); @@ -560,6 +562,8 @@ create index \\"author2_name_age_index\\" on \\"author2\\" (\\"name\\", \\"age\\ alter table \\"author2\\" add constraint \\"author2_name_email_unique\\" unique (\\"name\\", \\"email\\"); create table \\"address2\\" (\\"author_id\\" int4 not null, \\"value\\" varchar(255) not null); +comment on table \\"address2\\" is 'This is address table'; +comment on column \\"address2\\".\\"value\\" is 'This is address property'; alter table \\"address2\\" add constraint \\"address2_pkey\\" primary key (\\"author_id\\"); alter table \\"address2\\" add constraint \\"address2_author_id_unique\\" unique (\\"author_id\\"); @@ -997,7 +1001,7 @@ alter table \`author2_following\` add index \`author2_following_author2_1_id_ind alter table \`author2_following\` add index \`author2_following_author2_2_id_index\`(\`author2_2_id\`); alter table \`author2_following\` add primary key \`author2_following_pkey\`(\`author2_1_id\`, \`author2_2_id\`); -create table \`address2\` (\`author_id\` int(11) unsigned not null, \`value\` varchar(255) not null) default character set utf8mb4 engine = InnoDB; +create table \`address2\` (\`author_id\` int(11) unsigned not null, \`value\` varchar(255) not null comment \'This is address property\') default character set utf8mb4 engine = InnoDB comment = \'This is address table\'; alter table \`address2\` add primary key \`address2_pkey\`(\`author_id\`); alter table \`address2\` add index \`address2_author_id_index\`(\`author_id\`); alter table \`address2\` add unique \`address2_author_id_unique\`(\`author_id\`); @@ -1085,6 +1089,8 @@ create index \\"author2_name_age_index\\" on \\"author2\\" (\\"name\\", \\"age\\ alter table \\"author2\\" add constraint \\"author2_name_email_unique\\" unique (\\"name\\", \\"email\\"); create table \\"address2\\" (\\"author_id\\" int4 not null, \\"value\\" varchar(255) not null); +comment on table \\"address2\\" is 'This is address table'; +comment on column \\"address2\\".\\"value\\" is 'This is address property'; alter table \\"address2\\" add constraint \\"address2_pkey\\" primary key (\\"author_id\\"); alter table \\"address2\\" add constraint \\"address2_author_id_unique\\" unique (\\"author_id\\"); diff --git a/tests/entities-sql/Address2.ts b/tests/entities-sql/Address2.ts index 46d31cfd1f34..fde24e59f7c0 100644 --- a/tests/entities-sql/Address2.ts +++ b/tests/entities-sql/Address2.ts @@ -1,13 +1,13 @@ import { Entity, Property, OneToOne } from '@mikro-orm/core'; import { Author2 } from './Author2'; -@Entity() +@Entity({ comment: 'This is address table' }) export class Address2 { @OneToOne({ entity: () => Author2, primary: true, joinColumn: 'author_id', unique: 'address2_author_id_unique' }) author: Author2; - @Property() + @Property({ comment: 'This is address property' }) value: string; constructor(author: Author2, value: string) {