feat: refactor module joiner config and links generation (#7859)
* feat: refactor module joiner config and links generation * improve typings * WIP * WIP * WIP * rename type file * create link config * finish typings and add utils * improve links * WIP typings * finalize ExportModule utils * finalize ExportModule utils * fix: dml tests * improve and fixes * simplify typings with id changes * add toJSON * multiple fixes and entity builder fixes * fix currency searchable * fix tests * medusa service refactoring * cleanup * cleanup and fixes * make module name optional * renaming --------- Co-authored-by: Harminder Virk <virk.officials@gmail.com>
This commit is contained in:
committed by
GitHub
parent
5aa62e59e4
commit
617a5972bf
@@ -1,5 +1,6 @@
|
||||
import { model } from "@medusajs/utils"
|
||||
|
||||
export const dmlEntity = model.define("dmlEntity", {
|
||||
id: model.id().primaryKey(),
|
||||
name: model.text(),
|
||||
})
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { model } from "@medusajs/utils"
|
||||
|
||||
export const entityModel = model.define("entityModel", {
|
||||
id: model.id().primaryKey(),
|
||||
name: model.text(),
|
||||
})
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { model } from "@medusajs/utils"
|
||||
|
||||
export const dmlEntity = model.define("dmlEntity", {
|
||||
id: model.id().primaryKey(),
|
||||
name: model.text(),
|
||||
})
|
||||
|
||||
@@ -11,11 +11,11 @@ import {
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
createMedusaContainer,
|
||||
createMikrORMEntity,
|
||||
defineJoinerConfig,
|
||||
DmlEntity,
|
||||
MedusaModuleType,
|
||||
ModulesSdkUtils,
|
||||
toMikroOrmEntities,
|
||||
} from "@medusajs/utils"
|
||||
import { asFunction, asValue } from "awilix"
|
||||
import { statSync } from "fs"
|
||||
@@ -270,12 +270,11 @@ export async function loadResources(
|
||||
),
|
||||
])
|
||||
|
||||
const entityBuilder = createMikrORMEntity()
|
||||
const cleanupResources = (resources) => {
|
||||
return Object.values(resources)
|
||||
.map((resource) => {
|
||||
if (DmlEntity.isDmlEntity(resource)) {
|
||||
return entityBuilder(resource as DmlEntity<any>)
|
||||
return resource
|
||||
}
|
||||
|
||||
if (typeof resource === "function") {
|
||||
@@ -289,11 +288,12 @@ export async function loadResources(
|
||||
|
||||
const potentialServices = [...new Set(cleanupResources(services))]
|
||||
const potentialModels = [...new Set(cleanupResources(models))]
|
||||
const mikroOrmModels = toMikroOrmEntities(potentialModels)
|
||||
const potentialRepositories = [...new Set(cleanupResources(repositories))]
|
||||
|
||||
const finalLoaders = prepareLoaders({
|
||||
loadedModuleLoaders,
|
||||
models: potentialModels,
|
||||
models: mikroOrmModels,
|
||||
repositories: potentialRepositories,
|
||||
services: potentialServices,
|
||||
moduleResolution,
|
||||
@@ -308,7 +308,7 @@ export async function loadResources(
|
||||
|
||||
return {
|
||||
services: potentialServices,
|
||||
models: potentialModels,
|
||||
models: mikroOrmModels,
|
||||
repositories: potentialRepositories,
|
||||
loaders: finalLoaders,
|
||||
moduleService,
|
||||
@@ -433,15 +433,20 @@ function generateJoinerConfigIfNecessary({
|
||||
}: {
|
||||
moduleResolution: ModuleResolution
|
||||
service: Constructor<IModuleService>
|
||||
models: Function[]
|
||||
models: (Function | DmlEntity<any, any>)[]
|
||||
}) {
|
||||
if (service.prototype.__joinerConfig) {
|
||||
return
|
||||
}
|
||||
const originalJoinerConfigFn = service.prototype.__joinerConfig
|
||||
|
||||
service.prototype.__joinerConfig = function () {
|
||||
if (originalJoinerConfigFn) {
|
||||
return {
|
||||
serviceName: moduleResolution.definition.key,
|
||||
...originalJoinerConfigFn(),
|
||||
}
|
||||
}
|
||||
|
||||
return defineJoinerConfig(moduleResolution.definition.key, {
|
||||
entityQueryingConfig: models,
|
||||
models,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user