Skip to content

Commit

Permalink
Revert "feat: add optional fields" (#10159)
Browse files Browse the repository at this point in the history
Reverts #10150
  • Loading branch information
thetutlage authored Nov 19, 2024
1 parent 3f4e6a1 commit 9dff05c
Show file tree
Hide file tree
Showing 17 changed files with 18 additions and 373 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"@medusajs/utils": patch
---

feat: add optional fields
Revert "feat: add optional fields"
41 changes: 13 additions & 28 deletions packages/core/types/src/dml/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export type PropertyMetadata = {
fieldName: string
defaultValue?: any
nullable: boolean
optional: boolean
dataType: {
name: KnownDataTypes
options?: Record<string, any>
Expand Down Expand Up @@ -178,37 +177,23 @@ export type InferHasManyFields<Relation> = Relation extends () => IDmlEntity<
*/
export type InferManyToManyFields<Relation> = InferHasManyFields<Relation>

/**
* Only processed property that can be undefined and mark them as optional
*/
export type InferOptionalFields<Schema extends DMLSchema> = Prettify<{
[K in keyof Schema as undefined extends Schema[K]["$dataType"]
? K
: never]?: Schema[K]["$dataType"]
}>

/**
* Inferring the types of the schema fields from the DML
* entity
*/
export type InferSchemaFields<Schema extends DMLSchema> = Prettify<
{
// Omit optional properties to manage them separately and mark them as optional
[K in keyof Schema as undefined extends Schema[K]["$dataType"]
? never
: K]: Schema[K] extends RelationshipType<any>
? Schema[K]["type"] extends "belongsTo"
? InferBelongsToFields<Schema[K]["$dataType"]>
: Schema[K]["type"] extends "hasOne"
? InferHasOneFields<Schema[K]["$dataType"]>
: Schema[K]["type"] extends "hasMany"
? InferHasManyFields<Schema[K]["$dataType"]>
: Schema[K]["type"] extends "manyToMany"
? InferManyToManyFields<Schema[K]["$dataType"]>
: never
: Schema[K]["$dataType"]
} & InferOptionalFields<Schema>
>
export type InferSchemaFields<Schema extends DMLSchema> = Prettify<{
[K in keyof Schema]: Schema[K] extends RelationshipType<any>
? Schema[K]["type"] extends "belongsTo"
? InferBelongsToFields<Schema[K]["$dataType"]>
: Schema[K]["type"] extends "hasOne"
? InferHasOneFields<Schema[K]["$dataType"]>
: Schema[K]["type"] extends "hasMany"
? InferHasManyFields<Schema[K]["$dataType"]>
: Schema[K]["type"] extends "manyToMany"
? InferManyToManyFields<Schema[K]["$dataType"]>
: never
: Schema[K]["$dataType"]
}>

/**
* Helper to infer the schema type of a DmlEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ describe("Array property", () => {
name: "array",
},
nullable: false,
optional: false,
indexes: [],
relationships: [],
})
Expand Down
97 changes: 1 addition & 96 deletions 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,7 +20,6 @@ describe("Base property", () => {
name: "text",
},
nullable: false,
optional: false,
indexes: [],
relationships: [],
})
Expand All @@ -39,7 +38,6 @@ describe("Base property", () => {
},
},
nullable: false,
optional: false,
indexes: [],
relationships: [],
})
Expand All @@ -61,7 +59,6 @@ describe("Base property", () => {
name: "text",
},
nullable: true,
optional: false,
indexes: [],
relationships: [],
})
Expand All @@ -84,98 +81,6 @@ describe("Base property", () => {
},
defaultValue: "foo",
nullable: false,
optional: false,
indexes: [],
relationships: [],
})
})

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

const property = new StringProperty().optional()

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

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

const property = new StringProperty().optional().nullable()
expectTypeOf(property["$dataType"]).toEqualTypeOf<
string | undefined | null
>()
expect(property.parse("username")).toEqual({
fieldName: "username",
dataType: {
name: "text",
},
nullable: true,
optional: true,
indexes: [],
relationships: [],
})
})

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

const property = new StringProperty().nullable().optional()
expectTypeOf(property["$dataType"]).toEqualTypeOf<
string | null | undefined
>()
expect(property.parse("username")).toEqual({
fieldName: "username",
dataType: {
name: "text",
},
nullable: true,
optional: true,
indexes: [],
relationships: [],
})
})

test("define default value as a callback", () => {
class StringProperty extends BaseProperty<string> {
protected dataType: PropertyMetadata["dataType"] = {
name: "text",
}
}

const property = new StringProperty().default(() => "22")

expectTypeOf(property["$dataType"]).toEqualTypeOf<string>()
expect(property.parse("username")).toEqual({
fieldName: "username",
dataType: {
name: "text",
},
defaultValue: expect.any(Function),
nullable: false,
optional: false,
indexes: [],
relationships: [],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ describe("Big Number property", () => {
name: "bigNumber",
},
nullable: false,
optional: false,
indexes: [],
relationships: [],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ describe("Boolean property", () => {
name: "boolean",
},
nullable: false,
optional: false,
indexes: [],
relationships: [],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ describe("DateTime property", () => {
name: "dateTime",
},
nullable: false,
optional: false,
indexes: [],
relationships: [],
})
Expand Down
125 changes: 0 additions & 125 deletions packages/core/utils/src/dml/__tests__/entity-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
toMikroOrmEntities,
toMikroORMEntity,
} from "../helpers/create-mikro-orm-entity"
import { InferTypeOf } from "@medusajs/types"

describe("Entity builder", () => {
beforeEach(() => {
Expand Down Expand Up @@ -1448,130 +1447,6 @@ describe("Entity builder", () => {
},
})
})

test("define a property with default runtime value", () => {
const user = model.define("user", {
id: model.number(),
username: model.text().default((schema) => {
const { email } = schema as InferTypeOf<typeof user>
return email.replace(/\@.*/, "")
}),
email: model.text(),
spend_limit: model.bigNumber().default(500.4),
})

