chore: make module loaders DML aware and auto generate joiner config (#7781)
* chore: make module loaders DML aware and auto generate joiner config * fixes and cleanup * improve dml entity check * add unit tests on load resources * cleanup deps * cleanup deps * cleanup Modules * finalise * fix modules-sdk jest * fix modules-sdk jest * fix import * fix import
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
export * as ModuleWithDmlMixedWithoutJoinerConfigFixtures from "./module-with-dml-mixed-without-joiner-config"
|
||||
export * as ModuleWithDmlWithoutJoinerConfigFixtures from "./module-with-dml-without-joiner-config"
|
||||
export * as ModuleWithoutJoinerConfigFixtures from "./module-without-joiner-config"
|
||||
export * as ModuleWithJoinerConfigFixtures from "./module-with-joiner-config"
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { ModuleService } from "./services/module-service"
|
||||
|
||||
const moduleExports: ModuleExports = {
|
||||
service: ModuleService,
|
||||
}
|
||||
|
||||
export * from "./models"
|
||||
export * from "./services/module-service"
|
||||
|
||||
export default moduleExports
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import { model } from "@medusajs/utils"
|
||||
|
||||
export const dmlEntity = model.define("dmlEntity", {
|
||||
name: model.text(),
|
||||
})
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
import { Entity, Property } from "@mikro-orm/core"
|
||||
|
||||
@Entity()
|
||||
export class EntityModel {
|
||||
@Property({ columnType: "int" })
|
||||
id!: number
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export * from "./entity"
|
||||
export * from "./dml-entity"
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
import { IModuleService } from "@medusajs/types"
|
||||
|
||||
export class ModuleService implements IModuleService {}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { ModuleService } from "./services/module-service"
|
||||
|
||||
const moduleExports: ModuleExports = {
|
||||
service: ModuleService,
|
||||
}
|
||||
|
||||
export * from "./models"
|
||||
export * from "./services/module-service"
|
||||
|
||||
export default moduleExports
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import { model } from "@medusajs/utils"
|
||||
|
||||
export const entityModel = model.define("entityModel", {
|
||||
name: model.text(),
|
||||
})
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import { model } from "@medusajs/utils"
|
||||
|
||||
export const dmlEntity = model.define("dmlEntity", {
|
||||
name: model.text(),
|
||||
})
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export * from "./dml-entity"
|
||||
export * from "./dml-entity-model"
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
import { IModuleService } from "@medusajs/types"
|
||||
|
||||
export class ModuleService implements IModuleService {}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { ModuleService } from "./services/module-service"
|
||||
|
||||
const moduleExports: ModuleExports = {
|
||||
service: ModuleService,
|
||||
}
|
||||
|
||||
export * from "./models"
|
||||
export * from "./services/module-service"
|
||||
|
||||
export default moduleExports
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
import { Entity, Property } from "@mikro-orm/core"
|
||||
|
||||
@Entity()
|
||||
export class Entity2 {
|
||||
@Property({ columnType: "int" })
|
||||
id!: number
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
import { Entity, Property } from "@mikro-orm/core"
|
||||
|
||||
@Entity()
|
||||
export class EntityModel {
|
||||
@Property({ columnType: "int" })
|
||||
id!: number
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export * from "./entity"
|
||||
export * from "./entity-2"
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
import { IModuleService, ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { defineJoinerConfig } from "@medusajs/utils"
|
||||
|
||||
export class ModuleService implements IModuleService {
|
||||
__joinerConfig(): ModuleJoinerConfig {
|
||||
return defineJoinerConfig("module-service", {
|
||||
alias: [
|
||||
{
|
||||
name: ["custom_name"],
|
||||
args: {
|
||||
entity: "Custom",
|
||||
},
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { ModuleService } from "./services/module-service"
|
||||
|
||||
const moduleExports: ModuleExports = {
|
||||
service: ModuleService,
|
||||
}
|
||||
|
||||
export * from "./models"
|
||||
export * from "./services/module-service"
|
||||
|
||||
export default moduleExports
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
import { Entity, Property } from "@mikro-orm/core"
|
||||
|
||||
@Entity()
|
||||
export class Entity2 {
|
||||
@Property({ columnType: "int" })
|
||||
id!: number
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
import { Entity, Property } from "@mikro-orm/core"
|
||||
|
||||
@Entity()
|
||||
export class EntityModel {
|
||||
@Property({ columnType: "int" })
|
||||
id!: number
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export * from "./entity"
|
||||
export * from "./entity-2"
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
import { IModuleService } from "@medusajs/types"
|
||||
|
||||
export class ModuleService implements IModuleService {}
|
||||
@@ -0,0 +1,316 @@
|
||||
import { loadResources } from "../load-internal"
|
||||
import { IModuleService, ModuleResolution } from "@medusajs/types"
|
||||
import { join } from "path"
|
||||
import {
|
||||
ModuleWithDmlMixedWithoutJoinerConfigFixtures,
|
||||
ModuleWithDmlWithoutJoinerConfigFixtures,
|
||||
ModuleWithJoinerConfigFixtures,
|
||||
ModuleWithoutJoinerConfigFixtures,
|
||||
} from "../__fixtures__"
|
||||
import { upperCaseFirst } from "@medusajs/utils"
|
||||
|
||||
describe("load internal - load resources", () => {
|
||||
describe("when loading the module resources from a path", () => {
|
||||
test("should return the correct resources and generate the correct joiner config from a mix of DML entities and mikro orm entities", async () => {
|
||||
const { ModuleService, EntityModel, dmlEntity } =
|
||||
ModuleWithDmlMixedWithoutJoinerConfigFixtures
|
||||
|
||||
const moduleResolution: ModuleResolution = {
|
||||
resolutionPath: join(
|
||||
__dirname,
|
||||
"../__fixtures__/module-with-dml-mixed-without-joiner-config"
|
||||
),
|
||||
definition: {
|
||||
key: "module-with-dml-mixed-without-joiner-config",
|
||||
registrationName: "service",
|
||||
label: "Module with DML mixed without joiner config",
|
||||
defaultPackage: false,
|
||||
defaultModuleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "shared",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
expect(
|
||||
(ModuleService.prototype as IModuleService).__joinerConfig
|
||||
).toBeUndefined()
|
||||
|
||||
const resources = await loadResources(moduleResolution)
|
||||
|
||||
expect(resources).toBeDefined()
|
||||
expect(resources.services).toHaveLength(1)
|
||||
expect(resources.services[0]).toEqual(ModuleService)
|
||||
expect(resources.models).toHaveLength(2)
|
||||
expect(resources.models).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ name: upperCaseFirst(dmlEntity.name) }),
|
||||
expect.objectContaining({ name: upperCaseFirst(EntityModel.name) }),
|
||||
])
|
||||
)
|
||||
expect(resources.repositories).toHaveLength(0)
|
||||
expect(resources.loaders).toHaveLength(2)
|
||||
expect(resources.loaders).toEqual([
|
||||
expect.objectContaining({ name: "connectionLoader" }),
|
||||
expect.objectContaining({ name: "containerLoader" }),
|
||||
])
|
||||
expect(resources.moduleService).toEqual(ModuleService)
|
||||
|
||||
expect(
|
||||
(resources.moduleService.prototype as IModuleService).__joinerConfig
|
||||
).toBeDefined()
|
||||
|
||||
const generatedJoinerConfig = (
|
||||
resources.moduleService.prototype as IModuleService
|
||||
).__joinerConfig()
|
||||
|
||||
expect(generatedJoinerConfig).toEqual({
|
||||
serviceName: "module-with-dml-mixed-without-joiner-config",
|
||||
primaryKeys: ["id"],
|
||||
linkableKeys: {
|
||||
dml_entity_id: "DmlEntity",
|
||||
entity_model_id: "EntityModel",
|
||||
},
|
||||
alias: [
|
||||
{
|
||||
name: ["dml_entity", "dml_entities"],
|
||||
args: {
|
||||
entity: "DmlEntity",
|
||||
methodSuffix: "DmlEntities",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["entity_model", "entity_models"],
|
||||
args: {
|
||||
entity: "EntityModel",
|
||||
methodSuffix: "EntityModels",
|
||||
},
|
||||
},
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
test("should return the correct resources and generate the correct joiner config from DML entities", async () => {
|
||||
const { ModuleService, entityModel, dmlEntity } =
|
||||
ModuleWithDmlWithoutJoinerConfigFixtures
|
||||
|
||||
const moduleResolution: ModuleResolution = {
|
||||
resolutionPath: join(
|
||||
__dirname,
|
||||
"../__fixtures__/module-with-dml-without-joiner-config"
|
||||
),
|
||||
definition: {
|
||||
key: "module-with-dml-without-joiner-config",
|
||||
registrationName: "service",
|
||||
label: "Module with DML without joiner config",
|
||||
defaultPackage: false,
|
||||
defaultModuleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "shared",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
expect(
|
||||
(ModuleService.prototype as IModuleService).__joinerConfig
|
||||
).toBeUndefined()
|
||||
|
||||
const resources = await loadResources(moduleResolution)
|
||||
|
||||
expect(resources).toBeDefined()
|
||||
expect(resources.services).toHaveLength(1)
|
||||
expect(resources.services[0]).toEqual(ModuleService)
|
||||
expect(resources.models).toHaveLength(2)
|
||||
expect(resources.models).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ name: upperCaseFirst(dmlEntity.name) }),
|
||||
expect.objectContaining({ name: upperCaseFirst(entityModel.name) }),
|
||||
])
|
||||
)
|
||||
expect(resources.repositories).toHaveLength(0)
|
||||
expect(resources.loaders).toHaveLength(2)
|
||||
expect(resources.loaders).toEqual([
|
||||
expect.objectContaining({ name: "connectionLoader" }),
|
||||
expect.objectContaining({ name: "containerLoader" }),
|
||||
])
|
||||
expect(resources.moduleService).toEqual(ModuleService)
|
||||
|
||||
expect(
|
||||
(resources.moduleService.prototype as IModuleService).__joinerConfig
|
||||
).toBeDefined()
|
||||
|
||||
const generatedJoinerConfig = (
|
||||
resources.moduleService.prototype as IModuleService
|
||||
).__joinerConfig()
|
||||
|
||||
expect(generatedJoinerConfig).toEqual({
|
||||
serviceName: "module-with-dml-without-joiner-config",
|
||||
primaryKeys: ["id"],
|
||||
linkableKeys: {
|
||||
entity_model_id: "EntityModel",
|
||||
dml_entity_id: "DmlEntity",
|
||||
},
|
||||
alias: [
|
||||
{
|
||||
name: ["entity_model", "entity_models"],
|
||||
args: {
|
||||
entity: "EntityModel",
|
||||
methodSuffix: "EntityModels",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["dml_entity", "dml_entities"],
|
||||
args: {
|
||||
entity: "DmlEntity",
|
||||
methodSuffix: "DmlEntities",
|
||||
},
|
||||
},
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
test("should return the correct resources and generate the correct joiner config from mikro orm entities", async () => {
|
||||
const { ModuleService, EntityModel, Entity2 } =
|
||||
ModuleWithoutJoinerConfigFixtures
|
||||
|
||||
const moduleResolution: ModuleResolution = {
|
||||
resolutionPath: join(
|
||||
__dirname,
|
||||
"../__fixtures__/module-without-joiner-config"
|
||||
),
|
||||
definition: {
|
||||
key: "module-without-joiner-config",
|
||||
registrationName: "service",
|
||||
label: "Module without joiner config",
|
||||
defaultPackage: false,
|
||||
defaultModuleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "shared",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
expect(
|
||||
(ModuleService.prototype as IModuleService).__joinerConfig
|
||||
).toBeUndefined()
|
||||
|
||||
const resources = await loadResources(moduleResolution)
|
||||
|
||||
expect(resources).toBeDefined()
|
||||
expect(resources.services).toHaveLength(1)
|
||||
expect(resources.services[0]).toEqual(ModuleService)
|
||||
expect(resources.models).toHaveLength(2)
|
||||
expect(resources.models).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ name: upperCaseFirst(EntityModel.name) }),
|
||||
expect.objectContaining({ name: upperCaseFirst(Entity2.name) }),
|
||||
])
|
||||
)
|
||||
expect(resources.repositories).toHaveLength(0)
|
||||
expect(resources.loaders).toHaveLength(2)
|
||||
expect(resources.loaders).toEqual([
|
||||
expect.objectContaining({ name: "connectionLoader" }),
|
||||
expect.objectContaining({ name: "containerLoader" }),
|
||||
])
|
||||
expect(resources.moduleService).toEqual(ModuleService)
|
||||
|
||||
expect(
|
||||
(resources.moduleService.prototype as IModuleService).__joinerConfig
|
||||
).toBeDefined()
|
||||
|
||||
const generatedJoinerConfig = (
|
||||
resources.moduleService.prototype as IModuleService
|
||||
).__joinerConfig()
|
||||
|
||||
expect(generatedJoinerConfig).toEqual({
|
||||
serviceName: "module-without-joiner-config",
|
||||
primaryKeys: ["id"],
|
||||
linkableKeys: {
|
||||
entity2_id: "Entity2",
|
||||
entity_model_id: "EntityModel",
|
||||
},
|
||||
alias: [
|
||||
{
|
||||
name: ["entity2", "entity2s"],
|
||||
args: {
|
||||
entity: "Entity2",
|
||||
methodSuffix: "Entity2s",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ["entity_model", "entity_models"],
|
||||
args: {
|
||||
entity: "EntityModel",
|
||||
methodSuffix: "EntityModels",
|
||||
},
|
||||
},
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
test("should return the correct resources and use the given joiner config", async () => {
|
||||
const { ModuleService, EntityModel, Entity2 } =
|
||||
ModuleWithJoinerConfigFixtures
|
||||
|
||||
const moduleResolution: ModuleResolution = {
|
||||
resolutionPath: join(
|
||||
__dirname,
|
||||
"../__fixtures__/module-with-joiner-config"
|
||||
),
|
||||
definition: {
|
||||
key: "module-without-joiner-config",
|
||||
registrationName: "service",
|
||||
label: "Module without joiner config",
|
||||
defaultPackage: false,
|
||||
defaultModuleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "shared",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
expect(
|
||||
(ModuleService.prototype as IModuleService).__joinerConfig
|
||||
).toBeDefined()
|
||||
|
||||
const resources = await loadResources(moduleResolution)
|
||||
|
||||
expect(resources).toBeDefined()
|
||||
expect(resources.services).toHaveLength(1)
|
||||
expect(resources.services[0]).toEqual(ModuleService)
|
||||
expect(resources.models).toHaveLength(2)
|
||||
expect(resources.models).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ name: upperCaseFirst(EntityModel.name) }),
|
||||
expect.objectContaining({ name: upperCaseFirst(Entity2.name) }),
|
||||
])
|
||||
)
|
||||
expect(resources.repositories).toHaveLength(0)
|
||||
expect(resources.loaders).toHaveLength(2)
|
||||
expect(resources.loaders).toEqual([
|
||||
expect.objectContaining({ name: "connectionLoader" }),
|
||||
expect.objectContaining({ name: "containerLoader" }),
|
||||
])
|
||||
expect(resources.moduleService).toEqual(ModuleService)
|
||||
|
||||
const generatedJoinerConfig = (
|
||||
resources.moduleService.prototype as IModuleService
|
||||
).__joinerConfig()
|
||||
|
||||
expect(generatedJoinerConfig).toEqual({
|
||||
serviceName: "module-service",
|
||||
primaryKeys: ["id"],
|
||||
linkableKeys: {},
|
||||
alias: [
|
||||
{
|
||||
name: ["custom_name"],
|
||||
args: {
|
||||
entity: "Custom",
|
||||
methodSuffix: "Customs",
|
||||
},
|
||||
},
|
||||
],
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -10,9 +10,12 @@ import {
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
createMedusaContainer,
|
||||
createMikrORMEntity,
|
||||
defineJoinerConfig,
|
||||
DmlEntity,
|
||||
MedusaModuleType,
|
||||
ModulesSdkUtils,
|
||||
createMedusaContainer,
|
||||
} from "@medusajs/utils"
|
||||
import { asFunction, asValue } from "awilix"
|
||||
import { statSync } from "fs"
|
||||
@@ -78,9 +81,9 @@ export async function loadInternalModule(
|
||||
|
||||
if (resolution.resolutionPath) {
|
||||
moduleResources = await loadResources(
|
||||
loadedModule?.loaders ?? [],
|
||||
resolution,
|
||||
logger
|
||||
logger,
|
||||
loadedModule?.loaders ?? []
|
||||
)
|
||||
}
|
||||
|
||||
@@ -180,9 +183,9 @@ export async function loadModuleMigrations(
|
||||
// Generate migration scripts if they are not present
|
||||
if (!runMigrations || !revertMigration) {
|
||||
const moduleResources = await loadResources(
|
||||
loadedModule?.loaders ?? [],
|
||||
resolution,
|
||||
console as unknown as Logger
|
||||
console as unknown as Logger,
|
||||
loadedModule?.loaders ?? []
|
||||
)
|
||||
|
||||
const migrationScriptOptions = {
|
||||
@@ -240,10 +243,10 @@ async function importAllFromDir(path: string) {
|
||||
})
|
||||
}
|
||||
|
||||
async function loadResources(
|
||||
loadedModuleLoaders: ModuleLoaderFunction[],
|
||||
export async function loadResources(
|
||||
moduleResolution: ModuleResolution,
|
||||
logger: Logger
|
||||
logger: Logger = console as unknown as Logger,
|
||||
loadedModuleLoaders?: ModuleLoaderFunction[]
|
||||
): Promise<ModuleResource> {
|
||||
let modulePath = moduleResolution.resolutionPath as string
|
||||
let normalizedPath = modulePath
|
||||
@@ -267,12 +270,21 @@ async function loadResources(
|
||||
),
|
||||
])
|
||||
|
||||
const entityBuilder = createMikrORMEntity()
|
||||
const cleanupResources = (resources) => {
|
||||
return Object.values(resources).filter(
|
||||
(resource): resource is Function => {
|
||||
return typeof resource === "function"
|
||||
}
|
||||
)
|
||||
return Object.values(resources)
|
||||
.map((resource) => {
|
||||
if (DmlEntity.isDmlEntity(resource)) {
|
||||
return entityBuilder(resource as DmlEntity<any>)
|
||||
}
|
||||
|
||||
if (typeof resource === "function") {
|
||||
return resource
|
||||
}
|
||||
|
||||
return null
|
||||
})
|
||||
.filter((v): v is Function => !!v)
|
||||
}
|
||||
|
||||
const potentialServices = [...new Set(cleanupResources(services))]
|
||||
@@ -288,6 +300,12 @@ async function loadResources(
|
||||
migrationPath: normalizedPath + "/migrations",
|
||||
})
|
||||
|
||||
generateJoinerConfigIfNecessary({
|
||||
moduleResolution,
|
||||
service: moduleService,
|
||||
models: potentialModels,
|
||||
})
|
||||
|
||||
return {
|
||||
services: potentialServices,
|
||||
models: potentialModels,
|
||||
@@ -342,7 +360,7 @@ async function runLoaders(
|
||||
}
|
||||
|
||||
function prepareLoaders({
|
||||
loadedModuleLoaders,
|
||||
loadedModuleLoaders = [] as ModuleLoaderFunction[],
|
||||
models,
|
||||
repositories,
|
||||
services,
|
||||
@@ -407,3 +425,23 @@ function prepareLoaders({
|
||||
|
||||
return finalLoaders
|
||||
}
|
||||
|
||||
function generateJoinerConfigIfNecessary({
|
||||
moduleResolution,
|
||||
service,
|
||||
models,
|
||||
}: {
|
||||
moduleResolution: ModuleResolution
|
||||
service: Constructor<IModuleService>
|
||||
models: Function[]
|
||||
}) {
|
||||
if (service.prototype.__joinerConfig) {
|
||||
return
|
||||
}
|
||||
|
||||
service.prototype.__joinerConfig = function () {
|
||||
return defineJoinerConfig(moduleResolution.definition.key, {
|
||||
entityQueryingConfig: models,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user