你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

StoredProcedure class

按 ID 读取、替换、删除或执行特定现有存储过程的作。

若要执行创建、读取所有或查询存储过程的作,

属性

container
id
url

返回资源的引用 URL。 用于在权限中链接。

方法

delete(RequestOptions)

删除给定的 StoredProcedure

示例

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" });

await container.scripts.storedProcedure("<sproc-id>").delete();
execute<T>(PartitionKey, any[], RequestOptions)

执行给定的 StoredProcedure

客户端不强制执行指定的类型 T。 请务必验证来自存储过程的响应是否与你提供的类型 T 匹配。

示例

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 { resource: result } = await container.scripts
  .storedProcedure("<sproc-id>")
  .execute(undefined);
read(RequestOptions)

读取给定 StoredProcedureStoredProcedureDefinition

示例

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 { resource: sproc } = await container.scripts.storedProcedure("<sproc-id>").read();
replace(StoredProcedureDefinition, RequestOptions)

将给定的 StoredProcedure 替换为指定的 StoredProcedureDefinition

示例

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);

sproc.body = function () {
  const x = 20;
  console.log(x);
};
const { resource: replacedSproc } = await container.scripts
  .storedProcedure(sproc.id)
  .replace(sproc);

属性详细信息

container

container: Container

属性值

id

id: string

属性值

string

url

返回资源的引用 URL。 用于在权限中链接。

string url

属性值

string

方法详细信息

delete(RequestOptions)

删除给定的 StoredProcedure

示例

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" });

await container.scripts.storedProcedure("<sproc-id>").delete();
function delete(options?: RequestOptions): Promise<StoredProcedureResponse>

参数

options
RequestOptions

返回

execute<T>(PartitionKey, any[], RequestOptions)

执行给定的 StoredProcedure

客户端不强制执行指定的类型 T。 请务必验证来自存储过程的响应是否与你提供的类型 T 匹配。

示例

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 { resource: result } = await container.scripts
  .storedProcedure("<sproc-id>")
  .execute(undefined);
function execute<T>(partitionKey: PartitionKey, params?: any[], options?: RequestOptions): Promise<ResourceResponse<T>>

参数

partitionKey
PartitionKey

执行存储过程时要使用的分区键

params

any[]

要作为参数传递给给定 StoredProcedure的参数数组。

options
RequestOptions

其他选项,例如要调用 StoredProcedure 的分区键。 *

返回

Promise<ResourceResponse<T>>

read(RequestOptions)

读取给定 StoredProcedureStoredProcedureDefinition

示例

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 { resource: sproc } = await container.scripts.storedProcedure("<sproc-id>").read();
function read(options?: RequestOptions): Promise<StoredProcedureResponse>

参数

options
RequestOptions

返回

replace(StoredProcedureDefinition, RequestOptions)

将给定的 StoredProcedure 替换为指定的 StoredProcedureDefinition

示例

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);

sproc.body = function () {
  const x = 20;
  console.log(x);
};
const { resource: replacedSproc } = await container.scripts
  .storedProcedure(sproc.id)
  .replace(sproc);
function replace(body: StoredProcedureDefinition, options?: RequestOptions): Promise<StoredProcedureResponse>

参数

body
StoredProcedureDefinition

指定的 StoredProcedureDefinition 替换现有定义。

options
RequestOptions

返回