Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: convert MikroORM entities to DML entities #10043

Merged
merged 106 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
106 commits
Select commit Hold shift + click to select a range
57a2562
feat: convert MikroORM entities to DML entities
thetutlage Nov 12, 2024
27ea828
feat: wip on repository changes
thetutlage Nov 12, 2024
afd19d2
continue repositories and types rework
adrien2p Nov 12, 2024
693c016
fix order repository usage
adrien2p Nov 12, 2024
b42b180
continue to update product category repository
adrien2p Nov 12, 2024
34c8b4c
Add foreign key as part of the inferred DML type
adrien2p Nov 12, 2024
74f67f0
../../core/types/src/dml/index.ts
adrien2p Nov 12, 2024
af6ea07
../../core/types/src/dml/index.ts
adrien2p Nov 12, 2024
9200ccf
Merge branch 'develop' into feat/product-module-dml-migration
thetutlage Nov 13, 2024
5f57aec
fix: relationships mapping
thetutlage Nov 13, 2024
4a4c9a1
handle nullable foreign keys types
adrien2p Nov 13, 2024
f2c6f7e
handle nullable foreign keys types
adrien2p Nov 13, 2024
8e18193
handle nullable foreign keys types
adrien2p Nov 13, 2024
b28e016
continue to update product category repository
adrien2p Nov 13, 2024
a6a02c2
fix all product category repositories issues
adrien2p Nov 13, 2024
7c46858
fix product category service types
adrien2p Nov 13, 2024
9286e06
fix product module service types
adrien2p Nov 13, 2024
1db0746
fix product module service types
adrien2p Nov 13, 2024
c752d11
fix repository template type
adrien2p Nov 13, 2024
ba6bf6e
refactor: use a singleton DMLToMikroORM factory instance
thetutlage Nov 14, 2024
ce5e494
refactor: update product module to use DML in tests
thetutlage Nov 14, 2024
7512300
wip: tests
thetutlage Nov 14, 2024
5c0ad12
WIP product linkable fixes
adrien2p Nov 14, 2024
e6ec086
continue type fixing and start test fixing
adrien2p Nov 14, 2024
7334c33
test: fix more tests
thetutlage Nov 14, 2024
19ac9d4
fix repository
adrien2p Nov 14, 2024
7f4fd67
fix pivot table computaion + fix mikro orm repository
adrien2p Nov 14, 2024
8d53b32
fix many to many management and configuration
adrien2p Nov 14, 2024
ddb994a
fix many to many management and configuration
adrien2p Nov 14, 2024
dd5f67e
fix many to many management and configuration
adrien2p Nov 15, 2024
f77402d
update product tag relation configuration
adrien2p Nov 15, 2024
ec677fb
Introduce experimental dml hooks to fix some issues with categories
adrien2p Nov 15, 2024
c750b81
more fixes
adrien2p Nov 15, 2024
25ac61c
fix product tests
adrien2p Nov 15, 2024
bb8aaed
add missing id prefixes
adrien2p Nov 15, 2024
17fef8c
fix product category handle management
adrien2p Nov 15, 2024
b80e090
test: fix more failing tests
thetutlage Nov 15, 2024
3ab6945
test: make it all green
thetutlage Nov 15, 2024
7213140
resolve conflicts
adrien2p Nov 15, 2024
7ff10fb
Merge branch 'develop' into feat/product-module-dml-migration
thetutlage Nov 18, 2024
1c962b0
Merge branch 'develop' into feat/product-module-dml-migration
thetutlage Nov 19, 2024
f9e75c8
test: fix breaking tests
thetutlage Nov 19, 2024
ce2c02a
Merge branch 'develop' into feat/product-module-dml-migration
thetutlage Nov 19, 2024
a30cbf6
Merge branch 'develop' into feat/product-module-dml-migration
thetutlage Nov 19, 2024
8975cfb
fix: build issues
thetutlage Nov 19, 2024
1fd1f1b
Merge branch 'develop' into feat/product-module-dml-migration
thetutlage Nov 19, 2024
09ad177
fix: build issues
thetutlage Nov 19, 2024
4a33215
Merge branch 'develop' into feat/product-module-dml-migration
thetutlage Nov 19, 2024
7b981ab
fix: more breaking tests
thetutlage Nov 19, 2024
c87d157
Merge branch 'develop' into feat/product-module-dml-migration
thetutlage Nov 19, 2024
348b53d
refactor: fix issues after merge
thetutlage Nov 19, 2024
f589f7c
refactor: fix issues after merge
thetutlage Nov 19, 2024
44b06ad
Merge branch 'develop' into feat/product-module-dml-migration
thetutlage Nov 20, 2024
6452a64
refactor: surpress types error
thetutlage Nov 20, 2024
f5ebc5d
test: fix DML failing tests
thetutlage Nov 20, 2024
3aac614
improve many to many inference + tests
adrien2p Nov 20, 2024
c8c7682
resolve conflicts
adrien2p Nov 20, 2024
7241d91
Wip fix columns from product entity
adrien2p Nov 20, 2024
2ced83b
remove product model before create hook and manage handle validation …
adrien2p Nov 20, 2024
09551c3
test: fix breaking unit tests
thetutlage Nov 21, 2024
6d33f52
Merge branch 'develop' into feat/product-module-dml-migration
thetutlage Nov 21, 2024
ed8d0ec
fix: product module service to not update handle on product update
thetutlage Nov 21, 2024
06b1b28
fix define link and joiner config
adrien2p Nov 21, 2024
ecc5c1d
Merge branch 'develop' into feat/product-module-dml-migration
adrien2p Nov 21, 2024
933ea9a
test: fix joiner config test
thetutlage Nov 21, 2024
79301f2
test: fix joiner config test
thetutlage Nov 21, 2024
52c214b
fix joiner config primary keys
adrien2p Nov 21, 2024
7a7cee2
Merge branch 'develop' into feat/product-module-dml-migration
adrien2p Nov 21, 2024
9c0d566
Fix joiner config builder
adrien2p Nov 21, 2024
4172582
Merge branch 'develop' into feat/product-module-dml-migration
adrien2p Nov 21, 2024
4986cae
Fix joiner config builder
adrien2p Nov 21, 2024
71eaaae
Merge branch 'develop' into feat/product-module-dml-migration
adrien2p Nov 21, 2024
c2914c6
test: remove only modifier from test
thetutlage Nov 22, 2024
402fe53
Merge branch 'develop' into feat/product-module-dml-migration
thetutlage Nov 22, 2024
d033343
refactor: remove hooks usage from product collection
thetutlage Nov 22, 2024
6fb31fb
Merge branch 'develop' into feat/product-module-dml-migration
thetutlage Nov 22, 2024
e5bf1c8
refactor: remove hooks usage from product-option
thetutlage Nov 22, 2024
819b4c1
refactor: remove hooks usage for computing category handle
thetutlage Nov 22, 2024
b127543
refactor: remove hooks usage from productCategory model
thetutlage Nov 22, 2024
4094dda
refactor: remove hooks from DML
thetutlage Nov 22, 2024
b38f85e
refactor: remove cruft
thetutlage Nov 22, 2024
d6b994b
Merge branch 'develop' into feat/product-module-dml-migration
thetutlage Nov 22, 2024
b700c63
cleanup
adrien2p Nov 22, 2024
21ab7a1
re add foerign key indexes
adrien2p Nov 22, 2024
1ac18ee
Merge branch 'develop' into feat/product-module-dml-migration
thetutlage Nov 22, 2024
4a831fd
chore: remove unused types
thetutlage Nov 22, 2024
962ac0d
Merge branch 'develop' into feat/product-module-dml-migration
thetutlage Nov 22, 2024
0ca6939
Merge branch 'develop' into feat/product-module-dml-migration
thetutlage Nov 22, 2024
af8a543
Merge branch 'develop' into feat/product-module-dml-migration
thetutlage Nov 25, 2024
bfe6702
refactor: cleanup
thetutlage Nov 25, 2024
bf63323
Merge branch 'develop' into feat/product-module-dml-migration
thetutlage Nov 25, 2024
974be36
migration and models configuration adjustments
adrien2p Nov 25, 2024
35a6080
resolve conflicts
adrien2p Nov 25, 2024
dc29b25
cleanup
adrien2p Nov 25, 2024
e0d3ac0
fix random ordering
adrien2p Nov 25, 2024
6cc267b
fix
adrien2p Nov 25, 2024
f49c6c3
Merge branch 'develop' into feat/product-module-dml-migration
adrien2p Nov 25, 2024
25d9f27
Merge branch 'develop' into feat/product-module-dml-migration
thetutlage Nov 25, 2024
7c1631a
test: fix product-category tests
thetutlage Nov 25, 2024
206254f
test: update breaking DML tests
thetutlage Nov 25, 2024
35b1591
test: array assertion to not care about ordering
thetutlage Nov 25, 2024
9fb0549
fix: temporarily apply id ordering for products
thetutlage Nov 25, 2024
9b814d8
Merge branch 'develop' into feat/product-module-dml-migration
olivermrbl Nov 25, 2024
6dc6cd4
fix ordering
adrien2p Nov 26, 2024
1d06419
fix ordering remove logs
adrien2p Nov 26, 2024
95c3559
Merge branch 'develop' into feat/product-module-dml-migration
adrien2p Nov 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ medusaIntegrationTestRunner({
// BREAKING: Type input changed from {type: {value: string}} to {type_id: string}
type_id: baseType.id,
tags: [{ id: baseTag1.id }, { id: baseTag2.id }],
images: [{
images: [
{
url: "image-one",
},
{
Expand Down Expand Up @@ -139,7 +140,6 @@ medusaIntegrationTestRunner({
])
)
})


it("returns a list of products with all statuses when no status or invalid status is provided", async () => {
const res = await api
Expand Down Expand Up @@ -991,7 +991,10 @@ medusaIntegrationTestRunner({
})

it("should get a product with images ordered by rank", async () => {
const res = await api.get(`/admin/products/${baseProduct.id}`, adminHeaders)
const res = await api.get(
`/admin/products/${baseProduct.id}`,
adminHeaders
)

expect(res.data.product.images).toEqual(
expect.arrayContaining([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1436,7 +1436,7 @@ medusaIntegrationTestRunner({
},
{
url: "image-two",
}
},
],
})

Expand Down Expand Up @@ -1487,7 +1487,10 @@ medusaIntegrationTestRunner({
})

it("should retrieve product with images ordered by rank", async () => {
const response = await api.get(`/store/products/${product.id}`, storeHeaders)
const response = await api.get(
`/store/products/${product.id}`,
storeHeaders
)

expect(response.data.product.images).toEqual(
expect.arrayContaining([
Expand Down
10 changes: 6 additions & 4 deletions packages/core/types/src/dal/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { InferEntityType } from "../dml"
import { Dictionary, FilterQuery, Order } from "./utils"

export { FilterQuery, OperatorMap } from "./utils"
Expand All @@ -22,7 +23,7 @@ export interface BaseFilterable<T> {
/**
* The options to apply when retrieving an item.
*/
export interface OptionsQuery<T, P extends string = never> {
export interface OptionsQuery<T> {
/**
* Relations to populate in the retrieved items.
*/
Expand Down Expand Up @@ -54,7 +55,7 @@ export interface OptionsQuery<T, P extends string = never> {
/**
* Load strategy (e.g for mikro orm it accept select-in or joined)
*/
strategy?: 'select-in' | 'joined' | string & {}
strategy?: "select-in" | "joined" | (string & {})
}

/**
Expand All @@ -66,12 +67,13 @@ export type FindOptions<T = any> = {
/**
* The filters to apply on the items.
*/
where: FilterQuery<T> & BaseFilterable<FilterQuery<T>>
where: FilterQuery<InferEntityType<T>> &
BaseFilterable<FilterQuery<InferEntityType<T>>>

/**
* The options to apply when retrieving the items.
*/
options?: OptionsQuery<T, any>
options?: OptionsQuery<InferEntityType<T>>
}

/**
Expand Down
65 changes: 43 additions & 22 deletions packages/core/types/src/dal/repository-service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { RepositoryTransformOptions } from "../common"
import { Context } from "../shared-context"
import {
BaseFilterable,
FilterQuery,
FilterQuery as InternalFilterQuery,
FindOptions,
UpsertWithReplaceConfig,
} from "./index"
import { EntityClass } from "@mikro-orm/core"
import { IDmlEntity, InferTypeOf } from "../dml"

type EntityClassName = string
type EntityValues = { id: string }[]

/**
* Either infer the properties from a DML object or from a Mikro orm class prototype.
*/
export type InferRepositoryReturnType<T> = T extends IDmlEntity<any, any>
? InferTypeOf<T>
: EntityClass<T>["prototype"]

export type PerformedActions = {
created: Record<EntityClassName, EntityValues>
updated: Record<EntityClassName, EntityValues>
Expand All @@ -22,7 +29,7 @@ export type PerformedActions = {
* This layer helps to separate the business logic (service layer) from accessing the
* ORM directly and allows to switch to another ORM without changing the business logic.
*/
interface BaseRepositoryService<T = any> {
interface BaseRepositoryService {
transaction<TManager = unknown>(
task: (transactionManager: TManager) => Promise<any>,
context?: {
Expand All @@ -42,22 +49,28 @@ interface BaseRepositoryService<T = any> {
): Promise<TOutput>
}

export interface RepositoryService<T = any> extends BaseRepositoryService<T> {
find(options?: FindOptions<T>, context?: Context): Promise<T[]>
export interface RepositoryService<T = any> extends BaseRepositoryService {
find(
options?: FindOptions<T>,
context?: Context
): Promise<InferRepositoryReturnType<T>[]>

findAndCount(
options?: FindOptions<T>,
context?: Context
): Promise<[T[], number]>

create(data: any[], context?: Context): Promise<T[]>
): Promise<[InferRepositoryReturnType<T>[], number]>

update(data: { entity; update }[], context?: Context): Promise<T[]>
create(
data: any[],
context?: Context
): Promise<InferRepositoryReturnType<T>[]>

delete(
idsOrPKs: FilterQuery<T> & BaseFilterable<FilterQuery<T>>,
update(
data: { entity; update }[],
context?: Context
): Promise<void>
): Promise<InferRepositoryReturnType<T>[]>

delete(idsOrPKs: FindOptions<T>["where"], context?: Context): Promise<void>

/**
* Soft delete entities and cascade to related entities if configured.
Expand All @@ -74,37 +87,45 @@ export interface RepositoryService<T = any> extends BaseRepositoryService<T> {
| InternalFilterQuery
| InternalFilterQuery[],
context?: Context
): Promise<[T[], Record<string, unknown[]>]>
): Promise<[InferRepositoryReturnType<T>[], Record<string, unknown[]>]>

restore(
idsOrFilter: string[] | InternalFilterQuery,
context?: Context
): Promise<[T[], Record<string, unknown[]>]>
): Promise<[InferRepositoryReturnType<T>[], Record<string, unknown[]>]>

upsert(data: any[], context?: Context): Promise<T[]>
upsert(
data: any[],
context?: Context
): Promise<InferRepositoryReturnType<T>[]>

upsertWithReplace(
data: any[],
config?: UpsertWithReplaceConfig<T>,
config?: UpsertWithReplaceConfig<InferRepositoryReturnType<T>>,
context?: Context
): Promise<{ entities: T[]; performedActions: PerformedActions }>
): Promise<{
entities: InferRepositoryReturnType<T>[]
performedActions: PerformedActions
}>
}

export interface TreeRepositoryService<T = any>
extends BaseRepositoryService<T> {
export interface TreeRepositoryService<T = any> extends BaseRepositoryService {
find(
options?: FindOptions<T>,
transformOptions?: RepositoryTransformOptions,
context?: Context
): Promise<T[]>
): Promise<InferRepositoryReturnType<T>[]>

findAndCount(
options?: FindOptions<T>,
transformOptions?: RepositoryTransformOptions,
context?: Context
): Promise<[T[], number]>
): Promise<[InferRepositoryReturnType<T>[], number]>

create(data: unknown[], context?: Context): Promise<T[]>
create(
data: unknown[],
context?: Context
): Promise<InferRepositoryReturnType<T>[]>

delete(ids: string[], context?: Context): Promise<void>
}
Expand Down
40 changes: 23 additions & 17 deletions packages/core/types/src/dal/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Constructor } from "../modules-sdk"

type ExpandProperty<T> = T extends (infer U)[] ? NonNullable<U> : NonNullable<T>

export type Dictionary<T = any> = {
Expand Down Expand Up @@ -84,25 +86,29 @@ type FilterValue<T> =

type PrevLimit = [never, 0, 1, 2]

export type FilterQueryProperties<T, Prev extends number = 3> = {
[Key in keyof T]?: T[Key] extends
| boolean
| number
| string
| bigint
| symbol
| Date
? T[Key] | OperatorMap<T[Key]>
: T[Key] extends infer U
? U extends { [x: number]: infer V }
? V extends object
? FilterQuery<Partial<V>, PrevLimit[Prev]>
: never
: never
: never
}

export type FilterQuery<T = any, Prev extends number = 3> = Prev extends never
? never
: {
[Key in keyof T]?: T[Key] extends
| boolean
| number
| string
| bigint
| symbol
| Date
? T[Key] | OperatorMap<T[Key]>
: T[Key] extends infer U
? U extends { [x: number]: infer V }
? V extends object
? FilterQuery<Partial<V>, PrevLimit[Prev]>
: never
: never
: never
}
: T extends Constructor<infer Prototype>
? FilterQueryProperties<Prototype, Prev>
: FilterQueryProperties<T, Prev>

declare type QueryOrder = "ASC" | "DESC" | "asc" | "desc"

Expand Down
49 changes: 28 additions & 21 deletions packages/core/types/src/dml/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ export type RelationshipTypes =
| "belongsTo"
| "manyToMany"

/**
* Return true if the relationship is nullable
*/
export type IsNullableRelation<T> = T extends () => IDmlEntity<any, any> | null
? true
: false

/**
* The meta-data returned by the property parse method
*/
Expand Down Expand Up @@ -135,14 +142,12 @@ export interface EntityConstructor<Props> extends Function {
* "belongsTo" relation meaning "hasOne" and "ManyToOne"
*/
export type InferForeignKeys<Schema extends DMLSchema> = {
[K in keyof Schema as Schema[K] extends { type: infer Type }
? Type extends RelationshipTypes
? `${K & string}_id`
: never
: never]: Schema[K] extends { type: infer Type }
? Type extends RelationshipTypes
? string
: never
[K in keyof Schema as Schema[K] extends { type: "belongsTo" }
? `${K & string}_id`
: never]: Schema[K] extends { type: "belongsTo" }
? null extends Schema[K]["$dataType"]
? string | null
: string
: never
}

Expand Down Expand Up @@ -182,19 +187,21 @@ export type InferManyToManyFields<Relation> = InferHasManyFields<Relation>
* Inferring the types of the schema fields from the DML
* entity
*/
export type InferSchemaFields<Schema extends DMLSchema> = Prettify<{
[K in keyof Schema]: Schema[K] extends RelationshipType<any>
? Schema[K]["type"] extends "belongsTo"
? InferBelongsToFields<Schema[K]["$dataType"]>
: Schema[K]["type"] extends "hasOne"
? InferHasOneFields<Schema[K]["$dataType"]>
: Schema[K]["type"] extends "hasMany"
? InferHasManyFields<Schema[K]["$dataType"]>
: Schema[K]["type"] extends "manyToMany"
? InferManyToManyFields<Schema[K]["$dataType"]>
: never
: Schema[K]["$dataType"]
}>
export type InferSchemaFields<Schema extends DMLSchema> = Prettify<
{
[K in keyof Schema]: Schema[K] extends RelationshipType<any>
? Schema[K]["type"] extends "belongsTo"
? InferBelongsToFields<Schema[K]["$dataType"]>
: Schema[K]["type"] extends "hasOne"
? InferHasOneFields<Schema[K]["$dataType"]>
: Schema[K]["type"] extends "hasMany"
? InferHasManyFields<Schema[K]["$dataType"]>
: Schema[K]["type"] extends "manyToMany"
? InferManyToManyFields<Schema[K]["$dataType"]>
: never
: Schema[K]["$dataType"]
} & InferForeignKeys<Schema>
>

/**
* Helper to infer the schema type of a DmlEntity
Expand Down
2 changes: 1 addition & 1 deletion packages/core/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"scripts": {
"build": "rimraf dist && tsc --build",
"watch": "tsc --build --watch",
"test": "jest --silent=false --bail --maxWorkers=50% --forceExit --testPathIgnorePatterns='/integration-tests/' -- src/**/__tests__/**/*.ts",
"test": "jest --silent --bail --maxWorkers=50% --forceExit --testPathIgnorePatterns='/integration-tests/' -- src/**/__tests__/**/*.ts",
"test:integration": "jest --silent --bail --runInBand --forceExit -- src/**/integration-tests/__tests__/**/*.ts"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ class Entity3 {
}
}

const Entity1Repository = mikroOrmBaseRepositoryFactory<Entity1>(Entity1)
const Entity2Repository = mikroOrmBaseRepositoryFactory<Entity2>(Entity2)
const Entity3Repository = mikroOrmBaseRepositoryFactory<Entity3>(Entity3)
const Entity1Repository = mikroOrmBaseRepositoryFactory(Entity1)
const Entity2Repository = mikroOrmBaseRepositoryFactory(Entity2)
const Entity3Repository = mikroOrmBaseRepositoryFactory(Entity3)

describe("mikroOrmRepository", () => {
let orm!: MikroORM
Expand Down
Loading
Loading