fix insert into db

This commit is contained in:
Simon Larsen
2023-10-20 19:51:23 +01:00
parent e687a439e6
commit ed5a144735
7 changed files with 105 additions and 30 deletions

View File

@@ -9,7 +9,7 @@ show tables from oneuptime
## Show table structure
```sql
desc table_name from oneuptime
DESCRIBE TABLE oneuptime.Span
```
## Show table data

View File

@@ -90,6 +90,14 @@ export default class AnalyticsDataModel extends CommonModel {
data.allowAccessIfSubscriptionIsUnpaid || false;
this.accessControl = data.accessControl;
this.enableWorkflowOn = data.enableWorkflowOn;
// initialize Arrays.
for(const column of this.tableColumns){
if(column.type === TableColumnType.NestedModel){
this.setColumnValue(column.key, []);
}
}
}
private _enableWorkflowOn: EnableWorkflowOn | undefined;

View File

@@ -7,7 +7,7 @@ import BadDataException from '../Types/Exception/BadDataException';
import { JSONObject, JSONValue } from '../Types/JSON';
import ObjectID from '../Types/ObjectID';
export type RecordValue = string | number | boolean | Date | Array<CommonModel>;
export type RecordValue = ObjectID | string | number | boolean | Date | Array<CommonModel>;
export type Record = Array<RecordValue | Record>;
@@ -109,6 +109,11 @@ export default class CommonModel {
return;
}
if(recordValue instanceof Array) {
json[column.key] = CommonModel.toJSONArray(recordValue);
return;
}
json[column.key] = recordValue;
});

View File

@@ -548,8 +548,11 @@ export default class AnalyticsDatabaseService<
}
try {
const insertStatement: string = this.statementGenerator.toCreateStatement({ item: items });
await this.execute(
this.statementGenerator.toCreateStatement({ item: items })
insertStatement
);
if (!createBy.props.ignoreHooks) {

View File

@@ -66,18 +66,43 @@ export default class StatementGenerator<TBaseModel extends AnalyticsBaseModel> {
for (const nestedModelColumnName of nestedModelColumnNames) {
columnNames.push(`${column.key}.${nestedModelColumnName}`);
}
}
columnNames.push(column.key);
}else{
columnNames.push(column.key);
}
}
return columnNames;
}
public getRecordValuesStatement(record: Record): string {
let valueStatement: string = '';
for (const value of record) {
if (Array.isArray(value)) {
if(value.length === 0) {
valueStatement += `[], `;
continue;
}
valueStatement += `[${value.join(",")}], `;
} else {
valueStatement += `${value}, `;
}
}
valueStatement = valueStatement.substring(
0,
valueStatement.length - 2
); // remove last comma.
return valueStatement;
}
public getValuesStatement(records: Array<Record>): string {
let statement: string = '';
for (const record of records) {
statement += `(${record.join(', ')}), `;
statement += `(${this.getRecordValuesStatement(record)}), `;
}
statement = statement.substring(0, statement.length - 2); // remove last comma.
@@ -139,13 +164,16 @@ export default class StatementGenerator<TBaseModel extends AnalyticsBaseModel> {
for (const nestedModelItem of item.getColumnValue(
column.key
) as Array<CommonModel>) {
const value: RecordValue | undefined =
const value: RecordValue =
this.sanitizeValue(
nestedModelItem.getColumnValue(subColumn.key),
column
subColumn,
{
isNestedModel: true
}
);
subRecord.push(value || 'NULL');
subRecord.push(value);
}
record.push(subRecord);
@@ -156,7 +184,7 @@ export default class StatementGenerator<TBaseModel extends AnalyticsBaseModel> {
column
);
record.push(value || 'NULL');
record.push(value);
}
}
@@ -165,8 +193,27 @@ export default class StatementGenerator<TBaseModel extends AnalyticsBaseModel> {
private sanitizeValue(
value: RecordValue | undefined,
column: AnalyticsTableColumn
): RecordValue | undefined {
column: AnalyticsTableColumn,
options?: {
isNestedModel?: boolean;
}
): RecordValue {
if(!value && value !== 0 && value !== false) {
if(options?.isNestedModel){
if(column.type === TableColumnType.Text) {
return `''`;
}
if(column.type === TableColumnType.Number) {
return 0;
}
}
return 'NULL';
}
if (
column.type === TableColumnType.ObjectID ||
column.type === TableColumnType.Text
@@ -180,6 +227,18 @@ export default class StatementGenerator<TBaseModel extends AnalyticsBaseModel> {
)}')`;
}
if(column.type === TableColumnType.Number) {
if(typeof value === 'string') {
value = parseInt(value);
}
}
if(column.type === TableColumnType.Decimal) {
if(typeof value === 'string') {
value = parseFloat(value);
}
}
return value;
}

View File

@@ -27,7 +27,7 @@ export default class KeyValueNestedModel extends NestedModel {
title: 'Number Value',
description: 'Value of the attribute',
required: false,
type: TableColumnType.Text,
type: TableColumnType.Number,
}),
],
});

View File

@@ -72,14 +72,14 @@ export class SpanEvent extends NestedModel {
type: TableColumnType.Text,
}),
new AnalyticsTableColumn({
key: 'attributes',
title: 'Attributes',
description: 'Attributes',
required: false,
type: TableColumnType.NestedModel,
nestedModel: new KeyValueNestedModel(),
}),
// new AnalyticsTableColumn({
// key: 'attributes',
// title: 'Attributes',
// description: 'Attributes',
// required: false,
// type: TableColumnType.NestedModel,
// nestedModel: new KeyValueNestedModel(),
// }),
],
});
}
@@ -139,14 +139,14 @@ export class SpanLink extends NestedModel {
type: TableColumnType.Text,
}),
new AnalyticsTableColumn({
key: 'attributes',
title: 'Attributes',
description: 'Attributes',
required: false,
type: TableColumnType.NestedModel,
nestedModel: new KeyValueNestedModel(),
}),
// new AnalyticsTableColumn({
// key: 'attributes',
// title: 'Attributes',
// description: 'Attributes',
// required: false,
// type: TableColumnType.NestedModel,
// nestedModel: new KeyValueNestedModel(),
// }),
],
});
}