侧边栏壁纸

Javascript链接Azure NoSql数据库 - 可以增删查改(简易版)

2023年08月11日 585阅读 0评论 0点赞
const { CosmosClient } = require("@azure/cosmos");
console.log('good')
const endpoint = "https://ramuricosmosdbtesting.documents.azure.com:443/";
const key = "chuqphPGgUvyIYNGd09TrCEd3v3wEvXTheFiCMJVXAYCgN9NuqOnZY2xLS6iAFYn8aHExKFujWHRACDbRtNUkQ==";
const client = new CosmosClient({ endpoint, key });
// console.log(client)



// async function main() {
//
//   // The rest of the README samples are designed to be pasted into this function body
// }

async function main() {
 let counter = 1;
 const databaseId = "ramuricosmosdbtesting";

 const { database } = await client.databases.createIfNotExists({
   id: databaseId
 });

 const { container: recommendationContainer } = await database.containers.createIfNotExists({
   id: "RecommendationContainer"
 });

 const { container: clickContainer } = await database.containers.createIfNotExists({
   id: "ClickContainer"
 });

 console.log(database.id);
 console.log(recommendationContainer.id);
 console.log(clickContainer.id);
 // await container.item("f222febd-d574-47bc-9770-8f0873760f4c").read();

 console.log(`Connected to database: ${database.id}`);

 const { resources } = await recommendationContainer.items.query(
   "SELECT * from c "
 ).fetchAll();
 for (const city of resources) {
   console.log(counter);
   console.log(`The product title is : ${city.product_title},
     The Category is : ,${city.category}`);
   counter++;
 }

 // let time = Date.now();
 const currentDate = new Date();

 // Get the individual components
 const year = currentDate.getFullYear();
 const month = currentDate.getMonth() + 1; // Months are zero-based, so add 1
 const day = currentDate.getDate();
 const hours = currentDate.getHours();
 const minutes = currentDate.getMinutes();
 const seconds = currentDate.getSeconds();

 console.log(`Current date: ${year}-${month}-${day}`);
 console.log(`Current time: ${hours}:${minutes}:${seconds}`);
 let time = `${year}-${month}-${day}-${hours}:${minutes}:${seconds}`

 const newItem = {
   current_time : time,
   current_link : 'abc2fjlewjfdilwedjwasr844o8243oqruo4uro34',
   brand_name : 'B&K'
 };

 // const { resource: createdItem } = await container.items.create(newItem);
 // console.log(`Successfully inserted item with brand: ${createdItem.brand_name}`);
 await clickContainer.items.create(newItem);
 console.log('Successfully inserted item with brand');
}

main().catch((error) => {
 console.error(error);
});
0
打赏

—— 评论区 ——

请登录后发表评论
立即登录
LOGIN