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.
102 lines (82 loc) • 2.06 kB
text/typescript
import {
Collection,
DateType,
Entity,
ManyToOne,
OneToMany,
OneToOne,
PrimaryKey,
Property,
Ref,
ref,
} from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/postgresql';
import { v4 } from 'uuid';
class Author {
id = v4();
name!: string;
books = new Collection<Book>(this);
firstBook?: Ref<Book>;
constructor(name: string) {
this.name = name;
}
}
class Book {
id = v4();
releaseDate!: Date;
name!: string;
author!: Ref<Author>;
constructor(releaseDate: Date = new Date(), name: string, author: Author) {
this.releaseDate = releaseDate;
this.name = name;
this.author = ref((author));
}
}
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [Book, Author],
dbName: '4759',
});
await orm.schema.refreshDatabase();
});
afterAll(() => orm.close(true));
test(`GH issue 4759`, async () => {
const author = new Author('John');
const book1 = new Book(new Date('2023-09-01'), 'My second book', author);
const book2 = new Book(new Date('2023-01-01'), 'My first book', author);
await orm.em.fork().persistAndFlush([author, book1, book2]);
const authorFound = await orm.em.find(Author, { firstBook: { name: 'My first book' } }, { populate: ['books', 'firstBook'] });
expect(authorFound[0].firstBook?.$.name).toBe('My first book');
});