mirror of
https://github.com/Canner/WrenAI.git
synced 2026-09-24 23:29:49 +08:00
Add createCalculatedField resolver and migration file
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user