UNPKG

6.67 kBJavaScriptView Raw
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT license.
3import { Database, Databases } from "./client/Database";
4import { Offer, Offers } from "./client/Offer";
5import { ClientContext } from "./ClientContext";
6import { parseConnectionString } from "./common";
7import { Constants } from "./common/constants";
8import { getUserAgent } from "./common/platform";
9import { defaultConnectionPolicy } from "./documents";
10import { GlobalEndpointManager } from "./globalEndpointManager";
11import { ResourceResponse } from "./request";
12import { checkURL } from "./utils/checkURL";
13/**
14 * Provides a client-side logical representation of the Azure Cosmos DB database account.
15 * This client is used to configure and execute requests in the Azure Cosmos DB database service.
16 * @example Instantiate a client and create a new database
17 * ```typescript
18 * const client = new CosmosClient({endpoint: "<URL HERE>", auth: {masterKey: "<KEY HERE>"}});
19 * await client.databases.create({id: "<datbase name here>"});
20 * ```
21 * @example Instantiate a client with custom Connection Policy
22 * ```typescript
23 * const connectionPolicy = new ConnectionPolicy();
24 * connectionPolicy.RequestTimeout = 10000;
25 * const client = new CosmosClient({
26 * endpoint: "<URL HERE>",
27 * auth: {masterKey: "<KEY HERE>"},
28 * connectionPolicy
29 * });
30 * ```
31 */
32export class CosmosClient {
33 constructor(optionsOrConnectionString) {
34 var _a, _b;
35 if (typeof optionsOrConnectionString === "string") {
36 optionsOrConnectionString = parseConnectionString(optionsOrConnectionString);
37 }
38 const endpoint = checkURL(optionsOrConnectionString.endpoint);
39 if (!endpoint) {
40 throw new Error("Invalid endpoint specified");
41 }
42 optionsOrConnectionString.connectionPolicy = Object.assign({}, defaultConnectionPolicy, optionsOrConnectionString.connectionPolicy);
43 optionsOrConnectionString.defaultHeaders = optionsOrConnectionString.defaultHeaders || {};
44 optionsOrConnectionString.defaultHeaders[Constants.HttpHeaders.CacheControl] = "no-cache";
45 optionsOrConnectionString.defaultHeaders[Constants.HttpHeaders.Version] =
46 Constants.CurrentVersion;
47 if (optionsOrConnectionString.consistencyLevel !== undefined) {
48 optionsOrConnectionString.defaultHeaders[Constants.HttpHeaders.ConsistencyLevel] =
49 optionsOrConnectionString.consistencyLevel;
50 }
51 optionsOrConnectionString.defaultHeaders[Constants.HttpHeaders.UserAgent] = getUserAgent(optionsOrConnectionString.userAgentSuffix);
52 const globalEndpointManager = new GlobalEndpointManager(optionsOrConnectionString, async (opts) => this.getDatabaseAccount(opts));
53 this.clientContext = new ClientContext(optionsOrConnectionString, globalEndpointManager);
54 if (((_a = optionsOrConnectionString.connectionPolicy) === null || _a === void 0 ? void 0 : _a.enableEndpointDiscovery) &&
55 ((_b = optionsOrConnectionString.connectionPolicy) === null || _b === void 0 ? void 0 : _b.enableBackgroundEndpointRefreshing)) {
56 this.backgroundRefreshEndpointList(globalEndpointManager, optionsOrConnectionString.connectionPolicy.endpointRefreshRateInMs ||
57 defaultConnectionPolicy.endpointRefreshRateInMs);
58 }
59 this.databases = new Databases(this, this.clientContext);
60 this.offers = new Offers(this, this.clientContext);
61 }
62 /**
63 * Get information about the current {@link DatabaseAccount} (including which regions are supported, etc.)
64 */
65 async getDatabaseAccount(options) {
66 const response = await this.clientContext.getDatabaseAccount(options);
67 return new ResourceResponse(response.result, response.headers, response.code);
68 }
69 /**
70 * Gets the currently used write endpoint url. Useful for troubleshooting purposes.
71 *
72 * The url may contain a region suffix (e.g. "-eastus") if we're using location specific endpoints.
73 */
74 getWriteEndpoint() {
75 return this.clientContext.getWriteEndpoint();
76 }
77 /**
78 * Gets the currently used read endpoint. Useful for troubleshooting purposes.
79 *
80 * The url may contain a region suffix (e.g. "-eastus") if we're using location specific endpoints.
81 */
82 getReadEndpoint() {
83 return this.clientContext.getReadEndpoint();
84 }
85 /**
86 * Gets the known write endpoints. Useful for troubleshooting purposes.
87 *
88 * The urls may contain a region suffix (e.g. "-eastus") if we're using location specific endpoints.
89 */
90 getWriteEndpoints() {
91 return this.clientContext.getWriteEndpoints();
92 }
93 /**
94 * Gets the currently used read endpoint. Useful for troubleshooting purposes.
95 *
96 * The url may contain a region suffix (e.g. "-eastus") if we're using location specific endpoints.
97 */
98 getReadEndpoints() {
99 return this.clientContext.getReadEndpoints();
100 }
101 /**
102 * Used for reading, updating, or deleting a existing database by id or accessing containers belonging to that database.
103 *
104 * This does not make a network call. Use `.read` to get info about the database after getting the {@link Database} object.
105 *
106 * @param id - The id of the database.
107 * @example Create a new container off of an existing database
108 * ```typescript
109 * const container = client.database("<database id>").containers.create("<container id>");
110 * ```
111 *
112 * @example Delete an existing database
113 * ```typescript
114 * await client.database("<id here>").delete();
115 * ```
116 */
117 database(id) {
118 return new Database(this, id, this.clientContext);
119 }
120 /**
121 * Used for reading, or updating a existing offer by id.
122 * @param id - The id of the offer.
123 */
124 offer(id) {
125 return new Offer(this, id, this.clientContext);
126 }
127 /**
128 * Clears background endpoint refresher. Use client.dispose() when destroying the CosmosClient within another process.
129 */
130 dispose() {
131 clearTimeout(this.endpointRefresher);
132 }
133 async backgroundRefreshEndpointList(globalEndpointManager, refreshRate) {
134 this.endpointRefresher = setInterval(() => {
135 try {
136 globalEndpointManager.refreshEndpointList();
137 }
138 catch (e) {
139 console.warn("Failed to refresh endpoints", e);
140 }
141 }, refreshRate);
142 if (this.endpointRefresher.unref && typeof this.endpointRefresher.unref === "function") {
143 this.endpointRefresher.unref();
144 }
145 }
146}
147//# sourceMappingURL=CosmosClient.js.map
\No newline at end of file