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(fixtures): support for mongodb embeddables #14

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/fixtures/src/MetadataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import {
PropertyMetadata,
DefaultMetadataStore,
} from 'class-fixtures-factory';
import { MikroORM, Utils, EntityMetadata } from '@mikro-orm/core';
import {
MikroORM,
Utils,
EntityMetadata,
ReferenceType,
} from '@mikro-orm/core';

export class MetadataStore extends BaseMetadataStore {
private defaultStore = new DefaultMetadataStore(true);
Expand All @@ -32,6 +37,12 @@ export class MetadataStore extends BaseMetadataStore {
);
if (prop.primary) return null;
if (prop.type === 'method') return null;
if (Array.isArray(prop.embedded)) {
const parent = meta.properties[prop.embedded[0]];
if (parent.reference === ReferenceType.EMBEDDED && parent.object) {
return null;
}
}
return <PropertyMetadata>{
name: prop.name,
type: defaultMetaProp?.typeFromDecorator
Expand Down
8 changes: 8 additions & 0 deletions packages/fixtures/test/entities/profile.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Embeddable, Property } from '@mikro-orm/core';

@Embeddable()
export class Profile {
@Property() firstName!: string;
@Property() lastName!: string;
@Property() emailAddress!: string;
}
8 changes: 8 additions & 0 deletions packages/fixtures/test/entities/user.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Embedded, Entity } from '@mikro-orm/core';
import { BaseEntity } from './base.entity';
import { Profile } from './profile.entity';

@Entity()
export class User extends BaseEntity {
@Embedded({ entity: () => Profile, object: true }) profile!: Profile;
}
30 changes: 30 additions & 0 deletions packages/fixtures/test/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { FixtureFactory } from '../src/FixtureFactory';
import { WithSpecialType } from './entities/with-special-type.entity';
import { WithSpecialTypeFixed } from './entities/with-special-type-fixed.entity';
import { AuthorWithCustomization } from './entities/author-with-customization';
import { User } from './entities/user.entity';

jest.setTimeout(20000);

Expand Down Expand Up @@ -242,4 +243,33 @@ describe(`Factory`, () => {
expect(author.address).toBeUndefined();
});
});

describe('@Embeddable({ object: true }) [for mongodb subdocuments]', () => {
let user: User;

beforeAll(async () => {
// We can't be "connected" for this, as sqlite does not support embedded
// entities this way, but we can still access the metadata facilities of
// the EntityManager.
await orm.close();
orm = await MikroORM.init(ormConfig, false);
factory = factory = new FixtureFactory(orm);

user = factory.make(User).one();
});

it('populates the embeddable entity', () => {
expect(user.profile).toBeDefined();
expect(user.profile.firstName).toEqual(expect.any(String));
expect(user.profile.lastName).toEqual(expect.any(String));
expect(user.profile.emailAddress).toEqual(expect.any(String));
});

it('does not populate "prefixed" properties', () => {
const possiblyPrefixed = user as any;
expect(possiblyPrefixed.profile_firstName).not.toBeDefined();
expect(possiblyPrefixed.profile_lastName).not.toBeDefined();
expect(possiblyPrefixed.profile_emailAddress).not.toBeDefined();
});
});
});
4 changes: 4 additions & 0 deletions packages/fixtures/test/mikro-orm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
Book2,
Address2,
} from './entities/author-with-customization';
import { Profile } from './entities/profile.entity';
import { User } from './entities/user.entity';

export default <Options>{
metadataProvider: TsMorphMetadataProvider,
Expand All @@ -28,6 +30,8 @@ export default <Options>{
AuthorWithCustomization,
Book2,
Address2,
User,
Profile,
],
dbName: 'test.db',
type: 'sqlite',
Expand Down
69 changes: 69 additions & 0 deletions packages/fixtures/test/temp/Profile.ts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"data": {
"propertyOrder": {},
"properties": {
"firstName": {
"name": "firstName",
"reference": "scalar",
"getter": false,
"setter": false,
"type": "string"
},
"lastName": {
"name": "lastName",
"reference": "scalar",
"getter": false,
"setter": false,
"type": "string"
},
"emailAddress": {
"name": "emailAddress",
"reference": "scalar",
"getter": false,
"setter": false,
"type": "string"
}
},
"props": [
{
"name": "firstName",
"reference": "scalar",
"getter": false,
"setter": false,
"type": "string"
},
{
"name": "lastName",
"reference": "scalar",
"getter": false,
"setter": false,
"type": "string"
},
{
"name": "emailAddress",
"reference": "scalar",
"getter": false,
"setter": false,
"type": "string"
}
],
"primaryKeys": [],
"filters": {},
"hooks": {},
"indexes": [],
"uniques": [],
"className": "Profile",
"path": "./test/entities/profile.entity.ts",
"name": "Profile",
"embeddable": true,
"abstract": false,
"constructorParams": [],
"toJsonParams": [],
"useCache": true,
"relations": [],
"collection": "profile"
},
"origin": "./test/entities/profile.entity.ts",
"hash": "250bb9ee472a9fd9864259db6090675e",
"version": "4.5.1"
}
41 changes: 41 additions & 0 deletions packages/fixtures/test/temp/User.ts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"data": {
"propertyOrder": {},
"properties": {
"profile": {
"name": "profile",
"type": "Profile",
"reference": "embedded",
"object": true,
"prefix": true
}
},
"props": [
{
"name": "profile",
"type": "Profile",
"reference": "embedded",
"object": true,
"prefix": true
}
],
"primaryKeys": [],
"filters": {},
"hooks": {},
"indexes": [],
"uniques": [],
"className": "User",
"path": "./test/entities/user.entity.ts",
"name": "User",
"abstract": false,
"constructorParams": [],
"toJsonParams": [],
"extends": "BaseEntity",
"useCache": true,
"relations": [],
"collection": "user"
},
"origin": "./test/entities/user.entity.ts",
"hash": "9c16b52762f9d4c922a0faa76b82a775",
"version": "4.5.1"
}