@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
205 lines (204 loc) • 8.05 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 Databases_exports = {};
__export(Databases_exports, {
Databases: () => Databases
});
module.exports = __toCommonJS(Databases_exports);
var import_common = require("../../common/index.js");
var import_queryExecutionContext = require("../../queryExecutionContext/index.js");
var import_queryIterator = require("../../queryIterator.js");
var import_Database = require("./Database.js");
var import_DatabaseResponse = require("./DatabaseResponse.js");
var import_offers = require("../../utils/offers.js");
var import_diagnostics = require("../../utils/diagnostics.js");
class Databases {
/**
* @hidden
* @param client - The parent {@link CosmosClient} for the Database.
*/
constructor(client, clientContext, encryptionManager) {
this.client = client;
this.clientContext = clientContext;
this.encryptionManager = encryptionManager;
}
query(query, options) {
const cb = (diagNode, innerOptions) => {
return this.clientContext.queryFeed({
path: "/dbs",
resourceType: import_common.ResourceType.database,
resourceId: "",
resultFn: (result) => result.Databases,
query,
options: innerOptions,
diagnosticNode: diagNode
});
};
return new import_queryIterator.QueryIterator(this.clientContext, query, options, cb);
}
/**
* Send a request for creating a database.
*
* A database manages users, permissions and a set of containers.
* Each Azure Cosmos DB Database Account is able to support multiple independent named databases,
* with the database being the logical container for data.
*
* Each Database consists of one or more containers, each of which in turn contain one or more
* documents. Since databases are an administrative resource, the Service Master Key will be
* required in order to access and successfully complete any action using the User APIs.
*
* @param body - The {@link DatabaseDefinition} that represents the {@link Database} to be created.
* @param options - Use to set options like response page size, continuation tokens, etc.
* @example
* ```ts snippet:CosmosClientDatabases
* 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 { resource: databaseDefinition, database } = await client.databases.create({
* id: "<name here>",
* });
* ```
*/
async create(body, options = {}) {
return (0, import_diagnostics.withDiagnostics)(async (diagnosticNode) => {
return this.createInternal(diagnosticNode, body, options);
}, this.clientContext);
}
/**
* @hidden
*/
async createInternal(diagnosticNode, body, options = {}) {
const err = {};
if (!(0, import_common.isResourceValid)(body, err)) {
throw err;
}
(0, import_offers.validateOffer)(body);
if (body.maxThroughput) {
const autoscaleParams = {
maxThroughput: body.maxThroughput
};
if (body.autoUpgradePolicy) {
autoscaleParams.autoUpgradePolicy = body.autoUpgradePolicy;
}
const autoscaleHeaders = JSON.stringify(autoscaleParams);
options.initialHeaders = Object.assign({}, options.initialHeaders, {
[import_common.Constants.HttpHeaders.AutoscaleSettings]: autoscaleHeaders
});
delete body.maxThroughput;
delete body.autoUpgradePolicy;
}
if (body.throughput) {
options.initialHeaders = Object.assign({}, options.initialHeaders, {
[import_common.Constants.HttpHeaders.OfferThroughput]: body.throughput
});
delete body.throughput;
}
const path = "/dbs";
const response = await this.clientContext.create({
body,
path,
resourceType: import_common.ResourceType.database,
resourceId: void 0,
diagnosticNode,
options
});
const ref = new import_Database.Database(
this.client,
body.id,
this.clientContext,
this.encryptionManager,
response.result._rid
);
return new import_DatabaseResponse.DatabaseResponse(
response.result,
response.headers,
response.code,
ref,
(0, import_diagnostics.getEmptyCosmosDiagnostics)()
);
}
/**
* Check if a database exists, and if it doesn't, create it.
* This will make a read operation based on the id in the `body`, then if it is not found, a create operation.
*
* A database manages users, permissions and a set of containers.
* Each Azure Cosmos DB Database Account is able to support multiple independent named databases,
* with the database being the logical container for data.
*
* Each Database consists of one or more containers, each of which in turn contain one or more
* documents. Since databases are an an administrative resource, the Service Master Key will be
* required in order to access and successfully complete any action using the User APIs.
*
* @param body - The {@link DatabaseDefinition} that represents the {@link Database} to be created.
* @param options - Additional options for the request
* @example
* ```ts snippet:ReadmeSampleCreateDatabase
* 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" });
* ```
*/
async createIfNotExists(body, options) {
if (!body || body.id === null || body.id === void 0) {
throw new Error("body parameter must be an object with an id property");
}
return (0, import_diagnostics.withDiagnostics)(async (diagnosticNode) => {
try {
const readResponse = await this.client.database(body.id).readInternal(diagnosticNode, options);
return readResponse;
} catch (err) {
if (err.code === import_common.StatusCodes.NotFound) {
const createResponse = await this.createInternal(diagnosticNode, body, options);
(0, import_queryExecutionContext.mergeHeaders)(createResponse.headers, err.headers);
return createResponse;
} else {
throw err;
}
}
}, this.clientContext);
}
// TODO: DatabaseResponse for QueryIterator?
/**
* Reads all databases.
* @param options - Use to set options like response page size, continuation tokens, etc.
* @returns {@link QueryIterator} Allows you to return all databases in an array or iterate over them one at a time.
* @example Read all databases to array.
* ```ts snippet:DatabasesReadAll
* 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 { resources: databaseList } = await client.databases.readAll().fetchAll();
* ```
*/
readAll(options) {
return this.query(void 0, options);
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Databases
});