How to use @azure/cosmos - 8 common examples

To help you get started, we’ve selected a few @azure/cosmos examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pulumi / examples / azure-ts-serverless-url-shortener-global / index.ts View on Github external
callbackFactory: () => {
            const endpoint = account.endpoint.get();
            const key = account.primaryMasterKey.get();

            const client = new CosmosClient({ endpoint, key, connectionPolicy: { preferredLocations: [location] } });
            const container = client.database(database.name.get()).container(collection.name.get());
            return async (_, request: azure.appservice.HttpRequest) => {
                const key = request.params["key"];
                if (key === "ping") {
                    // Handle traffic manager live pings
                    return { status: 200, body: "Ping ACK" };
                }

                try {
                    const response = await container.item(key, undefined).read();
                    return response.resource && response.resource.url
                            // HTTP redirect for known URLs
                            ? { status: 301, headers: { location: response.resource.url }, body: "" }
                            // 404 for malformed documents
                            : { status: 404, body: "" };
                } catch (e) {
github pulumi / examples / azure-ts-cosmosapp-component / functionApp.ts View on Github external
callbackFactory: () => {
                const client = new CosmosClient({
                    endpoint: cosmosAccount.endpoint.get(),
                    key: cosmosAccount.primaryMasterKey.get(),
                    connectionPolicy: { preferredLocations: [location] },
                });
                const collection = client.database(database.name.get()).container(container.name.get());

                return async (_, request: azure.appservice.HttpRequest) => {
                    const key = request.params.key;
                    if (key === "ping") {
                        // Handle traffic manager live pings
                        return { status: 200, body: "Ping ACK" };
                    }

                    try {
                        const response = await collection.item(key, undefined).read();
github Azure / azure-sdk-for-js / common / smoke-test / CosmosDB.ts View on Github external
static async Run() {
    console.log(CosmosDB.dedent`
        ------------------------
        Cosmos DB
        ------------------------
        1) Create a database
        2) Create a collection
        3) Create documents (items) in the collection
        4) Delete the database (Clean up the resource)
        `);

    const endpoint = process.env["COSMOS_ENDPOINT"] || "";
    const masterKey = process.env["COSMOS_KEY"] || "";
    CosmosDB.client = new CosmosClient({ endpoint, key: masterKey });

    //Ensure that the resource is clean
    try {
      await CosmosDB.DeleteDatabase();
    } catch {}

    await CosmosDB.CreateDatabase();
    await CosmosDB.CreateCollection();
    await CosmosDB.CreateDocuments();
    await CosmosDB.DeleteDatabase();
  }
github microsoft / botbuilder-js / libraries / botbuilder-azure / src / cosmosDbPartitionedStorage.ts View on Github external
public async initialize(): Promise {
        if (!this.container) {
            if (!this.client) {
                this.client = new CosmosClient({
                    endpoint: this.cosmosDbStorageOptions.cosmosDbEndpoint,
                    key: this.cosmosDbStorageOptions.authKey,
                    ...this.cosmosDbStorageOptions.cosmosClientOptions,
                });
            }
            this.container = await _doOnce.waitFor(async ()=> await this.getOrCreateContainer());
        }
    }
github microsoft / WebTemplateStudio / mocks / react-mocks / server / sql / sqlClient.js View on Github external
constructor(databaseId, containerId) {
    this.client = new CosmosClient({
      endpoint: process.env.COSMOSDB_URI,
      auth: {
        masterKey: process.env.COSMOSDB_PRIMARY_KEY
      }
    });

    this.databaseId = databaseId;
    this.containerId = containerId;

    this.database = null;
    this.container = null;
  }
github johnpapa / heroes-angular-serverless / server / services / config.ts View on Github external
import { CosmosClient } from '@azure/cosmos';

const endpoint = process.env.CORE_API_URL;
const masterKey = process.env.CORE_API_KEY;
const databaseDefName = 'hero-db';
const heroContainerName = 'heroes';
const villainContainerName = 'villains';

const client = new CosmosClient({ endpoint, key: masterKey });

const containers = {
  heroes: client.database(databaseDefName).container(heroContainerName),
  villains: client.database(databaseDefName).container(villainContainerName)
};

export default containers;
github tmobile / jazz / builds / jazz_azure-create-service / components / function / cosmosDBHandler.js View on Github external
async function createDatabaseWithEndpoint(data, client, endpoint) {

  const keys = await client.databaseAccounts.listKeys(data.resourceGroupName, data.database_account);
  const masterKey = keys.primaryMasterKey;

  const dbClient = new CosmosClient({ endpoint, auth: { masterKey } });
  const dbResponse = await dbClient.databases.createIfNotExists({
    id: data.database
  });
  const database = dbResponse.database;

  await database.containers.createIfNotExists({
    id: data.table
  });

  return;
}
github pulumi / examples / azure-ts-serverless-url-shortener-global / cosmosclient.ts View on Github external
export async function getContainer(endpoint: string, masterKey: string, region: string) {
    const client = new cosmos.CosmosClient({
        endpoint,
        key: masterKey,
        connectionPolicy: { preferredLocations: [region] },
    });

    const db = client.database("thedb");
    const { container } = await db.containers.createIfNotExists({ id: "urls" });
    return container;
}

@azure/cosmos

Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API

MIT
Latest version published 1 month ago

Package Health Score

83 / 100
Full package analysis

Popular @azure/cosmos functions