fix(database): fix the index name too long error
(cherry picked from commit 7bfe6b8c46bef0183c4703683175561c7fc91aee)
This commit is contained in:
parent
7b371706ef
commit
933c3f4463
@ -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);
|
||||
});
|
||||
});
|
@ -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) {
|
||||
|
@ -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<any>;
|
||||
};
|
||||
|
||||
export function md5(value: string) {
|
||||
return crypto.createHash('md5').update(value).digest('hex');
|
||||
}
|
||||
|
||||
export async function handleAppendsQuery(options: HandleAppendsQueryOptions) {
|
||||
const { templateModel, queryPromises } = options;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user