Skip to content

Commit

Permalink
chore(inventory): convert to dml (medusajs#10569)
Browse files Browse the repository at this point in the history
Fixes: FRMW-2848

Co-authored-by: Harminder Virk <[email protected]>
  • Loading branch information
carlos-r-l-rodrigues and thetutlage authored Dec 13, 2024
1 parent ae1d875 commit 729eb5d
Show file tree
Hide file tree
Showing 33 changed files with 644 additions and 575 deletions.
7 changes: 7 additions & 0 deletions .changeset/big-days-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@medusajs/inventory": patch
"@medusajs/types": patch
"@medusajs/utils": patch
---

chore: Inventory DML
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,7 @@ medusaIntegrationTestRunner({
{ title: "variant two", options: { color: "blue" } },
],
})
console.log(product)

const [variantOne, variantTwo] = product.variants

const [itemOne, itemTwo, itemThree] =
Expand Down
1 change: 1 addition & 0 deletions packages/core/types/src/dml/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export type PropertyMetadata = {
fieldName: string
defaultValue?: any
nullable: boolean
computed: boolean
dataType: {
name: KnownDataTypes
options?: Record<string, any>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe("Array property", () => {
name: "array",
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe("Autoincrement property", () => {
options: {},
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
Expand Down
41 changes: 40 additions & 1 deletion packages/core/utils/src/dml/__tests__/base-property.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PropertyMetadata } from "@medusajs/types"
import { expectTypeOf } from "expect-type"
import { BaseProperty } from "../properties/base"
import { PropertyMetadata } from "@medusajs/types"
import { TextProperty } from "../properties/text"

describe("Base property", () => {
Expand All @@ -20,6 +20,7 @@ describe("Base property", () => {
name: "text",
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
Expand All @@ -38,6 +39,7 @@ describe("Base property", () => {
},
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
Expand All @@ -59,6 +61,42 @@ describe("Base property", () => {
name: "text",
},
nullable: true,
computed: false,
indexes: [],
relationships: [],
})
})

test("apply computed property", () => {
class StringProperty extends BaseProperty<string> {
protected dataType: PropertyMetadata["dataType"] = {
name: "text",
}
}

const property = new StringProperty().computed()
const property2 = new StringProperty().nullable().computed()

expectTypeOf(property["$dataType"]).toEqualTypeOf<string | null>()
expect(property.parse("username")).toEqual({
fieldName: "username",
dataType: {
name: "text",
},
nullable: false,
computed: true,
indexes: [],
relationships: [],
})

expectTypeOf(property2["$dataType"]).toEqualTypeOf<string | null>()
expect(property2.parse("username")).toEqual({
fieldName: "username",
dataType: {
name: "text",
},
nullable: true,
computed: true,
indexes: [],
relationships: [],
})
Expand All @@ -81,6 +119,7 @@ describe("Base property", () => {
},
defaultValue: "foo",
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe("Big Number property", () => {
name: "bigNumber",
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe("Boolean property", () => {
name: "boolean",
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe("DateTime property", () => {
name: "dateTime",
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
Expand Down
4 changes: 4 additions & 0 deletions packages/core/utils/src/dml/__tests__/enum-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe("Enum property", () => {
},
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
Expand All @@ -42,6 +43,7 @@ describe("Enum property", () => {
},
},
nullable: true,
computed: false,
indexes: [],
relationships: [],
})
Expand All @@ -66,6 +68,7 @@ describe("Enum property", () => {
},
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
Expand All @@ -90,6 +93,7 @@ describe("Enum property", () => {
},
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe("Float property", () => {
name: "float",
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
Expand Down
2 changes: 2 additions & 0 deletions packages/core/utils/src/dml/__tests__/id-property.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe("Id property", () => {
options: {},
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
Expand All @@ -29,6 +30,7 @@ describe("Id property", () => {
options: {},
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
primaryKey: true,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/utils/src/dml/__tests__/json-property.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe("JSON property", () => {
name: "json",
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
Expand All @@ -30,6 +31,7 @@ describe("JSON property", () => {
a: 1,
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe("Number property", () => {
options: {},
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
Expand Down
2 changes: 2 additions & 0 deletions packages/core/utils/src/dml/__tests__/text-property.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe("Text property", () => {
options: { searchable: false },
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
Expand All @@ -29,6 +30,7 @@ describe("Text property", () => {
options: { searchable: false },
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
primaryKey: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ export function createBigNumberProperties<Schema extends DMLSchema>(
continue
}

const jsonProperty = parsed.nullable
let jsonProperty = parsed.nullable
? new JSONProperty().nullable()
: new JSONProperty()

if (parsed.computed) {
jsonProperty = jsonProperty.computed() as unknown as
| JSONProperty
| NullableModifier<Record<string, unknown>, JSONProperty>
}

schemaWithBigNumber[`raw_${key}`] = jsonProperty
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ export function defineProperty(
BeforeCreate()(MikroORMEntity.prototype, defaultValueSetterHookName)
}

if (field.computed) {
return
}

if (SPECIAL_PROPERTIES[field.fieldName]) {
SPECIAL_PROPERTIES[field.fieldName](MikroORMEntity, field, tableName)
return
Expand Down
23 changes: 23 additions & 0 deletions packages/core/utils/src/dml/properties/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PropertyMetadata, PropertyType } from "@medusajs/types"
import { ComputedProperty } from "./computed"
import { NullableModifier } from "./nullable"

/**
Expand Down Expand Up @@ -48,6 +49,27 @@ export abstract class BaseProperty<T> implements PropertyType<T> {
return new NullableModifier<T, this>(this)
}

/**
* This method indicated that the property is a computed property.
* Computed properties are not stored in the database but are
* computed on the fly.
*
* @example
* import { model } from "@medusajs/framework/utils"
*
* const MyCustom = model.define("my_custom", {
* calculated_price: model.bigNumber().computed(),
* // ...
* })
*
* export default MyCustom
*
* @customNamespace Property Configuration Methods
*/
computed() {
return new ComputedProperty<T | null, this>(this)
}

/**
* This method defines an index on a property.
*
Expand Down Expand Up @@ -132,6 +154,7 @@ export abstract class BaseProperty<T> implements PropertyType<T> {
fieldName,
dataType: this.dataType,
nullable: false,
computed: false,
defaultValue: this.#defaultValue,
indexes: this.#indexes,
relationships: this.#relationships,
Expand Down
39 changes: 39 additions & 0 deletions packages/core/utils/src/dml/properties/computed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { PropertyType } from "@medusajs/types"

const IsComputedProperty = Symbol.for("isComputedProperty")
/**
* Computed property marks a schema node as computed
*/
export class ComputedProperty<T, Schema extends PropertyType<T>>
implements PropertyType<T | null>
{
[IsComputedProperty]: true = true

static isComputedProperty(obj: any): obj is ComputedProperty<any, any> {
return !!obj?.[IsComputedProperty]
}
/**
* A type-only property to infer the JavScript data-type
* of the schema property
*/
declare $dataType: T | null

/**
* The parent schema on which the computed property is
* applied
*/
#schema: Schema

constructor(schema: Schema) {
this.#schema = schema
}

/**
* Returns the serialized metadata
*/
parse(fieldName: string) {
const schema = this.#schema.parse(fieldName)
schema.computed = true
return schema
}
}
3 changes: 2 additions & 1 deletion packages/core/utils/src/dml/properties/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ export * from "./autoincrement"
export * from "./base"
export * from "./big-number"
export * from "./boolean"
export * from "./computed"
export * from "./date-time"
export * from "./enum"
export * from "./float"
export * from "./id"
export * from "./json"
export * from "./nullable"
export * from "./number"
export * from "./float"
export * from "./primary-key"
export * from "./text"
8 changes: 8 additions & 0 deletions packages/core/utils/src/dml/properties/nullable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PropertyType } from "@medusajs/types"
import { ComputedProperty } from "./computed"

const IsNullableModifier = Symbol.for("isNullableModifier")
/**
Expand Down Expand Up @@ -28,6 +29,13 @@ export class NullableModifier<T, Schema extends PropertyType<T>>
this.#schema = schema
}

/**
* This method indicated that the property is a computed property.
*/
computed() {
return new ComputedProperty<T | null, this>(this)
}

/**
* Returns the serialized metadata
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { dropDatabase } from "pg-god"
import {
createClient,
parseConnectionString,
dbExists,
createDb,
dbExists,
parseConnectionString,
} from "../../index"

const DB_HOST = process.env.DB_HOST ?? "localhost"
Expand Down
Loading

0 comments on commit 729eb5d

Please sign in to comment.