@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
173 lines (172 loc) • 6.07 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 User_exports = {};
__export(User_exports, {
User: () => User
});
module.exports = __toCommonJS(User_exports);
var import_common = require("../../common/index.js");
var import_Permission = require("../Permission/index.js");
var import_UserResponse = require("./UserResponse.js");
var import_diagnostics = require("../../utils/diagnostics.js");
class User {
/**
* @hidden
* @param database - The parent {@link Database}.
*/
constructor(database, id, clientContext) {
this.database = database;
this.id = id;
this.clientContext = clientContext;
this.permissions = new import_Permission.Permissions(this, this.clientContext);
}
/**
* Operations for creating, upserting, querying, or reading all operations.
*
* See `client.permission(id)` to read, replace, or delete a specific Permission by id.
*/
permissions;
/**
* Returns a reference URL to the resource. Used for linking in Permissions.
*/
get url() {
return (0, import_common.createUserUri)(this.database.id, this.id);
}
/**
* Operations to read, replace, or delete a specific Permission by id.
*
* See `client.permissions` for creating, upserting, querying, or reading all operations.
*/
permission(id) {
return new import_Permission.Permission(this, id, this.clientContext);
}
/**
* Read the {@link UserDefinition} for the given {@link User}.
* @example
* ```ts snippet:UserRead
* 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 { resource: user } = await database.user("<user-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.user,
resourceId: id,
options,
diagnosticNode
});
return new import_UserResponse.UserResponse(
response.result,
response.headers,
response.code,
this,
(0, import_diagnostics.getEmptyCosmosDiagnostics)()
);
}, this.clientContext);
}
/**
* Replace the given {@link User}'s definition with the specified {@link UserDefinition}.
* @param body - The specified {@link UserDefinition} to replace the definition.
* @example
* ```ts snippet:UserReplace
* 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 { resource: user } = await database.user("<user-id>").read();
* user.id = "<new user id>";
*
* await database.user("<user-id>").replace(user);
* ```
*/
async replace(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.url);
const id = (0, import_common.getIdFromLink)(this.url);
const response = await this.clientContext.replace({
body,
path,
resourceType: import_common.ResourceType.user,
resourceId: id,
options,
diagnosticNode
});
return new import_UserResponse.UserResponse(
response.result,
response.headers,
response.code,
this,
(0, import_diagnostics.getEmptyCosmosDiagnostics)()
);
}, this.clientContext);
}
/**
* Delete the given {@link User}.
* @example
* ```ts snippet:UserDelete
* 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.user("<user-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.user,
resourceId: id,
options,
diagnosticNode
});
return new import_UserResponse.UserResponse(
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 = {
User
});