refactor: Improve code readability and maintainability in AnalyticsDatabaseService.ts

This commit is contained in:
Simon Larsen 2024-05-03 19:05:56 +01:00
parent aede9af03d
commit 469e06280a
No known key found for this signature in database
GPG Key ID: 96C5DCA24769DBCA

View File

@ -53,14 +53,14 @@ import TableColumnType from 'Common/Types/AnalyticsDatabase/TableColumnType';
export default class AnalyticsDatabaseService< export default class AnalyticsDatabaseService<
TBaseModel extends AnalyticsBaseModel TBaseModel extends AnalyticsBaseModel
> extends BaseService { > extends BaseService {
public modelType!: { new(): TBaseModel }; public modelType!: { new (): TBaseModel };
public database!: ClickhouseDatabase; public database!: ClickhouseDatabase;
public model!: TBaseModel; public model!: TBaseModel;
public databaseClient!: ClickhouseClient; public databaseClient!: ClickhouseClient;
public statementGenerator!: StatementGenerator<TBaseModel>; public statementGenerator!: StatementGenerator<TBaseModel>;
public constructor(data: { public constructor(data: {
modelType: { new(): TBaseModel }; modelType: { new (): TBaseModel };
database?: ClickhouseDatabase | undefined; database?: ClickhouseDatabase | undefined;
}) { }) {
super(); super();
@ -334,18 +334,18 @@ export default class AnalyticsDatabaseService<
if (countBy.limit) { if (countBy.limit) {
statement.append(SQL` statement.append(SQL`
LIMIT ${{ LIMIT ${{
value: Number(countBy.limit), value: Number(countBy.limit),
type: TableColumnType.Number, type: TableColumnType.Number,
}} }}
`); `);
} }
if (countBy.skip) { if (countBy.skip) {
statement.append(SQL` statement.append(SQL`
OFFSET ${{ OFFSET ${{
value: Number(countBy.skip), value: Number(countBy.skip),
type: TableColumnType.Number, type: TableColumnType.Number,
}} }}
`); `);
} }
logger.info(`${this.model.tableName} Count Statement`); logger.info(`${this.model.tableName} Count Statement`);
@ -551,8 +551,8 @@ export default class AnalyticsDatabaseService<
statement instanceof Statement statement instanceof Statement
? statement ? statement
: { : {
query: statement, // TODO remove and only accept Statements query: statement, // TODO remove and only accept Statements
} }
); );
} }
@ -654,16 +654,16 @@ export default class AnalyticsDatabaseService<
const onCreate: OnCreate<TBaseModel> = createBy.props.ignoreHooks const onCreate: OnCreate<TBaseModel> = createBy.props.ignoreHooks
? { ? {
createBy: { createBy: {
data: data, data: data,
props: createBy.props, props: createBy.props,
}, },
carryForward: [], carryForward: [],
} }
: await this._onBeforeCreate({ : await this._onBeforeCreate({
data: data, data: data,
props: createBy.props, props: createBy.props,
}); });
data = onCreate.createBy.data; data = onCreate.createBy.data;
@ -765,7 +765,8 @@ export default class AnalyticsDatabaseService<
await Promise.allSettled(promises); await Promise.allSettled(promises);
} else { } else {
logger.warn( logger.warn(
`Realtime is not initialized. Skipping emitModelEvent for ${this.getModel().tableName `Realtime is not initialized. Skipping emitModelEvent for ${
this.getModel().tableName
}` }`
); );
} }
@ -868,7 +869,6 @@ export default class AnalyticsDatabaseService<
requiredField, requiredField,
data.getDefaultValueForColumn(requiredField) data.getDefaultValueForColumn(requiredField)
); );
} else { } else {
throw new BadDataException(`${requiredField} is required`); throw new BadDataException(`${requiredField} is required`);
} }