diff --git a/packages/core/database/src/__tests__/field-options/inddex.test.ts b/packages/core/database/src/__tests__/field-options/inddex.test.ts new file mode 100644 index 000000000..7eabe4ece --- /dev/null +++ b/packages/core/database/src/__tests__/field-options/inddex.test.ts @@ -0,0 +1,43 @@ +import { mockDatabase } from '../'; +import { Database } from '../../database'; +import { md5 } from '../../utils'; + +describe('index field options', () => { + let db: Database; + + beforeEach(async () => { + db = mockDatabase(); + }); + + afterEach(async () => { + await db.close(); + }); + + it('case 1', async () => { + const t1 = 't1234567890223456789032345678904234567890523456789'; + const f1 = 'f1234567890223456789032345678904234567890523456789062345678901'; + const f2 = 'f1234567890223456789032345678904234567890523456789062345678902'; + db.collection({ + name: t1, + fields: [ + { + type: 'string', + name: f1, + index: true, + }, + { + type: 'string', + name: f2, + index: true, + }, + ], + }); + await db.sync(); + // @ts-ignore + const indexes = db.getModel(t1)._indexes; + const index1 = indexes.find((item) => item.fields.includes(f1)); + const index2 = indexes.find((item) => item.fields.includes(f2)); + expect('i_' + md5(db.getTablePrefix() + `${t1}_${f1}`)).toBe(index1.name); + expect('i_' + md5(db.getTablePrefix() + `${t1}_${f2}`)).toBe(index2.name); + }); +}); diff --git a/packages/core/database/src/collection.ts b/packages/core/database/src/collection.ts index e66c8531e..3244ce530 100644 --- a/packages/core/database/src/collection.ts +++ b/packages/core/database/src/collection.ts @@ -13,6 +13,7 @@ import { Database } from './database'; import { Field, FieldOptions } from './fields'; import { Model } from './model'; import { Repository } from './repository'; +import { md5 } from './utils'; export type RepositoryType = typeof Repository; @@ -283,7 +284,7 @@ export class Collection< this.setField(options.name || name, options); } - addIndex(index: string | string[] | { fields: string[], unique?: boolean,[key: string]: any }) { + addIndex(index: string | string[] | { fields: string[]; unique?: boolean; [key: string]: any }) { if (!index) { return; } @@ -329,7 +330,13 @@ export class Collection< // @ts-ignore this.model._indexes = this.model.options.indexes // @ts-ignore - .map((index) => Utils.nameIndex(this.model._conformIndex(index), tableName)); + .map((index) => Utils.nameIndex(this.model._conformIndex(index), tableName)) + .map((item) => { + if (item.name && item.name.length > 63) { + item.name = 'i_' + md5(item.name); + } + return item; + }); } removeIndex(fields: any) { diff --git a/packages/core/database/src/utils.ts b/packages/core/database/src/utils.ts index 89eddddf8..627916f72 100644 --- a/packages/core/database/src/utils.ts +++ b/packages/core/database/src/utils.ts @@ -1,4 +1,4 @@ -import { omit } from 'lodash'; +import crypto from 'crypto'; import { Model } from './model'; type HandleAppendsQueryOptions = { @@ -6,6 +6,10 @@ type HandleAppendsQueryOptions = { queryPromises: Array; }; +export function md5(value: string) { + return crypto.createHash('md5').update(value).digest('hex'); +} + export async function handleAppendsQuery(options: HandleAppendsQueryOptions) { const { templateModel, queryPromises } = options;