Chore/rm main entity concept (#7709)
**What** Update the `MedusaService` class, factory and types to remove the concept of main modules. The idea being that all method will be explicitly named and suffixes to represent the object you are trying to manipulate. This pr also includes various fixes in different modules Co-authored-by: Stevche Radevski <4820812+sradevski@users.noreply.github.com> Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
Stevche Radevski
Oli Juhl
parent
2895ccfba8
commit
48963f55ef
@@ -1,7 +1,13 @@
|
||||
import { moduleDefinition } from "./module-definition"
|
||||
import { CurrencyModuleService } from "@services"
|
||||
import initialDataLoader from "./loaders/initial-data"
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
|
||||
export * from "./types"
|
||||
export * from "./models"
|
||||
export * from "./services"
|
||||
const service = CurrencyModuleService
|
||||
const loaders = [initialDataLoader]
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
loaders,
|
||||
}
|
||||
|
||||
export default moduleDefinition
|
||||
|
||||
@@ -1,33 +1,11 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { MapToConfig } from "@medusajs/utils"
|
||||
import Currency from "./models/currency"
|
||||
import {
|
||||
buildEntitiesNameToLinkableKeysMap,
|
||||
defineJoinerConfig,
|
||||
MapToConfig,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
export const LinkableKeys: Record<string, string> = {
|
||||
code: Currency.name,
|
||||
currency_code: Currency.name,
|
||||
default_currency_code: Currency.name,
|
||||
}
|
||||
export const joinerConfig = defineJoinerConfig(Modules.CURRENCY)
|
||||
|
||||
const entityLinkableKeysMap: MapToConfig = {}
|
||||
Object.entries(LinkableKeys).forEach(([key, value]) => {
|
||||
entityLinkableKeysMap[value] ??= []
|
||||
entityLinkableKeysMap[value].push({
|
||||
mapTo: key,
|
||||
valueFrom: key.split("_").pop()!,
|
||||
})
|
||||
})
|
||||
|
||||
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap
|
||||
|
||||
export const joinerConfig: ModuleJoinerConfig = {
|
||||
serviceName: Modules.CURRENCY,
|
||||
primaryKeys: ["code"],
|
||||
linkableKeys: LinkableKeys,
|
||||
alias: [
|
||||
{
|
||||
name: ["currency", "currencies"],
|
||||
args: { entity: Currency.name },
|
||||
},
|
||||
],
|
||||
} as ModuleJoinerConfig
|
||||
export const entityNameToLinkableKeysMap: MapToConfig =
|
||||
buildEntitiesNameToLinkableKeysMap(joinerConfig.linkableKeys)
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { CurrencyModuleService } from "@services"
|
||||
import initialDataLoader from "./loaders/initial-data"
|
||||
|
||||
const service = CurrencyModuleService
|
||||
const loaders = [initialDataLoader]
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
service,
|
||||
loaders,
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils"
|
||||
@@ -1,29 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import * as Models from "@models"
|
||||
import { EOL } from "os"
|
||||
|
||||
const args = process.argv
|
||||
const path = args.pop() as string
|
||||
|
||||
export default (async () => {
|
||||
const { config } = await import("dotenv")
|
||||
config()
|
||||
if (!path) {
|
||||
throw new Error(
|
||||
`filePath is required.${EOL}Example: medusa-currency-seed <filePath>`
|
||||
)
|
||||
}
|
||||
|
||||
const run = ModulesSdkUtils.buildSeedScript({
|
||||
moduleName: Modules.CURRENCY,
|
||||
models: Models,
|
||||
pathToMigrations: __dirname + "/../../migrations",
|
||||
seedHandler: async ({ manager, data }) => {
|
||||
// TODO: Add seed logic
|
||||
},
|
||||
})
|
||||
await run({ path })
|
||||
})()
|
||||
@@ -10,29 +10,24 @@ import {
|
||||
ModuleJoinerConfig,
|
||||
ModulesSdkTypes,
|
||||
} from "@medusajs/types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
|
||||
import { Currency } from "@models"
|
||||
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
|
||||
|
||||
const generateMethodForModels = {}
|
||||
import { MedusaService } from "@medusajs/utils"
|
||||
|
||||
type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
currencyService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
}
|
||||
|
||||
export default class CurrencyModuleService<TEntity extends Currency = Currency>
|
||||
extends ModulesSdkUtils.MedusaService<
|
||||
CurrencyTypes.CurrencyDTO,
|
||||
{
|
||||
Currency: { dto: CurrencyTypes.CurrencyDTO }
|
||||
}
|
||||
>(Currency, generateMethodForModels, entityNameToLinkableKeysMap)
|
||||
export default class CurrencyModuleService
|
||||
extends MedusaService<{
|
||||
Currency: { dto: CurrencyTypes.CurrencyDTO }
|
||||
}>({ Currency }, entityNameToLinkableKeysMap)
|
||||
implements ICurrencyModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected readonly currencyService_: ModulesSdkTypes.IMedusaInternalService<TEntity>
|
||||
protected readonly currencyService_: ModulesSdkTypes.IMedusaInternalService<Currency>
|
||||
|
||||
constructor(
|
||||
{ baseRepository, currencyService }: InjectedDependencies,
|
||||
@@ -48,36 +43,39 @@ export default class CurrencyModuleService<TEntity extends Currency = Currency>
|
||||
return joinerConfig
|
||||
}
|
||||
|
||||
retrieve(
|
||||
// @ts-expect-error
|
||||
async retrieveCurrency(
|
||||
code: string,
|
||||
config?: FindConfig<CurrencyTypes.CurrencyDTO>,
|
||||
sharedContext?: Context
|
||||
): Promise<CurrencyTypes.CurrencyDTO> {
|
||||
return this.currencyService_.retrieve(
|
||||
code?.toLowerCase(),
|
||||
return await super.retrieveCurrency(
|
||||
CurrencyModuleService.normalizeFilters({ code: [code] })!.code![0],
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
list(
|
||||
// @ts-expect-error
|
||||
async listCurrencies(
|
||||
filters?: FilterableCurrencyProps,
|
||||
config?: FindConfig<CurrencyTypes.CurrencyDTO>,
|
||||
sharedContext?: Context
|
||||
): Promise<CurrencyTypes.CurrencyDTO[]> {
|
||||
return this.currencyService_.list(
|
||||
return await super.listCurrencies(
|
||||
CurrencyModuleService.normalizeFilters(filters),
|
||||
config,
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
listAndCount(
|
||||
// @ts-expect-error
|
||||
async listAndCountCurrencies(
|
||||
filters?: FilterableCurrencyProps,
|
||||
config?: FindConfig<CurrencyTypes.CurrencyDTO>,
|
||||
sharedContext?: Context
|
||||
): Promise<[CurrencyTypes.CurrencyDTO[], number]> {
|
||||
return this.currencyService_.listAndCount(
|
||||
return await super.listAndCountCurrencies(
|
||||
CurrencyModuleService.normalizeFilters(filters),
|
||||
config,
|
||||
sharedContext
|
||||
|
||||
Reference in New Issue
Block a user