mikro-orm-find-dataloader
Version:
Additional dataloaders for the MikroORM EntityManager find/findOne/etc methods.
145 lines (144 loc) • 5.47 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-non-null-assertion */
const sqlite_1 = require("@mikro-orm/sqlite");
const findRepository_1 = require("./findRepository");
function mockLogger(orm, debug = ["query", "query-params"], mock = jest.fn()) {
const logger = orm.config.getLogger();
Object.assign(logger, { writer: mock });
orm.config.set("debug", debug);
logger.setDebugMode(debug);
return mock;
}
const findDataloaderDefault = true;
let Author = class Author {
id;
name;
books = new sqlite_1.Collection(this);
[sqlite_1.EntityRepositoryType];
constructor({ id, name }) {
if (id != null) {
this.id = id;
}
this.name = name;
}
};
__decorate([
(0, sqlite_1.PrimaryKey)(),
__metadata("design:type", Number)
], Author.prototype, "id", void 0);
__decorate([
(0, sqlite_1.Property)(),
__metadata("design:type", String)
], Author.prototype, "name", void 0);
__decorate([
(0, sqlite_1.OneToMany)(() => Book, (book) => book.author),
__metadata("design:type", Object)
], Author.prototype, "books", void 0);
Author = __decorate([
(0, sqlite_1.Entity)(),
__metadata("design:paramtypes", [Object])
], Author);
let Book = class Book {
id;
title;
author;
[sqlite_1.EntityRepositoryType];
constructor({ id, title, author }) {
if (id != null) {
this.id = id;
}
this.title = title;
this.author = (0, sqlite_1.ref)(author);
}
};
__decorate([
(0, sqlite_1.PrimaryKey)(),
__metadata("design:type", Number)
], Book.prototype, "id", void 0);
__decorate([
(0, sqlite_1.Property)(),
__metadata("design:type", String)
], Book.prototype, "title", void 0);
__decorate([
(0, sqlite_1.ManyToOne)(() => Author, { ref: true }),
__metadata("design:type", Object)
], Book.prototype, "author", void 0);
Book = __decorate([
(0, sqlite_1.Entity)(),
__metadata("design:paramtypes", [Object])
], Book);
async function populateDatabase(em) {
const authors = [
new Author({ id: 1, name: "a" }),
new Author({ id: 2, name: "b" }),
new Author({ id: 3, name: "c" }),
new Author({ id: 4, name: "d" }),
new Author({ id: 5, name: "e" }),
];
em.persist(authors);
const books = [
new Book({ id: 1, title: "One", author: authors[0] }),
new Book({ id: 2, title: "Two", author: authors[0] }),
new Book({ id: 3, title: "Three", author: authors[1] }),
new Book({ id: 4, title: "Four", author: authors[2] }),
new Book({ id: 5, title: "Five", author: authors[2] }),
new Book({ id: 6, title: "Six", author: authors[2] }),
];
em.persist(books);
await em.flush();
}
describe("find", () => {
let orm;
let em;
beforeAll(async () => {
orm = await sqlite_1.MikroORM.init({
entityRepository: (0, findRepository_1.getFindDataloaderEntityRepository)(findDataloaderDefault),
dbName: ":memory:",
entities: [Author, Book],
loggerFactory: (options) => new sqlite_1.SimpleLogger(options),
});
try {
await orm.schema.clearDatabase();
}
catch { }
try {
const generator = orm.getSchemaGenerator();
await generator.createSchema({ wrap: true });
}
catch { }
await populateDatabase(orm.em.fork());
});
beforeEach(async () => {
em = orm.em.fork();
});
it("should fetch books with the find dataloader", async () => {
const authors = await em.fork().find(Author, {});
const mock = mockLogger(orm);
const authorBooks = await Promise.all(authors.map(async ({ id }) => await em.getRepository(Book).find({ author: id })));
expect(authorBooks).toBeDefined();
expect(authorBooks).toMatchSnapshot();
expect(mock.mock.calls).toEqual([
["[query] select `b0`.* from `book` as `b0` where `b0`.`author_id` in (1, 2, 3, 4, 5)"],
]);
});
it("should return the same books as find", async () => {
const authors = await em.fork().find(Author, {});
const dataloaderBooks = await Promise.all(authors.map(async ({ id }) => await em.getRepository(Book).find({ author: id })));
const findBooks = await Promise.all(authors.map(async ({ id }) => await em.fork().find(Book, { author: id })));
expect(dataloaderBooks.map((res) => res.map(({ id }) => id))).toEqual(findBooks.map((res) => res.map(({ id }) => id)));
});
afterEach(async () => { });
afterAll(async () => {
await orm.close(true);
});
});