chore(utils): Add default ordering to the internal service factory list/listAndCount (#6436)
**What** Default ordering. By default, only the top level entity ordering is applied using the primary keys, the relations are default ordered by the foreign keys. It include tests fixing for deterministic data ordering
This commit is contained in:
committed by
GitHub
parent
691f68c3b8
commit
c319edb8e0
@@ -3,7 +3,12 @@ import { lowerCaseFirst } from "../../common"
|
||||
|
||||
const defaultContext = { __type: "MedusaContext" }
|
||||
|
||||
class Model {}
|
||||
class Model {
|
||||
static __meta = {
|
||||
primaryKeys: ["id"],
|
||||
}
|
||||
}
|
||||
|
||||
describe("Internal Module Service Factory", () => {
|
||||
const modelRepositoryName = `${lowerCaseFirst(Model.name)}Repository`
|
||||
|
||||
@@ -114,6 +119,34 @@ describe("Internal Module Service Factory", () => {
|
||||
expect(result).toEqual(entities)
|
||||
})
|
||||
|
||||
it("should list entities and relation with default ordering successfully", async () => {
|
||||
const entities = [
|
||||
{ id: "1", name: "Item" },
|
||||
{ id: "2", name: "Item2" },
|
||||
]
|
||||
containerMock[modelRepositoryName].find.mockResolvedValueOnce(entities)
|
||||
|
||||
const result = await instance.list(
|
||||
{},
|
||||
{
|
||||
relations: ["relation"],
|
||||
}
|
||||
)
|
||||
|
||||
expect(result).toEqual(entities)
|
||||
expect(containerMock[modelRepositoryName].find).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
options: expect.objectContaining({
|
||||
populate: ["relation"],
|
||||
orderBy: {
|
||||
id: "ASC",
|
||||
},
|
||||
}),
|
||||
}),
|
||||
expect.any(Object)
|
||||
)
|
||||
})
|
||||
|
||||
it("should list and count entities successfully", async () => {
|
||||
const entities = [
|
||||
{ id: "1", name: "Item" },
|
||||
|
||||
@@ -63,6 +63,24 @@ export function internalModuleServiceFactory<
|
||||
return keys.map((k) => data[k]).join("_")
|
||||
}
|
||||
|
||||
/**
|
||||
* Only apply top level default ordering as the relation
|
||||
* default ordering is already applied through the foreign key
|
||||
* @param config
|
||||
*/
|
||||
static applyDefaultOrdering(config: FindConfig<any>) {
|
||||
if (config.order) {
|
||||
return
|
||||
}
|
||||
|
||||
config.order = {}
|
||||
|
||||
const primaryKeys = AbstractService_.retrievePrimaryKeys(model)
|
||||
primaryKeys.forEach((primaryKey) => {
|
||||
config.order![primaryKey] = "ASC"
|
||||
})
|
||||
}
|
||||
|
||||
@InjectManager(propertyRepositoryName)
|
||||
async retrieve(
|
||||
idOrObject: string | object,
|
||||
@@ -129,6 +147,7 @@ export function internalModuleServiceFactory<
|
||||
config: FindConfig<any> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
AbstractService_.applyDefaultOrdering(config)
|
||||
const queryOptions = buildQuery(filters, config)
|
||||
|
||||
return await this[propertyRepositoryName].find(
|
||||
@@ -143,6 +162,7 @@ export function internalModuleServiceFactory<
|
||||
config: FindConfig<any> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], number]> {
|
||||
AbstractService_.applyDefaultOrdering(config)
|
||||
const queryOptions = buildQuery(filters, config)
|
||||
|
||||
return await this[propertyRepositoryName].findAndCount(
|
||||
|
||||
Reference in New Issue
Block a user