feat: Add Mongo Templates for plugin. (#11153)
Adds templates for the Aggregate, Distinct and Count commands for the MongoDB plugin.
This commit is contained in:
parent
7a8db4270a
commit
9bcd4afff1
|
|
@ -4,9 +4,11 @@ import com.appsmith.external.constants.DataType;
|
||||||
import com.appsmith.external.exceptions.pluginExceptions.AppsmithPluginError;
|
import com.appsmith.external.exceptions.pluginExceptions.AppsmithPluginError;
|
||||||
import com.appsmith.external.exceptions.pluginExceptions.AppsmithPluginException;
|
import com.appsmith.external.exceptions.pluginExceptions.AppsmithPluginException;
|
||||||
import com.appsmith.external.helpers.DataTypeStringUtils;
|
import com.appsmith.external.helpers.DataTypeStringUtils;
|
||||||
|
import com.appsmith.external.models.DatasourceStructure;
|
||||||
import com.appsmith.external.models.ActionConfiguration;
|
import com.appsmith.external.models.ActionConfiguration;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
import org.bson.BsonArray;
|
import org.bson.BsonArray;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
import org.bson.json.JsonParseException;
|
import org.bson.json.JsonParseException;
|
||||||
|
|
@ -14,16 +16,25 @@ import org.pf4j.util.StringUtils;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
import static com.appsmith.external.helpers.PluginUtils.getValueSafelyFromFormData;
|
import static com.appsmith.external.helpers.PluginUtils.getValueSafelyFromFormData;
|
||||||
|
import static com.appsmith.external.helpers.PluginUtils.setValueSafelyInFormData;
|
||||||
import static com.external.plugins.constants.FieldName.AGGREGATE_LIMIT;
|
import static com.external.plugins.constants.FieldName.AGGREGATE_LIMIT;
|
||||||
import static com.external.plugins.utils.MongoPluginUtils.parseSafely;
|
import static com.external.plugins.utils.MongoPluginUtils.parseSafely;
|
||||||
import static com.appsmith.external.helpers.PluginUtils.validConfigurationPresentInFormData;
|
import static com.appsmith.external.helpers.PluginUtils.validConfigurationPresentInFormData;
|
||||||
import static com.external.plugins.constants.FieldName.AGGREGATE_PIPELINE;
|
import static com.external.plugins.constants.FieldName.AGGREGATE_PIPELINE;
|
||||||
import static org.apache.commons.lang3.StringUtils.isBlank;
|
import static org.apache.commons.lang3.StringUtils.isBlank;
|
||||||
|
import static com.external.plugins.constants.FieldName.COLLECTION;
|
||||||
|
import static com.external.plugins.constants.FieldName.COMMAND;
|
||||||
|
import static com.external.plugins.constants.FieldName.SMART_SUBSTITUTION;
|
||||||
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
public class Aggregate extends MongoCommand {
|
public class Aggregate extends MongoCommand {
|
||||||
String pipeline;
|
String pipeline;
|
||||||
String limit;
|
String limit;
|
||||||
|
|
@ -98,4 +109,31 @@ public class Aggregate extends MongoCommand {
|
||||||
|
|
||||||
return commandDocument;
|
return commandDocument;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<DatasourceStructure.Template> generateTemplate(Map<String, Object> templateConfiguration) {
|
||||||
|
String collectionName = (String) templateConfiguration.get("collectionName");
|
||||||
|
|
||||||
|
Map<String, Object> configMap = new HashMap<>();
|
||||||
|
|
||||||
|
setValueSafelyInFormData(configMap, SMART_SUBSTITUTION, Boolean.TRUE);
|
||||||
|
setValueSafelyInFormData(configMap, COMMAND, "AGGREGATE");
|
||||||
|
setValueSafelyInFormData(configMap, COLLECTION, collectionName);
|
||||||
|
setValueSafelyInFormData(configMap, AGGREGATE_PIPELINE, "[ {\"$sort\" : {\"_id\": 1} } ]");
|
||||||
|
setValueSafelyInFormData(configMap, AGGREGATE_LIMIT, "10");
|
||||||
|
|
||||||
|
|
||||||
|
String rawQuery = "{\n" +
|
||||||
|
" \"aggregate\": \"" + collectionName + "\",\n" +
|
||||||
|
" \"pipeline\": " + "[ {\"$sort\" : {\"_id\": 1} } ],\n" +
|
||||||
|
" \"limit\": 10,\n" +
|
||||||
|
" \"explain\": \"true\"\n" + // Specifies to return the information on the processing of the pipeline. (This also avoids the use of the 'cursor' aggregate key according to Mongo doc)
|
||||||
|
"}\n";
|
||||||
|
|
||||||
|
return Collections.singletonList(new DatasourceStructure.Template(
|
||||||
|
"Aggregate",
|
||||||
|
rawQuery,
|
||||||
|
configMap
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,31 @@
|
||||||
package com.external.plugins.commands;
|
package com.external.plugins.commands;
|
||||||
|
|
||||||
import com.appsmith.external.models.ActionConfiguration;
|
import com.appsmith.external.models.ActionConfiguration;
|
||||||
|
import com.appsmith.external.models.DatasourceStructure;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
import org.pf4j.util.StringUtils;
|
import org.pf4j.util.StringUtils;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static com.appsmith.external.helpers.PluginUtils.getValueSafelyFromFormData;
|
import static com.appsmith.external.helpers.PluginUtils.getValueSafelyFromFormData;
|
||||||
import static com.external.plugins.utils.MongoPluginUtils.parseSafely;
|
import static com.external.plugins.utils.MongoPluginUtils.parseSafely;
|
||||||
|
import static com.appsmith.external.helpers.PluginUtils.setValueSafelyInFormData;
|
||||||
import static com.appsmith.external.helpers.PluginUtils.validConfigurationPresentInFormData;
|
import static com.appsmith.external.helpers.PluginUtils.validConfigurationPresentInFormData;
|
||||||
import static com.external.plugins.constants.FieldName.COUNT_QUERY;
|
import static com.external.plugins.constants.FieldName.COUNT_QUERY;
|
||||||
|
import static com.external.plugins.constants.FieldName.SMART_SUBSTITUTION;
|
||||||
|
import static com.external.plugins.constants.FieldName.COLLECTION;
|
||||||
|
import static com.external.plugins.constants.FieldName.COMMAND;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
public class Count extends MongoCommand {
|
public class Count extends MongoCommand {
|
||||||
String query;
|
String query;
|
||||||
|
|
||||||
|
|
@ -42,4 +53,27 @@ public class Count extends MongoCommand {
|
||||||
|
|
||||||
return document;
|
return document;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<DatasourceStructure.Template> generateTemplate(Map<String, Object> templateConfiguration) {
|
||||||
|
String collectionName = (String) templateConfiguration.get("collectionName");
|
||||||
|
|
||||||
|
Map<String, Object> configMap = new HashMap<>();
|
||||||
|
|
||||||
|
setValueSafelyInFormData(configMap, SMART_SUBSTITUTION, Boolean.TRUE);
|
||||||
|
setValueSafelyInFormData(configMap, COMMAND, "COUNT");
|
||||||
|
setValueSafelyInFormData(configMap, COUNT_QUERY, "{\"_id\": {\"$exists\": true}}");
|
||||||
|
setValueSafelyInFormData(configMap, COLLECTION, collectionName);
|
||||||
|
|
||||||
|
String rawQuery = "{\n" +
|
||||||
|
" \"count\": \"" + collectionName + "\",\n" +
|
||||||
|
" \"query\": " + "{\"_id\": {\"$exists\": true}} \n" +
|
||||||
|
"}\n";
|
||||||
|
|
||||||
|
return Collections.singletonList(new DatasourceStructure.Template(
|
||||||
|
"Count",
|
||||||
|
rawQuery,
|
||||||
|
configMap
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,34 @@
|
||||||
package com.external.plugins.commands;
|
package com.external.plugins.commands;
|
||||||
|
|
||||||
import com.appsmith.external.models.ActionConfiguration;
|
import com.appsmith.external.models.ActionConfiguration;
|
||||||
|
import com.appsmith.external.models.DatasourceStructure;
|
||||||
import com.external.plugins.constants.FieldName;
|
import com.external.plugins.constants.FieldName;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
import org.pf4j.util.StringUtils;
|
import org.pf4j.util.StringUtils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static com.appsmith.external.helpers.PluginUtils.getValueSafelyFromFormData;
|
import static com.appsmith.external.helpers.PluginUtils.getValueSafelyFromFormData;
|
||||||
import static com.external.plugins.utils.MongoPluginUtils.parseSafely;
|
import static com.external.plugins.utils.MongoPluginUtils.parseSafely;
|
||||||
|
import static com.appsmith.external.helpers.PluginUtils.setValueSafelyInFormData;
|
||||||
import static com.appsmith.external.helpers.PluginUtils.validConfigurationPresentInFormData;
|
import static com.appsmith.external.helpers.PluginUtils.validConfigurationPresentInFormData;
|
||||||
import static com.external.plugins.constants.FieldName.DISTINCT_QUERY;
|
import static com.external.plugins.constants.FieldName.DISTINCT_QUERY;
|
||||||
|
import static com.external.plugins.constants.FieldName.DISTINCT_KEY;
|
||||||
|
import static com.external.plugins.constants.FieldName.COLLECTION;
|
||||||
|
import static com.external.plugins.constants.FieldName.COMMAND;
|
||||||
|
import static com.external.plugins.constants.FieldName.SMART_SUBSTITUTION;
|
||||||
|
import static com.external.plugins.constants.FieldName.KEY;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
public class Distinct extends MongoCommand {
|
public class Distinct extends MongoCommand {
|
||||||
String query;
|
String query;
|
||||||
String key;
|
String key;
|
||||||
|
|
@ -63,4 +76,32 @@ public class Distinct extends MongoCommand {
|
||||||
|
|
||||||
return document;
|
return document;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<DatasourceStructure.Template> generateTemplate(Map<String, Object> templateConfiguration) {
|
||||||
|
Map<String, Object> configMap = new HashMap<>();
|
||||||
|
String collectionName = (String) templateConfiguration.get("collectionName");
|
||||||
|
|
||||||
|
setValueSafelyInFormData(configMap, SMART_SUBSTITUTION, Boolean.TRUE);
|
||||||
|
setValueSafelyInFormData(configMap, COMMAND, "DISTINCT");
|
||||||
|
setValueSafelyInFormData(configMap, DISTINCT_QUERY, "{ \"_id\": ObjectId(\"id_of_document_to_distinct\") }");
|
||||||
|
setValueSafelyInFormData(configMap, DISTINCT_KEY, "_id");
|
||||||
|
setValueSafelyInFormData(configMap, COLLECTION, collectionName);
|
||||||
|
|
||||||
|
|
||||||
|
String rawQuery = "{\n" +
|
||||||
|
" \"distinct\": \"" + collectionName + "\",\n" +
|
||||||
|
" \"query\": { \"_id\": ObjectId(\"id_of_document_to_distinct\") }," +
|
||||||
|
" \"key\": \"_id\"," +
|
||||||
|
"}\n";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return Collections.singletonList(new DatasourceStructure.Template(
|
||||||
|
"Distinct",
|
||||||
|
rawQuery,
|
||||||
|
configMap
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -185,6 +185,19 @@ public class MongoPluginUtils {
|
||||||
templates.addAll(
|
templates.addAll(
|
||||||
new Delete().generateTemplate(templateConfiguration)
|
new Delete().generateTemplate(templateConfiguration)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
templates.addAll(
|
||||||
|
new Count().generateTemplate(templateConfiguration)
|
||||||
|
);
|
||||||
|
|
||||||
|
templates.addAll(
|
||||||
|
new Distinct().generateTemplate(templateConfiguration)
|
||||||
|
);
|
||||||
|
|
||||||
|
templates.addAll(
|
||||||
|
new Aggregate().generateTemplate(templateConfiguration)
|
||||||
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String urlEncode(String text) {
|
public static String urlEncode(String text) {
|
||||||
|
|
|
||||||
|
|
@ -580,6 +580,47 @@ public class MongoPluginTest {
|
||||||
"{ \"_id\": ObjectId(\"id_of_document_to_delete\") }");
|
"{ \"_id\": ObjectId(\"id_of_document_to_delete\") }");
|
||||||
assertEquals(getValueSafelyFromFormData((Map<String, Object>) deleteTemplate.getConfiguration(), DELETE_LIMIT),
|
assertEquals(getValueSafelyFromFormData((Map<String, Object>) deleteTemplate.getConfiguration(), DELETE_LIMIT),
|
||||||
"SINGLE");
|
"SINGLE");
|
||||||
|
|
||||||
|
// Assert Count Command
|
||||||
|
DatasourceStructure.Template countTemplate = templates.get(5);
|
||||||
|
assertEquals(countTemplate.getTitle(), "Count");
|
||||||
|
assertEquals(countTemplate.getBody(), "{\n" +
|
||||||
|
" \"count\": \"users\",\n" +
|
||||||
|
" \"query\": " + "{\"_id\": {\"$exists\": true}} \n" +
|
||||||
|
"}\n");
|
||||||
|
assertEquals(((Map<String, Object>) countTemplate.getConfiguration()).get(COMMAND), "COUNT");
|
||||||
|
assertEquals(getValueSafelyFromFormData((Map<String, Object>) countTemplate.getConfiguration(), COUNT_QUERY),
|
||||||
|
"{\"_id\": {\"$exists\": true}}");
|
||||||
|
|
||||||
|
// Assert Distinct Command
|
||||||
|
DatasourceStructure.Template distinctTemplate = templates.get(6);
|
||||||
|
assertEquals(distinctTemplate.getTitle(), "Distinct");
|
||||||
|
assertEquals(distinctTemplate.getBody(), "{\n" +
|
||||||
|
" \"distinct\": \"users\",\n" +
|
||||||
|
" \"query\": { \"_id\": ObjectId(\"id_of_document_to_distinct\") }," +
|
||||||
|
" \"key\": \"_id\"," +
|
||||||
|
"}\n");
|
||||||
|
assertEquals(((Map<String, Object>) distinctTemplate.getConfiguration()).get(COMMAND), "DISTINCT");
|
||||||
|
assertEquals(getValueSafelyFromFormData((Map<String, Object>) distinctTemplate.getConfiguration(), DISTINCT_QUERY),
|
||||||
|
"{ \"_id\": ObjectId(\"id_of_document_to_distinct\") }");
|
||||||
|
assertEquals(getValueSafelyFromFormData((Map<String, Object>) distinctTemplate.getConfiguration(), DISTINCT_KEY),
|
||||||
|
"_id");
|
||||||
|
|
||||||
|
// Assert Aggregate Command
|
||||||
|
DatasourceStructure.Template aggregateTemplate = templates.get(7);
|
||||||
|
assertEquals(aggregateTemplate.getTitle(), "Aggregate");
|
||||||
|
assertEquals(aggregateTemplate.getBody(), "{\n" +
|
||||||
|
" \"aggregate\": \"users\",\n" +
|
||||||
|
" \"pipeline\": " + "[ {\"$sort\" : {\"_id\": 1} } ],\n" +
|
||||||
|
" \"limit\": 10,\n" +
|
||||||
|
" \"explain\": \"true\"\n" +
|
||||||
|
"}\n");
|
||||||
|
|
||||||
|
assertEquals(((Map<String, Object>) aggregateTemplate.getConfiguration()).get(COMMAND), "AGGREGATE");
|
||||||
|
assertEquals(getValueSafelyFromFormData((Map<String, Object>) aggregateTemplate.getConfiguration(), AGGREGATE_PIPELINE),
|
||||||
|
"[ {\"$sort\" : {\"_id\": 1} } ]");
|
||||||
|
|
||||||
|
|
||||||
})
|
})
|
||||||
.verifyComplete();
|
.verifyComplete();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user