Work with Azure Cosmos DB Flashcards
What version of the cosmos SDK are we currently using?
- version 3
- uses containers and items
- container is a collection, graph or table
- item is a document, edge/vertex or row and is the content of a container
What function creates a new cosmos client?
CosmosClient(endpoint, key)
- creates new cosmos client with the conn string
- thread safe
- only one instance recommended per lifetime
What functions are available for databases?
- CreateDatabaseIfNotExistsAsync(id, timeout); id only used to verify if theres an existing db
- readAsync()
- deleteAsync()
What functions are available for containers?
- CreateContainerIfNotExistsAsync(id, partitionkeypath, throughput)
- GetContainer(id)
- ReadContainerAsync()
- DeleteContainer()
What functions are available for items?
- CreateItemAsync(id, partitionkey)
- ReadItemAsync(id, partitionkey)
- GetItemQueryIterator(); creates a query for items under a container using a SQL statement with parameterised values
What does cosmosDB provide in terms of functions?
- provides language-integrated, transactional execution of JS that let you write stored procs, triggers and uder-defined functions
- need to be registered before being called
what are stored procedures in cosmosDB?
- can perform CRUD operations inside a container
- registered per collection
- can operate on any doc or attachment present in collection
- requires a context object which is used to access all operations that can be performed by cosmos DB as well as request and response objects
- when an item is created its inserted into container and an ID for the item is returned
What is the JS callback function in regards to cosmosDB functions?
- required when using stored procedures
- has two params; one for error object in case operation fails and another for a return value
- can either handle the exception or throw an error
- if no call provided, error will be thrown In cosmos DB runtime
What other params does stored proc function have?
- a param to set description as a bool
- when true and the description is missing the proc will throw an exception
- otherwise the rest of the proc continues to run
What is special about defining a stored proc?
- input params are always sent as a string to the stored proc
- even if you pass an array of strings the array is converted into a string
- to get around this you can define a function within proc to parse the string as an array
What must all cosmos DB operations do?
- Complete within a limited amount of time
- stored procs have limited time to run on server
- collection functions return a boolean value that represents if operation completed or not
How can we implement transactions on items within a container?
- By using a stored proc
- JS functions can implement a continuation-based model to batch or resume execution
- continuation values can be any value of your choice and apps can use this value to resume transactions from a new starting point
What triggers does cosmosDB support?
- pre and post
- pretriggers are executed before modifying a DB item and post are executed after modifying an item
- not automatically executed, must be specified for each DB operation where you want to execute them
- Need to be registered after definition by using cosmos DB SDKs
Can pretriggers have input params?
- No
- request object in the trigger is used to manipulate the request message associated with the operation
How can we specify the operations triggers can be ran with?
- When triggers are registered
- e.g. TriggerOperation.Create which means using the trigger in a replace operation isn’t permitted but in a create operation it is