feat(utils,currency): Migrate currency to use DML (#7807)
This commit is contained in:
@@ -1166,6 +1166,109 @@ describe("Entity builder", () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe("Entity builder | primaryKey", () => {
|
||||
test("should create both id fields and primaryKey fields", () => {
|
||||
const model = new EntityBuilder()
|
||||
const user = model.define("user", {
|
||||
id: model.id({ primaryKey: false }),
|
||||
email: model.text().primaryKey(),
|
||||
account_id: model.number().primaryKey(),
|
||||
})
|
||||
|
||||
const entityBuilder = createMikrORMEntity()
|
||||
const User = entityBuilder(user)
|
||||
|
||||
expectTypeOf(new User()).toMatchTypeOf<{
|
||||
id: string
|
||||
email: string
|
||||
account_id: number
|
||||
}>()
|
||||
|
||||
const metaData = MetadataStorage.getMetadataFromDecorator(User)
|
||||
const userInstance = new User()
|
||||
userInstance["generateId"]()
|
||||
|
||||
expect(metaData.className).toEqual("User")
|
||||
expect(metaData.path).toEqual("User")
|
||||
|
||||
expect(metaData.hooks).toEqual({
|
||||
beforeCreate: ["generateId"],
|
||||
onInit: ["generateId"],
|
||||
})
|
||||
|
||||
expect(metaData.filters).toEqual({
|
||||
softDeletable: {
|
||||
name: "softDeletable",
|
||||
cond: expect.any(Function),
|
||||
default: true,
|
||||
args: false,
|
||||
},
|
||||
})
|
||||
|
||||
expect(metaData.properties).toEqual({
|
||||
id: {
|
||||
reference: "scalar",
|
||||
type: "string",
|
||||
columnType: "text",
|
||||
name: "id",
|
||||
nullable: false,
|
||||
getter: false,
|
||||
setter: false,
|
||||
},
|
||||
email: {
|
||||
columnType: "text",
|
||||
name: "email",
|
||||
nullable: false,
|
||||
primary: true,
|
||||
reference: "scalar",
|
||||
type: "string",
|
||||
},
|
||||
account_id: {
|
||||
columnType: "integer",
|
||||
name: "account_id",
|
||||
nullable: false,
|
||||
primary: true,
|
||||
reference: "scalar",
|
||||
type: "number",
|
||||
},
|
||||
created_at: {
|
||||
reference: "scalar",
|
||||
type: "date",
|
||||
columnType: "timestamptz",
|
||||
name: "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",
|
||||
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",
|
||||
nullable: true,
|
||||
getter: false,
|
||||
setter: false,
|
||||
},
|
||||
})
|
||||
|
||||
expect(userInstance.id).toBeDefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe("Entity builder | indexes", () => {
|
||||
test("define index on a field", () => {
|
||||
const model = new EntityBuilder()
|
||||
|
||||
@@ -10,6 +10,9 @@ describe("Number property", () => {
|
||||
fieldName: "age",
|
||||
dataType: {
|
||||
name: "number",
|
||||
options: {
|
||||
primaryKey: false,
|
||||
},
|
||||
},
|
||||
nullable: false,
|
||||
indexes: [],
|
||||
|
||||
@@ -10,6 +10,7 @@ describe("Text property", () => {
|
||||
fieldName: "username",
|
||||
dataType: {
|
||||
name: "text",
|
||||
options: { primaryKey: false },
|
||||
},
|
||||
nullable: false,
|
||||
indexes: [],
|
||||
|
||||
@@ -240,7 +240,10 @@ export function createMikrORMEntity() {
|
||||
* Hook to generate entity within the code
|
||||
*/
|
||||
MikroORMEntity.prototype.generateId = function () {
|
||||
this.id = generateEntityId(this.id, field.dataType.options?.prefix)
|
||||
this[field.fieldName] = generateEntityId(
|
||||
this[field.fieldName],
|
||||
field.dataType.options?.prefix
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -248,6 +251,7 @@ export function createMikrORMEntity() {
|
||||
*/
|
||||
BeforeCreate()(MikroORMEntity.prototype, "generateId")
|
||||
OnInit()(MikroORMEntity.prototype, "generateId")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -257,6 +261,19 @@ export function createMikrORMEntity() {
|
||||
const columnType = COLUMN_TYPES[field.dataType.name]
|
||||
const propertyType = PROPERTY_TYPES[field.dataType.name]
|
||||
|
||||
/**
|
||||
* Defining a primary key property
|
||||
*/
|
||||
if (field.dataType.options?.primaryKey) {
|
||||
PrimaryKey({
|
||||
columnType,
|
||||
type: propertyType,
|
||||
nullable: false,
|
||||
})(MikroORMEntity.prototype, field.fieldName)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
Property({
|
||||
columnType,
|
||||
type: propertyType,
|
||||
|
||||
@@ -15,6 +15,9 @@ export class IdProperty extends BaseProperty<string> {
|
||||
|
||||
constructor(options?: { primaryKey?: boolean; prefix?: string }) {
|
||||
super()
|
||||
this.dataType = { name: "id", options: { primaryKey: true, ...options } }
|
||||
this.dataType = {
|
||||
name: "id",
|
||||
options: { primaryKey: true, ...options },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,25 @@ import { BaseProperty } from "./base"
|
||||
* property
|
||||
*/
|
||||
export class NumberProperty extends BaseProperty<number> {
|
||||
protected dataType = {
|
||||
name: "number",
|
||||
} as const
|
||||
protected dataType: {
|
||||
name: "number"
|
||||
options: {
|
||||
primaryKey: boolean
|
||||
}
|
||||
}
|
||||
|
||||
primaryKey() {
|
||||
this.dataType.options.primaryKey = true
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
constructor(options?: { primaryKey?: boolean }) {
|
||||
super()
|
||||
|
||||
this.dataType = {
|
||||
name: "number",
|
||||
options: { primaryKey: false, ...options },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,25 @@ import { BaseProperty } from "./base"
|
||||
* The TextProperty is used to define a textual property
|
||||
*/
|
||||
export class TextProperty extends BaseProperty<string> {
|
||||
protected dataType = {
|
||||
name: "text",
|
||||
} as const
|
||||
protected dataType: {
|
||||
name: "text"
|
||||
options: {
|
||||
primaryKey: boolean
|
||||
}
|
||||
}
|
||||
|
||||
primaryKey() {
|
||||
this.dataType.options.primaryKey = true
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
constructor(options?: { primaryKey?: boolean }) {
|
||||
super()
|
||||
|
||||
this.dataType = {
|
||||
name: "text",
|
||||
options: { primaryKey: false, ...options },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,4 +279,37 @@ describe("defineJoiner", () => {
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
it("should return a full joiner configuration with custom aliases overriding defaults", () => {
|
||||
const joinerConfig = defineJoinerConfig(Modules.FULFILLMENT, {
|
||||
entityQueryingConfig: [FulfillmentSet],
|
||||
alias: [
|
||||
{
|
||||
name: ["fulfillment_set", "fulfillment_sets"],
|
||||
args: {
|
||||
entity: "FulfillmentSet",
|
||||
methodSuffix: "fulfillmentSetCustom",
|
||||
},
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
expect(joinerConfig).toEqual({
|
||||
serviceName: Modules.FULFILLMENT,
|
||||
primaryKeys: ["id"],
|
||||
schema: undefined,
|
||||
linkableKeys: {
|
||||
fulfillment_set_id: FulfillmentSet.name,
|
||||
},
|
||||
alias: [
|
||||
{
|
||||
name: ["fulfillment_set", "fulfillment_sets"],
|
||||
args: {
|
||||
entity: "FulfillmentSet",
|
||||
methodSuffix: "fulfillmentSetCustom",
|
||||
},
|
||||
},
|
||||
],
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
export * from "./load-module-database-config"
|
||||
export * from "./decorators"
|
||||
export * from "./build-query"
|
||||
export * from "./loaders/mikro-orm-connection-loader"
|
||||
export * from "./loaders/mikro-orm-connection-loader-factory"
|
||||
export * from "./loaders/container-loader-factory"
|
||||
export * from "./create-pg-connection"
|
||||
export * from "./migration-scripts"
|
||||
export * from "./medusa-internal-service"
|
||||
export * from "./medusa-service"
|
||||
export * from "./decorators"
|
||||
export * from "./definition"
|
||||
export * from "./event-builder-factory"
|
||||
export * from "./joiner-config-builder"
|
||||
export * from "./load-module-database-config"
|
||||
export * from "./loaders/container-loader-factory"
|
||||
export * from "./loaders/load-models"
|
||||
export * from "./loaders/mikro-orm-connection-loader"
|
||||
export * from "./loaders/mikro-orm-connection-loader-factory"
|
||||
export * from "./medusa-internal-service"
|
||||
export * from "./medusa-service"
|
||||
export * from "./migration-scripts"
|
||||
export * from "./mikro-orm-cli-config-builder"
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { JoinerServiceConfigAlias, ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { join } from "path"
|
||||
import {
|
||||
camelToSnakeCase,
|
||||
deduplicate,
|
||||
@@ -7,7 +8,6 @@ import {
|
||||
pluralize,
|
||||
upperCaseFirst,
|
||||
} from "../common"
|
||||
import { join } from "path"
|
||||
import { loadModels } from "./loaders/load-models"
|
||||
|
||||
/**
|
||||
@@ -33,7 +33,7 @@ export function defineJoinerConfig(
|
||||
linkableKeys,
|
||||
primaryKeys,
|
||||
}: {
|
||||
alias?: ModuleJoinerConfig["alias"]
|
||||
alias?: JoinerServiceConfigAlias[]
|
||||
schema?: string
|
||||
entityQueryingConfig?: { name: string }[]
|
||||
linkableKeys?: Record<string, string>
|
||||
@@ -79,16 +79,22 @@ export function defineJoinerConfig(
|
||||
pluralize(upperCaseFirst(alias.args.entity)),
|
||||
},
|
||||
})),
|
||||
...models.map((entity, i) => ({
|
||||
name: [
|
||||
`${camelToSnakeCase(entity.name).toLowerCase()}`,
|
||||
`${pluralize(camelToSnakeCase(entity.name).toLowerCase())}`,
|
||||
],
|
||||
args: {
|
||||
entity: entity.name,
|
||||
methodSuffix: pluralize(upperCaseFirst(entity.name)),
|
||||
},
|
||||
})),
|
||||
...models
|
||||
.filter((model) => {
|
||||
return (
|
||||
!alias || !alias.some((alias) => alias.args?.entity === model.name)
|
||||
)
|
||||
})
|
||||
.map((entity, i) => ({
|
||||
name: [
|
||||
`${camelToSnakeCase(entity.name).toLowerCase()}`,
|
||||
`${pluralize(camelToSnakeCase(entity.name).toLowerCase())}`,
|
||||
],
|
||||
args: {
|
||||
entity: entity.name,
|
||||
methodSuffix: pluralize(upperCaseFirst(entity.name)),
|
||||
},
|
||||
})),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,10 +27,11 @@ export function loadModels(basePath: string) {
|
||||
|
||||
if (stats.isFile()) {
|
||||
try {
|
||||
const required = require(filePath)
|
||||
return Object.values(required).filter(
|
||||
(resource) => typeof resource === "function" && !!resource.name
|
||||
)
|
||||
const required = require(filePath) as {
|
||||
[key: string]: { name?: string }
|
||||
}
|
||||
|
||||
return Object.values(required).filter((resource) => !!resource.name)
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { MikroORMOptions } from "@mikro-orm/core/utils/Configuration"
|
||||
import { IDmlEntity } from "@medusajs/types"
|
||||
import { DmlEntity, toMikroORMEntity } from "../dml"
|
||||
import { TSMigrationGenerator } from "../dal"
|
||||
import { AnyEntity, EntityClassGroup, EntitySchema } from "@mikro-orm/core"
|
||||
@@ -11,7 +10,7 @@ type Options = Partial<MikroORMOptions> & {
|
||||
| EntityClass<AnyEntity>
|
||||
| EntityClassGroup<AnyEntity>
|
||||
| EntitySchema
|
||||
| IDmlEntity<any>
|
||||
| DmlEntity<any>
|
||||
)[]
|
||||
databaseName: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user