教程:使用 JavaScript SDK 生成 Node.js 控制台应用以管理 Azure Cosmos DB SQL API 数据Tutorial: Build a Node.js console app with the JavaScript SDK to manage Azure Cosmos DB SQL API data

作为开发人员,你可能有使用 NoSQL 文件数据的应用程序。As a developer, you might have applications that use NoSQL document data. 可以使用 Azure Cosmos DB 中的 SQL API 帐户存储和访问此文档数据。You can use a SQL API account in Azure Cosmos DB to store and access this document data. 本教程介绍如何生成 Node.js 控制台应用程序,以便创建 Azure Cosmos DB 资源并对其进行查询。This tutorial shows you how to build a Node.js console application to create Azure Cosmos DB resources and query them.

在本教程中,你将:In this tutorial, you will:

  • 创建并连接到 Azure Cosmos DB 帐户。Create and connect to an Azure Cosmos DB account.
  • 设置应用程序。Set up your application.
  • 创建数据库。Create a database.
  • 创建容器。Create a container.
  • 向容器添加项。Add items to the container.
  • 对项、容器和数据库执行基本操作。Perform basic operations on the items, container, and database.

先决条件Prerequisites

请确保具有以下资源:Make sure you have the following resources:

创建 Azure Cosmos DB 帐户Create Azure Cosmos DB account

创建 Azure Cosmos DB 帐户。Let's create an Azure Cosmos DB account. 如果已经有想要使用的帐户,可以跳到 安装 Node.js 应用程序If you already have an account you want to use, you can skip ahead to Set up your Node.js application. 如果使用 Azure Cosmos DB 模拟器,请遵循 Azure Cosmos DB 模拟器中的步骤设置该模拟器,并直接跳到设置 Node.js 应用程序If you are using the Azure Cosmos DB Emulator, follow the steps at Azure Cosmos DB Emulator to set up the emulator and skip ahead to Set up your Node.js application.

  1. 转到 Azure 门户以创建 Azure Cosmos DB 帐户。Go to the Azure portal to create an Azure Cosmos DB account. 搜索“Azure Cosmos DB”,然后选择它。 。Search for and select Azure Cosmos DB.

    Azure 门户“数据库”窗格

  2. 选择“添加” 。Select Add.

  3. 在“创建 Azure Cosmos DB 帐户”页上,输入新 Azure Cosmos 帐户的基本设置 。On the Create Azure Cosmos DB Account page, enter the basic settings for the new Azure Cosmos account.

    设置Setting Value DescriptionDescription
    订阅Subscription 订阅名称Subscription name 选择要用于此 Azure Cosmos 帐户的 Azure 订阅。Select the Azure subscription that you want to use for this Azure Cosmos account.
    资源组Resource Group 资源组名称Resource group name 选择一个资源组,或者选择“新建”,然后输入新资源组的唯一名称。 Select a resource group, or select Create new, then enter a unique name for the new resource group.
    帐户名Account Name 唯一的名称A unique name 输入标识此 Azure Cosmos 帐户的名称。Enter a name to identify your Azure Cosmos account. 由于 documents.azure.cn 字符串将追加到所提供的 ID 后面以创建 URI,因此,请使用唯一的 ID。Because documents.azure.cn is appended to the ID that you provide to create your URI, use a unique ID.

    ID 只能包含小写字母、数字和连字符 (-) 字符。The ID can only contain lowercase letters, numbers, and the hyphen (-) character. 它的长度必须介于 3 到 31 个字符之间。It must be between 3-31 characters in length.
    APIAPI 要创建的帐户的类型The type of account to create 选择“Core (SQL)”,以便使用 SQL 语法创建文档数据库并进行查询 。Select Core (SQL) to create a document database and query by using SQL syntax.

    API 确定要创建的帐户的类型。The API determines the type of account to create. Azure Cosmos DB 提供五种 API:适用于文档数据的 Core (SQL) 和 MongoDB、适用于图形数据的 Gremlin、Azure 表和 Cassandra。Azure Cosmos DB provides five APIs: Core (SQL) and MongoDB for document data, Gremlin for graph data, Azure Table, and Cassandra. 目前,你必须为每种 API 创建单独的帐户。Currently, you must create a separate account for each API.

    详细了解 SQL APILearn more about the SQL API.
    位置Location 离用户最近的区域The region closest to your users 选择用于托管 Azure Cosmos DB 帐户的地理位置。Select a geographic location to host your Azure Cosmos DB account. 使用离用户最近的位置,使他们能够以最快的速度访问数据。Use the location that is closest to your users to give them the fastest access to the data.

    Azure Cosmos DB 的“新建帐户”页

  4. 选择“查看 + 创建” 。Select Review + create. 可以跳过“网络”和“标记”部分 。You can skip the Network and Tags sections.

  5. 检查帐户设置,然后选择“创建”。 Review the account settings, and then select Create. 创建帐户需要几分钟时间。It takes a few minutes to create the account. 等待门户页显示“你的部署已完成” 消息。Wait for the portal page to display Your deployment is complete.

    Azure 门户“通知”窗格

  6. 选择“转到资源”,转到 Azure Cosmos DB 帐户页。 Select Go to resource to go to the Azure Cosmos DB account page.

    Azure Cosmos DB 帐户页