const User = toMikroORMEntity(user)
expectTypeOf(new User()).toMatchTypeOf<{
id: number
username: string
email: string
deleted_at: Date | null
}>()

const metaData = MetadataStorage.getMetadataFromDecorator(User)
expect(metaData.className).toEqual("User")
expect(metaData.path).toEqual("User")

expect(metaData.filters).toEqual({
softDeletable: {
name: "softDeletable",
cond: expect.any(Function),
default: true,
args: false,
},
})

expect(metaData.properties).toEqual({
id: {
reference: "scalar",
type: "number",
columnType: "integer",
name: "id",
fieldName: "id",
nullable: false,
getter: false,
setter: false,
},
username: {
reference: "scalar",
type: "string",
default: expect.any(Function),
columnType: "text",
name: "username",
fieldName: "username",
nullable: false,
getter: false,
setter: false,
},
email: {
reference: "scalar",
type: "string",
columnType: "text",
name: "email",
fieldName: "email",
nullable: false,
getter: false,
setter: false,
},
spend_limit: {
columnType: "numeric",
default: 500.4,
getter: true,
name: "spend_limit",
fieldName: "spend_limit",
nullable: false,
reference: "scalar",
setter: true,
trackChanges: false,
type: "any",
},
raw_spend_limit: {
columnType: "jsonb",
getter: false,
name: "raw_spend_limit",
fieldName: "raw_spend_limit",
nullable: false,
reference: "scalar",
setter: false,
type: "any",
},
created_at: {
reference: "scalar",
type: "date",
columnType: "timestamptz",
name: "created_at",
fieldName: "created_at",
defaultRaw: "now()",
onCreate: expect.any(Function),
nullable: false,
getter: false,
setter: false,
},
updated_at: {
reference: "scalar",
type: "date",
columnType: "timestamptz",
name: "updated_at",
fieldName: "updated_at",
defaultRaw: "now()",
onCreate: expect.any(Function),
onUpdate: expect.any(Function),
nullable: false,
getter: false,
setter: false,
},
deleted_at: {
reference: "scalar",
type: "date",
columnType: "timestamptz",
name: "deleted_at",
fieldName: "deleted_at",
nullable: true,
getter: false,
setter: false,
},
})
})
})

describe("Entity builder | id", () => {
Expand Down
4 changes: 0 additions & 4 deletions packages/core/utils/src/dml/__tests__/enum-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ describe("Enum property", () => {
},
},
nullable: false,
optional: false,
indexes: [],
relationships: [],
})
Expand All @@ -43,7 +42,6 @@ describe("Enum property", () => {
},
},
nullable: true,
optional: false,
indexes: [],
relationships: [],
})
Expand All @@ -68,7 +66,6 @@ describe("Enum property", () => {
},
},
nullable: false,
optional: false,
indexes: [],
relationships: [],
})
Expand All @@ -93,7 +90,6 @@ describe("Enum property", () => {
},
},
nullable: false,
optional: false,
indexes: [],
relationships: [],
})
Expand Down
Loading

0 comments on commit 9dff05c

Please sign in to comment.