Couchbase Capella is a fully managed JSON document Database-as-a-Service (DBaaS) offering that eliminates database-management duties and reduces overall costs. Capella offers robust performance, flexibility and scalability in a modern cloud native, distributed database that fuses the strengths of relational database features such as SQL and ACID transactions with JSON flexibility and scale that defines NoSQL.
Pydantic is a library that allows developers to define and validate custom JSON objects using Python type annotations. Pydantic accelerates coding time by enforcing type hints at runtime and raising user-friendly validation errors. The JSON documents generated using pydantic can then be used on the Capella platform without the concern for ill-formed documents that can occur when JSON documents are created via string standard concatenation.
Setting up pydantic
To run the quickstart project, you need the following prerequisites:
-
- Git repository URL: https://github.com/brickj/capella_pydantic
- Python 3 installed
- The pip package management tool
- Installed Couchbase Capella™ SDK:
- Full documentation is located here
- Example Python command: pip install couchbase
- Installed pydantic Python library:
- Full documentation is located here
- Example Python command: pip install pydantic
- Couchbase Capella account:
- Sign up for a free 30-day trial
- Couchbase Capella cluster and bucket named pydantic
In this blog, I will quickly walk through an example of using pydantic to create valid JSON documents and storing them in Couchbase Capella.
The Couchbase Python SDK enables developers to perform CRUD operations on a specified bucket in a Couchbase cluster. For example, the following code snippet performs an upsert (insert if the document key is not in the bucket or update if the document already exists):
1 2 |
# Store a Document cb_coll.upsert('u:king_arthur', {'name': 'Arthur', 'email': 'kingarthur@couchbase.com', 'interests': ['Holy Grail', 'African Swallows']}) |
Python developers generally create JSON documents using strings or integers to populate a dictionary object that stores key-value pairs. That dictionary object is then sent to the specified bucket in the Capella database for insertion. The sample code to create and populate the dictionary object is similar to the snippet below:
1 2 |
# Dictionary object with keys and values sample_dict = {1: 'document 1', 2: 'document 2'} |
With pydantic, developers have the flexibility to specify a schema for JSON objects that can be enforced as the documents are created. For example, the following steps through the creation of a JSON document for user posts. The document model looks like this:
Each UserPost object has the fields used in the post but each post can have multiple reviews that need to be nested within the post. To enable this functionality, developers can create two separate classes for the UserPost and the Review. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Review(BaseModel): author: str review: str likes: int class UserPost(BaseModel): author: str date: str title: Optional[str] = None content: str id: int likes: List[str] reviews: List[Review] |
Creating JSON documents in Python
Once the objects are created, populating the documents is easy:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
review = [ Review(author="johndoe", review="This is a comment!", likes=3), Review(author="rickJ", review="This is a Rick J comment!", likes=1), Review(author="janedoe", review="This is a Jane Doe comment!", likes=2) ] user_post1 = UserPost(author="johndoe", date="1/1/1970", title="Cool post", content="Cool content", id=10101, likes=["johndoe", "janedoe"], reviews=review) |
Now you can use the UserPost object as a dictionary and upsert it to Couchbase Capella using similar code to the upsert code shown earlier but using the object’s dict() method:
1 2 |
# Upsert JSON dict with key 'u:pydantic_document' cb_coll.upsert('u:pydantic_document', user_post1.dict()) |
The full project illustrated in this blog includes the following:
-
- Readme with instructions
- Code that creates the UserPost and Review objects
- Code that generates and prints a document to output
- Code that connects to Couchbase Capella using the Couchbase Python SDK
- Code that upserts the generated JSON document
The public project can be cloned for Git at: https://github.com/brickj/capella_pydantic
Next steps
To learn more about Couchbase Capella, our Database-as-a-Service offering:
-
- Sign up for a free 30-day trial if you haven’t already
- Connect your trial cluster to the Playground or connect a project to test it out for yourself
- If you’re already using Couchbase Capella, you can interact with your cluster by using the interactive Couchbase shell or via the Capella Control plane to:
- Check out the document viewer
- Connect to a project
- Take a look at the Capella Learning Path
- Check out our tutorials
- The Couchbase Developer Portal has tons of tutorials/quickstart guides and learning paths to help you get started
- See the documentation to learn more about the Couchbase SDKs
- Read Validate JSON Documents in Python Using Pydantic
At this point, you should be able to quickly use the pydantic library to create valid JSON documents for use with Couchbase Capella.
If you have any questions or comments, please connect with us on the Couchbase Forums!