feat(utils,currency): Migrate currency to use DML (#7807)

This commit is contained in:
Riqwan Thamir
2024-06-24 19:07:26 +02:00
committed by GitHub
parent 7414034ac4
commit 96fb7a962e
21 changed files with 312 additions and 116 deletions

View File

@@ -1,7 +1,7 @@
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import {
createAdminUser,
adminHeaders,
createAdminUser,
} from "../../../../helpers/create-admin-user"
jest.setTimeout(30000)
@@ -40,24 +40,26 @@ medusaIntegrationTestRunner({
)
expect(response.status).toEqual(200)
expect(response.data.currencies).toEqual([
expect.objectContaining({
code: "aud",
name: "Australian Dollar",
}),
expect.objectContaining({
code: "byn",
name: "Belarusian Ruble",
}),
expect.objectContaining({
code: "rub",
name: "Russian Ruble",
}),
expect.objectContaining({
code: "usd",
name: "US Dollar",
}),
])
expect(response.data.currencies).toEqual(
expect.arrayContaining([
expect.objectContaining({
code: "aud",
name: "Australian Dollar",
}),
expect.objectContaining({
code: "byn",
name: "Belarusian Ruble",
}),
expect.objectContaining({
code: "rub",
name: "Russian Ruble",
}),
expect.objectContaining({
code: "usd",
name: "US Dollar",
}),
])
)
})
})
},

View File

@@ -60,9 +60,10 @@ medusaIntegrationTestRunner({
adminHeaders
)
expect(listResp.data.currencies).toHaveLength(1)
expect(listResp.data.currencies[0]).toEqual(
expect.objectContaining({ code: "zwl", name: "Zimbabwean Dollar" })
expect(listResp.data.currencies).toEqual(
expect.arrayContaining([
expect.objectContaining({ code: "zwl", name: "Zimbabwean Dollar" }),
])
)
})
})

View File

@@ -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()

View File

@@ -10,6 +10,9 @@ describe("Number property", () => {
fieldName: "age",
dataType: {
name: "number",
options: {
primaryKey: false,
},
},
nullable: false,
indexes: [],

View File

@@ -10,6 +10,7 @@ describe("Text property", () => {
fieldName: "username",
dataType: {
name: "text",
options: { primaryKey: false },
},
nullable: false,
indexes: [],

View File

@@ -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,

View File

@@ -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 },
}
}
}

View File

@@ -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 },
}
}
}

View File

@@ -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 },
}
}
}

View File

@@ -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",
},
},
],
})
})
})

View File

@@ -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"

View File

@@ -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)),
},
})),
],
}
}

View File

@@ -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) {}
}

View File

@@ -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
}

View File

@@ -1,8 +1,7 @@
import { defineMikroOrmCliConfig } from "@medusajs/utils"
import * as entities from "./src/models"
module.exports = {
export default defineMikroOrmCliConfig({
entities: Object.values(entities),
schema: "public",
clientUrl: "postgres://postgres@localhost/medusa-currency",
type: "postgresql",
}
databaseName: "medusa-currency",
})

View File

@@ -5,7 +5,12 @@ import {
Modules,
} from "@medusajs/utils"
export const joinerConfig = defineJoinerConfig(Modules.CURRENCY)
export const joinerConfig = defineJoinerConfig(Modules.CURRENCY, {
primaryKeys: ["code"],
linkableKeys: {
currency_code: "currency",
},
})
export const entityNameToLinkableKeysMap: MapToConfig =
buildEntitiesNameToLinkableKeysMap(joinerConfig.linkableKeys)

View File

@@ -14,7 +14,7 @@ export default async ({
const logger =
container.resolve<Logger>(ContainerRegistrationKeys.LOGGER) ?? console
const { currencyService_ } = container.resolve<{
currencyService_: ModulesSdkTypes.IMedusaInternalService<Currency>
currencyService_: ModulesSdkTypes.IMedusaInternalService<typeof Currency>
}>(ModuleRegistrationName.CURRENCY)
try {

View File

@@ -42,7 +42,7 @@
},
"decimal_digits": {
"name": "decimal_digits",
"type": "int",
"type": "integer",
"unsigned": false,
"autoincrement": false,
"primary": false,
@@ -90,6 +90,16 @@
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"deleted_at": {
"name": "deleted_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"length": 6,
"mappedType": "datetime"
}
},
"name": "currency",

View File

@@ -0,0 +1,15 @@
import { Migration } from "@mikro-orm/migrations"
export class Migration20240624082354 extends Migration {
async up(): Promise<void> {
this.addSql(
'alter table if exists "currency" add column if not exists "deleted_at" timestamptz null;'
)
}
async down(): Promise<void> {
this.addSql(
'alter table if exists "currency" drop column if exists "deleted_at";'
)
}
}

View File

@@ -1,50 +1,10 @@
import { BigNumberRawValue } from "@medusajs/types"
import {
BigNumber,
MikroOrmBigNumberProperty,
Searchable,
} from "@medusajs/utils"
import { Entity, PrimaryKey, Property } from "@mikro-orm/core"
import { model } from "@medusajs/utils"
@Entity({ tableName: "currency" })
class Currency {
@Searchable()
@PrimaryKey({ columnType: "text" })
code!: string
@Property({ columnType: "text" })
symbol: string
@Property({ columnType: "text" })
symbol_native: string
@Searchable()
@Property({ columnType: "text" })
name: string
@Property({ columnType: "int", default: 0 })
decimal_digits: number
@MikroOrmBigNumberProperty({ default: 0 })
rounding: BigNumber | number
@Property({ columnType: "jsonb" })
raw_rounding: BigNumberRawValue
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
}
export default Currency
export default model.define("currency", {
code: model.text().primaryKey(),
symbol: model.text(),
symbol_native: model.text(),
name: model.text(),
decimal_digits: model.number().default(0),
rounding: model.bigNumber().default(0),
})

View File

@@ -11,9 +11,9 @@ import {
ModulesSdkTypes,
} from "@medusajs/types"
import { MedusaService } from "@medusajs/utils"
import { Currency } from "@models"
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
import { MedusaService } from "@medusajs/utils"
type InjectedDependencies = {
baseRepository: DAL.RepositoryService
@@ -27,7 +27,9 @@ export default class CurrencyModuleService
implements ICurrencyModuleService
{
protected baseRepository_: DAL.RepositoryService
protected readonly currencyService_: ModulesSdkTypes.IMedusaInternalService<Currency>
protected readonly currencyService_: ModulesSdkTypes.IMedusaInternalService<
typeof Currency
>
constructor(
{ baseRepository, currencyService }: InjectedDependencies,