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:
Adrien de Peretti
2024-06-20 18:18:07 +02:00
committed by GitHub
parent 03924a4ff6
commit 937a632eb6
191 changed files with 1105 additions and 613 deletions
@@ -3,6 +3,7 @@ import { MetadataStorage } from "@mikro-orm/core"
import { EntityConstructor } from "../types"
import { EntityBuilder } from "../entity-builder"
import { createMikrORMEntity } from "../helpers/create-mikro-orm-entity"
import { DmlEntity } from "../entity"
describe("Entity builder", () => {
beforeEach(() => {
@@ -10,6 +11,21 @@ describe("Entity builder", () => {
})
describe("Entity builder | properties", () => {
test("should identify a DML entity correctly", () => {
const model = new EntityBuilder()
const user = model.define("user", {
id: model.number(),
username: model.text(),
email: model.text(),
})
expect(DmlEntity.isDmlEntity(user)).toBe(true)
const nonDmlEntity = new Object()
expect(DmlEntity.isDmlEntity(nonDmlEntity)).toBe(false)
})
test("define an entity", () => {
const model = new EntityBuilder()
const user = model.define("user", {
@@ -10,9 +10,9 @@ import { BelongsTo } from "./relations/belongs-to"
import { DateTimeProperty } from "./properties/date-time"
import { ManyToMany } from "./relations/many-to-many"
import type {
PropertyType,
RelationshipOptions,
RelationshipType,
PropertyType,
} from "./types"
import { NullableModifier } from "./properties/nullable"
import { IdProperty } from "./properties/id"
@@ -179,3 +179,5 @@ export class EntityBuilder {
return new ManyToMany<T>(entityBuilder, options || {})
}
}
export const model = new EntityBuilder()
+21 -2
View File
@@ -1,11 +1,13 @@
import { BelongsTo } from "./relations/belongs-to"
import {
PropertyType,
EntityCascades,
RelationshipType,
ExtractEntityRelations,
PropertyType,
RelationshipType,
} from "./types"
const IsDmlEntity = Symbol("isDmlEntity")
/**
* Dml entity is a representation of a DML model with a unique
* name, its schema and relationships.
@@ -13,9 +15,26 @@ import {
export class DmlEntity<
Schema extends Record<string, PropertyType<any> | RelationshipType<any>>
> {
[IsDmlEntity] = true
#cascades: EntityCascades<string[]> = {}
constructor(public name: string, public schema: Schema) {}
/**
* A static method to check if an entity is an instance of DmlEntity.
* It allows us to identify a specific object as being an instance of
* DmlEntity.
*
* @param entity
*/
static isDmlEntity(entity: unknown): entity is DmlEntity<any> {
return (
!!entity &&
(entity instanceof DmlEntity ||
(typeof entity === "object" && entity[IsDmlEntity] === true))
)
}
/**
* Parse entity to get its underlying information
*/
@@ -618,3 +618,5 @@ export function createMikrORMEntity() {
) as Infer<T>
}
}
export const toMikroORMEntity = createMikrORMEntity()
+1
View File
@@ -1,2 +1,3 @@
export * from "./entity-builder"
export * from "./entity"
export * from "./helpers/create-mikro-orm-entity"
@@ -22,6 +22,7 @@ import {
} from "../common"
import { InjectManager, MedusaContext } from "./decorators"
import { ModuleRegistrationName } from "./definition"
import { DmlEntity } from "../dml"
type BaseMethods =
| "retrieve"
@@ -83,16 +84,20 @@ type ExtractSingularName<T extends Record<any, any>, K = keyof T> = Capitalize<
* @deprecated should all notion of singular and plural be removed once all modules are aligned with the convention
* The pluralize will move to where it should be used instead
*/
type ExtractPluralName<T extends Record<any, any>, K = keyof T> = T[K] extends {
plural?: string
}
? T[K]["plural"] & string
: Pluralize<K & string>
type ExtractPluralName<T extends Record<any, any>, K = keyof T> = Capitalize<
T[K] extends {
plural?: string
}
? T[K]["plural"] & string
: Pluralize<K & string>
>
// TODO: The future expected entry will be a DML object but in the meantime we have to maintain backward compatibility for ouw own modules and therefore we need to support Constructor<any> as well as this temporary object
type TEntityEntries<Keys = string> = Record<
Keys & string,
Constructor<any> | { name?: string; singular?: string; plural?: string }
| Constructor<any>
| DmlEntity<any>
| { name?: string; singular?: string; plural?: string }
>
type ExtractKeysFromConfig<EntitiesConfig> = EntitiesConfig extends {