设置 Node.js 应用程序Set up your Node.js application

在开始编写生成应用程序所需的代码之前,可以生成应用的框架。Before you start writing code to build the application, you can build the framework for your app. 运行以下步骤,设置包含框架代码的 Node.js 应用程序:Run the following steps to set up your Node.js application that has the framework code:

  1. 打开偏好的终端。Open your favorite terminal.

  2. 找到想要在其中保存 Node.js 应用程序的文件夹或目录。Locate the folder or directory where you'd like to save your Node.js application.

  3. 使用以下命令创建两个空的 JavaScript 文件:Create two empty JavaScript files with the following commands:

    • Windows:Windows:

      • fsutil file createnew app.js 0
      • fsutil file createnew config.js 0
    • Linux/OS X:Linux/OS X:

      • touch app.js
      • touch config.js
  4. 创建并初始化 package.json 文件。Create and initialize a package.json file. 请使用以下命令:Use the following command:

    • npm init -y
  5. 通过 npm 安装 @azure/cosmos 模块。Install the @azure/cosmos module via npm. 请使用以下命令:Use the following command:

    • npm install @azure/cosmos --save

设置应用的配置Set your app's configurations

有了应用以后,需确保它可以与 Azure Cosmos DB 通信。Now that your app exists, you need to make sure it can talk to Azure Cosmos DB. 如以下步骤所示,更新一些配置设置即可将应用设置为与 Azure Cosmos DB 通信:By updating a few configuration settings, as shown in the following steps, you can set your app to talk to Azure Cosmos DB:

  1. 在喜爱的文本编辑器中打开 config.jsOpen config.js in your favorite text editor.

  2. 复制并粘贴以下代码片段,并将属性 config.endpointconfig.key 设置为 Azure Cosmos DB 终结点 URI 和主密钥。Copy and paste the code snippet below and set properties config.endpoint and config.key to your Azure Cosmos DB endpoint URI and primary key. 这两项配置都可以在 Azure 门户中找到。Both these configurations can be found in the Azure portal.

    从 Azure 门户获取密钥的屏幕截图

    // ADD THIS PART TO YOUR CODE
    var config = {}
    
    config.endpoint = "~your Azure Cosmos DB endpoint uri here~";
    config.key = "~your primary key here~";
    
  3. 复制 databasecontaineritems 数据并将其粘贴到在其中设置了 config.endpointconfig.key 属性的 config 对象(见下)。Copy and paste the database, container, and items data to your config object below where you set your config.endpoint and config.key properties. 如果已有要在数据库中存储的数据,则可以使用 Azure Cosmos DB 中的数据迁移工具,而不是在此处定义数据。If you already have data you'd like to store in your database, you can use the Data Migration tool in Azure Cosmos DB rather than defining the data here. config.js 文件应包含以下代码:You config.js file should have the following code:

    var config = {}
    
    config.endpoint = '~your Azure Cosmos DB account endpoint uri here~'
    config.key = '~your primary key here~'
    
    config.database = {
     id: 'FamilyDatabase'
    }
    
    config.container = {
     id: 'FamilyContainer'
    }
    
    config.items = {
     Andersen: {
       id: 'Anderson.1',
       Country: 'USA',
       lastName: 'Andersen',
       parents: [
         {
           firstName: 'Thomas'
         },
         {
           firstName: 'Mary Kay'
         }
       ],
       children: [
         {
           firstName: 'Henriette Thaulow',
           gender: 'female',
           grade: 5,
           pets: [
             {
               givenName: 'Fluffy'
             }
           ]
         }
       ],
       address: {
         state: 'WA',
         county: 'King',
         city: 'Seattle'
       }
     },
     Wakefield: {
       id: 'Wakefield.7',
       Country: 'Italy',
       parents: [
         {
           familyName: 'Wakefield',
           firstName: 'Robin'
         },
         {
           familyName: 'Miller',
           firstName: 'Ben'
         }
       ],
       children: [
         {
           familyName: 'Merriam',
           firstName: 'Jesse',
           gender: 'female',
           grade: 8,
           pets: [
             {
               givenName: 'Goofy'
             },
             {
               givenName: 'Shadow'
             }
           ]
         },
         {
           familyName: 'Miller',
           firstName: 'Lisa',
           gender: 'female',
           grade: 1
         }
       ],
       address: {
         state: 'NY',
         county: 'Manhattan',
         city: 'NY'
       },
       isRegistered: false
     }
    }
    
    module.exports = config
    
    

    JavaScript SDK 使用通用术语“容器”和“项”。 JavaScript SDK uses the generic terms container and item. 容器可以是集合、图或表。A container can be a collection, graph, or table. 项可以是文档、边缘/顶点或行,是容器中的内容。An item can be a document, edge/vertex, or row, and is the content inside a container.

    module.exports = config; 代码用于导出 config 对象,以便可以在 app.js 文件中引用该对象。module.exports = config; code is used to export your config object, so that you can reference it within the app.js file.

