chore(inventory): convert to dml (#10569)

Fixes: FRMW-2848

Co-authored-by: Harminder Virk <1706381+thetutlage@users.noreply.github.com>
This commit is contained in:
Carlos R. L. Rodrigues
2024-12-13 09:51:26 -03:00
committed by GitHub
parent ae1d875fcf
commit 729eb5da7b
33 changed files with 662 additions and 593 deletions
@@ -12,6 +12,7 @@ describe("Array property", () => {
name: "array",
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
@@ -13,6 +13,7 @@ describe("Autoincrement property", () => {
options: {},
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
@@ -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", () => {
@@ -20,6 +20,7 @@ describe("Base property", () => {
name: "text",
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
@@ -38,6 +39,7 @@ describe("Base property", () => {
},
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
@@ -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: [],
})
@@ -81,6 +119,7 @@ describe("Base property", () => {
},
defaultValue: "foo",
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
@@ -12,6 +12,7 @@ describe("Big Number property", () => {
name: "bigNumber",
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
@@ -12,6 +12,7 @@ describe("Boolean property", () => {
name: "boolean",
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
@@ -12,6 +12,7 @@ describe("DateTime property", () => {
name: "dateTime",
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
@@ -17,6 +17,7 @@ describe("Enum property", () => {
},
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
@@ -42,6 +43,7 @@ describe("Enum property", () => {
},
},
nullable: true,
computed: false,
indexes: [],
relationships: [],
})
@@ -66,6 +68,7 @@ describe("Enum property", () => {
},
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
@@ -90,6 +93,7 @@ describe("Enum property", () => {
},
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
@@ -12,6 +12,7 @@ describe("Float property", () => {
name: "float",
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
@@ -13,6 +13,7 @@ describe("Id property", () => {
options: {},
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
@@ -29,6 +30,7 @@ describe("Id property", () => {
options: {},
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
primaryKey: true,
@@ -12,6 +12,7 @@ describe("JSON property", () => {
name: "json",
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
@@ -30,6 +31,7 @@ describe("JSON property", () => {
a: 1,
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
@@ -13,6 +13,7 @@ describe("Number property", () => {
options: {},
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
@@ -13,6 +13,7 @@ describe("Text property", () => {
options: { searchable: false },
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
})
@@ -29,6 +30,7 @@ describe("Text property", () => {
options: { searchable: false },
},
nullable: false,
computed: false,
indexes: [],
relationships: [],
primaryKey: true,
@@ -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
}
}
@@ -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
@@ -1,4 +1,5 @@
import { PropertyMetadata, PropertyType } from "@medusajs/types"
import { ComputedProperty } from "./computed"
import { NullableModifier } from "./nullable"
/**
@@ -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.
*
@@ -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,
@@ -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,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"
@@ -1,4 +1,5 @@
import { PropertyType } from "@medusajs/types"
import { ComputedProperty } from "./computed"
const IsNullableModifier = Symbol.for("isNullableModifier")
/**
@@ -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
*/
@@ -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"