UNPKG

@azure/cosmos

Version:
227 lines (226 loc) • 8.8 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var StoredProcedure_exports = {}; __export(StoredProcedure_exports, { StoredProcedure: () => StoredProcedure }); module.exports = __toCommonJS(StoredProcedure_exports); var import_common = require("../../common/index.js"); var import_extractPartitionKey = require("../../extractPartitionKey.js"); var import_request = require("../../request/index.js"); var import_ClientUtils = require("../ClientUtils.js"); var import_StoredProcedureResponse = require("./StoredProcedureResponse.js"); var import_diagnostics = require("../../utils/diagnostics.js"); class StoredProcedure { /** * Creates a new instance of {@link StoredProcedure} linked to the parent {@link Container}. * @param container - The parent {@link Container}. * @param id - The id of the given {@link StoredProcedure}. * @hidden */ constructor(container, id, clientContext) { this.container = container; this.id = id; this.clientContext = clientContext; } /** * Returns a reference URL to the resource. Used for linking in Permissions. */ get url() { return (0, import_common.createStoredProcedureUri)(this.container.database.id, this.container.id, this.id); } /** * Read the {@link StoredProcedureDefinition} for the given {@link StoredProcedure}. * @example * ```ts snippet:StoredProcedureRead * 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(); * ``` */ async read(options) { return (0, import_diagnostics.withDiagnostics)(async (diagnosticNode) => { const path = (0, import_common.getPathFromLink)(this.url); const id = (0, import_common.getIdFromLink)(this.url); const response = await this.clientContext.read({ path, resourceType: import_common.ResourceType.sproc, resourceId: id, options, diagnosticNode }); return new import_StoredProcedureResponse.StoredProcedureResponse( response.result, response.headers, response.code, this, (0, import_diagnostics.getEmptyCosmosDiagnostics)() ); }, this.clientContext); } /** * Replace the given {@link StoredProcedure} with the specified {@link StoredProcedureDefinition}. * @param body - The specified {@link StoredProcedureDefinition} to replace the existing definition. * @example * ```ts snippet:StoredProcedureReplace * 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); * ``` */ async replace(body, options) { return (0, import_diagnostics.withDiagnostics)(async (diagnosticNode) => { if (body.body) { body.body = body.body.toString(); } const err = {}; if (!(0, import_common.isResourceValid)(body, err)) { throw err; } const path = (0, import_common.getPathFromLink)(this.url); const id = (0, import_common.getIdFromLink)(this.url); const response = await this.clientContext.replace({ body, path, resourceType: import_common.ResourceType.sproc, resourceId: id, options, diagnosticNode }); return new import_StoredProcedureResponse.StoredProcedureResponse( response.result, response.headers, response.code, this, (0, import_diagnostics.getEmptyCosmosDiagnostics)() ); }, this.clientContext); } /** * Delete the given {@link StoredProcedure}. * @example * ```ts snippet:StoredProcedureDelete * 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(); * ``` */ async delete(options) { return (0, import_diagnostics.withDiagnostics)(async (diagnosticNode) => { const path = (0, import_common.getPathFromLink)(this.url); const id = (0, import_common.getIdFromLink)(this.url); const response = await this.clientContext.delete({ path, resourceType: import_common.ResourceType.sproc, resourceId: id, options, diagnosticNode }); return new import_StoredProcedureResponse.StoredProcedureResponse( response.result, response.headers, response.code, this, (0, import_diagnostics.getEmptyCosmosDiagnostics)() ); }, this.clientContext); } /** * Execute the given {@link StoredProcedure}. * * The specified type, T, is not enforced by the client. * Be sure to validate the response from the stored procedure matches the type, T, you provide. * * @param partitionKey - The partition key to use when executing the stored procedure * @param params - Array of parameters to pass as arguments to the given {@link StoredProcedure}. * @param options - Additional options, such as the partition key to invoke the {@link StoredProcedure} on. * * @example * ```ts snippet:StoredProcedureExecute * 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); * ``` */ async execute(partitionKey, params, options) { return (0, import_diagnostics.withDiagnostics)(async (diagnosticNode) => { if (partitionKey === void 0) { const partitionKeyResponse = await (0, import_ClientUtils.readPartitionKeyDefinition)( diagnosticNode, this.container ); partitionKey = (0, import_extractPartitionKey.undefinedPartitionKey)(partitionKeyResponse); } const response = await this.clientContext.execute({ sprocLink: this.url, params, options, partitionKey, diagnosticNode }); return new import_request.ResourceResponse( response.result, response.headers, response.code, (0, import_diagnostics.getEmptyCosmosDiagnostics)() ); }, this.clientContext); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { StoredProcedure });