add skip generation of non-primary key indexes option

This commit is contained in:
Francesco Spilla 2021-12-27 19:55:21 +01:00
parent 1d3eaa0dc3
commit 9315b4a992
8 changed files with 53 additions and 13 deletions

View File

@ -23,6 +23,7 @@ export default interface IGenerationOptions {
skipSchema: boolean;
indexFile: boolean;
exportType: "named" | "default";
skipNonPrimaryKeyIndexes: boolean;
}
export const eolConverter = {
@ -49,6 +50,7 @@ export function getDefaultGenerationOptions(): IGenerationOptions {
skipSchema: false,
indexFile: false,
exportType: "named",
skipNonPrimaryKeyIndexes: false,
};
return generationOptions;
}

View File

@ -184,7 +184,8 @@ export default abstract class AbstractDriver {
await this.GetIndexesFromEntity(
dbModel,
connectionOptions.schemaNames,
connectionOptions.databaseNames
connectionOptions.databaseNames,
generationOptions,
);
AbstractDriver.FindPrimaryColumnsFromIndexes(dbModel);
dbModel = await this.GetRelations(
@ -385,7 +386,8 @@ export default abstract class AbstractDriver {
public abstract GetIndexesFromEntity(
entities: Entity[],
schemas: string[],
dbNames: string[]
dbNames: string[],
generationOptions: IGenerationOptions
): Promise<Entity[]>;
public abstract GetRelations(

View File

@ -272,7 +272,8 @@ export default class MssqlDriver extends AbstractDriver {
public async GetIndexesFromEntity(
entities: Entity[],
schemas: string[],
dbNames: string[]
dbNames: string[],
generationOptions: IGenerationOptions,
): Promise<Entity[]> {
const request = new this.MSSQL.Request(this.Connection);
/* eslint-disable camelcase */
@ -340,7 +341,9 @@ export default class MssqlDriver extends AbstractDriver {
records.forEach((record) => {
indexInfo.columns.push(record.ColumnName);
});
ent.indices.push(indexInfo);
if (!generationOptions.skipNonPrimaryKeyIndexes || indexInfo.primary) {
ent.indices.push(indexInfo);
}
});
});

View File

@ -319,7 +319,8 @@ export default class MysqlDriver extends AbstractDriver {
public async GetIndexesFromEntity(
entities: Entity[],
schemas: string[],
dbNames: string[]
dbNames: string[],
generationOptions: IGenerationOptions,
): Promise<Entity[]> {
/* eslint-disable camelcase */
const response = await this.ExecQuery<{
@ -359,7 +360,9 @@ export default class MysqlDriver extends AbstractDriver {
records.forEach((record) => {
indexInfo.columns.push(record.ColumnName);
});
ent.indices.push(indexInfo);
if (!generationOptions.skipNonPrimaryKeyIndexes || indexInfo.primary) {
ent.indices.push(indexInfo);
}
});
});

View File

@ -236,7 +236,12 @@ export default class OracleDriver extends AbstractDriver {
return entities;
}
public async GetIndexesFromEntity(entities: Entity[]): Promise<Entity[]> {
public async GetIndexesFromEntity(
entities: Entity[],
schemas: string[],
dbNames: string[],
generationOptions: IGenerationOptions,
): Promise<Entity[]> {
const response = (
await this.Connection.execute<{
COLUMN_NAME: string;
@ -271,7 +276,9 @@ export default class OracleDriver extends AbstractDriver {
records.forEach((record) => {
indexInfo.columns.push(record.COLUMN_NAME);
});
ent.indices.push(indexInfo);
if (!generationOptions.skipNonPrimaryKeyIndexes || indexInfo.primary) {
ent.indices.push(indexInfo);
}
});
});

View File

@ -451,7 +451,9 @@ export default class PostgresDriver extends AbstractDriver {
public async GetIndexesFromEntity(
entities: Entity[],
schemas: string[]
schemas: string[],
dbNames: string[],
generationOptions: IGenerationOptions,
): Promise<Entity[]> {
const response: {
tablename: string;
@ -506,7 +508,9 @@ export default class PostgresDriver extends AbstractDriver {
records.forEach((record) => {
indexInfo.columns.push(record.columnname);
});
ent.indices.push(indexInfo);
if (!generationOptions.skipNonPrimaryKeyIndexes || indexInfo.primary) {
ent.indices.push(indexInfo);
}
});
});

View File

@ -258,7 +258,12 @@ export default class SqliteDriver extends AbstractDriver {
return entities;
}
public async GetIndexesFromEntity(entities: Entity[]): Promise<Entity[]> {
public async GetIndexesFromEntity(
entities: Entity[],
schemas: string[],
dbNames: string[],
generationOptions: IGenerationOptions,
): Promise<Entity[]> {
await Promise.all(
entities.map(async (ent) => {
const response = await this.ExecQuery<{
@ -299,7 +304,9 @@ export default class SqliteDriver extends AbstractDriver {
v.options.unique = true;
});
}
ent.indices.push(indexInfo);
if (!generationOptions.skipNonPrimaryKeyIndexes || indexInfo.primary) {
ent.indices.push(indexInfo);
}
})
);
})

View File

@ -277,6 +277,11 @@ function checkYargsParameters(options: options): options {
describe:
"Skip schema generation for specific tables. You can pass multiple values separated by comma",
},
skipNonPrimaryKeyIndexes: {
boolean: true,
default: options.generationOptions.skipNonPrimaryKeyIndexes,
describe: "Skip generation of non-primary key indexes",
},
tables: {
string: true,
default: options.connectionOptions.onlyTables.join(","),
@ -297,7 +302,7 @@ function checkYargsParameters(options: options): options {
boolean: true,
default: options.generationOptions.exportType === "default",
describe: "Generate index file",
},
}
});
options.connectionOptions.databaseNames = argv.d.split(",");
@ -342,6 +347,7 @@ function checkYargsParameters(options: options): options {
argv.pv as IGenerationOptions["propertyVisibility"];
options.generationOptions.relationIds = argv.relationIds;
options.generationOptions.skipSchema = argv.skipSchema;
options.generationOptions.skipNonPrimaryKeyIndexes = argv.skipNonPrimaryKeyIndexes;
options.generationOptions.resultsPath = argv.o;
options.generationOptions.pluralizeNames = !argv.disablePluralization;
options.generationOptions.strictMode =
@ -615,6 +621,11 @@ async function useInquirer(options: options): Promise<options> {
options.generationOptions.exportType ===
"default",
},
{
name: "Skip generation of non-primary key indexes",
value: "skipNonPrimaryKeyIndexes",
checked: options.generationOptions.skipNonPrimaryKeyIndexes,
}
],
message: "Available customizations",
name: "selected",
@ -667,6 +678,7 @@ async function useInquirer(options: options): Promise<options> {
)
? "default"
: "named";
options.generationOptions.skipNonPrimaryKeyIndexes = customizations.includes("skipNonPrimaryKeyIndexes");
if (customizations.includes("namingStrategy")) {
const namingStrategyPath = (