use of com.azure.cosmos.models.CosmosStoredProcedureProperties in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SampleStoredProcedureAsync method readAllSprocs.
private void readAllSprocs() throws Exception {
CosmosPagedFlux<CosmosStoredProcedureProperties> fluxResponse = container.getScripts().readAllStoredProcedures();
final CountDownLatch completionLatch = new CountDownLatch(1);
fluxResponse.flatMap(storedProcedureProperties -> {
logger.info(String.format("Stored Procedure: %s\n", storedProcedureProperties.getId()));
return Mono.empty();
}).subscribe(s -> {
}, err -> {
if (err instanceof CosmosException) {
// Client-specific errors
CosmosException cerr = (CosmosException) err;
cerr.printStackTrace();
logger.info(String.format("Read Item failed with %s\n", cerr));
} else {
// General errors
err.printStackTrace();
}
completionLatch.countDown();
}, () -> {
completionLatch.countDown();
});
completionLatch.await();
}
use of com.azure.cosmos.models.CosmosStoredProcedureProperties in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SampleStoredProcedureAsync method createStoredProcedure.
public void createStoredProcedure() throws Exception {
logger.info("Creating stored procedure...\n");
sprocId = "createMyDocument";
String sprocBody = "function createMyDocument() {\n" + "var documentToCreate = {\"id\":\"test_doc\"}\n" + "var context = getContext();\n" + "var collection = context.getCollection();\n" + "var accepted = collection.createDocument(collection.getSelfLink(), documentToCreate,\n" + " function (err, documentCreated) {\n" + "if (err) throw new Error('Error' + err.message);\n" + "context.getResponse().setBody(documentCreated.id)\n" + "});\n" + "if (!accepted) return;\n" + "}";
CosmosStoredProcedureProperties storedProcedureDef = new CosmosStoredProcedureProperties(sprocId, sprocBody);
container.getScripts().createStoredProcedure(storedProcedureDef, new CosmosStoredProcedureRequestOptions()).block();
}
use of com.azure.cosmos.models.CosmosStoredProcedureProperties in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SampleStoredProcedure method createStoredProcedure.
public void createStoredProcedure() throws Exception {
logger.info("Creating stored procedure...");
sprocId = "createMyDocument";
String sprocBody = "function createMyDocument() {\n" + "var documentToCreate = {\"id\":\"test_doc\", \"city\":\"Seattle\"}\n" + "var context = getContext();\n" + "var collection = context.getCollection();\n" + "var accepted = collection.createDocument(collection.getSelfLink(), documentToCreate,\n" + " function (err, documentCreated) {\n" + "if (err) throw new Error('Error' + err.message);\n" + "context.getResponse().setBody(documentCreated.id)\n" + "});\n" + "if (!accepted) return;\n" + "}";
CosmosStoredProcedureProperties storedProcedureDef = new CosmosStoredProcedureProperties(sprocId, sprocBody);
container.getScripts().createStoredProcedure(storedProcedureDef, new CosmosStoredProcedureRequestOptions());
}
use of com.azure.cosmos.models.CosmosStoredProcedureProperties in project DataSpaceConnector by eclipse-dataspaceconnector.
the class CosmosDbApiImpl method uploadStoredProcedure.
@Override
public void uploadStoredProcedure(String name) {
// upload stored procedure
var is = Thread.currentThread().getContextClassLoader().getResourceAsStream(name + ".js");
if (is == null) {
throw new AssertionError("The input stream referring to the " + name + " file cannot be null!");
}
var s = new Scanner(is).useDelimiter("\\A");
if (!s.hasNext()) {
throw new IllegalArgumentException("Error loading resource with name " + ".js");
}
var body = s.next();
var props = new CosmosStoredProcedureProperties(name, body);
var scripts = container.getScripts();
if (scripts.readAllStoredProcedures().stream().noneMatch(sp -> sp.getId().equals(name))) {
scripts.createStoredProcedure(props);
}
}
use of com.azure.cosmos.models.CosmosStoredProcedureProperties in project scalardb by scalar-labs.
the class CosmosAdmin method createContainer.
private void createContainer(String database, String table, TableMetadata metadata) throws ExecutionException {
if (!databaseExists(database)) {
throw new ExecutionException("the database does not exists");
}
CosmosDatabase cosmosDatabase = client.getDatabase(database);
CosmosContainerProperties properties = computeContainerProperties(table, metadata);
cosmosDatabase.createContainer(properties);
CosmosStoredProcedureProperties storedProcedureProperties = computeContainerStoredProcedureProperties();
cosmosDatabase.getContainer(table).getScripts().createStoredProcedure(storedProcedureProperties);
}
Aggregations