|
1 |
| -//@ts-check |
2 |
| -const CosmosClient = require('@azure/cosmos').CosmosClient |
3 |
| - |
4 |
| -const config = require('./config') |
5 |
| -const url = require('url') |
6 |
| - |
7 |
| -const endpoint = config.endpoint |
8 |
| -const key = config.key |
9 |
| - |
10 |
| -const databaseId = config.database.id |
11 |
| -const containerId = config.container.id |
12 |
| -const partitionKey = { kind: 'Hash', paths: ['/Country'] } |
13 |
| - |
14 |
| -const client = new CosmosClient({ endpoint, key }) |
15 |
| - |
16 |
| -/** |
17 |
| - * Create the database if it does not exist |
18 |
| - */ |
19 |
| -async function createDatabase() { |
20 |
| - const { database } = await client.databases.createIfNotExists({ |
21 |
| - id: databaseId |
22 |
| - }) |
23 |
| - console.log(`Created database:\n${database.id}\n`) |
24 |
| -} |
25 |
| - |
26 |
| -/** |
27 |
| - * Read the database definition |
28 |
| - */ |
29 |
| -async function readDatabase() { |
30 |
| - const { resource: databaseDefinition } = await client |
31 |
| - .database(databaseId) |
32 |
| - .read() |
33 |
| - console.log(`Reading database:\n${databaseDefinition.id}\n`) |
34 |
| -} |
35 |
| - |
36 |
| -/** |
37 |
| - * Create the container if it does not exist |
38 |
| - */ |
39 |
| -async function createContainer() { |
40 |
| - const { container } = await client |
41 |
| - .database(databaseId) |
42 |
| - .containers.createIfNotExists( |
43 |
| - { id: containerId, partitionKey }, |
44 |
| - { offerThroughput: 400 } |
45 |
| - ) |
46 |
| - console.log(`Created container:\n${config.container.id}\n`) |
47 |
| -} |
48 |
| - |
49 |
| -/** |
50 |
| - * Read the container definition |
51 |
| - */ |
52 |
| -async function readContainer() { |
53 |
| - const { resource: containerDefinition } = await client |
54 |
| - .database(databaseId) |
55 |
| - .container(containerId) |
56 |
| - .read() |
57 |
| - console.log(`Reading container:\n${containerDefinition.id}\n`) |
58 |
| -} |
59 |
| - |
60 |
| -/** |
61 |
| - * Create family item if it does not exist |
62 |
| - */ |
63 |
| -async function createFamilyItem(itemBody) { |
64 |
| - const { item } = await client |
65 |
| - .database(databaseId) |
66 |
| - .container(containerId) |
67 |
| - .items.upsert(itemBody) |
68 |
| - console.log(`Created family item with id:\n${itemBody.id}\n`) |
69 |
| -} |
70 |
| - |
71 |
| -/** |
72 |
| - * Query the container using SQL |
73 |
| - */ |
74 |
| -async function queryContainer() { |
75 |
| - console.log(`Querying container:\n${config.container.id}`) |
76 |
| - |
77 |
| - // query to return all children in a family |
78 |
| - const querySpec = { |
79 |
| - query: 'SELECT VALUE r.children FROM root r WHERE r.lastName = @lastName', |
80 |
| - parameters: [ |
81 |
| - { |
82 |
| - name: '@lastName', |
83 |
| - value: 'Andersen' |
84 |
| - } |
85 |
| - ] |
| 1 | +// @ts-check |
| 2 | +const CosmosClient = require("@azure/cosmos").CosmosClient; |
| 3 | +const config = require("./config"); |
| 4 | +const dbContext = require("./data/databaseContext"); |
| 5 | + |
| 6 | +const newItem = { |
| 7 | + id: "3", |
| 8 | + category: "fun", |
| 9 | + name: "Cosmos DB", |
| 10 | + description: "Complete Cosmos DB Node.js Quickstart ⚡", |
| 11 | + isComplete: false |
| 12 | +}; |
| 13 | + |
| 14 | +async function main() { |
| 15 | + const { endpoint, key, databaseId, containerId } = config; |
| 16 | + |
| 17 | + const client = new CosmosClient({ endpoint, key }); |
| 18 | + |
| 19 | + const database = client.database(databaseId); |
| 20 | + const container = database.container(containerId); |
| 21 | + |
| 22 | + // Make sure Tasks database is already setup. If not, create it. |
| 23 | + await dbContext.create(client, databaseId, containerId); |
| 24 | + |
| 25 | + try { |
| 26 | + console.log(`Querying container: Items`); |
| 27 | + |
| 28 | + // query to return all items |
| 29 | + const querySpec = { |
| 30 | + query: "SELECT * from c" |
| 31 | + }; |
| 32 | + |
| 33 | + // read all items in the Items container |
| 34 | + const { resources: items } = await container.items |
| 35 | + .query(querySpec) |
| 36 | + .fetchAll(); |
| 37 | + |
| 38 | + console.log(items); |
| 39 | + |
| 40 | + // Create a new item |
| 41 | + const { resource: createdItem } = await container.items.create(newItem); |
| 42 | + console.log(`Created item: %s`, createdItem); |
| 43 | + |
| 44 | + const { id, category } = createdItem; |
| 45 | + |
| 46 | + // Update the item |
| 47 | + createdItem.isComplete = true; |
| 48 | + const { resource: updatedItem } = await container |
| 49 | + .item(id, category) |
| 50 | + .replace(createdItem); |
| 51 | + console.log(`Updated item: %s`, updatedItem); |
| 52 | + |
| 53 | + // Delete the item |
| 54 | + const { resource: result } = await container.item(id, category).delete(); |
| 55 | + console.log("Deleted item with id: %s", id); |
| 56 | + } catch (err) { |
| 57 | + console.log(err.message); |
86 | 58 | }
|
87 |
| - |
88 |
| - const { resources: results } = await client |
89 |
| - .database(databaseId) |
90 |
| - .container(containerId) |
91 |
| - .items.query(querySpec) |
92 |
| - .fetchAll() |
93 |
| - for (var queryResult of results) { |
94 |
| - let resultString = JSON.stringify(queryResult) |
95 |
| - console.log(`\tQuery returned ${resultString}\n`) |
96 |
| - } |
97 |
| -} |
98 |
| - |
99 |
| -/** |
100 |
| - * Replace the item by ID. |
101 |
| - */ |
102 |
| -async function replaceFamilyItem(itemBody) { |
103 |
| - console.log(`Replacing item:\n${itemBody.id}\n`) |
104 |
| - // Change property 'grade' |
105 |
| - itemBody.children[0].grade = 6 |
106 |
| - const { item } = await client |
107 |
| - .database(databaseId) |
108 |
| - .container(containerId) |
109 |
| - .item(itemBody.id, itemBody.Country) |
110 |
| - .replace(itemBody) |
111 |
| -} |
112 |
| - |
113 |
| -/** |
114 |
| - * Delete the item by ID. |
115 |
| - */ |
116 |
| -async function deleteFamilyItem(itemBody) { |
117 |
| - await client |
118 |
| - .database(databaseId) |
119 |
| - .container(containerId) |
120 |
| - .item(itemBody.id, itemBody.Country) |
121 |
| - .delete(itemBody) |
122 |
| - console.log(`Deleted item:\n${itemBody.id}\n`) |
123 |
| -} |
124 |
| - |
125 |
| -/** |
126 |
| - * Cleanup the database and collection on completion |
127 |
| - */ |
128 |
| -async function cleanup() { |
129 |
| - await client.database(databaseId).delete() |
130 |
| -} |
131 |
| - |
132 |
| -/** |
133 |
| - * Exit the app with a prompt |
134 |
| - * @param {string} message - The message to display |
135 |
| - */ |
136 |
| -function exit(message) { |
137 |
| - console.log(message) |
138 |
| - console.log('Press any key to exit') |
139 |
| - process.stdin.setRawMode(true) |
140 |
| - process.stdin.resume() |
141 |
| - process.stdin.on('data', process.exit.bind(process, 0)) |
142 | 59 | }
|
143 | 60 |
|
144 |
| -createDatabase() |
145 |
| - .then(() => readDatabase()) |
146 |
| - .then(() => createContainer()) |
147 |
| - .then(() => readContainer()) |
148 |
| - .then(() => createFamilyItem(config.items.Andersen)) |
149 |
| - .then(() => createFamilyItem(config.items.Wakefield)) |
150 |
| - .then(() => queryContainer()) |
151 |
| - .then(() => replaceFamilyItem(config.items.Andersen)) |
152 |
| - .then(() => queryContainer()) |
153 |
| - .then(() => deleteFamilyItem(config.items.Andersen)) |
154 |
| - .then(() => { |
155 |
| - exit(`Completed successfully`) |
156 |
| - }) |
157 |
| - .catch(error => { |
158 |
| - exit(`Completed with error ${JSON.stringify(error)}`) |
159 |
| - }) |
| 61 | +main(); |
0 commit comments