连接到 Azure Cosmos DB 帐户Connect to an Azure Cosmos DB account

  1. 在文本编辑器中打开空的 app.js 文件。Open your empty app.js file in the text editor. 复制并粘贴以下代码,以导入 @azure/cosmos 模块和新建的 config 模块。Copy and paste the code below to import the @azure/cosmos module and your newly created config module.

    // ADD THIS PART TO YOUR CODE
    const CosmosClient = require('@azure/cosmos').CosmosClient;
    
    const config = require('./config');
    
  2. 复制并粘贴以下代码,以使用前面保存的 config.endpointconfig.key 来创建新的 CosmosClient。Copy and paste the code to use the previously saved config.endpoint and config.key to create a new CosmosClient.

    const config = require('./config');
    
    // ADD THIS PART TO YOUR CODE
    const endpoint = config.endpoint;
    const key = config.key;
    
    const client = new CosmosClient({ endpoint, key });
    

备注

如果连接到 Cosmos DB 模拟器,请为节点进程禁用 SSL 验证:If connecting to the Cosmos DB Emulator, disable SSL verification for your node process:

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
const client = new CosmosClient({ endpoint, key });

现已获得用于初始化 Azure Cosmos DB 客户端的代码,接下来请看如何使用 Azure Cosmos DB 资源。Now that you have the code to initialize the Azure Cosmos DB client, let's take a look at how to work with Azure Cosmos DB resources.

