Skip to content

Commit

Permalink
fix(core): respect serialization options like hidden on embeddables
Browse files Browse the repository at this point in the history
Closes #3429
  • Loading branch information
B4nan committed Aug 31, 2022
1 parent 541d62d commit d198e44
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 35 deletions.
14 changes: 6 additions & 8 deletions packages/core/src/entity/EntityHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@ export class EntityHelper {

static decorate<T extends object>(meta: EntityMetadata<T>, em: EntityManager): void {
const fork = em.fork(); // use fork so we can access `EntityFactory`

if (meta.embeddable) {
EntityHelper.defineBaseProperties(meta, meta.prototype, fork);
return;
}

const pk = meta.properties[meta.primaryKeys[0]];

if (pk.name === '_id' && meta.serializedPrimaryKey === 'id') {
if (pk?.name === '_id' && meta.serializedPrimaryKey === 'id') {
EntityHelper.defineIdProperty(meta, em.getPlatform());
}

EntityHelper.defineBaseProperties(meta, meta.prototype, fork);
EntityHelper.defineProperties(meta);

if (!meta.embeddable) {
EntityHelper.defineProperties(meta);
}

const prototype = meta.prototype as Dictionary;

if (!prototype.toJSON) { // toJSON can be overridden
Expand Down
6 changes: 1 addition & 5 deletions packages/core/src/entity/wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ export function wrap<T, PK extends keyof T | unknown = PrimaryProperty<T>>(entit
return entity as unknown as IWrappedEntity<T, PK>;
}

if (entity) {
return entity.__helper!;
}

return entity as unknown as IWrappedEntity<T, PK>;
return entity?.__helper ?? entity;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/migrations/src/Migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class Migrator implements IMigrator {
const expected = new Set<string>();

Object.values(this.em.getMetadata().getAll())
.filter(meta => meta.collection)
.filter(meta => meta.tableName && !meta.embeddable && !meta.virtual)
.forEach(meta => {
const schema = meta.schema ?? this.config.get('schema', this.em.getPlatform().getDefaultSchemaName());
expected.add(schema ? `${schema}.${meta.collection}` : meta.collection);
Expand Down
7 changes: 2 additions & 5 deletions tests/MetadataValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,14 @@ describe('MetadataValidator', () => {
age: { reference: 'scalar', name: 'age', type: 'string' },
totalBooks: { reference: 'scalar', name: 'totalBooks', type: 'number' },
usedTags: { reference: 'scalar', name: 'usedTags', type: 'string[]' },
invalid1: { reference: 'embedded', name: 'invalid1', type: 'object' },
invalid2: { reference: '1:m', name: 'invalid2', type: 'Foo' },
invalid1: { reference: '1:m', name: 'invalid1', type: 'Foo' },
};
const meta = { AuthorProfile: { expression: '...', name: 'AuthorProfile', className: 'AuthorProfile', properties } } as any;
meta.AuthorProfile.root = meta.AuthorProfile;
expect(() => validator.validateEntityDefinition(new MetadataStorage(meta as any), 'AuthorProfile')).toThrowError(`Virtual entity AuthorProfile cannot have primary key AuthorProfile.id`);
delete properties.id.primary;
expect(() => validator.validateEntityDefinition(new MetadataStorage(meta as any), 'AuthorProfile')).toThrowError(`Only scalar properties are allowed inside virtual entity. Found 'embedded' in AuthorProfile.invalid1`);
expect(() => validator.validateEntityDefinition(new MetadataStorage(meta as any), 'AuthorProfile')).toThrowError(`Only scalar and embedded properties are allowed inside virtual entity. Found '1:m' in AuthorProfile.invalid1`);
delete properties.invalid1;
expect(() => validator.validateEntityDefinition(new MetadataStorage(meta as any), 'AuthorProfile')).toThrowError(`Only scalar properties are allowed inside virtual entity. Found '1:m' in AuthorProfile.invalid2`);
delete properties.invalid2;
expect(() => validator.validateEntityDefinition(new MetadataStorage(meta as any), 'AuthorProfile')).not.toThrowError();
});

Expand Down
2 changes: 1 addition & 1 deletion tests/MikroORM.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('MikroORM', () => {
discovery: {},
migrations: { path: './dist/migrations', glob: '*.js' },
});
expect(Object.keys(orm.getMetadata().getAll()).sort()).toEqual(['Author4', 'Book4', 'BookTag4', 'FooBar4', 'FooBaz4', 'Publisher4', 'Test4', 'User4', 'publisher4_tests', 'tags_ordered', 'tags_unordered']);
expect(Object.keys(orm.getMetadata().getAll()).sort()).toEqual(['Author4', 'Book4', 'BookTag4', 'FooBar4', 'FooBaz4', 'Identity', 'Publisher4', 'Test4', 'User4', 'publisher4_tests', 'tags_ordered', 'tags_unordered']);
});

test('should work with dynamic passwords/tokens', async () => {
Expand Down
4 changes: 2 additions & 2 deletions tests/RequestContext.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RequestContext, MikroORM, wrap } from '@mikro-orm/core';
import { SqliteDriver } from '@mikro-orm/sqlite';
import { Author4, BaseEntity5, Book4, BookTag4, FooBar4, FooBaz4, Publisher4, Test4 } from './entities-schema';
import { Author4, BaseEntity5, Book4, BookTag4, FooBar4, FooBaz4, Publisher4, Test4, IdentitySchema } from './entities-schema';
import { initORMMongo } from './bootstrap';
import { Author, Book } from './entities';

Expand Down Expand Up @@ -72,7 +72,7 @@ describe('MultiRequestContext', () => {

beforeAll(async () => {
orm1 = await MikroORM.init<SqliteDriver>({
entities: [Author4, Book4, BookTag4, Publisher4, Test4, BaseEntity5],
entities: [Author4, Book4, BookTag4, Publisher4, Test4, BaseEntity5, IdentitySchema],
dbName: ':memory:',
driver: SqliteDriver,
contextName: 'orm1',
Expand Down
4 changes: 2 additions & 2 deletions tests/TransactionContext.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { MikroORM } from '@mikro-orm/core';
import { SqliteDriver } from '@mikro-orm/sqlite';
import { Author4, BaseEntity5, Book4, BookTag4, FooBar4, FooBaz4, Publisher4, Test4 } from './entities-schema';
import { Author4, BaseEntity5, Book4, BookTag4, FooBar4, FooBaz4, Publisher4, Test4, IdentitySchema } from './entities-schema';

let orm1: MikroORM<SqliteDriver>;
let orm2: MikroORM<SqliteDriver>;

beforeAll(async () => {
orm1 = await MikroORM.init<SqliteDriver>({
entities: [Author4, Book4, BookTag4, Publisher4, Test4, BaseEntity5],
entities: [Author4, Book4, BookTag4, Publisher4, Test4, BaseEntity5, IdentitySchema],
dbName: ':memory:',
driver: SqliteDriver,
contextName: 'orm1',
Expand Down
4 changes: 2 additions & 2 deletions tests/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
Author2, Book2, BookTag2, FooBar2, FooBaz2, Publisher2, Test2, Label2, Configuration2, Address2, FooParam2,
} from './entities-sql';
import FooBar from './entities/FooBar';
import { Author4, Book4, BookTag4, Publisher4, Test4, FooBar4, FooBaz4, BaseEntity5 } from './entities-schema';
import { Author4, Book4, BookTag4, Publisher4, Test4, FooBar4, FooBaz4, BaseEntity5, IdentitySchema } from './entities-schema';
import { Author2Subscriber } from './subscribers/Author2Subscriber';
import { Test2Subscriber } from './subscribers/Test2Subscriber';
import { EverythingSubscriber } from './subscribers/EverythingSubscriber';
Expand Down Expand Up @@ -170,7 +170,7 @@ export async function initORMSqlite() {

export async function initORMSqlite2(type: 'sqlite' | 'better-sqlite' = 'sqlite') {
const orm = await MikroORM.init<SqliteDriver>({
entities: [Author4, Book4, BookTag4, Publisher4, Test4, FooBar4, FooBaz4, BaseEntity5],
entities: [Author4, Book4, BookTag4, Publisher4, Test4, FooBar4, FooBaz4, BaseEntity5, IdentitySchema],
dbName: ':memory:',
baseDir: BASE_DIR,
type,
Expand Down
6 changes: 3 additions & 3 deletions tests/entities-schema/Author4.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Collection, EventArgs } from '@mikro-orm/core';
import { EntitySchema, DateType, TimeType, BooleanType, t, ReferenceType } from '@mikro-orm/core';
import { EntitySchema, DateType, TimeType, BooleanType, t, ReferenceType, wrap } from '@mikro-orm/core';
import type { IBaseEntity5 } from './BaseEntity5';
import type { IBook4 } from './Book4';

Expand Down Expand Up @@ -35,8 +35,8 @@ export const IdentitySchema = new EntitySchema({
class: Identity,
embeddable: true,
properties: {
foo: { type: 'string', nullable: true },
bar: { type: 'number', nullable: true },
foo: { type: 'string', hidden: true },
bar: { type: 'number', hidden: true },
fooBar: { type: 'string', getter: true, persist: false },
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Migration20191013214813 extends Migration {
this.addSql('create index \`book4_author_id_index\` on \`book4\` (\`author_id\`);');
this.addSql('create index \`book4_publisher_id_index\` on \`book4\` (\`publisher_id\`);');
this.addSql('create table \`author4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`email\` text not null, \`age\` integer null, \`terms_accepted\` integer not null default 0, \`identities\` text null, \`born\` date(3) null, \`born_time\` time(3) null, \`favourite_book_id\` integer null, constraint \`author4_favourite_book_id_foreign\` foreign key(\`favourite_book_id\`) references \`book4\`(\`id\`) on delete set null on update cascade);');
this.addSql('create table \`author4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`email\` text not null, \`age\` integer null, \`terms_accepted\` integer not null default 0, \`identities\` text null, \`born\` date(3) null, \`born_time\` time(3) null, \`favourite_book_id\` integer null, \`identity\` json null, constraint \`author4_favourite_book_id_foreign\` foreign key(\`favourite_book_id\`) references \`book4\`(\`id\`) on delete set null on update cascade);');
this.addSql('create unique index \`author4_email_unique\` on \`author4\` (\`email\`);');
this.addSql('create index \`author4_favourite_book_id_index\` on \`author4\` (\`favourite_book_id\`);');
Expand Down Expand Up @@ -59,7 +59,7 @@ export class Migration20191013214813 extends Migration {
"create index \`book4_author_id_index\` on \`book4\` (\`author_id\`);",
"create index \`book4_publisher_id_index\` on \`book4\` (\`publisher_id\`);",
"",
"create table \`author4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`email\` text not null, \`age\` integer null, \`terms_accepted\` integer not null default 0, \`identities\` text null, \`born\` date(3) null, \`born_time\` time(3) null, \`favourite_book_id\` integer null, constraint \`author4_favourite_book_id_foreign\` foreign key(\`favourite_book_id\`) references \`book4\`(\`id\`) on delete set null on update cascade);",
"create table \`author4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`email\` text not null, \`age\` integer null, \`terms_accepted\` integer not null default 0, \`identities\` text null, \`born\` date(3) null, \`born_time\` time(3) null, \`favourite_book_id\` integer null, \`identity\` json null, constraint \`author4_favourite_book_id_foreign\` foreign key(\`favourite_book_id\`) references \`book4\`(\`id\`) on delete set null on update cascade);",
"create unique index \`author4_email_unique\` on \`author4\` (\`email\`);",
"create index \`author4_favourite_book_id_index\` on \`author4\` (\`favourite_book_id\`);",
"",
Expand Down Expand Up @@ -103,7 +103,7 @@ export class Migration20191013214813 extends Migration {
this.addSql('create index \`book4_author_id_index\` on \`book4\` (\`author_id\`);');
this.addSql('create index \`book4_publisher_id_index\` on \`book4\` (\`publisher_id\`);');
this.addSql('create table \`author4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`email\` text not null, \`age\` integer null, \`terms_accepted\` integer not null default 0, \`identities\` text null, \`born\` date(3) null, \`born_time\` time(3) null, \`favourite_book_id\` integer null, constraint \`author4_favourite_book_id_foreign\` foreign key(\`favourite_book_id\`) references \`book4\`(\`id\`) on delete set null on update cascade);');
this.addSql('create table \`author4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`email\` text not null, \`age\` integer null, \`terms_accepted\` integer not null default 0, \`identities\` text null, \`born\` date(3) null, \`born_time\` time(3) null, \`favourite_book_id\` integer null, \`identity\` json null, constraint \`author4_favourite_book_id_foreign\` foreign key(\`favourite_book_id\`) references \`book4\`(\`id\`) on delete set null on update cascade);');
this.addSql('create unique index \`author4_email_unique\` on \`author4\` (\`email\`);');
this.addSql('create index \`author4_favourite_book_id_index\` on \`author4\` (\`favourite_book_id\`);');
Expand Down Expand Up @@ -141,7 +141,7 @@ export class Migration20191013214813 extends Migration {
"create index \`book4_author_id_index\` on \`book4\` (\`author_id\`);",
"create index \`book4_publisher_id_index\` on \`book4\` (\`publisher_id\`);",
"",
"create table \`author4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`email\` text not null, \`age\` integer null, \`terms_accepted\` integer not null default 0, \`identities\` text null, \`born\` date(3) null, \`born_time\` time(3) null, \`favourite_book_id\` integer null, constraint \`author4_favourite_book_id_foreign\` foreign key(\`favourite_book_id\`) references \`book4\`(\`id\`) on delete set null on update cascade);",
"create table \`author4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`email\` text not null, \`age\` integer null, \`terms_accepted\` integer not null default 0, \`identities\` text null, \`born\` date(3) null, \`born_time\` time(3) null, \`favourite_book_id\` integer null, \`identity\` json null, constraint \`author4_favourite_book_id_foreign\` foreign key(\`favourite_book_id\`) references \`book4\`(\`id\`) on delete set null on update cascade);",
"create unique index \`author4_email_unique\` on \`author4\` (\`email\`);",
"create index \`author4_favourite_book_id_index\` on \`author4\` (\`favourite_book_id\`);",
"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ create table \`book4\` (\`id\` integer not null primary key autoincrement, \`cre
create index \`book4_author_id_index\` on \`book4\` (\`author_id\`);
create index \`book4_publisher_id_index\` on \`book4\` (\`publisher_id\`);
create table \`author4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`email\` text not null, \`age\` integer null, \`terms_accepted\` integer not null default 0, \`identities\` text null, \`born\` date(3) null, \`born_time\` time(3) null, \`favourite_book_id\` integer null, constraint \`author4_favourite_book_id_foreign\` foreign key(\`favourite_book_id\`) references \`book4\`(\`id\`) on delete set null on update cascade);
create table \`author4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`email\` text not null, \`age\` integer null, \`terms_accepted\` integer not null default 0, \`identities\` text null, \`born\` date(3) null, \`born_time\` time(3) null, \`favourite_book_id\` integer null, \`identity\` json null, constraint \`author4_favourite_book_id_foreign\` foreign key(\`favourite_book_id\`) references \`book4\`(\`id\`) on delete set null on update cascade);
create unique index \`author4_email_unique\` on \`author4\` (\`email\`);
create index \`author4_favourite_book_id_index\` on \`author4\` (\`favourite_book_id\`);
Expand Down Expand Up @@ -101,7 +101,7 @@ create table \`book4\` (\`id\` integer not null primary key autoincrement, \`cre
create index \`book4_author_id_index\` on \`book4\` (\`author_id\`);
create index \`book4_publisher_id_index\` on \`book4\` (\`publisher_id\`);
create table \`author4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`email\` text not null, \`age\` integer null, \`terms_accepted\` integer not null default 0, \`identities\` text null, \`born\` date(3) null, \`born_time\` time(3) null, \`favourite_book_id\` integer null, constraint \`author4_favourite_book_id_foreign\` foreign key(\`favourite_book_id\`) references \`book4\`(\`id\`) on delete set null on update cascade);
create table \`author4\` (\`id\` integer not null primary key autoincrement, \`created_at\` datetime null, \`updated_at\` datetime null, \`name\` text not null, \`email\` text not null, \`age\` integer null, \`terms_accepted\` integer not null default 0, \`identities\` text null, \`born\` date(3) null, \`born_time\` time(3) null, \`favourite_book_id\` integer null, \`identity\` json null, constraint \`author4_favourite_book_id_foreign\` foreign key(\`favourite_book_id\`) references \`book4\`(\`id\`) on delete set null on update cascade);
create unique index \`author4_email_unique\` on \`author4\` (\`email\`);
create index \`author4_favourite_book_id_index\` on \`author4\` (\`favourite_book_id\`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ describe('virtual entities (sqlite)', () => {
expect(mock.mock.calls).toHaveLength(2); // from cache, no additional queries

expect(total).toBe(3);
expect(JSON.parse(JSON.stringify(profiles[0])).identity).toEqual({
fooBar: 'foo 123',
});
expect(profiles).toEqual([
{
name: 'Jon Snow 1',
Expand Down

0 comments on commit d198e44

Please sign in to comment.