PlagueHO / CosmosDB

PowerShell Module for working with Azure Cosmos DB databases, collections, documents, attachments, offers, users, permissions, triggers, stored procedures and user defined functions.
http://dscottraynsford.com
MIT License
154 stars 46 forks source link

Unique keys #197

Closed tlanda closed 5 years ago

tlanda commented 6 years ago

Would you consider to support creating Collections with with a unique key policy? https://docs.microsoft.com/en-us/azure/cosmos-db/unique-keys

PlagueHO commented 6 years ago

On initial investigation it appears the REST API documentation for collections (https://docs.microsoft.com/en-us/rest/api/cosmos-db/collections) has not been updated yet with information on how to set Unique Keys.

I could probably do some reverse engineering on the Azure Cosmos DB .NET Core SDK, but it hasn't been open sourced yet (https://github.com/Azure/azure-cosmosdb-dotnet/tree/master/sdk). Definitely possible to do, but will require more digging.

PlagueHO commented 6 years ago

Update: I've reached out to the MVP community and Cosmos DB team to see if there is any documentation available on this. Hopefully they'll just be able to quickly update the documentation.

PlagueHO commented 6 years ago

Update: I got some great info from the Cosmos DB team and MVPs. To create a unique policy we must add the following into the collection body:

"uniqueKeyPolicy": {
    "uniqueKeys": [
      { "paths": [ "/name", "/address "] },
      { "paths": [ "/email" ] }
    ]
  }

And:

coll = {"id": "collection_unique_keys", 'uniqueKeyPolicy': {'uniqueKeys': [{'paths': ['/field1/field2', '/field3']}]}}
PlagueHO commented 5 years ago

@tlanda - I've finished work on this and am just finishing up the integration tests on this now. I'll have this out in the next release later on this week.

tlanda commented 5 years ago

Great, thank you very much!

PlagueHO commented 5 years ago

A pleasure! Just releasing this to the PSGallery now as 2.1.12. Check there tomorrow.

PlagueHO commented 5 years ago

Published to gallery here: https://www.powershellgallery.com/packages/CosmosDB/2.1.12.137

Documentation on how to use it in the Wiki: https://github.com/PlagueHO/CosmosDB/wiki#creating-a-collection-with-a-custom-unique-key-policy