创建数据库Create a database

  1. 复制并粘贴以下代码,以设置数据库 ID 和容器 ID。Copy and paste the code below to set the database ID, and the container ID. 通过这些 ID,可了解 Azure Cosmos DB 客户端如何查找正确的数据库和容器。These IDs are how the Azure Cosmos DB client will find the right database and container.

    const client = new CosmosClient({ endpoint, key });
    
    // ADD THIS PART TO YOUR CODE
    const HttpStatusCodes = { NOTFOUND: 404 };
    
    const databaseId = config.database.id;
    const containerId = config.container.id;
    const partitionKey = { kind: "Hash", paths: ["/Country"] };
    

    可以使用 Databases 类的 createIfNotExists 或 create 函数创建数据库。A database can be created by using either the createIfNotExists or create function of the Databases class. 数据库是跨容器分区的项的逻辑容器。A database is the logical container of items partitioned across containers.

  2. 复制 createDatabasereadDatabase 方法并将其粘贴到 app.js 文件的 databaseIdcontainerId 定义下。Copy and paste the createDatabase and readDatabase methods into the app.js file under the databaseId and containerId definition. createDatabase 函数会使用通过 config 对象指定的 ID FamilyDatabase 来创建新数据库(如果不存在数据库) 。The createDatabase function will create a new database with ID FamilyDatabase, specified from the config object if it does not already exist. readDatabase 函数会读取数据库的定义,确保数据库存在。The readDatabase function will read the database's definition to ensure that the database exists.

    /**
    * Create the database if it does not exist
    */
    async function createDatabase() {
       const { database } = await client.databases.createIfNotExists({ id: databaseId });
       console.log(`Created database:\n${database.id}\n`);
    }
    
    /**
    * Read the database definition
    */
    async function readDatabase() {
      const { resource: databaseDefinition } = await client.database(databaseId).read();
      console.log(`Reading database:\n${databaseDefinition.id}\n`);
    }
    
  3. 复制并粘贴以下代码,以便设置 createDatabasereadDatabase 函数,添加可列显退出消息的帮助程序函数 exitCopy and paste the code below where you set the createDatabase and readDatabase functions to add the helper function exit that will print the exit message.

    // ADD THIS PART TO YOUR CODE
    function exit(message) {
      console.log(message);
      console.log('Press any key to exit');
      process.stdin.setRawMode(true);
      process.stdin.resume();
      process.stdin.on('data', process.exit.bind(process, 0));
    };
    
  4. 复制并粘贴以下代码,以便设置 exit 函数,方便调用 createDatabasereadDatabase 函数。Copy and paste the code below where you set the exit function to call the createDatabase and readDatabase functions.

    createDatabase()
     .then(() => readDatabase())
     .then(() => { exit(`Completed successfully`); })
     .catch((error) => { exit(`Completed with error \${JSON.stringify(error)}`) });
    

    此时 app.js 中的代码应如以下代码所示:At this point, your code in app.js should now look as following code:

    const CosmosClient = require('@azure/cosmos').CosmosClient;
    
    const config = require('./config');
    
    const endpoint = config.endpoint;
    const key = config.key;
    
    const client = new CosmosClient({ endpoint, key });
    
    const HttpStatusCodes = { NOTFOUND: 404 };
    
    const databaseId = config.database.id;
    const containerId = config.container.id;
    const partitionKey = { kind: "Hash", paths: ["/Country"] };
    
    /**
    * Create the database if it does not exist
    */
    async function createDatabase() {
     const { database } = await client.databases.createIfNotExists({ id: databaseId });
     console.log(`Created database:\n${database.id}\n`);
    }
    
    /**
    * Read the database definition
    */
    async function readDatabase() {
     const { resource: databaseDefinition } = await client.database(databaseId).read();
    console.log(`Reading database:\n${databaseDefinition.id}\n`);
    }
    
    /**
    * Exit the app with a prompt
    * @param {message} message - The message to display
    */
    function exit(message) {
     console.log(message);
     console.log('Press any key to exit');
     process.stdin.setRawMode(true);
     process.stdin.resume();
     process.stdin.on('data', process.exit.bind(process, 0));
    }
    
    createDatabase()
     .then(() => readDatabase())
     .then(() => { exit(`Completed successfully`); })
     .catch((error) => { exit(`Completed with error ${JSON.stringify(error) }`) });
    
  5. 在终端中,找到 app.js 文件并运行以下命令:In your terminal, locate your app.js file and run the command:

    node app.js
    

创建容器Create a container

接下来在 Azure Cosmos DB 帐户中创建一个容器,用于存储和查询数据。Next create a container within the Azure Cosmos DB account, so that you can store and query the data.

警告

创建容器涉及到定价。Creating a container has pricing implications. 请访问定价页,了解相关信息。Visit our pricing page so you know what to expect.

