Skip to content

Commit

Permalink
feat(core): add support for entity and property comment (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinverdy authored and B4nan committed Aug 9, 2020
1 parent 5ecb6a3 commit c01b338
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/docs/decorators.md
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/decorators/Entity.ts
Expand Up @@ -20,5 +20,6 @@ export type EntityOptions<T> = {
discriminatorColumn?: string;
discriminatorMap?: Dictionary<string>;
discriminatorValue?: string;
comment?: string;
customRepository?: () => Constructor<EntityRepository<T>>;
};
1 change: 1 addition & 0 deletions packages/core/src/decorators/Property.ts
Expand Up @@ -55,6 +55,7 @@ export type PropertyOptions<T> = {
lazy?: boolean;
primary?: boolean;
serializedPrimaryKey?: boolean;
comment?: string;
};

export interface ReferenceOptions<T, O> extends PropertyOptions<O> {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/typings.ts
Expand Up @@ -151,6 +151,7 @@ export interface EntityProperty<T extends AnyEntity<T> = any> {
inverseJoinColumns: string[];
referencedColumnNames: string[];
referencedTableName: string;
comment?: string;
}

export interface EntityMetadata<T extends AnyEntity<T> = any> {
Expand Down Expand Up @@ -180,6 +181,7 @@ export interface EntityMetadata<T extends AnyEntity<T> = any> {
class: Constructor<T>;
abstract: boolean;
useCache: boolean;
comment?: string;
}

export interface ISchemaGenerator {
Expand Down
4 changes: 4 additions & 0 deletions packages/knex/src/schema/SchemaGenerator.ts
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 9 additions & 3 deletions tests/__snapshots__/SchemaGenerator.test.ts.snap
Expand Up @@ -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\`);
Expand Down Expand Up @@ -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\`);
Expand Down Expand Up @@ -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\\");
Expand Down Expand Up @@ -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\\");
Expand Down Expand Up @@ -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\`);
Expand Down Expand Up @@ -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\\");
Expand Down
4 changes: 2 additions & 2 deletions 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) {
Expand Down

0 comments on commit c01b338

Please sign in to comment.