feature: add support for check constraints in DML (#10391)
This commit is contained in:
@@ -6890,4 +6890,104 @@ describe("Entity builder", () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("Entity builder | checks", () => {
|
||||
test("should define checks for an entity", () => {
|
||||
const group = model
|
||||
.define("group", {
|
||||
id: model.number(),
|
||||
name: model.text(),
|
||||
})
|
||||
.checks([
|
||||
(columns) => {
|
||||
expectTypeOf(columns).toEqualTypeOf<{
|
||||
id: string
|
||||
name: string
|
||||
created_at: string
|
||||
updated_at: string
|
||||
deleted_at: string
|
||||
}>()
|
||||
return `${columns.id} > 1`
|
||||
},
|
||||
])
|
||||
|
||||
const Group = toMikroORMEntity(group)
|
||||
const metaData = MetadataStorage.getMetadataFromDecorator(Group)
|
||||
|
||||
expect(metaData.checks).toHaveLength(1)
|
||||
expect(metaData.checks[0].expression.toString()).toMatchInlineSnapshot(`
|
||||
"(columns)=>{
|
||||
(0, _expecttype.expectTypeOf)(columns).toEqualTypeOf();
|
||||
return \`\${columns.id} > 1\`;
|
||||
}"
|
||||
`)
|
||||
})
|
||||
|
||||
test("should define checks as an object", () => {
|
||||
const group = model
|
||||
.define("group", {
|
||||
id: model.number(),
|
||||
name: model.text(),
|
||||
})
|
||||
.checks([
|
||||
{
|
||||
name: "my_custom_check",
|
||||
expression: (columns) => {
|
||||
expectTypeOf(columns).toEqualTypeOf<{
|
||||
id: string
|
||||
name: string
|
||||
created_at: string
|
||||
updated_at: string
|
||||
deleted_at: string
|
||||
}>()
|
||||
return `${columns.id} > 1`
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
const Group = toMikroORMEntity(group)
|
||||
const metaData = MetadataStorage.getMetadataFromDecorator(Group)
|
||||
|
||||
expect(metaData.checks).toHaveLength(1)
|
||||
expect(metaData.checks[0].name).toEqual("my_custom_check")
|
||||
expect(metaData.checks[0].expression.toString()).toMatchInlineSnapshot(`
|
||||
"(columns)=>{
|
||||
(0, _expecttype.expectTypeOf)(columns).toEqualTypeOf();
|
||||
return \`\${columns.id} > 1\`;
|
||||
}"
|
||||
`)
|
||||
})
|
||||
|
||||
test("should infer foreign keys inside the checks callback", () => {
|
||||
const group = model
|
||||
.define("group", {
|
||||
id: model.number(),
|
||||
name: model.text(),
|
||||
parent_group: model.belongsTo(() => group, {
|
||||
mappedBy: "groups",
|
||||
}),
|
||||
groups: model.hasMany(() => group, {
|
||||
mappedBy: "parent_group",
|
||||
}),
|
||||
})
|
||||
.checks([
|
||||
(columns) => {
|
||||
expectTypeOf(columns).toEqualTypeOf<{
|
||||
id: string
|
||||
name: string
|
||||
parent_group_id: string
|
||||
created_at: string
|
||||
updated_at: string
|
||||
deleted_at: string
|
||||
}>()
|
||||
return `${columns.id} > 1`
|
||||
},
|
||||
])
|
||||
|
||||
const Group = toMikroORMEntity(group)
|
||||
const metaData = MetadataStorage.getMetadataFromDecorator(Group)
|
||||
|
||||
expect(metaData.checks).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import {
|
||||
DMLSchema,
|
||||
EntityCascades,
|
||||
EntityIndex,
|
||||
ExtractEntityRelations,
|
||||
IDmlEntity,
|
||||
IDmlEntityConfig,
|
||||
InferDmlEntityNameFromConfig,
|
||||
DMLSchema,
|
||||
EntityIndex,
|
||||
CheckConstraint,
|
||||
EntityCascades,
|
||||
QueryCondition,
|
||||
IDmlEntityConfig,
|
||||
ExtractEntityRelations,
|
||||
InferDmlEntityNameFromConfig,
|
||||
} from "@medusajs/types"
|
||||
import { isObject, isString, toCamelCase, upperCaseFirst } from "../common"
|
||||
import { transformIndexWhere } from "./helpers/entity-builder/build-indexes"
|
||||
@@ -72,6 +73,7 @@ export class DmlEntity<
|
||||
readonly #tableName: string
|
||||
#cascades: EntityCascades<string[]> = {}
|
||||
#indexes: EntityIndex<Schema>[] = []
|
||||
#checks: CheckConstraint<Schema>[] = []
|
||||
|
||||
constructor(nameOrConfig: TConfig, schema: Schema) {
|
||||
const { name, tableName } = extractNameAndTableName(nameOrConfig)
|
||||
@@ -100,6 +102,7 @@ export class DmlEntity<
|
||||
schema: DMLSchema
|
||||
cascades: EntityCascades<string[]>
|
||||
indexes: EntityIndex<Schema>[]
|
||||
checks: CheckConstraint<Schema>[]
|
||||
} {
|
||||
return {
|
||||
name: this.name,
|
||||
@@ -107,6 +110,7 @@ export class DmlEntity<
|
||||
schema: this.schema,
|
||||
cascades: this.#cascades,
|
||||
indexes: this.#indexes,
|
||||
checks: this.#checks,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -238,4 +242,11 @@ export class DmlEntity<
|
||||
this.#indexes = indexes as EntityIndex<Schema>[]
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
checks(checks: CheckConstraint<Schema>[]) {
|
||||
this.#checks = checks
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,11 +11,12 @@ import { Entity, Filter } from "@mikro-orm/core"
|
||||
import { DmlEntity } from "../entity"
|
||||
import { IdProperty } from "../properties/id"
|
||||
import { DuplicateIdPropertyError } from "../errors"
|
||||
import { applyChecks } from "./mikro-orm/apply-checks"
|
||||
import { mikroOrmSoftDeletableFilterOptions } from "../../dal"
|
||||
import { applySearchable } from "./entity-builder/apply-searchable"
|
||||
import { defineProperty } from "./entity-builder/define-property"
|
||||
import { defineRelationship } from "./entity-builder/define-relationship"
|
||||
import { applySearchable } from "./entity-builder/apply-searchable"
|
||||
import { parseEntityName } from "./entity-builder/parse-entity-name"
|
||||
import { defineRelationship } from "./entity-builder/define-relationship"
|
||||
import { applyEntityIndexes, applyIndexes } from "./mikro-orm/apply-indexes"
|
||||
|
||||
/**
|
||||
@@ -47,7 +48,7 @@ function createMikrORMEntity() {
|
||||
function createEntity<T extends DmlEntity<any, any>>(entity: T): Infer<T> {
|
||||
class MikroORMEntity {}
|
||||
|
||||
const { schema, cascades, indexes: entityIndexes = [] } = entity.parse()
|
||||
const { schema, cascades, indexes: entityIndexes, checks } = entity.parse()
|
||||
const { modelName, tableName } = parseEntityName(entity)
|
||||
if (ENTITIES[modelName]) {
|
||||
return ENTITIES[modelName] as Infer<T>
|
||||
@@ -96,6 +97,7 @@ function createMikrORMEntity() {
|
||||
})
|
||||
|
||||
applyEntityIndexes(MikroORMEntity, tableName, entityIndexes)
|
||||
applyChecks(MikroORMEntity, checks)
|
||||
|
||||
/**
|
||||
* Converting class to a MikroORM entity
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import { Check, CheckOptions } from "@mikro-orm/core"
|
||||
import { CheckConstraint, EntityConstructor } from "@medusajs/types"
|
||||
|
||||
/**
|
||||
* Defines PostgreSQL constraints using the MikrORM's "@Check"
|
||||
* decorator
|
||||
*/
|
||||
export function applyChecks(
|
||||
MikroORMEntity: EntityConstructor<any>,
|
||||
entityChecks: CheckConstraint<any>[] = []
|
||||
) {
|
||||
entityChecks.forEach((check) => {
|
||||
Check(
|
||||
typeof check === "function"
|
||||
? {
|
||||
expression: check as CheckOptions["expression"],
|
||||
}
|
||||
: (check as CheckOptions)
|
||||
)(MikroORMEntity)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user