chore: Internal medusa service proper typings with DML (#7792)
This commit is contained in:
committed by
GitHub
parent
944051a951
commit
90e6ca0e9e
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
BaseFilterable,
|
||||
Context,
|
||||
ExtractEntityType,
|
||||
FilterQuery,
|
||||
FilterQuery as InternalFilterQuery,
|
||||
FindConfig,
|
||||
@@ -100,9 +101,9 @@ export function MedusaInternalService<TContainer extends object = object>(
|
||||
@InjectManager(propertyRepositoryName)
|
||||
async retrieve(
|
||||
idOrObject: string | object,
|
||||
config: FindConfig<TEntity> = {},
|
||||
config: FindConfig<ExtractEntityType<TEntity>> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity> {
|
||||
): Promise<ExtractEntityType<TEntity>> {
|
||||
const primaryKeys = AbstractService_.retrievePrimaryKeys(model)
|
||||
|
||||
if (
|
||||
@@ -162,7 +163,7 @@ export function MedusaInternalService<TContainer extends object = object>(
|
||||
filters: FilterQuery<any> | BaseFilterable<FilterQuery<any>> = {},
|
||||
config: FindConfig<any> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
): Promise<ExtractEntityType<TEntity>[]> {
|
||||
AbstractService_.applyDefaultOrdering(config)
|
||||
AbstractService_.applyFreeTextSearchFilter(filters, config)
|
||||
|
||||
@@ -179,7 +180,7 @@ export function MedusaInternalService<TContainer extends object = object>(
|
||||
filters: FilterQuery<any> | BaseFilterable<FilterQuery<any>> = {},
|
||||
config: FindConfig<any> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], number]> {
|
||||
): Promise<[ExtractEntityType<TEntity>[], number]> {
|
||||
AbstractService_.applyDefaultOrdering(config)
|
||||
AbstractService_.applyFreeTextSearchFilter(filters, config)
|
||||
|
||||
@@ -191,16 +192,24 @@ export function MedusaInternalService<TContainer extends object = object>(
|
||||
)
|
||||
}
|
||||
|
||||
create(data: any, sharedContext?: Context): Promise<TEntity>
|
||||
create(data: any[], sharedContext?: Context): Promise<TEntity[]>
|
||||
create(
|
||||
data: any,
|
||||
sharedContext?: Context
|
||||
): Promise<ExtractEntityType<TEntity>>
|
||||
create(
|
||||
data: any[],
|
||||
sharedContext?: Context
|
||||
): Promise<ExtractEntityType<TEntity>[]>
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, propertyRepositoryName)
|
||||
async create(
|
||||
data: any | any[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity | TEntity[]> {
|
||||
): Promise<ExtractEntityType<TEntity> | ExtractEntityType<TEntity>[]> {
|
||||
if (!isDefined(data) || (Array.isArray(data) && data.length === 0)) {
|
||||
return (Array.isArray(data) ? [] : void 0) as TEntity | TEntity[]
|
||||
return (Array.isArray(data) ? [] : void 0) as
|
||||
| ExtractEntityType<TEntity>
|
||||
| ExtractEntityType<TEntity>[]
|
||||
}
|
||||
|
||||
const data_ = Array.isArray(data) ? data : [data]
|
||||
@@ -212,24 +221,32 @@ export function MedusaInternalService<TContainer extends object = object>(
|
||||
return Array.isArray(data) ? entities : entities[0]
|
||||
}
|
||||
|
||||
update(data: any[], sharedContext?: Context): Promise<TEntity[]>
|
||||
update(data: any, sharedContext?: Context): Promise<TEntity>
|
||||
update(
|
||||
data: any[],
|
||||
sharedContext?: Context
|
||||
): Promise<ExtractEntityType<TEntity>[]>
|
||||
update(
|
||||
data: any,
|
||||
sharedContext?: Context
|
||||
): Promise<ExtractEntityType<TEntity>>
|
||||
update(
|
||||
selectorAndData: SelectorAndData,
|
||||
sharedContext?: Context
|
||||
): Promise<TEntity[]>
|
||||
): Promise<ExtractEntityType<TEntity>[]>
|
||||
update(
|
||||
selectorAndData: SelectorAndData[],
|
||||
sharedContext?: Context
|
||||
): Promise<TEntity[]>
|
||||
): Promise<ExtractEntityType<TEntity>[]>
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, propertyRepositoryName)
|
||||
async update(
|
||||
input: any | any[] | SelectorAndData | SelectorAndData[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity | TEntity[]> {
|
||||
): Promise<ExtractEntityType<TEntity> | ExtractEntityType<TEntity>[]> {
|
||||
if (!isDefined(input) || (Array.isArray(input) && input.length === 0)) {
|
||||
return (Array.isArray(input) ? [] : void 0) as TEntity | TEntity[]
|
||||
return (Array.isArray(input) ? [] : void 0) as
|
||||
| ExtractEntityType<TEntity>
|
||||
| ExtractEntityType<TEntity>[]
|
||||
}
|
||||
|
||||
const primaryKeys = AbstractService_.retrievePrimaryKeys(model)
|
||||
@@ -294,7 +311,8 @@ export function MedusaInternalService<TContainer extends object = object>(
|
||||
|
||||
// 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<TEntity>).name ?? model
|
||||
const entityName =
|
||||
(model as EntityClass<ExtractEntityType<TEntity>>).name ?? model
|
||||
|
||||
const compositeKeysValuesForFoundEntities = new Set(
|
||||
entitiesToUpdate.map((entity) => {
|
||||
@@ -447,7 +465,7 @@ export function MedusaInternalService<TContainer extends object = object>(
|
||||
| InternalFilterQuery
|
||||
| InternalFilterQuery[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], Record<string, unknown[]>]> {
|
||||
): Promise<[ExtractEntityType<TEntity>[], Record<string, unknown[]>]> {
|
||||
if (
|
||||
(Array.isArray(idsOrFilter) && !idsOrFilter.length) ||
|
||||
(!Array.isArray(idsOrFilter) && !idsOrFilter)
|
||||
@@ -465,21 +483,27 @@ export function MedusaInternalService<TContainer extends object = object>(
|
||||
async restore(
|
||||
idsOrFilter: string[] | InternalFilterQuery,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], Record<string, unknown[]>]> {
|
||||
): Promise<[ExtractEntityType<TEntity>[], Record<string, unknown[]>]> {
|
||||
return await this[propertyRepositoryName].restore(
|
||||
idsOrFilter,
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
upsert(data: any[], sharedContext?: Context): Promise<TEntity[]>
|
||||
upsert(data: any, sharedContext?: Context): Promise<TEntity>
|
||||
upsert(
|
||||
data: any[],
|
||||
sharedContext?: Context
|
||||
): Promise<ExtractEntityType<TEntity>[]>
|
||||
upsert(
|
||||
data: any,
|
||||
sharedContext?: Context
|
||||
): Promise<ExtractEntityType<TEntity>>
|
||||
|
||||
@InjectTransactionManager(propertyRepositoryName)
|
||||
async upsert(
|
||||
data: any | any[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity | TEntity[]> {
|
||||
): Promise<ExtractEntityType<TEntity> | ExtractEntityType<TEntity>[]> {
|
||||
const data_ = Array.isArray(data) ? data : [data]
|
||||
const entities = await this[propertyRepositoryName].upsert(
|
||||
data_,
|
||||
@@ -490,24 +514,30 @@ export function MedusaInternalService<TContainer extends object = object>(
|
||||
|
||||
upsertWithReplace(
|
||||
data: any[],
|
||||
config?: UpsertWithReplaceConfig<TEntity>,
|
||||
config?: UpsertWithReplaceConfig<ExtractEntityType<TEntity>>,
|
||||
sharedContext?: Context
|
||||
): Promise<{ entities: TEntity[]; performedActions: PerformedActions }>
|
||||
): Promise<{
|
||||
entities: ExtractEntityType<TEntity>[]
|
||||
performedActions: PerformedActions
|
||||
}>
|
||||
upsertWithReplace(
|
||||
data: any,
|
||||
config?: UpsertWithReplaceConfig<TEntity>,
|
||||
config?: UpsertWithReplaceConfig<ExtractEntityType<TEntity>>,
|
||||
sharedContext?: Context
|
||||
): Promise<{ entities: TEntity; performedActions: PerformedActions }>
|
||||
): Promise<{
|
||||
entities: ExtractEntityType<TEntity>
|
||||
performedActions: PerformedActions
|
||||
}>
|
||||
|
||||
@InjectTransactionManager(propertyRepositoryName)
|
||||
async upsertWithReplace(
|
||||
data: any | any[],
|
||||
config: UpsertWithReplaceConfig<TEntity> = {
|
||||
config: UpsertWithReplaceConfig<ExtractEntityType<TEntity>> = {
|
||||
relations: [],
|
||||
},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<{
|
||||
entities: TEntity | TEntity[]
|
||||
entities: ExtractEntityType<TEntity> | TEntity[]
|
||||
performedActions: PerformedActions
|
||||
}> {
|
||||
const data_ = Array.isArray(data) ? data : [data]
|
||||
|
||||
Reference in New Issue
Block a user