Skip to content

Commit

Permalink
feat(core): readonly entity (#738)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinverdy committed Aug 12, 2020
1 parent 2acee0c commit 7581592
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/core/src/decorators/Entity.ts
Expand Up @@ -21,5 +21,6 @@ export type EntityOptions<T> = {
discriminatorMap?: Dictionary<string>;
discriminatorValue?: string;
comment?: string;
readonly?: boolean;
customRepository?: () => Constructor<EntityRepository<T>>;
};
1 change: 1 addition & 0 deletions packages/core/src/typings.ts
Expand Up @@ -182,6 +182,7 @@ export interface EntityMetadata<T extends AnyEntity<T> = any> {
useCache: boolean;
filters: Dictionary<FilterDef<T>>;
comment?: string;
readonly?: boolean;
}

export interface ISchemaGenerator {
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/unit-of-work/ChangeSetComputer.ts
Expand Up @@ -19,6 +19,10 @@ export class ChangeSetComputer {
const changeSet = { entity } as ChangeSet<T>;
const meta = this.metadata.get(entity.constructor.name);

if (meta.readonly) {
return null;
}

changeSet.name = meta.name;
changeSet.type = this.originalEntityData[wrap(entity, true).__uuid] ? ChangeSetType.UPDATE : ChangeSetType.CREATE;
changeSet.collection = meta.collection;
Expand Down
8 changes: 8 additions & 0 deletions tests/UnitOfWork.test.ts
Expand Up @@ -3,6 +3,7 @@ import { ChangeSet, ChangeSetComputer, ChangeSetType, EntityValidator, EventSubs
import { initORMMongo, wipeDatabase } from './bootstrap';
import FooBar from './entities/FooBar';
import { FooBaz } from './entities/FooBaz';
import { Dummy } from './entities/Dummy';

describe('UnitOfWork', () => {

Expand Down Expand Up @@ -93,6 +94,13 @@ describe('UnitOfWork', () => {
expect(uow.getIdentityMap()).toEqual({});
});

test('changeSet is null for readonly entity', async () => {
const dummy = new Dummy();
uow.merge(dummy);
const changeSet = await computer.computeChangeSet(dummy);
expect(changeSet).toBeNull();
});

test('persist and remove will add entity to given stack only once', async () => {
const author = new Author('test', 'test');
author.id = '00000001885f0a3cc37dc9f0';
Expand Down
10 changes: 10 additions & 0 deletions tests/__snapshots__/EntityGenerator.test.ts.snap
Expand Up @@ -270,6 +270,16 @@ export class Configuration2 {
@Property({ length: 255 })
value!: string;
}
",
"import { Entity, PrimaryKey } from 'mikro-orm';
@Entity()
export class Dummy2 {
@PrimaryKey()
id!: number;
}
",
"import { Entity, OneToOne, PrimaryKey, Property } from 'mikro-orm';
Expand Down
8 changes: 8 additions & 0 deletions tests/__snapshots__/SchemaGenerator.test.ts.snap
Expand Up @@ -21,6 +21,8 @@ alter table \`foo_param2\` add index \`foo_param2_bar_id_index\`(\`bar_id\`);
alter table \`foo_param2\` add index \`foo_param2_baz_id_index\`(\`baz_id\`);
alter table \`foo_param2\` add primary key \`foo_param2_pkey\`(\`bar_id\`, \`baz_id\`);
create table \`dummy2\` (\`id\` int unsigned not null auto_increment primary key) default character set utf8mb4 engine = InnoDB;
create table \`car2\` (\`name\` varchar(100) not null, \`year\` int(11) unsigned not null, \`price\` int(11) not null) default character set utf8mb4 engine = InnoDB;
alter table \`car2\` add index \`car2_name_index\`(\`name\`);
alter table \`car2\` add index \`car2_year_index\`(\`year\`);
Expand Down Expand Up @@ -180,6 +182,7 @@ drop table if exists \`user2_sandwiches\`;
drop table if exists \`user2\`;
drop table if exists \`car_owner2\`;
drop table if exists \`car2\`;
drop table if exists \`dummy2\`;
drop table if exists \`foo_param2\`;
drop table if exists \`foo_bar2\`;
drop table if exists \`foo_baz2\`;
Expand Down Expand Up @@ -211,6 +214,7 @@ drop table if exists \`user2_sandwiches\`;
drop table if exists \`user2\`;
drop table if exists \`car_owner2\`;
drop table if exists \`car2\`;
drop table if exists \`dummy2\`;
drop table if exists \`foo_param2\`;
drop table if exists \`foo_bar2\`;
drop table if exists \`foo_baz2\`;
Expand All @@ -234,6 +238,8 @@ alter table \`foo_param2\` add index \`foo_param2_bar_id_index\`(\`bar_id\`);
alter table \`foo_param2\` add index \`foo_param2_baz_id_index\`(\`baz_id\`);
alter table \`foo_param2\` add primary key \`foo_param2_pkey\`(\`bar_id\`, \`baz_id\`);
create table \`dummy2\` (\`id\` int unsigned not null auto_increment primary key) default character set utf8mb4 engine = InnoDB;
create table \`car2\` (\`name\` varchar(100) not null, \`year\` int(11) unsigned not null, \`price\` int(11) not null) default character set utf8mb4 engine = InnoDB;
alter table \`car2\` add index \`car2_name_index\`(\`name\`);
alter table \`car2\` add index \`car2_year_index\`(\`year\`);
Expand Down Expand Up @@ -918,6 +924,8 @@ alter table \`foo_param2\` add index \`foo_param2_bar_id_index\`(\`bar_id\`);
alter table \`foo_param2\` add index \`foo_param2_baz_id_index\`(\`baz_id\`);
alter table \`foo_param2\` add primary key \`foo_param2_pkey\`(\`bar_id\`, \`baz_id\`);
create table \`dummy2\` (\`id\` int unsigned not null auto_increment primary key) default character set utf8mb4 engine = InnoDB;
create table \`car2\` (\`name\` varchar(100) not null, \`year\` int(11) unsigned not null, \`price\` int(11) not null) default character set utf8mb4 engine = InnoDB;
alter table \`car2\` add index \`car2_name_index\`(\`name\`);
alter table \`car2\` add index \`car2_year_index\`(\`year\`);
Expand Down
6 changes: 6 additions & 0 deletions tests/entities-sql/Dummy2.ts
@@ -0,0 +1,6 @@
import { Entity } from '@mikro-orm/core';
import { BaseEntity2 } from './BaseEntity2';

@Entity({ readonly: true })
export class Dummy2 extends BaseEntity2 {}

5 changes: 5 additions & 0 deletions tests/entities/Dummy.ts
@@ -0,0 +1,5 @@
import { Entity } from '@mikro-orm/core';
import { BaseEntity } from './BaseEntity';

@Entity({ readonly: true })
export class Dummy extends BaseEntity<Dummy> {}
3 changes: 3 additions & 0 deletions tests/mysql-schema.sql
Expand Up @@ -12,6 +12,7 @@ drop table if exists `foo_baz2`;
drop table if exists `foo_param2`;
drop table if exists `configuration2`;
drop table if exists `car2`;
drop table if exists `dummy2`;
drop table if exists `car_owner2`;
drop table if exists `user2`;
drop table if exists `base_user2`;
Expand Down Expand Up @@ -80,6 +81,8 @@ alter table `configuration2` add index `configuration2_property_index`(`property
alter table `configuration2` add index `configuration2_test_id_index`(`test_id`);
alter table `configuration2` add primary key `configuration2_pkey`(`property`, `test_id`);

create table `dummy2` (`id` int unsigned not null auto_increment primary key) default character set utf8mb4 engine = InnoDB;

create table `car2` (`name` varchar(100) not null, `year` int(11) unsigned not null, `price` int(11) not null) default character set utf8mb4 engine = InnoDB;
alter table `car2` add index `car2_name_index`(`name`);
alter table `car2` add index `car2_year_index`(`year`);
Expand Down

0 comments on commit 7581592

Please sign in to comment.