-
-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): allow persisting 1:m collections
Previously only initialized items were allowed to be persisted when adding to 1:m collection, due to how propagation works. With this change we now allow having inverse side collections dirty as well, firing single update query for those if we see the items are now initialized. Closes #467
- Loading branch information
Showing
8 changed files
with
143 additions
and
19 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { unlinkSync } from 'fs'; | ||
import { Collection, Entity, PrimaryKey, ManyToOne, OneToMany, MikroORM, IdentifiedReference } from '@mikro-orm/core'; | ||
import { SqliteDriver } from '@mikro-orm/sqlite'; | ||
|
||
@Entity() | ||
class A { | ||
|
||
@PrimaryKey() | ||
id!: string; | ||
|
||
@ManyToOne({ entity: 'B', wrappedReference: true, nullable: true }) | ||
b?: IdentifiedReference<B>; | ||
|
||
} | ||
|
||
@Entity() | ||
class B { | ||
|
||
@PrimaryKey() | ||
id!: string; | ||
|
||
@OneToMany(() => A, a => a.b) | ||
as = new Collection<A>(this); | ||
|
||
} | ||
|
||
describe('GH issue 467', () => { | ||
|
||
let orm: MikroORM<SqliteDriver>; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [A, B], | ||
dbName: __dirname + '/../../temp/mikro_orm_test_gh467.db', | ||
debug: false, | ||
highlight: false, | ||
type: 'sqlite', | ||
}); | ||
await orm.getSchemaGenerator().dropSchema(); | ||
await orm.getSchemaGenerator().createSchema(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(true); | ||
unlinkSync(orm.config.get('dbName')!); | ||
}); | ||
|
||
test(`wrap().assign to collections is persisted`, async () => { | ||
const a = new A(); | ||
a.id = 'a1'; | ||
await orm.em.persistAndFlush(a); | ||
|
||
const b = new B(); | ||
orm.em.assign(b, { | ||
id: 'b1', | ||
as: ['a1'], | ||
}); | ||
await orm.em.persistAndFlush(b); | ||
orm.em.clear(); | ||
|
||
const b1 = await orm.em.findOneOrFail(B, 'b1', ['as']); | ||
expect(b1.as.getIdentifiers()).toEqual(['a1']); | ||
}); | ||
|
||
test(`assigning to new collection from inverse side is persisted`, async () => { | ||
const a = new A(); | ||
a.id = 'a2'; | ||
await orm.em.persistAndFlush(a); | ||
|
||
const b = new B(); | ||
b.id = 'b2'; | ||
b.as.set([orm.em.getReference(A, 'a2')]); | ||
await orm.em.persistAndFlush(b); | ||
orm.em.clear(); | ||
|
||
const b1 = await orm.em.findOneOrFail(B, 'b2', ['as']); | ||
expect(b1.as.getIdentifiers()).toEqual(['a2']); | ||
}); | ||
|
||
test(`assigning to loaded collection from inverse side is persisted`, async () => { | ||
const a = new A(); | ||
a.id = 'a3'; | ||
await orm.em.persistAndFlush(a); | ||
|
||
const b = new B(); | ||
b.id = 'b3'; | ||
await orm.em.persistAndFlush(b); | ||
orm.em.clear(); | ||
|
||
const b1 = await orm.em.findOneOrFail(B, 'b3', ['as']); | ||
b1.as.set([orm.em.getReference(A, 'a3')]); | ||
await orm.em.flush(); | ||
orm.em.clear(); | ||
|
||
const b2 = await orm.em.findOneOrFail(B, 'b3', ['as']); | ||
expect(b2.as.getIdentifiers()).toEqual(['a3']); | ||
}); | ||
|
||
}); |