undeexcepturi
Version:
TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.
69 lines (51 loc) • 1.53 kB
text/typescript
import { MikroORM, Embeddable, Embedded, Entity, PrimaryKey, Property, Hidden, Opt, wrap } from '@mikro-orm/sqlite';
class Address {
addressLine1!: Hidden & string;
addressLine2!: Hidden<string>;
get address(): Opt<string> {
return [this.addressLine1, this.addressLine2].join(' ');
}
city!: string;
country!: string;
}
export class Organization {
id!: number;
address!: Address;
}
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
dbName: ':memory:',
entities: [Organization],
});
await orm.schema.createSchema();
});
afterAll(async () => {
await orm.close();
});
test('embeddable serialization flags', async () => {
const org = orm.em.create(Organization, {
address: {
addressLine1: 'l1',
addressLine2: 'l2',
city: 'city 1',
country: 'country 1',
},
});
await orm.em.persistAndFlush(org);
expect(JSON.stringify(org)).toBe(`{"id":1,"address":{"city":"city 1","country":"country 1","address":"l1 l2"}}`);
expect(JSON.stringify([org])).toBe(`[{"id":1,"address":{"city":"city 1","country":"country 1","address":"l1 l2"}}]`);
// @ts-expect-error
expect(wrap(org).toObject().address.addressLine1).toBeUndefined();
// @ts-expect-error
expect(wrap(org).toObject().address.addressLine2).toBeUndefined();
});