Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@azure/cosmos

Package Overview
Dependencies
Maintainers
2
Versions
808
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@azure/cosmos

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

  • 4.2.1-alpha.20241125.2
  • npm
  • Socket score

Version published
Weekly downloads
513K
decreased by-9.98%
Maintainers
2
Weekly downloads
 
Created

What is @azure/cosmos?

@azure/cosmos is an npm package that provides a client library for interacting with Azure Cosmos DB, a globally distributed, multi-model database service. The package allows developers to perform various database operations such as creating, reading, updating, and deleting databases, containers, and items. It also supports querying data using SQL-like syntax and managing throughput and indexing policies.

What are @azure/cosmos's main functionalities?

Create a Database

This feature allows you to create a new database in Azure Cosmos DB if it does not already exist. The code sample demonstrates how to initialize the CosmosClient and create a database named 'sample-database'.

const { CosmosClient } = require('@azure/cosmos');
const endpoint = 'your-endpoint';
const key = 'your-key';
const client = new CosmosClient({ endpoint, key });
async function createDatabase() {
  const { database } = await client.databases.createIfNotExists({ id: 'sample-database' });
  console.log(`Created database: ${database.id}`);
}
createDatabase().catch(console.error);

Create a Container

This feature allows you to create a new container within a specified database in Azure Cosmos DB. The code sample demonstrates how to create a container named 'sample-container' with a specified partition key.

const { CosmosClient } = require('@azure/cosmos');
const endpoint = 'your-endpoint';
const key = 'your-key';
const client = new CosmosClient({ endpoint, key });
async function createContainer() {
  const database = client.database('sample-database');
  const { container } = await database.containers.createIfNotExists({ id: 'sample-container', partitionKey: { kind: 'Hash', paths: ['/partitionKey'] } });
  console.log(`Created container: ${container.id}`);
}
createContainer().catch(console.error);

Insert an Item

This feature allows you to insert a new item into a specified container in Azure Cosmos DB. The code sample demonstrates how to insert an item with an id, partition key, and name into the 'sample-container'.

const { CosmosClient } = require('@azure/cosmos');
const endpoint = 'your-endpoint';
const key = 'your-key';
const client = new CosmosClient({ endpoint, key });
async function insertItem() {
  const container = client.database('sample-database').container('sample-container');
  const item = { id: '1', partitionKey: 'partition1', name: 'Sample Item' };
  const { resource } = await container.items.create(item);
  console.log(`Inserted item: ${resource.id}`);
}
insertItem().catch(console.error);

Query Items

This feature allows you to query items in a specified container using SQL-like syntax. The code sample demonstrates how to query items with a specific partition key from the 'sample-container'.

const { CosmosClient } = require('@azure/cosmos');
const endpoint = 'your-endpoint';
const key = 'your-key';
const client = new CosmosClient({ endpoint, key });
async function queryItems() {
  const container = client.database('sample-database').container('sample-container');
  const querySpec = {
    query: 'SELECT * FROM c WHERE c.partitionKey = @partitionKey',
    parameters: [{ name: '@partitionKey', value: 'partition1' }]
  };
  const { resources: items } = await container.items.query(querySpec).fetchAll();
  items.forEach(item => console.log(`Queried item: ${item.id}`));
}
queryItems().catch(console.error);

Other packages similar to @azure/cosmos

Keywords

FAQs

Package last updated on 26 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc