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

bug: QueryBuilder execute Method Doesn't Properly Map Embeddables #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
106 changes: 83 additions & 23 deletions src/example.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,71 @@
import { Entity, MikroORM, PrimaryKey, Property } from '@mikro-orm/sqlite';
import { EntitySchema, MikroORM } from '@mikro-orm/sqlite';

@Entity()
class User {

@PrimaryKey()
id!: number;
class PersonName {
constructor(readonly givenName: string, readonly surname: string) {}
}

@Property()
name: string;
export const PersonNameSchema = new EntitySchema<PersonName>({
class: PersonName,
embeddable: true,
properties: {
givenName: { type: 'string' },
surname: { type: 'string' },
},
});

@Property({ unique: true })
email: string;
class EmergencyContact {
constructor(readonly name: PersonName, readonly relationship: string) {}
}

constructor(name: string, email: string) {
this.name = name;
this.email = email;
}
export const EmergencyContactSchema = new EntitySchema<EmergencyContact>({
class: EmergencyContact,
embeddable: true,
properties: {
name: {
kind: 'embedded',
entity: () => PersonName,
prefix: 'emergency_contact_', // have to override the parent prefix
},
relationship: { type: 'string' },
},
});

class Patient {
constructor(
readonly id: string,
readonly name: PersonName,
readonly emergencyContact: EmergencyContact
) {}
}

export const PatientSchema = new EntitySchema<Patient>({
class: Patient,
properties: {
id: {
type: 'text',
primary: true,
},
name: {
kind: 'embedded',
entity: () => PersonName,
prefix: false,
},
emergencyContact: {
kind: 'embedded',
entity: () => EmergencyContact,
prefix: 'emergency_contact_',
nullable: true,
},
},
});


let orm: MikroORM;

beforeAll(async () => {
orm = await MikroORM.init({
dbName: ':memory:',
entities: [User],
entities: [PatientSchema, PersonNameSchema, EmergencyContactSchema],
debug: ['query', 'query-params'],
allowGlobalContext: true, // only for testing
});
Expand All @@ -36,16 +77,35 @@ afterAll(async () => {
});

test('basic CRUD example', async () => {
orm.em.create(User, { name: 'Foo', email: 'foo' });
orm.em.create(Patient, { id: '1', name: {
givenName: 'John',
surname: 'Doe',
}, emergencyContact: {
name: {
givenName: 'Jane',
surname: 'Doe',
},
relationship: 'wife',
} });
await orm.em.flush();
orm.em.clear();

const user = await orm.em.findOneOrFail(User, { email: 'foo' });
expect(user.name).toBe('Foo');
user.name = 'Bar';
orm.em.remove(user);
await orm.em.flush();
const qb = orm.em.createQueryBuilder('Patient');

const count = await orm.em.count(User, { email: 'foo' });
expect(count).toBe(0);
qb.select(['*']).where({ id: '1' });
const patient = await qb.execute('get', true);
expect(patient).toStrictEqual({
id: '1',
name: {
givenName: 'John',
surname: 'Doe'
},
emergencyContact: {
name: {
givenName: 'Jane',
surname: 'Doe'
},
relationship: 'wife'
}
});
});