UNPKG

@azure/cosmos

Version:
171 lines (170 loc) 6.53 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 UserDefinedFunction_exports = {}; __export(UserDefinedFunction_exports, { UserDefinedFunction: () => UserDefinedFunction }); module.exports = __toCommonJS(UserDefinedFunction_exports); var import_common = require("../../common/index.js"); var import_UserDefinedFunctionResponse = require("./UserDefinedFunctionResponse.js"); var import_diagnostics = require("../../utils/diagnostics.js"); class UserDefinedFunction { /** * @hidden * @param container - The parent {@link Container}. * @param id - The id of the given {@link UserDefinedFunction}. */ 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.createUserDefinedFunctionUri)(this.container.database.id, this.container.id, this.id); } /** * Read the {@link UserDefinedFunctionDefinition} for the given {@link UserDefinedFunction}. * @example * ```ts snippet:UserDefinedFunctionRead * 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: udf } = await container.scripts.userDefinedFunction("<udf-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.udf, resourceId: id, options, diagnosticNode }); return new import_UserDefinedFunctionResponse.UserDefinedFunctionResponse( response.result, response.headers, response.code, this, (0, import_diagnostics.getEmptyCosmosDiagnostics)() ); }, this.clientContext); } /** * Replace the given {@link UserDefinedFunction} with the specified {@link UserDefinedFunctionDefinition}. * @param options - * @example * ```ts snippet:UserDefinedFunctionReplace * import { CosmosClient, UserDefinedFunctionDefinition } 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 udfDefinition: UserDefinedFunctionDefinition = { * id: "sample udf", * body: "function () { const x = 10; }", * }; * await container.scripts.userDefinedFunctions.create(udfDefinition); * * udfDefinition.body = "function () { const x = 20; }"; * const { resource: replacedUdf } = await container.scripts * .userDefinedFunction(udfDefinition.id) * .replace(udfDefinition); * ``` */ 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.udf, resourceId: id, options, diagnosticNode }); return new import_UserDefinedFunctionResponse.UserDefinedFunctionResponse( response.result, response.headers, response.code, this, (0, import_diagnostics.getEmptyCosmosDiagnostics)() ); }, this.clientContext); } /** * Delete the given {@link UserDefined}. * @example * ```ts snippet:UserDefinedFunctionDelete * 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.userDefinedFunction("<udf-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.udf, resourceId: id, options, diagnosticNode }); return new import_UserDefinedFunctionResponse.UserDefinedFunctionResponse( response.result, response.headers, response.code, this, (0, import_diagnostics.getEmptyCosmosDiagnostics)() ); }, this.clientContext); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { UserDefinedFunction });