Skip to content

Commit

Permalink
feat(CreateTables): Only create tables and indexes when necessary.
Browse files Browse the repository at this point in the history
  • Loading branch information
benMain authored Dec 5, 2023
2 parents 025516f + 7facb8d commit 8e7334b
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,21 @@ export class Repository<T> {
*/

private async createTable(): Promise<number> {
const selectTable = await this.queryService.querySingle<{
name: string;
}>(
[
`SELECT i.name`,
`FROM information_schema.user_tables AS i`,
`WHERE i.name = ? AND i.status = ?`,
].join(' '),
this.config.tableName,
'ACTIVE',
);
if (selectTable?.name === this.config.tableName) {
return 1;
}

const result = await this.queryService.execute(
`CREATE TABLE ${this.config.tableName}`,
);
Expand All @@ -176,7 +191,25 @@ export class Repository<T> {

private async createIndexes(indexFields: string[]) {
const results: Result[] = [];
for (const field of indexFields) {

const selectIndex = await this.queryService.query<{
expr: string;
indexId: string;
status: string;
}>(
[
`SELECT VALUE indexes`,
`FROM information_schema.user_tables AS i, i.indexes AS indexes`,
`WHERE i.name = ?`,
].join(' '),
);

const currentIndex = selectIndex.map(val => val.expr);
const newIndexFields = indexFields.filter(
field => currentIndex.indexOf(field) === -1,
);

for (const field of newIndexFields) {
try {
const result = await this.queryService.execute(
`CREATE INDEX ON ${this.config.tableName} (${field})`,
Expand Down

0 comments on commit 8e7334b

Please sign in to comment.