Add createCalculatedField resolver and migration file

This commit is contained in:
onlyjackfrost
2024-05-03 17:24:53 +08:00
committed by Andy Yen
parent 57f6e98f8d
commit e01b7bc8d3
2 changed files with 41 additions and 0 deletions
+3
View File
@@ -59,6 +59,9 @@ const resolvers = {
updateCalculatedField: modelResolver.updateCalculatedField,
deleteCalculatedField: modelResolver.deleteCalculatedField,
// calculated field
createCalculatedField: modelResolver.createCalculatedField,
// Ask
createAskingTask: askingResolver.createAskingTask,
cancelAskingTask: askingResolver.cancelAskingTask,
@@ -200,6 +200,44 @@ export class ModelService implements IModelService {
return updatedColumn;
}
public async createCalculatedField(
data: CreateCalculatedFieldData,
): Promise<ModelColumn> {
const { modelId, name, expression, lineage } = data;
const model = await this.modelRepository.findOneBy({
id: modelId,
});
if (!model) {
throw new Error('Model not found');
}
const { valid, message } = await this.validateCalculatedField(
name,
modelId,
);
if (!valid) {
throw new Error(message);
}
const inputFieldId = lineage[lineage.length - 1];
const dataType = await this.inferCalculatedFieldDataType(
expression,
inputFieldId,
);
const column = await this.modelColumnRepository.createOne({
modelId,
displayName: name,
sourceColumnName: name,
referenceName: name,
type: dataType,
isCalculated: true,
isPk: false,
notNull: false,
aggregation: expression,
lineage: JSON.stringify(lineage),
properties: JSON.stringify({ description: '' }),
});
return column;
}
public async batchUpdateModelProperties(tables: SampleDatasetTable[]) {
logger.debug('start batch update model description');
const project = await this.projectService.getCurrentProject();