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.
80 lines (62 loc) • 1.27 kB
text/typescript
import {
Collection,
Entity,
ManyToMany,
ManyToOne,
MikroORM,
PrimaryKey,
Property,
ref,
Ref,
wrap,
} from '@mikro-orm/sqlite';
()
class Item {
()
id!: number;
()
name: string;
constructor(name: string) {
this.name = name;
}
}
()
class User {
()
id!: number;
()
name: string;
({ unique: true })
email: string;
(() => Item, { ref: true, nullable: true })
item?: Ref<Item>;
(() => Item)
items = new Collection<Item>(this);
constructor(name: string, email: string) {
this.name = name;
this.email = email;
}
}
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
dbName: ':memory:',
entities: [User, Item],
});
await orm.schema.createSchema();
await orm.em.insert(Item, { id: 1, name: 'item' });
});
afterAll(async () => {
await orm.close(true);
});
test('load on not managed entity (GH #5082)', async () => {
const u = new User('foo', 'foo@x.com');
u.id = 123;
u.item = ref(Item, 1);
orm.em.persist(u);
await u.items.load();
await u.item.load();
expect(wrap(u).isManaged()).toBe(false);
await orm.em.flush();
expect(wrap(u).isManaged()).toBe(true);
});