Share via


StoredProcedures class

Operations for creating, upserting, or reading/querying all Stored Procedures.

For operations to read, replace, delete, or execute a specific, existing stored procedure by id, see container.storedProcedure().

Properties

container

Methods

create(StoredProcedureDefinition, RequestOptions)

Create a StoredProcedure.

Azure Cosmos DB allows stored procedures to be executed in the storage tier, directly against an item container. The script gets executed under ACID transactions on the primary storage partition of the specified container. For additional details, refer to the server-side JavaScript API documentation.

Example

import { CosmosClient, StoredProcedureDefinition } from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({ endpoint, key });
const { database } = await client.databases.createIfNotExists({ id: "Test Database" });
const { container } = await database.containers.createIfNotExists({ id: "Test Container" });

const sprocDefinition: StoredProcedureDefinition = {
  id: "sample sproc",
  body: "function () { const x = 10; }",
};

const { resource: sproc } = await container.scripts.storedProcedures.create(sprocDefinition);
query(SqlQuerySpec, FeedOptions)

Query all Stored Procedures.

Example

Read all stored procedures to array.

import { CosmosClient, SqlQuerySpec } from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({ endpoint, key });

const { database } = await client.databases.createIfNotExists({ id: "Test Database" });

const { container } = await database.containers.createIfNotExists({ id: "Test Container" });

const querySpec: SqlQuerySpec = {
  query: `SELECT * FROM root r WHERE r.id = @sproc`,
  parameters: [{ name: "@sproc", value: "Todo" }],
};
const { resources: storedProceduresList } = await container.scripts.storedProcedures
  .query(querySpec)
  .fetchAll();
query<T>(SqlQuerySpec, FeedOptions)

Query all Stored Procedures.

Example

Read all stored procedures to array.

import { CosmosClient, SqlQuerySpec } from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({ endpoint, key });

const { database } = await client.databases.createIfNotExists({ id: "Test Database" });

const { container } = await database.containers.createIfNotExists({ id: "Test Container" });

const querySpec: SqlQuerySpec = {
  query: `SELECT * FROM root r WHERE r.id = @sproc`,
  parameters: [{ name: "@sproc", value: "Todo" }],
};
const { resources: storedProceduresList } = await container.scripts.storedProcedures
  .query(querySpec)
  .fetchAll();
readAll(FeedOptions)

Read all stored procedures.

Example

Read all stored procedures to array.

import { CosmosClient } from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({ endpoint, key });

const { database } = await client.databases.createIfNotExists({ id: "Test Database" });

const { container } = await database.containers.createIfNotExists({ id: "Test Container" });

const { resources: storedProceduresList } = await container.scripts.storedProcedures
  .readAll()
  .fetchAll();

Property Details

container

container: Container

Property Value

Method Details

create(StoredProcedureDefinition, RequestOptions)

Create a StoredProcedure.

Azure Cosmos DB allows stored procedures to be executed in the storage tier, directly against an item container. The script gets executed under ACID transactions on the primary storage partition of the specified container. For additional details, refer to the server-side JavaScript API documentation.

Example

import { CosmosClient, StoredProcedureDefinition } from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({ endpoint, key });
const { database } = await client.databases.createIfNotExists({ id: "Test Database" });
const { container } = await database.containers.createIfNotExists({ id: "Test Container" });

const sprocDefinition: StoredProcedureDefinition = {
  id: "sample sproc",
  body: "function () { const x = 10; }",
};

const { resource: sproc } = await container.scripts.storedProcedures.create(sprocDefinition);
function create(body: StoredProcedureDefinition, options?: RequestOptions): Promise<StoredProcedureResponse>

Parameters

options
RequestOptions

Returns

query(SqlQuerySpec, FeedOptions)

Query all Stored Procedures.

Example

Read all stored procedures to array.

import { CosmosClient, SqlQuerySpec } from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({ endpoint, key });

const { database } = await client.databases.createIfNotExists({ id: "Test Database" });

const { container } = await database.containers.createIfNotExists({ id: "Test Container" });

const querySpec: SqlQuerySpec = {
  query: `SELECT * FROM root r WHERE r.id = @sproc`,
  parameters: [{ name: "@sproc", value: "Todo" }],
};
const { resources: storedProceduresList } = await container.scripts.storedProcedures
  .query(querySpec)
  .fetchAll();
function query(query: SqlQuerySpec, options?: FeedOptions): QueryIterator<any>

Parameters

query
SqlQuerySpec

Query configuration for the operation. See SqlQuerySpec for more info on how to configure a query.

options
FeedOptions

Returns

query<T>(SqlQuerySpec, FeedOptions)

Query all Stored Procedures.

Example

Read all stored procedures to array.

import { CosmosClient, SqlQuerySpec } from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({ endpoint, key });

const { database } = await client.databases.createIfNotExists({ id: "Test Database" });

const { container } = await database.containers.createIfNotExists({ id: "Test Container" });

const querySpec: SqlQuerySpec = {
  query: `SELECT * FROM root r WHERE r.id = @sproc`,
  parameters: [{ name: "@sproc", value: "Todo" }],
};
const { resources: storedProceduresList } = await container.scripts.storedProcedures
  .query(querySpec)
  .fetchAll();
function query<T>(query: SqlQuerySpec, options?: FeedOptions): QueryIterator<T>

Parameters

query
SqlQuerySpec

Query configuration for the operation. See SqlQuerySpec for more info on how to configure a query.

options
FeedOptions

Returns

readAll(FeedOptions)

Read all stored procedures.

Example

Read all stored procedures to array.

import { CosmosClient } from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com";
const key = "<database account masterkey>";
const client = new CosmosClient({ endpoint, key });

const { database } = await client.databases.createIfNotExists({ id: "Test Database" });

const { container } = await database.containers.createIfNotExists({ id: "Test Container" });

const { resources: storedProceduresList } = await container.scripts.storedProcedures
  .readAll()
  .fetchAll();
function readAll(options?: FeedOptions): QueryIterator<StoredProcedureDefinition & Resource>

Parameters

options
FeedOptions

Returns