UNPKG

@azure/cosmos

Version:
157 lines (156 loc) 5.71 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 Users_exports = {}; __export(Users_exports, { Users: () => Users }); module.exports = __toCommonJS(Users_exports); var import_common = require("../../common/index.js"); var import_queryIterator = require("../../queryIterator.js"); var import_User = require("./User.js"); var import_UserResponse = require("./UserResponse.js"); var import_diagnostics = require("../../utils/diagnostics.js"); class Users { /** * @hidden * @param database - The parent {@link Database}. */ constructor(database, clientContext) { this.database = database; this.clientContext = clientContext; } query(query, options) { const path = (0, import_common.getPathFromLink)(this.database.url, import_common.ResourceType.user); const id = (0, import_common.getIdFromLink)(this.database.url); return new import_queryIterator.QueryIterator(this.clientContext, query, options, (diagnosticNode, innerOptions) => { return this.clientContext.queryFeed({ path, resourceType: import_common.ResourceType.user, resourceId: id, resultFn: (result) => result.Users, query, options: innerOptions, diagnosticNode }); }); } /** * Read all users.- * @example Read all users to array. * ```ts snippet:UsersReadAll * 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 { resources: usersList } = await database.users.readAll().fetchAll(); * ``` */ readAll(options) { return this.query(void 0, options); } /** * Create a database user with the specified {@link UserDefinition}. * @param body - The specified {@link UserDefinition}. * @example * ```ts snippet:UsersCreate * 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" }); * * await database.users.create({ id: "<user-id>" }); * ``` */ async create(body, options) { return (0, import_diagnostics.withDiagnostics)(async (diagnosticNode) => { const err = {}; if (!(0, import_common.isResourceValid)(body, err)) { throw err; } const path = (0, import_common.getPathFromLink)(this.database.url, import_common.ResourceType.user); const id = (0, import_common.getIdFromLink)(this.database.url); const response = await this.clientContext.create({ body, path, resourceType: import_common.ResourceType.user, resourceId: id, options, diagnosticNode }); const ref = new import_User.User(this.database, response.result.id, this.clientContext); return new import_UserResponse.UserResponse( response.result, response.headers, response.code, ref, (0, import_diagnostics.getEmptyCosmosDiagnostics)() ); }, this.clientContext); } /** * Upsert a database user with a specified {@link UserDefinition}. * @param body - The specified {@link UserDefinition}. * @example * ```ts snippet:UsersUpsert * 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" }); * * await database.users.upsert({ id: "<user-id>" }); * ``` */ async upsert(body, options) { return (0, import_diagnostics.withDiagnostics)(async (diagnosticNode) => { const err = {}; if (!(0, import_common.isResourceValid)(body, err)) { throw err; } const path = (0, import_common.getPathFromLink)(this.database.url, import_common.ResourceType.user); const id = (0, import_common.getIdFromLink)(this.database.url); const response = await this.clientContext.upsert({ body, path, resourceType: import_common.ResourceType.user, resourceId: id, options, diagnosticNode }); const ref = new import_User.User(this.database, response.result.id, this.clientContext); return new import_UserResponse.UserResponse( 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 = { Users });