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.
143 lines (97 loc) • 2.94 kB
text/typescript
import { Cascade, Collection, Entity, ManyToOne, MikroORM, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';
import { PostgreSqlDriver } from '@mikro-orm/postgresql';
()
export class Country {
()
id!: number;
()
name!: string;
()
currency!: string;
()
currencySymbol!: string;
('State', 'country', { cascade: [Cascade.ALL], nullable: true })
states = new Collection<State>(this);
}
()
export class State {
(() => Country, { primary: true })
country!: Country;
()
id!: number;
()
name!: string;
('City', 'state', { cascade: [Cascade.ALL], nullable: true })
cities = new Collection<City>(this);
}
()
export class City {
(() => State, { primary: true })
state!: State;
()
id!: number;
()
name!: string;
}
()
export class User {
()
id!: string;
()
email!: string;
({ nullable: true })
first_name?: string;
({ nullable: true })
last_name?: string;
({ columnType: 'date', nullable: true })
date_of_birth?: Date;
({ columnType: 'timestamptz', nullable: false })
created = new Date();
({ columnType: 'timestamptz', onUpdate: () => new Date().toISOString() })
modified = new Date();
}
({ tableName: 'user' })
export class User1 {
()
id!: string;
()
email!: string;
({ nullable: true })
first_name?: string;
({ nullable: true })
last_name?: string;
({ columnType: 'date', nullable: true })
date_of_birth?: Date;
({ columnType: 'timestamptz', nullable: false })
created = new Date();
({ columnType: 'timestamptz', onUpdate: () => new Date().toISOString() })
modified = new Date();
()
city!: City;
}
describe('adding m:1 with composite PK (FK as PK + scalar PK) (GH 1687)', () => {
let orm: MikroORM<PostgreSqlDriver>;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [City, User, Country, State],
dbName: `mikro_orm_test_composite_fks`,
driver: PostgreSqlDriver,
});
await orm.schema.ensureDatabase();
await orm.schema.dropSchema();
});
afterAll(() => orm.close(true));
test('schema generator adds the m:1 columns and FK properly', async () => {
const diff0 = await orm.schema.getUpdateSchemaMigrationSQL({ wrap: false });
expect(diff0).toMatchSnapshot();
await orm.schema.execute(diff0.up);
orm.getMetadata().reset('User');
await orm.discoverEntity(User1);
const diff1 = await orm.schema.getUpdateSchemaMigrationSQL({ wrap: false });
expect(diff1).toMatchSnapshot();
await orm.schema.execute(diff1.up);
// down
await orm.schema.execute(diff1.down);
await orm.schema.execute(diff0.down);
});
});