Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(index): index dml #10482

Merged
merged 9 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/yellow-penguins-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@medusajs/index": patch
"@medusajs/types": patch
"@medusajs/utils": patch
---

chore: index module to DML
6 changes: 6 additions & 0 deletions packages/core/types/src/dml/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type KnownDataTypes =
| "enum"
| "number"
| "bigNumber"
| "serial"
| "dateTime"
| "array"
| "json"
Expand Down Expand Up @@ -310,6 +311,11 @@ export type EntityIndex<
* Conditions to restrict which records are indexed.
*/
where?: Where

/**
* The type of the index. (e.g: GIN)
*/
type?: string
}

export type SimpleQueryValue = string | number | boolean | null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expectTypeOf } from "expect-type"
import { AutoIncrementProperty } from "../properties/autoincrement"

describe("Autoincrement property", () => {
test("create autoincrement property type", () => {
const property = new AutoIncrementProperty()

expectTypeOf(property["$dataType"]).toEqualTypeOf<number>()
expect(property.parse("display_id")).toEqual({
fieldName: "display_id",
dataType: {
name: "serial",
options: {},
},
nullable: false,
indexes: [],
relationships: [],
})
})
})
33 changes: 18 additions & 15 deletions packages/core/utils/src/dml/__tests__/entity-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3736,6 +3736,7 @@ describe("Entity builder", () => {
},
group: {
entity: "Group",
fieldName: "group_id",
name: "group",
nullable: false,
persist: false,
Expand Down Expand Up @@ -4355,6 +4356,7 @@ describe("Entity builder", () => {
},
user: {
entity: "User",
fieldName: "user_id",
name: "user",
nullable: false,
persist: false,
Expand All @@ -4363,8 +4365,8 @@ describe("Entity builder", () => {
user_id: {
columnType: "text",
entity: "User",
fieldName: "user_id",
mapToPk: true,
fieldName: "user_id",
name: "user_id",
nullable: false,
onDelete: "cascade",
Expand Down Expand Up @@ -4949,6 +4951,7 @@ describe("Entity builder", () => {
name: "user",
reference: "m:1",
entity: "User",
fieldName: "user_id",
persist: false,
nullable: false,
},
Expand Down Expand Up @@ -5136,6 +5139,7 @@ describe("Entity builder", () => {
user: {
name: "user",
reference: "m:1",
fieldName: "user_id",
entity: "User",
persist: false,
nullable: true,
Expand Down Expand Up @@ -5697,6 +5701,7 @@ describe("Entity builder", () => {
},
parent: {
name: "parent",
fieldName: "parent_id",
reference: "m:1",
entity: "User",
persist: false,
Expand Down Expand Up @@ -7447,39 +7452,37 @@ describe("Entity builder", () => {
},
user_id: {
name: "user_id",
reference: "m:1",
entity: "User",
reference: "scalar",
columnType: "text",
mapToPk: true,
fieldName: "user_id",
getter: false,
setter: false,
nullable: false,
type: "User",
},
user: {
reference: "scalar",
type: "User",
reference: "m:1",
entity: "User",
persist: false,
nullable: false,
name: "user",
getter: false,
setter: false,
},
team_id: {
name: "team_id",
reference: "m:1",
entity: "Team",
reference: "scalar",
columnType: "text",
mapToPk: true,
fieldName: "team_id",
nullable: false,
getter: false,
setter: false,
type: "Team",
},
team: {
reference: "scalar",
type: "Team",
reference: "m:1",
entity: "Team",
persist: false,
nullable: false,
name: "team",
getter: false,
setter: false,
},
created_at: {
reference: "scalar",
Expand Down
35 changes: 32 additions & 3 deletions packages/core/utils/src/dml/entity-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
DMLSchemaDefaults,
} from "./helpers/entity-builder/create-default-properties"
import { ArrayProperty } from "./properties/array"
import { AutoIncrementProperty } from "./properties/autoincrement"
import { BigNumberProperty } from "./properties/big-number"
import { BooleanProperty } from "./properties/boolean"
import { DateTimeProperty } from "./properties/date-time"
Expand All @@ -24,8 +25,8 @@ import { TextProperty } from "./properties/text"
import { BelongsTo } from "./relations/belongs-to"
import { HasMany } from "./relations/has-many"
import { HasOne } from "./relations/has-one"
import { ManyToMany } from "./relations/many-to-many"
import { HasOneWithForeignKey } from "./relations/has-one-fk"
import { ManyToMany } from "./relations/many-to-many"

/**
* The implicit properties added by EntityBuilder in every schema
Expand Down Expand Up @@ -60,11 +61,11 @@ export type ManyToManyOptions = RelationshipOptions &
/**
* The column name in the pivot table that for the current entity
*/
joinColumn?: string
joinColumn?: string | string[]
/**
* The column name in the pivot table for the opposite entity
*/
inverseJoinColumn?: string
inverseJoinColumn?: string | string[]
}
| {
/**
Expand All @@ -77,6 +78,14 @@ export type ManyToManyOptions = RelationshipOptions &
* database for this relationship.
*/
pivotEntity?: () => DmlEntity<any, any>
/**
* The column name in the pivot table that for the current entity
*/
joinColumn?: string | string[]
/**
* The column name in the pivot table for the opposite entity
*/
inverseJoinColumn?: string | string[]
}
)

Expand Down Expand Up @@ -240,6 +249,26 @@ export class EntityBuilder {
return new BigNumberProperty()
}

/**
* This method defines an autoincrement property.
*
* @example
* import { model } from "@medusajs/framework/utils"
*
* const MyCustom = model.define("my_custom", {
* serial_id: model.autoincrement(),
* // ...
* })
*
* export default MyCustom
*
* @customNamespace Property
*/

autoincrement() {
return new AutoIncrementProperty()
}

/**
* This method defines an array of strings property.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {
PropertyMetadata,
PropertyType,
} from "@medusajs/types"
import { MikroOrmBigNumberProperty } from "../../../dal"
import { generateEntityId, isDefined } from "../../../common"
import {
ArrayType,
BeforeCreate,
Expand All @@ -15,6 +13,8 @@ import {
Property,
Utils,
} from "@mikro-orm/core"
import { generateEntityId, isDefined } from "../../../common"
import { MikroOrmBigNumberProperty } from "../../../dal"
import { PrimaryKeyModifier } from "../../properties/primary-key"
import { applyEntityIndexes } from "../mikro-orm/apply-indexes"

Expand All @@ -32,6 +32,7 @@ const COLUMN_TYPES: {
dateTime: "timestamptz",
number: "integer",
bigNumber: "numeric",
serial: "number",
text: "text",
json: "jsonb",
array: "array",
Expand All @@ -51,6 +52,7 @@ const PROPERTY_TYPES: {
dateTime: "date",
number: "number",
bigNumber: "number",
serial: "number",
text: "string",
json: "any",
array: "string[]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import { camelToSnakeCase, pluralize } from "../../../common"
import { DmlEntity } from "../../entity"
import { HasMany } from "../../relations/has-many"
import { HasOne } from "../../relations/has-one"
import { HasOneWithForeignKey } from "../../relations/has-one-fk"
import { ManyToMany as DmlManyToMany } from "../../relations/many-to-many"
import { applyEntityIndexes } from "../mikro-orm/apply-indexes"
import { parseEntityName } from "./parse-entity-name"
import { HasOneWithForeignKey } from "../../relations/has-one-fk"

type Context = {
MANY_TO_MANY_TRACKED_RELATIONS: Record<string, boolean>
Expand Down Expand Up @@ -329,25 +329,32 @@ export function defineBelongsToRelationship(
) {
const foreignKeyName = camelToSnakeCase(`${relationship.name}Id`)

ManyToOne({
entity: relatedModelName,
columnType: "text",
mapToPk: true,
fieldName: foreignKeyName,
nullable: relationship.nullable,
onDelete: shouldCascade ? "cascade" : undefined,
})(MikroORMEntity.prototype, foreignKeyName)

if (DmlManyToMany.isManyToMany(otherSideRelation)) {
Property({
type: relatedModelName,
persist: false,
columnType: "text",
fieldName: foreignKeyName,
nullable: relationship.nullable,
})(MikroORMEntity.prototype, foreignKeyName)

ManyToOne({
entity: relatedModelName,
nullable: relationship.nullable,
persist: false,
})(MikroORMEntity.prototype, relationship.name)
} else {
// HasMany case
ManyToOne({
entity: relatedModelName,
columnType: "text",
mapToPk: true,
fieldName: foreignKeyName,
nullable: relationship.nullable,
onDelete: shouldCascade ? "cascade" : undefined,
})(MikroORMEntity.prototype, foreignKeyName)

ManyToOne({
entity: relatedModelName,
fieldName: foreignKeyName,
persist: false,
nullable: relationship.nullable,
})(MikroORMEntity.prototype, relationship.name)
Expand Down Expand Up @@ -446,9 +453,30 @@ export function defineManyToManyRelationship(
let inversedBy: undefined | string
let pivotEntityName: undefined | string
let pivotTableName: undefined | string
let joinColumn: undefined | string = relationship.options.joinColumn
let inverseJoinColumn: undefined | string =

const joinColumn: undefined | string = !Array.isArray(
relationship.options.joinColumn
)
? relationship.options.joinColumn
: undefined

const joinColumns: undefined | string[] = Array.isArray(
relationship.options.joinColumn
)
? relationship.options.joinColumn
: undefined

const inverseJoinColumn: undefined | string = !Array.isArray(
relationship.options.inverseJoinColumn
)
? relationship.options.inverseJoinColumn
: undefined

const inverseJoinColumns: undefined | string[] = Array.isArray(
relationship.options.inverseJoinColumn
)
? relationship.options.inverseJoinColumn
: undefined

const [otherSideRelationshipProperty, otherSideRelationship] =
retrieveOtherSideRelationshipManyToMany({
Expand Down Expand Up @@ -540,7 +568,9 @@ export function defineManyToManyRelationship(

const configuresRelationship = !!(
joinColumn ||
joinColumns ||
inverseJoinColumn ||
inverseJoinColumns ||
relationship.options.pivotTable
)
const relatedOneConfiguresRelationship = !!(
Expand Down Expand Up @@ -586,7 +616,16 @@ export function defineManyToManyRelationship(
const mappedByPropValue =
mappedBy ?? inversedBy ?? otherSideRelationshipProperty

ManyToMany({
const joinColumnProp = Array.isArray(relationship.options.joinColumn)
? "joinColumns"
: "joinColumn"
const inverseJoinColumnProp = Array.isArray(
relationship.options.inverseJoinColumn
)
? "inverseJoinColumns"
: "inverseJoinColumn"

const manytoManyOptions = {
owner: isOwner,
entity: relatedModelName,
...(pivotTableName
Expand All @@ -598,9 +637,11 @@ export function defineManyToManyRelationship(
: {}),
...(pivotEntityName ? { pivotEntity: pivotEntityName } : {}),
...({ [mappedByProp]: mappedByPropValue } as any),
...(joinColumn ? { joinColumn } : {}),
...(inverseJoinColumn ? { inverseJoinColumn } : {}),
})(MikroORMEntity.prototype, relationship.name)
[joinColumnProp]: joinColumn ?? joinColumns,
[inverseJoinColumnProp]: inverseJoinColumn ?? inverseJoinColumns,
}

ManyToMany(manytoManyOptions)(MikroORMEntity.prototype, relationship.name)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function applyEntityIndexes(
columns: index.on as string[],
unique: index.unique,
where: index.where,
type: index.type,
})

entityIndexStatement.MikroORMIndex()(MikroORMEntity)
Expand Down
Loading
Loading