Skip to content

Commit

Permalink
Add cosmosdb push test
Browse files Browse the repository at this point in the history
  • Loading branch information
aboodman committed Feb 20, 2021
1 parent 48f9454 commit 3d90acd
Show file tree
Hide file tree
Showing 5 changed files with 2,880 additions and 2,724 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"start": "next start"
},
"dependencies": {
"@azure/cosmos": "^3.9.5",
"@supabase/supabase-js": "^1.3.4",
"faunadb": "^4.0.3",
"next": "10.0.5",
Expand Down
7 changes: 7 additions & 0 deletions pages/api/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
endpoint: "https://replidraw.documents.azure.com:443/",
key: "Wl0K3w4jCedslIsKlwsgXGOOaiiPz2uwM9HrmaSkkrrjnn3eFrSiy9zAZWXU3btml5gbis3B2pcRHhvJg0y0tg==",
databaseId: "replidraw",
containerId: "c1",
partitionKey: { kind: "Hash", paths: ["/partition"] }
};
32 changes: 32 additions & 0 deletions pages/api/databaseContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

import config from './config';
import {CosmosClient} from '@azure/cosmos';

/*
// This script ensures that the database is setup and populated correctly
*/
async function create(client, databaseId, containerId) {
const partitionKey = config.partitionKey;

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

/**
* Create the container if it does not exist
*/
const { container } = await client
.database(databaseId)
.containers.createIfNotExists(
{ id: containerId, partitionKey },
{ offerThroughput: 400 }
);

console.log(`Created container:\n${container.id}\n`);
}

export default {create};
42 changes: 42 additions & 0 deletions pages/api/replicache-push-cosmos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { CosmosClient } from '@azure/cosmos';
import config from './config';
import dbContext from './databaseContext';

import type { NextApiRequest, NextApiResponse } from "next";

export default async (req: NextApiRequest, res: NextApiResponse) => {
const uniqueID = Math.random().toString(36).substr(2);

const { endpoint, key, databaseId, containerId } = config;
const client = new CosmosClient({ endpoint, key });
const database = client.database(databaseId);
const container = database.container(containerId);

console.time('Processing push: ' + uniqueID);

const newItem = {
id: Math.random().toString(36).substr(2),
content: `{
"ref": Ref(Collection("objects"), "290282750093558285"),
"ts": 1613670707753000,
"data": {
"width": 163,
"height": 84,
"rotate": 0,
"strokeWidth": 0,
"fill": "pink",
"radius": "0",
"blendMode": "normal",
"type": "rectangle",
"x": 190,
"y": 334
}
}`,
};
const { resource: createdItem } = await container.items.create(newItem);
console.timeEnd('Processing push: ' + uniqueID);
console.log(`\r\nCreated new item: ${createdItem.id}\r\n`);


res.status(200).json({});
};
Loading

0 comments on commit 3d90acd

Please sign in to comment.