UNPKG

@azure/cosmos

Version:
134 lines (133 loc) 5.2 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 StoredProcedures_exports = {}; __export(StoredProcedures_exports, { StoredProcedures: () => StoredProcedures }); module.exports = __toCommonJS(StoredProcedures_exports); var import_common = require("../../common/index.js"); var import_queryIterator = require("../../queryIterator.js"); var import_StoredProcedure = require("./StoredProcedure.js"); var import_StoredProcedureResponse = require("./StoredProcedureResponse.js"); var import_diagnostics = require("../../utils/diagnostics.js"); class StoredProcedures { /** * @param container - The parent {@link Container}. * @hidden */ constructor(container, clientContext) { this.container = container; this.clientContext = clientContext; } query(query, options) { const path = (0, import_common.getPathFromLink)(this.container.url, import_common.ResourceType.sproc); const id = (0, import_common.getIdFromLink)(this.container.url); return new import_queryIterator.QueryIterator(this.clientContext, query, options, (diagNode, innerOptions) => { return this.clientContext.queryFeed({ path, resourceType: import_common.ResourceType.sproc, resourceId: id, resultFn: (result) => result.StoredProcedures, query, options: innerOptions, diagnosticNode: diagNode }); }); } /** * Read all stored procedures. * @example Read all stored procedures to array. * ```ts snippet:StoredProceduresReadAll * 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(); * ``` */ readAll(options) { return this.query(void 0, options); } /** * 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 * ```ts snippet:StoredProceduresCreate * 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); * ``` */ async create(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.container.url, import_common.ResourceType.sproc); const id = (0, import_common.getIdFromLink)(this.container.url); const response = await this.clientContext.create({ body, path, resourceType: import_common.ResourceType.sproc, resourceId: id, options, diagnosticNode }); const ref = new import_StoredProcedure.StoredProcedure(this.container, response.result.id, this.clientContext); return new import_StoredProcedureResponse.StoredProcedureResponse( response.result, response.headers, response.code, ref, (0, import_diagnostics.getEmptyCosmosDiagnostics)() ); }, this.clientContext); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { StoredProcedures });