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,5 +1,7 @@
{
"namespaces": ["public"],
"namespaces": [
"public"
],
"name": "public",
"tables": [
{
@@ -31,36 +33,81 @@
"nullable": false,
"default": "'{}'",
"mappedType": "json"
},
"created_at": {
"name": "created_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"updated_at": {
"name": "updated_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"deleted_at": {
"name": "deleted_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 6,
"mappedType": "datetime"
}
},
"name": "index_data",
"schema": "public",
"indexes": [
{
"columnNames": ["id"],
"composite": false,
"keyName": "IDX_index_data_id",
"primary": false,
"unique": false
},
{
"columnNames": ["name"],
"composite": false,
"keyName": "IDX_index_data_name",
"primary": false,
"unique": false
},
{
"keyName": "IDX_index_data_gin",
"columnNames": ["data"],
"keyName": "IDX_index_data_deleted_at",
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
"type": "GIN"
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_index_data_deleted_at\" ON \"index_data\" (deleted_at) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_index_data_gin",
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_index_data_gin\" ON \"index_data\" USING GIN (data) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_index_data_id",
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_index_data_id\" ON \"index_data\" (id) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_index_data_name",
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_index_data_name\" ON \"index_data\" (name) WHERE deleted_at IS NULL"
},
{
"keyName": "index_data_pkey",
"columnNames": ["id", "name"],
"columnNames": [
"id",
"name"
],
"composite": true,
"primary": true,
"unique": true
@@ -73,12 +120,12 @@
"columns": {
"id": {
"name": "id",
"type": "text",
"type": "number",
"unsigned": true,
"autoincrement": true,
"primary": false,
"primary": true,
"nullable": false,
"mappedType": "text"
"mappedType": "integer"
},
"pivot": {
"name": "pivot",
@@ -89,15 +136,6 @@
"nullable": false,
"mappedType": "text"
},
"parent_id": {
"name": "parent_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"parent_name": {
"name": "parent_name",
"type": "text",
@@ -107,6 +145,24 @@
"nullable": false,
"mappedType": "text"
},
"child_name": {
"name": "child_name",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"parent_id": {
"name": "parent_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"child_id": {
"name": "child_id",
"type": "text",
@@ -116,30 +172,72 @@
"nullable": false,
"mappedType": "text"
},
"child_name": {
"name": "child_name",
"type": "text",
"created_at": {
"name": "created_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"updated_at": {
"name": "updated_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"deleted_at": {
"name": "deleted_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 6,
"mappedType": "datetime"
}
},
"name": "index_relation",
"schema": "public",
"indexes": [
{
"keyName": "IDX_index_relation_child_id",
"columnNames": ["child_id"],
"keyName": "IDX_index_relation_parent_id",
"columnNames": [],
"composite": false,
"primary": false,
"unique": false
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_index_relation_parent_id\" ON \"index_relation\" (parent_id) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_index_relation_child_id",
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_index_relation_child_id\" ON \"index_relation\" (child_id) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_index_relation_deleted_at",
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_index_relation_deleted_at\" ON \"index_relation\" (deleted_at) WHERE deleted_at IS NULL"
},
{
"keyName": "index_relation_pkey",
"columnNames": ["id", "pivot"],
"composite": true,
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
}

View File

@@ -0,0 +1,35 @@
import { Migration } from "@mikro-orm/migrations"
export class Migration20241209173313 extends Migration {
async up(): Promise<void> {
this.addSql(`
ALTER TABLE "index_data"
ADD COLUMN IF NOT EXISTS "created_at" timestamptz NOT NULL DEFAULT now(),
ADD COLUMN IF NOT EXISTS "updated_at" timestamptz NOT NULL DEFAULT now(),
ADD COLUMN IF NOT EXISTS "deleted_at" timestamptz NULL;
`)
this.addSql(`
ALTER TABLE "index_relation"
ADD COLUMN IF NOT EXISTS "created_at" timestamptz NOT NULL DEFAULT now(),
ADD COLUMN IF NOT EXISTS "updated_at" timestamptz NOT NULL DEFAULT now(),
ADD COLUMN IF NOT EXISTS "deleted_at" timestamptz NULL;
`)
}
async down(): Promise<void> {
this.addSql(`
ALTER TABLE "index_data"
DROP COLUMN IF EXISTS "created_at",
DROP COLUMN IF EXISTS "updated_at",
DROP COLUMN IF EXISTS "deleted_at";
`)
this.addSql(`
ALTER TABLE "index_relation"
DROP COLUMN IF EXISTS "created_at",
DROP COLUMN IF EXISTS "updated_at",
DROP COLUMN IF EXISTS "deleted_at";
`)
}
}

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"

View File

@@ -12,6 +12,7 @@ import {
InjectTransactionManager,
isDefined,
MedusaContext,
toMikroORMEntity,
} from "@medusajs/framework/utils"
import { EntityManager, SqlEntityManager } from "@mikro-orm/postgresql"
import { IndexData, IndexRelation } from "@models"
@@ -338,8 +339,10 @@ export class PostgresProvider implements IndexTypes.StorageProvider {
const { transactionManager: em } = sharedContext as {
transactionManager: SqlEntityManager
}
const indexRepository = em.getRepository(IndexData)
const indexRelationRepository = em.getRepository(IndexRelation)
const indexRepository = em.getRepository(toMikroORMEntity(IndexData))
const indexRelationRepository = em.getRepository(
toMikroORMEntity(IndexRelation)
)
const {
data: data_,
@@ -432,7 +435,7 @@ export class PostgresProvider implements IndexTypes.StorageProvider {
const { transactionManager: em } = sharedContext as {
transactionManager: SqlEntityManager
}
const indexRepository = em.getRepository(IndexData)
const indexRepository = em.getRepository(toMikroORMEntity(IndexData))
const { data: data_, entityProperties } = PostgresProvider.parseData(
data,
@@ -479,8 +482,10 @@ export class PostgresProvider implements IndexTypes.StorageProvider {
const { transactionManager: em } = sharedContext as {
transactionManager: SqlEntityManager
}
const indexRepository = em.getRepository(IndexData)
const indexRelationRepository = em.getRepository(IndexRelation)
const indexRepository = em.getRepository(toMikroORMEntity(IndexData))
const indexRelationRepository = em.getRepository(
toMikroORMEntity(IndexRelation)
)
const { data: data_ } = PostgresProvider.parseData(
data,
@@ -533,8 +538,10 @@ export class PostgresProvider implements IndexTypes.StorageProvider {
const { transactionManager: em } = sharedContext as {
transactionManager: SqlEntityManager
}
const indexRepository = em.getRepository(IndexData)
const indexRelationRepository = em.getRepository(IndexRelation)
const indexRepository = em.getRepository(toMikroORMEntity(IndexData))
const indexRelationRepository = em.getRepository(
toMikroORMEntity(IndexRelation)
)
const { data: data_, entityProperties } = PostgresProvider.parseData(
data,
@@ -654,8 +661,10 @@ export class PostgresProvider implements IndexTypes.StorageProvider {
const { transactionManager: em } = sharedContext as {
transactionManager: SqlEntityManager
}
const indexRepository = em.getRepository(IndexData)
const indexRelationRepository = em.getRepository(IndexRelation)
const indexRepository = em.getRepository(toMikroORMEntity(IndexData))
const indexRelationRepository = em.getRepository(
toMikroORMEntity(IndexRelation)
)
const { data: data_ } = PostgresProvider.parseData(
data,