chore(index): index dml (#10482)

What:
- DML - `autoincrement` property
- Index `type` option (GIN)
- Index module - DML
This commit is contained in:
Carlos R. L. Rodrigues
2024-12-10 07:55:12 -03:00
committed by GitHub
parent 096f1c2a9b
commit 69f4c4f4e0
17 changed files with 482 additions and 215 deletions

View File

@@ -1,45 +1,35 @@
import {
Cascade,
Collection,
Entity,
Index,
ManyToMany,
OptionalProps,
PrimaryKey,
PrimaryKeyType,
Property,
} from "@mikro-orm/core"
import { IndexRelation } from "./index-relation"
import { model } from "@medusajs/framework/utils"
import IndexRelation from "./index-relation"
type OptionalRelations = "parents"
@Entity({
tableName: "index_data",
})
export class IndexData {
[OptionalProps]: OptionalRelations
@PrimaryKey({ columnType: "text" })
@Index({ name: "IDX_index_data_id" })
id!: string
@PrimaryKey({ columnType: "text" })
@Index({ name: "IDX_index_data_name" })
name: string;
[PrimaryKeyType]?: [string, string]
@Index({ name: "IDX_index_data_gin", type: "GIN" })
@Property({ columnType: "jsonb", default: "{}" })
data: Record<string, unknown>
@ManyToMany({
owner: true,
entity: () => IndexData,
pivotEntity: () => IndexRelation,
cascade: [Cascade.REMOVE],
inverseJoinColumns: ["parent_id", "parent_name"],
joinColumns: ["child_id", "child_name"],
const IndexData = model
.define("IndexData", {
id: model.text().primaryKey(),
name: model.text().primaryKey(),
data: model.json().default({}),
parents: model.manyToMany(() => IndexData, {
mappedBy: "children",
pivotEntity: () => IndexRelation,
joinColumn: ["child_id", "child_name"],
inverseJoinColumn: ["parent_id", "parent_name"],
}),
children: model.manyToMany(() => IndexData, {
mappedBy: "parents",
}),
})
parents = new Collection<IndexData>(this)
}
.indexes([
{
name: "IDX_index_data_gin",
type: "GIN",
on: ["data"],
},
{
name: "IDX_index_data_id",
on: ["id"],
},
{
name: "IDX_index_data_name",
on: ["name"],
},
])
export default IndexData

View File

@@ -1,64 +1,16 @@
import {
Entity,
Index,
ManyToOne,
OptionalProps,
PrimaryKey,
Property,
Ref,
} from "@mikro-orm/core"
import { IndexData } from "./index-data"
import { model } from "@medusajs/framework/utils"
import IndexData from "./index-data"
type OptionalRelations =
| "parent"
| "child"
| "parent_id"
| "child_id"
| "parent_name"
| "child_name"
@Entity({
tableName: "index_relation",
const IndexRelation = model.define("IndexRelation", {
id: model.autoincrement().primaryKey(),
pivot: model.text(),
parent_name: model.text(),
child_name: model.text(),
parent: model.belongsTo(() => IndexData, {
mappedBy: "parents",
}),
child: model.belongsTo(() => IndexData, {
mappedBy: "children",
}),
})
@Index({
name: "IDX_index_relation_child_id",
properties: ["child_id"],
})
export class IndexRelation {
[OptionalProps]: OptionalRelations
@PrimaryKey({ columnType: "integer", autoincrement: true })
id!: string
// if added as PK, BeforeCreate value isn't set
@Property({
columnType: "text",
})
pivot: string
@Property({ columnType: "text" })
parent_id?: string
@Property({ columnType: "text" })
parent_name?: string
@Property({ columnType: "text" })
child_id?: string
@Property({ columnType: "text" })
child_name?: string
@ManyToOne({
entity: () => IndexData,
onDelete: "cascade",
persist: false,
})
parent?: Ref<IndexData>
@ManyToOne({
entity: () => IndexData,
onDelete: "cascade",
persist: false,
})
child?: Ref<IndexData>
}
export default IndexRelation

View File

@@ -1,2 +1,2 @@
export { IndexData } from "./index-data"
export { IndexRelation } from "./index-relation"
export { default as IndexData } from "./index-data"
export { default as IndexRelation } from "./index-relation"