可以使用 Containers 类的 createIfNotExists 或 create 函数创建容器。A container can be created by using either the createIfNotExists or create function from the Containers class. 容器包含项(在使用 SQL API 的情况下为 JSON 文档)和关联的 JavaScript 应用程序逻辑。A container consists of items (which in the case of the SQL API is JSON documents) and associated JavaScript application logic.

  1. 复制 createContainerreadContainer 函数并将其粘贴到 app.js 文件的 readDatabase 函数下。Copy and paste the createContainer and readContainer function underneath the readDatabase function in the app.js file. createContainer 函数会使用通过 config 对象指定的 containerId 来创建新容器(如果不存在容器)。The createContainer function will create a new container with the containerId specified from the config object if it does not already exist. readContainer 函数会读取容器定义,以便验证容器是否存在。The readContainer function will read the container definition to verify the container exists.

    /**
    * Create the container if it does not exist
    */
    
    async function createContainer() {
    
    const { container } = await client.database(databaseId).containers.createIfNotExists({ id: containerId, partitionKey }, { offerThroughput: 400 });
    console.log(`Created container:\n${config.container.id}\n`);
    }
    
    /**
    * Read the container definition
    */
    async function readContainer() {
      const { resource: containerDefinition } = await client.database(databaseId).container(containerId).read();
    console.log(`Reading container:\n${containerDefinition.id}\n`);
    }
    
  2. 复制以下代码并将其粘贴到对 readDatabase 的调用下面,以便执行 createContainerreadContainer 函数。Copy and paste the code underneath the call to readDatabase to execute the createContainer and readContainer functions.

    createDatabase()
     .then(() => readDatabase())
    
     // ADD THIS PART TO YOUR CODE
     .then(() => createContainer())
     .then(() => readContainer())
     // ENDS HERE
    
     .then(() => { exit(`Completed successfully`); })
     .catch((error) => { exit(`Completed with error ${JSON.stringify(error)}`) });
    

    此时 app.js 中的代码应如下所示:At this point, your code in app.js should now look like this:

    const CosmosClient = require('@azure/cosmos').CosmosClient;
    
    const config = require('./config');
    
    const endpoint = config.endpoint;
    const key = config.key;
    
    const client = new CosmosClient({ endpoint, key });
    
    const HttpStatusCodes = { NOTFOUND: 404 };
    
    const databaseId = config.database.id;
    const containerId = config.container.id;
    const partitionKey = { kind: "Hash", paths: ["/Country"] };
    
    /**
    * Create the database if it does not exist
    */
    async function createDatabase() {
     const { database } = await client.databases.createIfNotExists({ id: databaseId });
     console.log(`Created database:\n${database.id}\n`);
    }
    
    /**
    * Read the database definition
    */
    async function readDatabase() {
     const { body: databaseDefinition } = await client.database(databaseId).read();
     console.log(`Reading database:\n${databaseDefinition.id}\n`);
    }
    
    /**
    * Create the container if it does not exist
    */
    
    async function createContainer() {
    
    const { container } = await client.database(databaseId).containers.createIfNotExists({ id: containerId, partitionKey }, { offerThroughput: 400 });
    console.log(`Created container:\n${config.container.id}\n`);
    }
    
    /**
    * Read the container definition
    */
    async function readContainer() {
      const { resource: containerDefinition } = await client.database(databaseId).container(containerId).read();
    console.log(`Reading container:\n${containerDefinition.id}\n`);
    }
    
    /**
    * Exit the app with a prompt
    * @param {message} message - The message to display
    */
    function exit(message) {
     console.log(message);
     console.log('Press any key to exit');
     process.stdin.setRawMode(true);
     process.stdin.resume();
     process.stdin.on('data', process.exit.bind(process, 0));
    }
    
    createDatabase()
     .then(() => readDatabase())
     .then(() => createContainer())
     .then(() => readContainer())
     .then(() => { exit(`Completed successfully`); })
     .catch((error) => { exit(`Completed with error ${JSON.stringify(error)}`) });
    
  3. 在终端中,找到 app.js 文件并运行以下命令:In your terminal, locate your app.js file and run the command:

    node app.js
    

创建项Create an item

可以使用 Items 类的 create 函数创建项。An item can be created by using the create function of the Items class. 使用 SQL API 时,项会投射为文档,后者是用户定义的(任意)JSON 内容。When you're using the SQL API, items are projected as documents, which are user-defined (arbitrary) JSON content. 现在,可以将项插入 Azure Cosmos DB 中。You can now insert an item into Azure Cosmos DB.

  1. createFamilyItem 函数复制并粘贴到 readContainer 函数下面。Copy and paste the createFamilyItem function underneath the readContainer function. createFamilyItem 函数创建的项包含在 config 对象中保存的 JSON 数据。The createFamilyItem function creates the items containing the JSON data saved in the config object. 我们会进行检查以确保在创建项之前不存在具有相同 ID 的项。We'll check to make sure an item with the same ID does not already exist before creating it.

    /**
    * Create family item
    */
    async function createFamilyItem(itemBody) {
      const { item } = await client.database(databaseId).container(containerId).items.upsert(itemBody);
      console.log(`Created family item with id:\n${itemBody.id}\n`);
    };
    
  2. 复制以下代码并将其粘贴到对 readContainer 的调用下面,以便执行 createFamilyItem 函数。Copy and paste the code below the call to readContainer to execute the createFamilyItem function.

    createDatabase()
     .then(() => readDatabase())
     .then(() => createContainer())
     .then(() => readContainer())
    
     // ADD THIS PART TO YOUR CODE
     .then(() => createFamilyItem(config.items.Andersen))
     .then(() => createFamilyItem(config.items.Wakefield))
     // ENDS HERE
    
     .then(() => { exit(`Completed successfully`); })
     .catch((error) => { exit(`Completed with error ${JSON.stringify(error)}`) });
    
  3. 在终端中,找到 app.js 文件并运行以下命令:In your terminal, locate your app.js file and run the command:

    node app.js
    

查询 Azure Cosmos DB 资源Query Azure Cosmos DB resources

