Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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) {
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();
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();
}
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());
}
}
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;
}
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;
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;
}
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;
}