committed by
GitHub
parent
cb79a5dbff
commit
d6ff526820
@@ -0,0 +1,26 @@
|
||||
import { compressName } from "../compress-name"
|
||||
|
||||
describe("compressName", () => {
|
||||
it("should remove consecutive duplicate names in sequence if it exceds 58 chars", () => {
|
||||
const name =
|
||||
"product_product_variant_id_order_order_id_long_long_long_long_name"
|
||||
const result = compressName(name)
|
||||
expect(result).toBe("product_variant_id_order_id_long_name33bb7b344")
|
||||
})
|
||||
|
||||
it("should remove duplicate names and truncate name to append a hash", () => {
|
||||
const name = "product_product_variant_id_order_order_id"
|
||||
const result = compressName(name, 30)
|
||||
expect(result).toBe("product_variant_id_ord91d0cda8")
|
||||
})
|
||||
|
||||
it("handles very long names with truncation and appends hash when necessary", () => {
|
||||
const name =
|
||||
"CustomModuleImplementationContainingAReallyBigNameThatExceedsPosgresLimitToNameATableModule"
|
||||
const result = compressName(name)
|
||||
expect(result).toHaveLength(58)
|
||||
expect(result).toBe(
|
||||
"cust_modu_impl_cont_area_big_name_that_exce_posg_1f1cc72da"
|
||||
)
|
||||
})
|
||||
})
|
||||
38
packages/modules/link-modules/src/utils/compress-name.ts
Normal file
38
packages/modules/link-modules/src/utils/compress-name.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { camelToSnakeCase, simpleHash } from "@medusajs/utils"
|
||||
|
||||
export function compressName(name: string, limit = 58) {
|
||||
if (name.length <= limit) {
|
||||
return name.toLowerCase()
|
||||
}
|
||||
|
||||
const hash = simpleHash(name).toLowerCase()
|
||||
const hashLength = hash.length
|
||||
|
||||
// Remove consecutive duplicates
|
||||
const parts = name.toLowerCase().split("_")
|
||||
const deduplicatedParts: string[] = []
|
||||
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
if (i === 0 || parts[i] !== parts[i - 1]) {
|
||||
deduplicatedParts.push(parts[i])
|
||||
}
|
||||
}
|
||||
|
||||
let result = deduplicatedParts.join("_")
|
||||
|
||||
// If still greater than the limit, truncate each part to 4 characters
|
||||
if (result.length > limit) {
|
||||
const allParts = camelToSnakeCase(name).split("_")
|
||||
result = allParts.map((part) => part.substring(0, 4)).join("_")
|
||||
}
|
||||
|
||||
name = result
|
||||
const nameLength = name.length
|
||||
const diff = nameLength + hashLength - limit
|
||||
|
||||
if (diff > 0) {
|
||||
return name.slice(0, nameLength - diff) + hash
|
||||
}
|
||||
|
||||
return (name + hash).toLowerCase()
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
} from "@medusajs/utils"
|
||||
|
||||
import { EntitySchema } from "@mikro-orm/core"
|
||||
import { compressName } from "./compress-name"
|
||||
|
||||
function getClass(...properties) {
|
||||
return class LinkModel {
|
||||
@@ -62,7 +63,7 @@ export function generateEntity(
|
||||
class: getClass(
|
||||
...fieldNames.concat("created_at", "updated_at", "deleted_at")
|
||||
) as any,
|
||||
tableName,
|
||||
tableName: compressName(tableName),
|
||||
properties: {
|
||||
id: {
|
||||
type: "string",
|
||||
|
||||
Reference in New Issue
Block a user