-
-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(count): initial implementation of loadCount (#955)
Closes #949
- Loading branch information
Showing
9 changed files
with
251 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2178,6 +2178,16 @@ describe('EntityManagerMongo', () => { | |
await expect(driver.nativeInsert(Author.name, { name: 'author', email: 'email' })).rejects.toThrow(UniqueConstraintViolationException); | ||
}); | ||
|
||
test('loadCount with 1:n relationships', async () => { | ||
let author = new Author('Jon Snow', '[email protected]'); | ||
author.books.add(new Book('b1'), new Book('b2'), new Book('b3'), new Book('b4')); | ||
await orm.em.persistAndFlush(author); | ||
orm.em.clear(); | ||
|
||
author = await orm.em.findOneOrFail(Author, author.id); | ||
await expect(author.books.loadCount()).resolves.toEqual(4); | ||
}); | ||
|
||
afterAll(async () => orm.close(true)); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { Collection, EntityManager, LockMode, MikroORM, QueryOrder, Logger, ValidationError, wrap } from '@mikro-orm/core'; | ||
import { Collection, EntityManager, LockMode, MikroORM, QueryOrder, Logger, ValidationError, wrap, ArrayCollection } from '@mikro-orm/core'; | ||
import { SqliteDriver } from '@mikro-orm/sqlite'; | ||
import { initORMSqlite2, wipeDatabaseSqlite2 } from './bootstrap'; | ||
import { Author4, Book4, BookTag4, FooBar4, IAuthor4, IPublisher4, Publisher4, PublisherType, Test4 } from './entities-schema'; | ||
|
@@ -932,6 +932,76 @@ describe('EntityManagerSqlite2', () => { | |
await orm.em.flush(); | ||
}); | ||
|
||
test('loadCount to get the number of entries without initializing the collection (GH issue #949)', async () => { | ||
let author = orm.em.create(Author4, { name: 'Jon Doe', email: '[email protected]' }); | ||
author.books.add(orm.em.create(Book4, { title: 'bo1' })); | ||
// Entity not managed yet | ||
expect(() => author.books.loadCount()).rejects.toThrow(ValidationError); | ||
|
||
await orm.em.persistAndFlush(author); | ||
|
||
const reloadedBook = await author.books.loadCount(); | ||
expect(reloadedBook).toBe(1); | ||
|
||
// Adding new items | ||
const laterRemoved = orm.em.create(Book4, { title: 'bo2' }); | ||
author.books.add(laterRemoved, orm.em.create(Book4, { title: 'bo3' })); | ||
const threeItms = await author.books.loadCount(); | ||
expect(threeItms).toEqual(3); | ||
|
||
// Force refresh | ||
expect(await author.books.loadCount(true)).toEqual(1); | ||
// Testing array collection implementation | ||
await orm.em.flush(); | ||
orm.em.clear(); | ||
|
||
|
||
// Updates when removing an item | ||
author = (await orm.em.findOneOrFail(Author4, author.id)); | ||
expect(await author.books.loadCount()).toEqual(3); | ||
await author.books.init(); | ||
author.books.remove(author.books[0]); | ||
expect(await author.books.loadCount()).toEqual(2); | ||
expect(await author.books.loadCount(true)).toEqual(3); | ||
await orm.em.flush(); | ||
orm.em.clear(); | ||
|
||
// Resets the counter when hydrating | ||
author = (await orm.em.findOneOrFail(Author4, author.id)); | ||
await author.books.loadCount(); | ||
author.books.hydrate([]); | ||
expect(await author.books.loadCount()).toEqual(0); | ||
expect(await author.books.loadCount(true)).toEqual(2); | ||
|
||
// Code coverage ? | ||
const arryCollection = new ArrayCollection(author); | ||
expect(await arryCollection.loadCount()).toEqual(0); | ||
|
||
// n:m relations | ||
let taggedBook = orm.em.create(Book4, { title: 'FullyTagged' }); | ||
await orm.em.persistAndFlush(taggedBook); | ||
const tags = [orm.em.create(BookTag4, { name: 'science-fiction' }), orm.em.create(BookTag4, { name: 'adventure' }), orm.em.create(BookTag4, { name: 'horror' })]; | ||
taggedBook.tags.add(...tags); | ||
await expect(taggedBook.tags.loadCount()).resolves.toEqual(0); | ||
await orm.em.flush(); | ||
orm.em.clear(); | ||
|
||
taggedBook = await orm.em.findOneOrFail(Book4, taggedBook.id); | ||
await expect(taggedBook.tags.loadCount()).resolves.toEqual(tags.length); | ||
expect(taggedBook.tags.isInitialized()).toBe(false); | ||
await taggedBook.tags.init(); | ||
await expect(taggedBook.tags.loadCount()).resolves.toEqual(tags.length); | ||
const removing = taggedBook.tags[0]; | ||
taggedBook.tags.remove(removing); | ||
await expect(taggedBook.tags.loadCount()).resolves.toEqual(tags.length - 1); | ||
await expect(taggedBook.tags.loadCount(true)).resolves.toEqual(tags.length); | ||
await orm.em.flush(); | ||
orm.em.clear(); | ||
|
||
taggedBook = await orm.em.findOneOrFail(Book4, taggedBook.id); | ||
await expect(taggedBook.tags.loadCount()).resolves.toEqual(tags.length - 1); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(true); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { Entity, MikroORM, PrimaryKey, OneToMany, ManyToOne, Collection, ValidationError, ArrayCollection } from '@mikro-orm/core'; | ||
import { SqliteDriver } from '@mikro-orm/sqlite'; | ||
|
||
@Entity() | ||
class A { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
@OneToMany(() => B, b => b.a) | ||
bItems = new Collection<B>(this); | ||
|
||
} | ||
|
||
@Entity() | ||
class B { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@ManyToOne(() => A) | ||
a!: A; | ||
|
||
} | ||
describe('GH issue 949', () => { | ||
let orm: MikroORM<SqliteDriver>; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [A, B], | ||
dbName: ':memory:', | ||
type: 'sqlite', | ||
}); | ||
await orm.getSchemaGenerator().createSchema(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(true); | ||
}); | ||
|
||
test(`GH issue 949`, async () => { | ||
let aEntity = new A(); | ||
aEntity.bItems.add(new B()); | ||
// Entity not managed yet | ||
expect(() => aEntity.bItems.loadCount()).rejects.toThrow(ValidationError); | ||
|
||
await orm.em.persistAndFlush(aEntity); | ||
|
||
if (!aEntity) { return; } | ||
const reloadedBook = await aEntity.bItems.loadCount(); | ||
expect(reloadedBook).toBe(1); | ||
|
||
// Adding new items | ||
const laterRemoved = new B(); | ||
aEntity.bItems.add(laterRemoved, new B()); | ||
const threeItms = await aEntity.bItems.loadCount(); | ||
expect(threeItms).toEqual(3); | ||
|
||
// Force refresh | ||
expect(await aEntity.bItems.loadCount(true)).toEqual(1); | ||
// Testing array collection implemntation | ||
await orm.em.flush(); | ||
orm.em.clear(); | ||
|
||
|
||
// Updates when removing an item | ||
aEntity = (await orm.em.findOne(A, aEntity.id))!; | ||
expect(await aEntity.bItems.loadCount()).toEqual(3); | ||
await aEntity.bItems.init(); | ||
aEntity.bItems.remove(aEntity.bItems[0]); | ||
expect(await aEntity.bItems.loadCount()).toEqual(2); | ||
expect(await aEntity.bItems.loadCount(true)).toEqual(3); | ||
await orm.em.flush(); | ||
orm.em.clear(); | ||
|
||
// Resets the counter when hydrating | ||
aEntity = (await orm.em.findOne(A, aEntity.id))!; | ||
await aEntity.bItems.loadCount(); | ||
aEntity.bItems.hydrate([]); | ||
expect(await aEntity.bItems.loadCount()).toEqual(0); | ||
expect(await aEntity.bItems.loadCount(true)).toEqual(2); | ||
|
||
// Code coverage ? | ||
const arryCollection = new ArrayCollection(aEntity); | ||
expect(await arryCollection.loadCount()).toEqual(0); | ||
}); | ||
}); |