共用方式為


StoredProcedure class

依標識符讀取、取代、刪除或執行特定現有預存程序的作業。

若要讓作業建立、讀取或查詢預存程式,

屬性

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

傳回