Azure Cosmos DB 支持对存储在每个容器中的 JSON 文档进行各种查询。Azure Cosmos DB supports rich queries against JSON documents stored in each container. 下面的示例代码演示了一个可以针对容器中文档运行的查询。The following sample code shows a query that you can run against the documents in your container.

  1. 复制 queryContainer 函数并将其粘贴到 app.js 文件的 createFamilyItem 函数下面。Copy and paste the queryContainer function below the createFamilyItem function in the app.js file. Azure Cosmos DB 支持类似 SQL 的查询,如下所示。Azure Cosmos DB supports SQL-like queries as shown below.

    /**
    * Query the container using SQL
    */
    async function queryContainer() {
     console.log(`Querying container:\n${config.container.id}`);
    
     // query to return all children in a family
     const querySpec = {
        query: "SELECT VALUE r.children FROM root r WHERE r.lastName = @lastName",
        parameters: [
            {
                name: "@lastName",
                value: "Andersen"
            }
        ]
    };
    
    const { resources } = await client.database(databaseId).container(containerId).items.query(querySpec, {enableCrossPartitionQuery:true}).fetchAll();
    for (var queryResult of resources) {
        let resultString = JSON.stringify(queryResult);
        console.log(`\tQuery returned ${resultString}\n`);
    }
    };
    
  2. 复制以下代码并将其粘贴到对 createFamilyItem 的调用下面,以便执行 queryContainer 函数。Copy and paste the code below the calls to createFamilyItem to execute the queryContainer function.

    createDatabase()
     .then(() => readDatabase())
     .then(() => createContainer())
     .then(() => readContainer())
     .then(() => createFamilyItem(config.items.Andersen))
     .then(() => createFamilyItem(config.items.Wakefield))
    
     // ADD THIS PART TO YOUR CODE
     .then(() => queryContainer())
     // ENDS HERE
    
     .then(() => { exit(`Completed successfully`); })
     .catch((error) => { exit(`Completed with error ${JSON.stringify(error)}`) });
    
  3. 在终端中,找到 app.js 文件并运行以下命令:In your terminal, locate your app.js file and run the command:

    node app.js
    

替换项Replace an item

Azure Cosmos DB 支持替换项的内容。Azure Cosmos DB supports replacing the content of items.

  1. 复制 replaceFamilyItem 函数并将其粘贴到 app.js 文件的 queryContainer 函数下面。Copy and paste the replaceFamilyItem function below the queryContainer function in the app.js file. 请注意,我们已将某个子项的属性“grade”从以前的值 5 更改为 6。Note we've changed the property 'grade' of a child to 6 from the previous value of 5.

    // ADD THIS PART TO YOUR CODE
    /**
    * Replace the item by ID.
    */
    async function replaceFamilyItem(itemBody) {
      console.log(`Replacing item:\n${itemBody.id}\n`);
      // Change property 'grade'
      itemBody.children[0].grade = 6;
      const { item } = await client.database(databaseId).container(containerId).item(itemBody.id, itemBody.Country).replace(itemBody);
    };
    
  2. 复制以下代码并将其粘贴到对 queryContainer 的调用下面,以便执行 replaceFamilyItem 函数。Copy and paste the code below the call to queryContainer to execute the replaceFamilyItem function. 另外,请再次将此代码添加到 queryContainer 调用,以验证是否已成功更改项。Also, add the code to call queryContainer again to verify that item has successfully changed.

    createDatabase()
     .then(() => readDatabase())
     .then(() => createContainer())
     .then(() => readContainer())
     .then(() => createFamilyItem(config.items.Andersen))
     .then(() => createFamilyItem(config.items.Wakefield))
     .then(() => queryContainer())
    
     // ADD THIS PART TO YOUR CODE
     .then(() => replaceFamilyItem(config.items.Andersen))
     .then(() => queryContainer())
     // ENDS HERE
    
     .then(() => { exit(`Completed successfully`); })
     .catch((error) => { exit(`Completed with error ${JSON.stringify(error)}`) });
    
  3. 在终端中,找到 app.js 文件并运行以下命令:In your terminal, locate your app.js file and run the command:

    node app.js
    

删除项Delete an item

