fix(utils): always apply default indexes (#7858)
what: - fixes default indexes not being applied always
This commit is contained in:
1 parent
91faa5ffdb
commit
91098877c2
3 files changed
+45
-4
No files matched your search
@@ -2746,6 +2746,49 @@ describe("Entity builder", () => {
|
|||||||
`Cannot apply indexes on fields (doesnotexist, anotherdoesnotexist) for model User`
|
`Cannot apply indexes on fields (doesnotexist, anotherdoesnotexist) for model User`
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test("should define indexes for an entity", () => {
|
||||||
|
const group = model.define("group", {
|
||||||
|
id: model.number(),
|
||||||
|
name: model.text(),
|
||||||
|
users: model.hasMany(() => user),
|
||||||
|
})
|
||||||
|
|
||||||
|
const setting = model.define("setting", {
|
||||||
|
name: model.text(),
|
||||||
|
user: model.belongsTo(() => user),
|
||||||
|
})
|
||||||
|
|
||||||
|
const user = model.define("user", {
|
||||||
|
email: model.text(),
|
||||||
|
account: model.text(),
|
||||||
|
organization: model.text(),
|
||||||
|
group: model.belongsTo(() => group, { mappedBy: "users" }),
|
||||||
|
setting: model.hasOne(() => setting),
|
||||||
|
})
|
||||||
|
|
||||||
|
const User = toMikroORMEntity(user)
|
||||||
|
const metaData = MetadataStorage.getMetadataFromDecorator(User)
|
||||||
|
|
||||||
|
expect(metaData.indexes).toEqual([
|
||||||
|
{
|
||||||
|
expression:
|
||||||
|
'CREATE INDEX IF NOT EXISTS "IDX_user_group_id" ON "user" (group_id) WHERE deleted_at IS NULL',
|
||||||
|
name: "IDX_user_group_id",
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
const Setting = toMikroORMEntity(setting)
|
||||||
|
const settingMetadata = MetadataStorage.getMetadataFromDecorator(Setting)
|
||||||
|
|
||||||
|
expect(settingMetadata.indexes).toEqual([
|
||||||
|
{
|
||||||
|
expression:
|
||||||
|
'CREATE INDEX IF NOT EXISTS "IDX_setting_user_id" ON "setting" (user_id) WHERE deleted_at IS NULL',
|
||||||
|
name: "IDX_setting_user_id",
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("Entity builder | hasMany", () => {
|
describe("Entity builder | hasMany", () => {
|
||||||
|
|||||||
@@ -69,9 +69,7 @@ export function createMikrORMEntity() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if (entityIndexes?.length) {
|
applyEntityIndexes(MikroORMEntity, tableName, entityIndexes)
|
||||||
applyEntityIndexes(MikroORMEntity, tableName, entityIndexes)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converting class to a MikroORM entity
|
* Converting class to a MikroORM entity
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ export function applyIndexes(
|
|||||||
export function applyEntityIndexes(
|
export function applyEntityIndexes(
|
||||||
MikroORMEntity: EntityConstructor<any>,
|
MikroORMEntity: EntityConstructor<any>,
|
||||||
tableName: string,
|
tableName: string,
|
||||||
entityIndexes: EntityIndex[]
|
entityIndexes: EntityIndex[] = []
|
||||||
) {
|
) {
|
||||||
const foreignKeyIndexes = applyForeignKeyIndexes(MikroORMEntity)
|
const foreignKeyIndexes = applyForeignKeyIndexes(MikroORMEntity)
|
||||||
const indexes = [...entityIndexes, ...foreignKeyIndexes]
|
const indexes = [...entityIndexes, ...foreignKeyIndexes]
|
||||||
|
|||||||
Reference in new issue
Block a user