mirror of
https://github.com/directus/directus.git
synced 2026-01-26 14:07:56 -05:00
Use hash instead of random for default index name (#9448)
* [Fix]: Use hash instead of random for default index name * Move hash function to separate util file * Reduce max size of index name to 60 from 64 Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
This commit is contained in:
@@ -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}`;
|
||||
}
|
||||
|
||||
13
api/src/utils/get-simple-hash.ts
Normal file
13
api/src/utils/get-simple-hash.ts
Normal file
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user