Azure Cosmos DB 支持删除 JSON 项。Azure Cosmos DB supports deleting JSON items.

  1. deleteFamilyItem 函数复制并粘贴到 replaceFamilyItem 函数下面。Copy and paste the deleteFamilyItem function underneath the replaceFamilyItem function.

    /**
    * Delete the item by ID.
    */
    async function deleteFamilyItem(itemBody) {
      await client.database(databaseId).container(containerId).item(itemBody.id, itemBody.Country).delete(itemBody);
      console.log(`Deleted item:\n${itemBody.id}\n`);
    };
    
  2. 复制以下代码并将其粘贴到对第二个 queryContainer 的调用下面,以便执行 deleteFamilyItem 函数。Copy and paste the code below the call to the second queryContainer to execute the deleteFamilyItem function.

    createDatabase()
      .then(() => readDatabase())
      .then(() => createContainer())
      .then(() => readContainer())
      .then(() => createFamilyItem(config.items.Andersen))
      .then(() => createFamilyItem(config.items.Wakefield))
      .then(() => queryContainer
      ())
      .then(() => replaceFamilyItem(config.items.Andersen))
      .then(() => queryContainer())
    
    // ADD THIS PART TO YOUR CODE
      .then(() => deleteFamilyItem(config.items.Andersen))
    // ENDS HERE
    
    .then(() => { exit(`Completed successfully`); })
    .catch((error) => { exit(`Completed with error ${JSON.stringify(error)}`) });
    
  3. 在终端中,找到 app.js 文件并运行以下命令:In your terminal, locate your app.js file and run the command:

    node app.js
    

删除数据库Delete the database

删除已创建的数据库会删除该数据库及其所有子资源(容器、项等)。Deleting the created database will remove the database and all children resources (containers, items, etc.).

  1. 复制 cleanup 函数并将其粘贴到 deleteFamilyItem 函数下面,以便删除数据库及其所有子资源。Copy and paste the cleanup function underneath the deleteFamilyItem function to remove the database and all its children resources.

    /**
    * Cleanup the database and container on completion
    */
    async function cleanup() {
     await client.database(databaseId).delete();
    }
    
  2. 复制以下代码并将其粘贴到对 deleteFamilyItem 的调用下面,以便执行 cleanup 函数。Copy and paste the code below the call to deleteFamilyItem to execute the cleanup function.

    createDatabase()
      .then(() => readDatabase())
      .then(() => createContainer())
      .then(() => readContainer())
      .then(() => createFamilyItem(config.items.Andersen))
      .then(() => createFamilyItem(config.items.Wakefield))
      .then(() => queryContainer())
      .then(() => replaceFamilyItem(config.items.Andersen))
      .then(() => queryContainer())
      .then(() => deleteFamilyItem(config.items.Andersen))
    
      // ADD THIS PART TO YOUR CODE
      .then(() => cleanup())
      // ENDS HERE
    
      .then(() => { exit(`Completed successfully`); })
      .catch((error) => { exit(`Completed with error ${JSON.stringify(error)}`) });
    

运行 Node.js 应用程序Run your Node.js application

总起来看,代码应如下所示:Altogether, your code should look like this:

//@ts-check
const CosmosClient = require('@azure/cosmos').CosmosClient

const config = require('./config')
const url = require('url')

const endpoint = config.endpoint
const key = config.key

const databaseId = config.database.id
const containerId = config.container.id
const partitionKey = { kind: 'Hash', paths: ['/Country'] }

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

/**
 * Create the database if it does not exist
 */
async function createDatabase() {
  const { database } = await client.databases.createIfNotExists({
    id: databaseId
  })
  console.log(`Created database:\n${database.id}\n`)
}

/**
 * Read the database definition
 */
async function readDatabase() {
  const { resource: databaseDefinition } = await client
    .database(databaseId)
    .read()
  console.log(`Reading database:\n${databaseDefinition.id}\n`)
}

/**
 * Create the container if it does not exist
 */
async function createContainer() {
  const { container } = await client
    .database(databaseId)
    .containers.createIfNotExists(
      { id: containerId, partitionKey },
      { offerThroughput: 400 }
    )
  console.log(`Created container:\n${config.container.id}\n`)
}

/**
 * Read the container definition
 */
async function readContainer() {
  const { resource: containerDefinition } = await client
    .database(databaseId)
    .container(containerId)
    .read()
  console.log(`Reading container:\n${containerDefinition.id}\n`)
}

/**
 * Create family item if it does not exist
 */
async function createFamilyItem(itemBody) {
  const { item } = await client
    .database(databaseId)
    .container(containerId)
    .items.upsert(itemBody)
  console.log(`Created family item with id:\n${itemBody.id}\n`)
}

/**
 * Query the container using SQL
 */
