Skip to main content

DataverseORM is a Python module that simplifies working with Microsoft Dataverse by providing a lightweight Object-Relational Mapper (ORM) for querying, creating, updating, and deleting entities. It uses the Dataverse Web API for communication and includes optional metadata validation to ensure that entities and properties exist in the connected Dataverse environment.

Project description

DataverseORM

DataverseORM is a Python module that simplifies working with Microsoft Dataverse by providing a lightweight Object-Relational Mapper (ORM) for querying, creating, updating, and deleting entities. It uses the Dataverse Web API for communication.

Installation

pip install ms-dataverse

Dependencies

  • Python 3.6+

Usage

To use DataverseORM, import the DataverseORM class from the dataverse_orm.py file:

from ms_dataverse import DataverseORM

Create an instance of the DataverseORM class, passing in the URL to your Dataverse environment and an access token for authentication:

orm = DataverseORM(dynamics_url="https://your_environment.crm.dynamics.com", access_token="your_access_token")

Authentication

You can obtain an access token using the MSAL Python package available at: https://github.com/AzureAD/microsoft-authentication-library-for-python

Both PublicClientApplication and ConfidentialClientApplication classes work effectively with this package and the Dataverse Web API.

Refresh token callback

The refresh_token_callback parameter is an optional function that you can provide to handle token expiration. When the access token expires, the callback function will be called automatically to refresh the token. The callback function should return a new access token. To use the refresh_token_callback, pass it when creating an instance of DataverseORM:

def refresh_access_token():
    # Your token refresh logic here
    return new_access_token

orm = DataverseORM(dynamics_url="https://your_environment.crm.dynamics.com", access_token="your_access_token", refresh_token_callback=refresh_access_token)

By providing a refresh_token_callback, you can ensure seamless operation of the DataverseORM instance, even when the access token expires during its usage.

Working with entities

To work with a specific entity, get an instance of the Entity class:

account = orm.entity("accounts")

Query entities

To query entities, use the query method with optional OData filter expressions, select fields, and order by:

accounts = account.query(filter_expression="name eq 'Contoso'", select_fields=["name", "email"], order_by="name")

Create a new entity

To create a new entity, use the create method with a dictionary of property names and values:

new_account = {"name": "Contoso", "email": "info@contoso.com"}
created_account = account.create(new_account)

Update an existing entity

To update an existing entity, use the update method with the entity ID and a dictionary of property names and values to update:

updated_account = {"name": "Contoso Ltd.", "email": "info@contoso.com"}
account.update(entity_id="account_id", entity_data=updated_account)

Delete an entity

To delete an entity, use the delete method with the entity ID:

account.delete(entity_id="account_id")

Get an entity by ID

To get an entity by ID, use the get method with the entity ID:

retrieved_account = account.get(entity_id="account_id")

Error handling

The module provides a custom DataverseError exception class for handling errors that may occur during API requests or metadata validation. To handle errors, wrap your code in a try-except block and catch the DataverseError exception:

from dataverse_orm import DataverseError

try:
    # Your code here
except DataverseError as e:
    print(f"An error occurred: {e}")

The DataverseError exception provides additional information such as the status code and the response object from the API request.

License

This project is licensed under the MIT License.

Contributions

We welcome contributions to the ms-dataverse project! If you'd like to contribute, follow these steps:

  1. Fork the repository: Click the "Fork" button at the top-right corner of the repository page on GitHub to create a copy of the repository under your own account.

  2. Clone the forked repository: Clone your forked repository to your local machine:

git clone https://github.com/yourusername/ms-dataverse.git

Replace yourusername with your GitHub username.

  1. Create a new branch: Create a new branch for your changes:
git checkout -b your-new-branch-name

Replace your-new-branch-name with a descriptive name for your branch.

  1. Make your changes: Modify the code, fix bugs, or add new features to your branch.

  2. Commit your changes: After making your changes, commit them to your branch:

git add .
git commit -m "Your commit message"

Replace Your commit message with a short description of the changes you made.

  1. Push your changes: Push your changes to your forked repository on GitHub:
git push origin your-new-branch-name
  1. Create a pull request: Go to the original ms-dataverse repository on GitHub and click the "New pull request" button. Choose your forked repository and the branch you created as the source, and the main ms-dataverse repository as the destination. Provide a brief description of your changes and submit the pull request.

Once you've submitted your pull request, we'll review your changes and, if everything looks good, merge them into the main repository. Thank you for your contributions!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ms-dataverse-0.1.4.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ms_dataverse-0.1.4-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

Details for the file ms-dataverse-0.1.4.tar.gz.

File metadata

  • Download URL: ms-dataverse-0.1.4.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for ms-dataverse-0.1.4.tar.gz
Algorithm Hash digest
SHA256 18348f666f0287cce0e1c405879aadb7df43d83bcd83591f9ceb70ad660298cb
MD5 e3457b1a7c74f2a9123b57fcee178d3b
BLAKE2b-256 ef85b8df616fe269cb20cb8c4393f61fe80541ebca0acbcf1ddb05eefe99376c

See more details on using hashes here.

File details

Details for the file ms_dataverse-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: ms_dataverse-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 3.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for ms_dataverse-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a751cc74031865b2bfa5b7f98334e01e142c835c41c8a46f5fc27300be52609f
MD5 9fc2bfb6b1d214dca84e3e54e10c0757
BLAKE2b-256 a6ece193aeeea2e789567786cba6fc24e9f4cb1d767ebb0845d4b7df37915f98

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page