@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
133 lines (132 loc) • 5 kB
JavaScript
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 Triggers_exports = {};
__export(Triggers_exports, {
Triggers: () => Triggers
});
module.exports = __toCommonJS(Triggers_exports);
var import_common = require("../../common/index.js");
var import_queryIterator = require("../../queryIterator.js");
var import_Trigger = require("./Trigger.js");
var import_TriggerResponse = require("./TriggerResponse.js");
var import_diagnostics = require("../../utils/diagnostics.js");
class Triggers {
/**
* @hidden
* @param container - The parent {@link Container}.
*/
constructor(container, clientContext) {
this.container = container;
this.clientContext = clientContext;
}
query(query, options) {
const path = (0, import_common.getPathFromLink)(this.container.url, import_common.ResourceType.trigger);
const id = (0, import_common.getIdFromLink)(this.container.url);
return new import_queryIterator.QueryIterator(this.clientContext, query, options, (diagnosticNode, innerOptions) => {
return this.clientContext.queryFeed({
path,
resourceType: import_common.ResourceType.trigger,
resourceId: id,
resultFn: (result) => result.Triggers,
query,
options: innerOptions,
diagnosticNode
});
});
}
/**
* Read all Triggers.
* @example Read all trigger to array.
* ```ts snippet:TriggersReadAllTriggers
* 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: triggerList } = await container.scripts.triggers.readAll().fetchAll();
* ```
*/
readAll(options) {
return this.query(void 0, options);
}
/**
* Create a trigger.
*
* Azure Cosmos DB supports pre and post triggers defined in JavaScript to be executed
* on creates, updates and deletes.
*
* For additional details, refer to the server-side JavaScript API documentation.
* @example
* ```ts snippet:TriggersCreate
* import { CosmosClient, TriggerDefinition, TriggerType, TriggerOperation } 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 triggerDefinition: TriggerDefinition = {
* id: "sample trigger",
* body: "serverScript() { var x = 10; }",
* triggerType: TriggerType.Pre,
* triggerOperation: TriggerOperation.All,
* };
*
* const { resource: trigger } = await container.scripts.triggers.create(triggerDefinition);
* ```
*/
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.trigger);
const id = (0, import_common.getIdFromLink)(this.container.url);
const response = await this.clientContext.create({
body,
path,
resourceType: import_common.ResourceType.trigger,
resourceId: id,
options,
diagnosticNode
});
const ref = new import_Trigger.Trigger(this.container, response.result.id, this.clientContext);
return new import_TriggerResponse.TriggerResponse(
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 = {
Triggers
});