feat(index): Index module foundation (#9095)
**What** Index module foundation Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>
This commit is contained in:
co-authored by
Carlos R. L. Rodrigues
parent
3cfcd075ae
commit
58167b5dfa
@@ -0,0 +1,46 @@
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import {
|
||||
SchemaObjectRepresentation,
|
||||
schemaObjectRepresentationPropertiesToOmit,
|
||||
} from "../types"
|
||||
|
||||
export async function createPartitions(
|
||||
schemaObjectRepresentation: SchemaObjectRepresentation,
|
||||
manager: SqlEntityManager
|
||||
): Promise<void> {
|
||||
const activeSchema = manager.config.get("schema")
|
||||
? `"${manager.config.get("schema")}".`
|
||||
: ""
|
||||
const partitions = Object.keys(schemaObjectRepresentation)
|
||||
.filter(
|
||||
(key) =>
|
||||
!schemaObjectRepresentationPropertiesToOmit.includes(key) &&
|
||||
schemaObjectRepresentation[key].listeners.length > 0
|
||||
)
|
||||
.map((key) => {
|
||||
const cName = key.toLowerCase()
|
||||
const part: string[] = []
|
||||
part.push(
|
||||
`CREATE TABLE IF NOT EXISTS ${activeSchema}cat_${cName} PARTITION OF ${activeSchema}index_data FOR VALUES IN ('${key}')`
|
||||
)
|
||||
|
||||
for (const parent of schemaObjectRepresentation[key].parents) {
|
||||
const pKey = `${parent.ref.entity}-${key}`
|
||||
const pName = `${parent.ref.entity}${key}`.toLowerCase()
|
||||
part.push(
|
||||
`CREATE TABLE IF NOT EXISTS ${activeSchema}cat_pivot_${pName} PARTITION OF ${activeSchema}index_relation FOR VALUES IN ('${pKey}')`
|
||||
)
|
||||
}
|
||||
return part
|
||||
})
|
||||
.flat()
|
||||
|
||||
if (!partitions.length) {
|
||||
return
|
||||
}
|
||||
|
||||
partitions.push(`analyse ${activeSchema}index_data`)
|
||||
partitions.push(`analyse ${activeSchema}index_relation`)
|
||||
|
||||
await manager.execute(partitions.join("; "))
|
||||
}
|
||||
Reference in New Issue
Block a user