feat(utils): Utils to create an object select, relation from a string[] (#5213)
This commit is contained in:
committed by
GitHub
parent
f9264c479e
commit
cb569c2dfe
5
.changeset/stale-apricots-itch.md
Normal file
5
.changeset/stale-apricots-itch.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/utils": patch
|
||||
---
|
||||
|
||||
feat(utils): Utils to create an object select, relation from a string[]
|
||||
@@ -0,0 +1,68 @@
|
||||
import { stringToSelectRelationObject } from "../string-to-select-relation-object"
|
||||
|
||||
const fields = [
|
||||
"id",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"deleted_at",
|
||||
"url",
|
||||
"metadata",
|
||||
"tags.id",
|
||||
"tags.created_at",
|
||||
"tags.updated_at",
|
||||
"tags.deleted_at",
|
||||
"tags.value",
|
||||
"options.id",
|
||||
"options.created_at",
|
||||
"options.updated_at",
|
||||
"options.deleted_at",
|
||||
"options.title",
|
||||
"options.product_id",
|
||||
"options.metadata",
|
||||
"options.values.id",
|
||||
"options.values.created_at",
|
||||
"options.values.updated_at",
|
||||
"options.values.deleted_at",
|
||||
"options.values.value",
|
||||
"options.values.option_id",
|
||||
"options.values.variant_id",
|
||||
"options.values.metadata",
|
||||
]
|
||||
|
||||
describe("stringToSelectRelationObject", function () {
|
||||
it("should return an object containing the select and relation properties", function () {
|
||||
const output = stringToSelectRelationObject(fields)
|
||||
|
||||
expect(output).toEqual({
|
||||
select: [
|
||||
"id",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"deleted_at",
|
||||
"url",
|
||||
"metadata",
|
||||
"tags.id",
|
||||
"tags.created_at",
|
||||
"tags.updated_at",
|
||||
"tags.deleted_at",
|
||||
"tags.value",
|
||||
"options.id",
|
||||
"options.created_at",
|
||||
"options.updated_at",
|
||||
"options.deleted_at",
|
||||
"options.title",
|
||||
"options.product_id",
|
||||
"options.metadata",
|
||||
"options.values.id",
|
||||
"options.values.created_at",
|
||||
"options.values.updated_at",
|
||||
"options.values.deleted_at",
|
||||
"options.values.value",
|
||||
"options.values.option_id",
|
||||
"options.values.variant_id",
|
||||
"options.values.metadata",
|
||||
],
|
||||
relations: ["tags", "options", "options.values"],
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -25,3 +25,5 @@ export * from "./to-pascal-case"
|
||||
export * from "./transaction"
|
||||
export * from "./upper-case-first"
|
||||
export * from "./wrap-handler"
|
||||
export * from "./string-to-remote-query-object"
|
||||
export * from "./string-to-select-relation-object"
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* Convert a string fields array to a specific object such as { select, relation }
|
||||
* @param fields
|
||||
*
|
||||
* @example
|
||||
* const fields = [
|
||||
* "id",
|
||||
* "created_at",
|
||||
* "updated_at",
|
||||
* "deleted_at",
|
||||
* "url",
|
||||
* "metadata",
|
||||
* "tags.id",
|
||||
* "tags.created_at",
|
||||
* "tags.updated_at",
|
||||
* "tags.deleted_at",
|
||||
* "tags.value",
|
||||
* "options.id",
|
||||
* "options.created_at",
|
||||
* "options.updated_at",
|
||||
* "options.deleted_at",
|
||||
* "options.title",
|
||||
* "options.product_id",
|
||||
* "options.metadata",
|
||||
* "options.values.id",
|
||||
* "options.values.created_at",
|
||||
* "options.values.updated_at",
|
||||
* "options.values.deleted_at",
|
||||
* "options.values.value",
|
||||
* "options.values.option_id",
|
||||
* "options.values.variant_id",
|
||||
* "options.values.metadata",
|
||||
* ]
|
||||
*
|
||||
* const remoteQueryObject = stringToSelectRelationObject(fields)
|
||||
*
|
||||
* console.log(remoteQueryObject)
|
||||
* // {
|
||||
* // select: [
|
||||
* // "id",
|
||||
* // "created_at",
|
||||
* // "updated_at",
|
||||
* // "deleted_at",
|
||||
* // "url",
|
||||
* // "metadata",
|
||||
* // "tags.id",
|
||||
* // "tags.created_at",
|
||||
* // "tags.updated_at",
|
||||
* // "tags.deleted_at",
|
||||
* // "tags.value",
|
||||
* // "options.id",
|
||||
* // "options.created_at",
|
||||
* // "options.updated_at",
|
||||
* // "options.deleted_at",
|
||||
* // "options.title",
|
||||
* // "options.product_id",
|
||||
* // "options.metadata",
|
||||
* // "options.values.id",
|
||||
* // "options.values.created_at",
|
||||
* // "options.values.updated_at",
|
||||
* // "options.values.deleted_at",
|
||||
* // "options.values.value",
|
||||
* // "options.values.option_id",
|
||||
* // "options.values.variant_id",
|
||||
* // "options.values.metadata",
|
||||
* // ],
|
||||
* // relations: ["tags", "options", "options.values"],
|
||||
* // }
|
||||
*/
|
||||
export function stringToSelectRelationObject(fields: string[]): {
|
||||
select: string[]
|
||||
relations: string[]
|
||||
} {
|
||||
const tempResult = {
|
||||
select: new Set<string>(),
|
||||
relations: new Set<string>(),
|
||||
}
|
||||
|
||||
for (const field of fields) {
|
||||
tempResult.select.add(field)
|
||||
|
||||
if (!field.includes(".")) {
|
||||
continue
|
||||
}
|
||||
|
||||
const segments = field.split(".")
|
||||
segments.pop()
|
||||
const relationPath = segments.join(".")
|
||||
|
||||
tempResult.relations.add(relationPath)
|
||||
}
|
||||
|
||||
return {
|
||||
select: Array.from(tempResult.select),
|
||||
relations: Array.from(tempResult.relations),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user