-
-
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.
fix(schema): allow 1:m properties in pivot entities
- Loading branch information
Showing
3 changed files
with
116 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { | ||
Collection, | ||
Entity, | ||
ManyToMany, | ||
ManyToOne, | ||
MikroORM, | ||
OneToMany, | ||
PrimaryKey, | ||
PrimaryKeyProp, | ||
} from '@mikro-orm/sqlite'; | ||
import { v4 } from 'uuid'; | ||
|
||
@Entity() | ||
class Bar { | ||
|
||
@PrimaryKey({ | ||
unique: true, | ||
}) | ||
id: string = v4(); | ||
|
||
@ManyToMany({ | ||
entity: () => Foo, | ||
mappedBy: o => o.bars, | ||
}) | ||
foos = new Collection<Foo>(this); | ||
|
||
} | ||
|
||
@Entity() | ||
class Foo { | ||
|
||
@PrimaryKey({ | ||
unique: true, | ||
}) | ||
id: string = v4(); | ||
|
||
@ManyToMany({ | ||
entity: () => Bar, | ||
pivotEntity: () => FooBar, | ||
type: Bar, | ||
}) | ||
bars = new Collection<Bar>(this); | ||
|
||
} | ||
|
||
@Entity() | ||
class FooBar { | ||
|
||
@ManyToOne({ | ||
entity: () => Foo, | ||
primary: true, | ||
}) | ||
foo!: Foo; | ||
|
||
@ManyToOne({ | ||
entity: () => Bar, | ||
primary: true, | ||
}) | ||
user!: Bar; | ||
|
||
@OneToMany(() => Baz, baz => baz.foobar, { nullable: true, default: null }) | ||
bazes = new Collection<Baz>(this); | ||
|
||
[PrimaryKeyProp]?: ['foo', 'bar']; | ||
|
||
} | ||
|
||
@Entity() | ||
class Baz { | ||
|
||
@PrimaryKey({ | ||
unique: true, | ||
}) | ||
id: string = v4(); | ||
|
||
@ManyToOne(() => FooBar) | ||
foobar!: FooBar; | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [FooBar], | ||
dbName: ':memory:', | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(true); | ||
}); | ||
|
||
test('1:m property inside pivot entity', async () => { | ||
await expect(orm.schema.getUpdateSchemaSQL()).resolves.toMatchSnapshot(); | ||
}); |
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,19 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`1:m property inside pivot entity 1`] = ` | ||
"pragma foreign_keys = off; | ||
create table \`bar\` (\`id\` text not null, primary key (\`id\`)); | ||
create table \`foo\` (\`id\` text not null, primary key (\`id\`)); | ||
create table \`foo_bar\` (\`foo_id\` text not null, \`user_id\` text not null, constraint \`foo_bar_foo_id_foreign\` foreign key(\`foo_id\`) references \`foo\`(\`id\`) on update cascade, constraint \`foo_bar_user_id_foreign\` foreign key(\`user_id\`) references \`bar\`(\`id\`) on update cascade, primary key (\`foo_id\`, \`user_id\`)); | ||
create index \`foo_bar_foo_id_index\` on \`foo_bar\` (\`foo_id\`); | ||
create index \`foo_bar_user_id_index\` on \`foo_bar\` (\`user_id\`); | ||
create table \`baz\` (\`id\` text not null, \`foobar_foo_id\` text not null, \`foobar_user_id\` text not null, constraint \`baz_foobar_foo_id_foobar_user_id_foreign\` foreign key(\`foobar_foo_id\`, \`foobar_user_id\`) references \`foo_bar\`(\`foo_id\`, \`user_id\`) on update cascade, primary key (\`id\`)); | ||
create index \`baz_foobar_foo_id_foobar_user_id_index\` on \`baz\` (\`foobar_foo_id\`, \`foobar_user_id\`); | ||
pragma foreign_keys = on; | ||
" | ||
`; |