diff --git a/api/src/utils/get-default-index-name.ts b/api/src/utils/get-default-index-name.ts index 2c6e94cf38..b9a70fc9cf 100644 --- a/api/src/utils/get-default-index-name.ts +++ b/api/src/utils/get-default-index-name.ts @@ -1,6 +1,4 @@ -import { customAlphabet } from 'nanoid'; - -const generateID = customAlphabet('abcdefghijklmnopqrstuvxyz', 5); +import { simpleHash } from './get-simple-hash'; /** * Generate an index name for a given collection + fields combination. @@ -20,10 +18,10 @@ export function getDefaultIndexName( const table = collection.replace(/\.|-/g, '_'); const indexName = (table + '_' + fields.join('_') + '_' + type).toLowerCase(); - if (indexName.length <= 64) return indexName; + if (indexName.length <= 60) return indexName; - const suffix = `__${generateID()}_${type}`; - const prefix = indexName.substring(0, 64 - suffix.length); + const suffix = `__${simpleHash(indexName)}_${type}`; + const prefix = indexName.substring(0, 60 - suffix.length); - return `${prefix}__${generateID()}_${type}`; + return `${prefix}${suffix}`; } diff --git a/api/src/utils/get-simple-hash.ts b/api/src/utils/get-simple-hash.ts new file mode 100644 index 0000000000..29c172ee91 --- /dev/null +++ b/api/src/utils/get-simple-hash.ts @@ -0,0 +1,13 @@ +/** + * Generate a simple short hash for a given string + * This is not cryptographically secure in any way, and has a high chance of collision + */ +export function simpleHash(str: string) { + let hash = 0; + + for (let i = 0; i < str.length; hash &= hash) { + hash = 31 * hash + str.charCodeAt(i++); + } + + return hash.toString(16); +}