Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): support pivot entities with autoincrement PK
Closes #4988
- Loading branch information
Showing
2 changed files
with
148 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { BigIntType, Collection, EntitySchema, Ref } from '@mikro-orm/core'; | ||
import { MikroORM } from '@mikro-orm/postgresql'; | ||
|
||
class ProductEntity { | ||
|
||
readonly id!: number; | ||
readonly name!: string; | ||
|
||
constructor(props: { name: string; id?: number }) { | ||
Object.assign(this, props); | ||
} | ||
|
||
} | ||
|
||
class Company { | ||
|
||
readonly id!: number; | ||
readonly name!: string; | ||
readonly products = new Collection<ProductEntity>(this); | ||
|
||
constructor(props: { name: string; id?: number }) { | ||
Object.assign(this, props); | ||
} | ||
|
||
} | ||
|
||
class CompanyProduct { | ||
|
||
readonly id!: number; | ||
readonly product!: Ref<ProductEntity>; | ||
readonly company!: Ref<CompanyProduct>; | ||
readonly createdAt!: Date; | ||
readonly updatedAt!: Date; | ||
|
||
} | ||
|
||
const productSchema = new EntitySchema({ | ||
class: ProductEntity, | ||
tableName: 'product', | ||
properties: { | ||
id: { | ||
type: BigIntType, | ||
primary: true, | ||
}, | ||
name: { | ||
type: String, | ||
}, | ||
}, | ||
}); | ||
|
||
const companySchema = new EntitySchema({ | ||
class: Company, | ||
tableName: 'company', | ||
properties: { | ||
id: { | ||
type: BigIntType, | ||
primary: true, | ||
}, | ||
name: { | ||
type: String, | ||
}, | ||
products: { | ||
reference: 'm:n', | ||
entity: () => ProductEntity, | ||
pivotEntity: () => CompanyProduct, | ||
fixedOrder: true, | ||
}, | ||
}, | ||
}); | ||
|
||
const companyProductsSchema = new EntitySchema({ | ||
class: CompanyProduct, | ||
uniques: [ | ||
{ name: 'uniqueCompanyProduct', properties: ['company', 'product'] }, | ||
], | ||
properties: { | ||
id: { | ||
type: BigIntType, | ||
autoincrement: true, | ||
primary: true, | ||
}, | ||
product: { | ||
reference: 'm:1', | ||
entity: () => ProductEntity, | ||
}, | ||
company: { | ||
reference: 'm:1', | ||
entity: () => Company, | ||
}, | ||
createdAt: { | ||
type: 'timestamp', | ||
onCreate: () => new Date(), | ||
defaultRaw: 'current_timestamp', | ||
}, | ||
updatedAt: { | ||
type: 'timestamp', | ||
onUpdate: () => new Date(), | ||
defaultRaw: 'current_timestamp', | ||
}, | ||
}, | ||
}); | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
schema: 'test', | ||
dbName: '4988', | ||
entities: [companySchema, productSchema, companyProductsSchema], | ||
}); | ||
|
||
await orm.getSchemaGenerator().refreshDatabase(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(); | ||
}); | ||
|
||
test('creates a company with products', async () => { | ||
const company = new Company({ | ||
name: 'Acme', | ||
}); | ||
await orm.em.persistAndFlush(company); | ||
|
||
const product = new ProductEntity({ name: 'ProductA' }); | ||
company.products.add(product); | ||
await orm.em.flush(); | ||
|
||
expect(company.products).toHaveLength(1); | ||
}); |