async function queryContainer() {
  console.log(`Querying container:\n${config.container.id}`)

  // query to return all children in a family
  const querySpec = {
    query: 'SELECT VALUE r.children FROM root r WHERE r.lastName = @lastName',
    parameters: [
      {
        name: '@lastName',
        value: 'Andersen'
      }
    ]
  }

  const { resources: results } = await client
    .database(databaseId)
    .container(containerId)
    .items.query(querySpec)
    .fetchAll()
  for (var queryResult of results) {
    let resultString = JSON.stringify(queryResult)
    console.log(`\tQuery returned ${resultString}\n`)
  }
}

/**
 * Replace the item by ID.
 */
async function replaceFamilyItem(itemBody) {
  console.log(`Replacing item:\n${itemBody.id}\n`)
  // Change property 'grade'
  itemBody.children[0].grade = 6
  const { item } = await client
    .database(databaseId)
    .container(containerId)
    .item(itemBody.id, itemBody.Country)
    .replace(itemBody)
}

/**
 * Delete the item by ID.
 */
async function deleteFamilyItem(itemBody) {
  await client
    .database(databaseId)
    .container(containerId)
    .item(itemBody.id, itemBody.Country)
    .delete(itemBody)
  console.log(`Deleted item:\n${itemBody.id}\n`)
}

/**
 * Cleanup the database and collection on completion
 */
async function cleanup() {
  await client.database(databaseId).delete()
}

/**
 * Exit the app with a prompt
 * @param {string} message - The message to display
 */
function exit(message) {
  console.log(message)
  console.log('Press any key to exit')
  process.stdin.setRawMode(true)
  process.stdin.resume()
  process.stdin.on('data', process.exit.bind(process, 0))
}

createDatabase()
  .then(() => readDatabase())
  .then(() => createContainer())
  .then(() => readContainer())
  .then(() => createFamilyItem(config.items.Andersen))
  .then(() => createFamilyItem(config.items.Wakefield))
  .then(() => queryContainer())
  .then(() => replaceFamilyItem(config.items.Andersen))
  .then(() => queryContainer())
  .then(() => deleteFamilyItem(config.items.Andersen))
  .then(() => {
    exit(`Completed successfully`)
  })
  .catch(error => {
    exit(`Completed with error ${JSON.stringify(error)}`)
  })

在终端中,找到 app.js 文件并运行以下命令:In your terminal, locate your app.js file and run the command:

node app.js

应该看到已启动应用的输出。You should see the output of your get started app. 输出应该匹配下面的示例文本。The output should match the example text below.

Created database:
FamilyDatabase

Reading database:
FamilyDatabase

Created container:
FamilyContainer

Reading container:
FamilyContainer

Created family item with id:
Anderson.1

Created family item with id:
Wakefield.7

Querying container:
FamilyContainer
        Query returned [{"firstName":"Henriette Thaulow","gender":"female","grade":5,"pets":[{"givenName":"Fluffy"}]}]

Replacing item:
Anderson.1

Querying container:
FamilyContainer
        Query returned [{"firstName":"Henriette Thaulow","gender":"female","grade":6,"pets":[{"givenName":"Fluffy"}]}]

Deleted item:
Anderson.1

Completed successfully
Press any key to exit

获取完整的 Node.js 教程解决方案Get the complete Node.js tutorial solution

如果没有时间完成本教程中的步骤,或者只需下载代码,则可从 GitHub 获取。If you didn't have time to complete the steps in this tutorial, or just want to download the code, you can get it from GitHub.

若要运行包含本文所有代码的入门解决方案,需要以下项:To run the getting started solution that contains all the code in this article, you will need:

通过 npm 安装项目的依赖项。Install the project's dependencies via npm. 请使用以下命令:Use the following command:

  • npm install

接下来,在 config.js 文件中更新 config.endpoint 和 config.key 的值,如步骤 3:设置应用的配置中所述。Next, in the config.js file, update the config.endpoint and config.key values as described in Step 3: Set your app's configurations.

然后在终端中找到 app.js 文件并运行以下命令:Then in your terminal, locate your app.js file and run the command:

node app.js 

清理资源Clean up resources

不再需要资源组、Azure Cosmos DB 帐户和所有相关的资源时,可将这些资源删除。When these resources are no longer needed, you can delete the resource group, Azure Cosmos DB account, and all the related resources. 为此,请选择用于 Azure Cosmos DB 帐户的资源组,接着选择“删除” ,然后确认要删除的资源组的名称。To do so, select the resource group that you used for the Azure Cosmos DB account, select Delete, and then confirm the name of the resource group to delete.

后续步骤Next steps