nestjs / azure-database

Azure CosmosDB Database module for Nest framework (node.js) ☁️
https://nestjs.com
MIT License
106 stars 52 forks source link

Can't use cosmoDB readonly key / Must use cosmoDB readwrite key #1079

Open vinaybedre opened 2 months ago

vinaybedre commented 2 months ago

I'm submitting a...


[ ] Regression 
[ ] Bug report
[X] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

The library usage requires us to have read-write cosmodb API key, where as it could be used for a readonly purpose with a readonly key. In this line, https://github.com/nestjs/azure-database/blob/78511e49b8af060bccbadd55a5eae0f58a84542d/lib/cosmos-db/cosmos-db-core.module.ts#L26 we try to run a createIfNotExists which needs a readwrite key.

Expected behavior

The library must be able to use with both readonly and readwrite keys. We could enhance the module options with a isReadOnly property, which skips running createIfNotExists

Minimal reproduction of the problem with instructions

  1. Create any cosmodb in azure portal.
  2. Get the primary/secondary readonly keys to use in the library.
  3. Use the module AzureCosmosDbModule in a nestjs app with readonly key

What is the motivation / use case for changing the behavior?

Flexibility. There could be business usecases, where some of the micro services should only have readonly access to the database, instead of readwrite. Currently this flexibility is missing in the library.

Environment


Nest version: 10.3.7


For Tooling issues:
- Node version: v20.11.1  
- Platform: Mac 

Others:

jeremylcarter commented 2 months ago

I think you're right, there needs to be an option to avoid the createIfNotExists in both places in the codebase.

Temporarily you can monkey patch the Databases or Containers class to do nothing with the createIfNotExists method.

For example:

Databases.prototype.createIfNotExists = async function () {
   // Your logic here
};
Containers.prototype.createIfNotExists = async function () {
 // Your logic here
};
manekinekko commented 2 months ago

Thank you for submitting this feature request @vinaybedre! Is this something you feel comfortable implementing? I can provide support if you need 😀

jeremylcarter commented 2 months ago

Yes I am able to create a version where there is an option called createIfNotExists which has a default value of true.

manekinekko commented 2 months ago

@jeremylcarter please feel free to submit a PR.