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

feat(core): add support for entity and property comment #668

Merged
merged 3 commits into from
Jul 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/docs/decorators.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
5 changes: 5 additions & 0 deletions packages/knex/src/schema/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ 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);
}

B4nan marked this conversation as resolved.
Show resolved Hide resolved
if (meta.comment) {
vinverdy marked this conversation as resolved.
Show resolved Hide resolved
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 +386,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
Original file line number Diff line number Diff line change
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 \'It is address property\') default character set utf8mb4 engine = InnoDB comment = \'It is address table\';
B4nan marked this conversation as resolved.
Show resolved Hide resolved
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 \'It is address property\') default character set utf8mb4 engine = InnoDB comment = \'It is address table\';
B4nan marked this conversation as resolved.
Show resolved Hide resolved
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 'It is address table';
comment on column \\"address2\\".\\"value\\" is 'It is address property';
B4nan marked this conversation as resolved.
Show resolved Hide resolved
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 'It is address table';
comment on column \\"address2\\".\\"value\\" is 'It is address property';
B4nan marked this conversation as resolved.
Show resolved Hide resolved
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 \'It is address property\') default character set utf8mb4 engine = InnoDB comment = \'It is address table\';
B4nan marked this conversation as resolved.
Show resolved Hide resolved
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 'It is address table';
comment on column \\"address2\\".\\"value\\" is 'It is address property';
B4nan marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Entity, Property, OneToOne } from '@mikro-orm/core';
import { Author2 } from './Author2';

@Entity()
@Entity({comment: 'It is address table'})
B4nan marked this conversation as resolved.
Show resolved Hide resolved
export class Address2 {

@OneToOne({ entity: () => Author2, primary: true, joinColumn: 'author_id', unique: 'address2_author_id_unique' })
author: Author2;

@Property()
@Property({comment: 'It is address property'})
B4nan marked this conversation as resolved.
Show resolved Hide resolved
value: string;

constructor(author: Author2, value: string) {
Expand Down