diff --git a/packages/core/types/src/dml/index.ts b/packages/core/types/src/dml/index.ts index 05d29e9708..0032a6a792 100644 --- a/packages/core/types/src/dml/index.ts +++ b/packages/core/types/src/dml/index.ts @@ -159,6 +159,6 @@ export type InferTypeOf> = InstanceType> /** * Used in the module sdk internal service to infer propert entity typings from DML */ -export type ExtractEntityType = T extends IDmlEntity +export type InferEntityType = T extends IDmlEntity ? InferTypeOf : T diff --git a/packages/core/types/src/modules-sdk/medusa-internal-service.ts b/packages/core/types/src/modules-sdk/medusa-internal-service.ts index c4e7830652..04cebe171e 100644 --- a/packages/core/types/src/modules-sdk/medusa-internal-service.ts +++ b/packages/core/types/src/modules-sdk/medusa-internal-service.ts @@ -7,7 +7,7 @@ import { PerformedActions, UpsertWithReplaceConfig, } from "../dal" -import { ExtractEntityType } from "../dml" +import { InferEntityType } from "../dml" export interface IMedusaInternalService< TEntity extends {}, @@ -19,56 +19,50 @@ export interface IMedusaInternalService< idOrObject: string, config?: FindConfig, sharedContext?: Context - ): Promise> + ): Promise> retrieve( idOrObject: object, config?: FindConfig, sharedContext?: Context - ): Promise> + ): Promise> list( filters?: FilterQuery | BaseFilterable>, config?: FindConfig, sharedContext?: Context - ): Promise[]> + ): Promise[]> listAndCount( filters?: FilterQuery | BaseFilterable>, config?: FindConfig, sharedContext?: Context - ): Promise<[ExtractEntityType[], number]> + ): Promise<[InferEntityType[], number]> create( data: any[], sharedContext?: Context - ): Promise[]> - create( - data: any, - sharedContext?: Context - ): Promise> + ): Promise[]> + create(data: any, sharedContext?: Context): Promise> update( data: any[], sharedContext?: Context - ): Promise[]> - update( - data: any, - sharedContext?: Context - ): Promise> + ): Promise[]> + update(data: any, sharedContext?: Context): Promise> update( selectorAndData: { selector: FilterQuery | BaseFilterable> data: any }, sharedContext?: Context - ): Promise[]> + ): Promise[]> update( selectorAndData: { selector: FilterQuery | BaseFilterable> data: any }[], sharedContext?: Context - ): Promise[]> + ): Promise[]> delete(idOrSelector: string, sharedContext?: Context): Promise delete(idOrSelector: string[], sharedContext?: Context): Promise @@ -88,28 +82,25 @@ export interface IMedusaInternalService< | InternalFilterQuery | InternalFilterQuery[], sharedContext?: Context - ): Promise<[ExtractEntityType[], Record]> + ): Promise<[InferEntityType[], Record]> restore( idsOrFilter: string[] | InternalFilterQuery, sharedContext?: Context - ): Promise<[ExtractEntityType[], Record]> + ): Promise<[InferEntityType[], Record]> upsert( data: any[], sharedContext?: Context - ): Promise[]> - upsert( - data: any, - sharedContext?: Context - ): Promise> + ): Promise[]> + upsert(data: any, sharedContext?: Context): Promise> upsertWithReplace( data: any[], - config?: UpsertWithReplaceConfig>, + config?: UpsertWithReplaceConfig>, sharedContext?: Context ): Promise<{ - entities: ExtractEntityType[] + entities: InferEntityType[] performedActions: PerformedActions }> } diff --git a/packages/core/utils/src/modules-sdk/medusa-internal-service.ts b/packages/core/utils/src/modules-sdk/medusa-internal-service.ts index 707ac565b2..a548fb5772 100644 --- a/packages/core/utils/src/modules-sdk/medusa-internal-service.ts +++ b/packages/core/utils/src/modules-sdk/medusa-internal-service.ts @@ -1,10 +1,10 @@ import { BaseFilterable, Context, - ExtractEntityType, FilterQuery, FilterQuery as InternalFilterQuery, FindConfig, + InferEntityType, ModulesSdkTypes, PerformedActions, UpsertWithReplaceConfig, @@ -101,9 +101,9 @@ export function MedusaInternalService( @InjectManager(propertyRepositoryName) async retrieve( idOrObject: string | object, - config: FindConfig> = {}, + config: FindConfig> = {}, @MedusaContext() sharedContext: Context = {} - ): Promise> { + ): Promise> { const primaryKeys = AbstractService_.retrievePrimaryKeys(model) if ( @@ -163,7 +163,7 @@ export function MedusaInternalService( filters: FilterQuery | BaseFilterable> = {}, config: FindConfig = {}, @MedusaContext() sharedContext: Context = {} - ): Promise[]> { + ): Promise[]> { AbstractService_.applyDefaultOrdering(config) AbstractService_.applyFreeTextSearchFilter(filters, config) @@ -180,7 +180,7 @@ export function MedusaInternalService( filters: FilterQuery | BaseFilterable> = {}, config: FindConfig = {}, @MedusaContext() sharedContext: Context = {} - ): Promise<[ExtractEntityType[], number]> { + ): Promise<[InferEntityType[], number]> { AbstractService_.applyDefaultOrdering(config) AbstractService_.applyFreeTextSearchFilter(filters, config) @@ -195,21 +195,21 @@ export function MedusaInternalService( create( data: any, sharedContext?: Context - ): Promise> + ): Promise> create( data: any[], sharedContext?: Context - ): Promise[]> + ): Promise[]> @InjectTransactionManager(shouldForceTransaction, propertyRepositoryName) async create( data: any | any[], @MedusaContext() sharedContext: Context = {} - ): Promise | ExtractEntityType[]> { + ): Promise | InferEntityType[]> { if (!isDefined(data) || (Array.isArray(data) && data.length === 0)) { return (Array.isArray(data) ? [] : void 0) as - | ExtractEntityType - | ExtractEntityType[] + | InferEntityType + | InferEntityType[] } const data_ = Array.isArray(data) ? data : [data] @@ -224,29 +224,29 @@ export function MedusaInternalService( update( data: any[], sharedContext?: Context - ): Promise[]> + ): Promise[]> update( data: any, sharedContext?: Context - ): Promise> + ): Promise> update( selectorAndData: SelectorAndData, sharedContext?: Context - ): Promise[]> + ): Promise[]> update( selectorAndData: SelectorAndData[], sharedContext?: Context - ): Promise[]> + ): Promise[]> @InjectTransactionManager(shouldForceTransaction, propertyRepositoryName) async update( input: any | any[] | SelectorAndData | SelectorAndData[], @MedusaContext() sharedContext: Context = {} - ): Promise | ExtractEntityType[]> { + ): Promise | InferEntityType[]> { if (!isDefined(input) || (Array.isArray(input) && input.length === 0)) { return (Array.isArray(input) ? [] : void 0) as - | ExtractEntityType - | ExtractEntityType[] + | InferEntityType + | InferEntityType[] } const primaryKeys = AbstractService_.retrievePrimaryKeys(model) @@ -312,7 +312,7 @@ export function MedusaInternalService( // Only throw for missing entities when we dont have selectors involved as selector by design can return 0 entities if (entitiesToUpdate.length !== keySelectorDataMap.size) { const entityName = - (model as EntityClass>).name ?? model + (model as EntityClass>).name ?? model const compositeKeysValuesForFoundEntities = new Set( entitiesToUpdate.map((entity) => { @@ -465,7 +465,7 @@ export function MedusaInternalService( | InternalFilterQuery | InternalFilterQuery[], @MedusaContext() sharedContext: Context = {} - ): Promise<[ExtractEntityType[], Record]> { + ): Promise<[InferEntityType[], Record]> { if ( (Array.isArray(idsOrFilter) && !idsOrFilter.length) || (!Array.isArray(idsOrFilter) && !idsOrFilter) @@ -483,7 +483,7 @@ export function MedusaInternalService( async restore( idsOrFilter: string[] | InternalFilterQuery, @MedusaContext() sharedContext: Context = {} - ): Promise<[ExtractEntityType[], Record]> { + ): Promise<[InferEntityType[], Record]> { return await this[propertyRepositoryName].restore( idsOrFilter, sharedContext @@ -493,17 +493,17 @@ export function MedusaInternalService( upsert( data: any[], sharedContext?: Context - ): Promise[]> + ): Promise[]> upsert( data: any, sharedContext?: Context - ): Promise> + ): Promise> @InjectTransactionManager(propertyRepositoryName) async upsert( data: any | any[], @MedusaContext() sharedContext: Context = {} - ): Promise | ExtractEntityType[]> { + ): Promise | InferEntityType[]> { const data_ = Array.isArray(data) ? data : [data] const entities = await this[propertyRepositoryName].upsert( data_, @@ -514,30 +514,30 @@ export function MedusaInternalService( upsertWithReplace( data: any[], - config?: UpsertWithReplaceConfig>, + config?: UpsertWithReplaceConfig>, sharedContext?: Context ): Promise<{ - entities: ExtractEntityType[] + entities: InferEntityType[] performedActions: PerformedActions }> upsertWithReplace( data: any, - config?: UpsertWithReplaceConfig>, + config?: UpsertWithReplaceConfig>, sharedContext?: Context ): Promise<{ - entities: ExtractEntityType + entities: InferEntityType performedActions: PerformedActions }> @InjectTransactionManager(propertyRepositoryName) async upsertWithReplace( data: any | any[], - config: UpsertWithReplaceConfig> = { + config: UpsertWithReplaceConfig> = { relations: [], }, @MedusaContext() sharedContext: Context = {} ): Promise<{ - entities: ExtractEntityType | ExtractEntityType[] + entities: InferEntityType | InferEntityType[] performedActions: PerformedActions }> { const data_ = Array.isArray(data) ? data : [data] diff --git a/packages/modules/currency/src/services/currency-module-service.ts b/packages/modules/currency/src/services/currency-module-service.ts index 2d7e3ee579..8e07dbaa73 100644 --- a/packages/modules/currency/src/services/currency-module-service.ts +++ b/packages/modules/currency/src/services/currency-module-service.ts @@ -17,7 +17,7 @@ import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config" type InjectedDependencies = { baseRepository: DAL.RepositoryService - currencyService: ModulesSdkTypes.IMedusaInternalService + currencyService: ModulesSdkTypes.IMedusaInternalService } export default class CurrencyModuleService