TinyDB – Discussion

TinyDB – Discussion ”; Previous Next TinyDB is a lightweight database to operate various formats of the document. It is an easy and hustles free database to handle data of several applications. TinyDB is based on python code and supports clean API. This database does not need any coding language. It handles small projects without any configurations. Generally, a database can store, retrieve, and modify data in a JSON file. Print Page Previous Next Advertisements ”;

TinyDB – Useful Resources

TinyDB – Useful Resources ”; Previous Next The following resources contain additional information on TinyDB. Please use them to get more in-depth knowledge on this. Useful Video Courses Python Flask and SQLAlchemy ORM 22 Lectures 1.5 hours Jack Chan More Detail Python and Elixir Programming Bundle Course 81 Lectures 9.5 hours Pranjal Srivastava More Detail TKinter Course – Build Python GUI Apps 49 Lectures 4 hours John Elder More Detail A Beginner”s Guide to Python and Data Science 81 Lectures 8.5 hours Datai Team Academy More Detail Deploy Face Recognition Project With Python, Django, And Machine Learning Best Seller 93 Lectures 6.5 hours Srikanth Guskra More Detail Professional Python Web Development with Flask 80 Lectures 12 hours Stone River ELearning More Detail Print Page Previous Next Advertisements ”;

TinyDB – Extensions

TinyDB – Extensions ”; Previous Next Given below is a set of TinyDB extensions that can be used for various operations − Sr.No. Operation & Description 1 TinyDBTimestamps It is an experimental extension that can be used to automatically add and update at timestamps to TinyDB documents. 2 aiotinydb It is a stable extension that enables us to use TinyDB in asyncio-aware contexts without slowing done the synchromous Input/Output(I/O). 3 tinydb – smartcache It is also a stable extension. It provides users with a smart query cache. It automatically updates the query cache so that the cache does not get invalidated after inserting, updating, or removing any documents. 4 tinydb – serialization It is a stable extension that provides serialization of the database object. 5 tinydb – appengine It provides storage for App Engine. It is also a stable extension. 6 Tinyrecord A stable extension that implements experimental atomic transaction support for the TinyDB NoSQL DB. 7 TinyMP It is also stable extension used for storage based on the MessagePack. 8 Tinymongo It is an experimental extension which is used for the replacement of MongoDB. 9 Tinyindex It is also an experimental extension of the database. As name implies, it is used for indexing data of the database. Print Page Previous Next Advertisements ”;

TinyDB – Quick Guide

TinyDB – Quick Guide ”; Previous Next TinyDB – Introduction What is TinyDB? TinyDB, written in pure Python programming language, is a small and lightweight document-orineted database with no external dependencies. It provides simple APIs that makes it easy to use. We can use TinyDB database for small project applications without any configuration. TinyDB module, available as a third-party module for Python programs, can be used to store, retereive, and modify the data in JSON format. Features of TinyDB TinyDB is a clean and hustle-free database to operate several formats of documents. It has the following features. Really tiny − TinyDB database is truly tiny in nature with only 1800 lines of code and 1600 lines tests. Easy to use − TinyDB is easy to use because of its simple and clean APIs. Document oriented − In TinyDB, we can store any document. The document will be represented as dict. Independent − The TinyDB database is independent of any external server and external dependencies from PyPI. Compatible with Python 3.6 or latest − TinyDB is tested and compatible with Python 3.6 and latest. It also works fine with PyPY3. Extensible − TinDB is easily extensible either by writing new storages or by modifying the behaviour of storages. Advantages of TinyDB TinyDB provide various benefits for students, users, and developers. TinyDB is open-sourced database aand it does not require any external configirations. It is quite easy-to-use, and the user can effortlessly handle documents. It automatically stores documents in the database. TinyDB is ideal in case of personal projects where we need to install some data. It is suitable for small applications that would be blown away by large databases like SQL or an external DB server. It uses a simple command line and query to operate data. There is 100% test coverage i.e., no explanation needed. Limitatations of TinyDB TinyDB will not be the right choice for your project if you need to − create indexes for tables, manage relationships between tables, use an HTTP server, or access from multiple processors. Comparison with Other Databases The following table highlights how TinyDB is different from MySQL and Oracle databases − Comparison Basis MySQL Oracle TinyDB Configurations Several Configurations Several Configurations Less Configurations, lightweight database Complicated Yes Yes No, easy-to-use and hustle-free Affordable No No Affordable than other databases Manageable Big database, hence difficult to manage Big database, hence difficult to manage Small and manageable TinyDB – Environmental Setup Prerequisite for TinyDB setup To install TinyDB, you must have Python 3.6 or newer installed in your system. You can go to the link www.python.org and select the latest version for your OS, i.e., Windows and Linux/Unix. We have a comprehensive tutorial on Python, which you can refer at www.tutorialspoint.com Installing TinyDB You can install TinyDB in three different ways: using the Pack Manager, from its Source, or from GitHub. Using the Package Manager The latest release versions of TinyDB are available over both the package managers, pip and conda. Let us check how you can use them to install TinyDB − To install TinyDB using pip, you can use the following command − pip install tinydb To install TinyDB via conda-forge, you can use the following command − conda install -c conda-forge tinydb From Source You can also install TinyDB from source distribution. Go to link https://pypi.org/project/tinydb/#files to download the files and building it from source. From GitHub To install TinyDB using GitHub, grab the latest development version, unpack the files, and use the following command to install it − pip install Setting up TinyDB Once installed, use the following steps to set up the TinyDB database. Step 1: Import TinyDB and its Query First, we need to import TinyDB and its Query. Use the following command − from tinydb import TinyDB, Query Step 2: Create a file TinyDB database can store data in many formats like XML, JSON, and others. We will be creating a JSON file by using the following file − db = TinyDB(”Leekha.json”) The above command will create an instance of TinyDB class and pass the file Leekha.Json to it. This is the file where our data will be stored. Now, the TinyDB database set up is ready, and you can work on it. We can now insert data and operate the value in the database. Uinstalling TinyDB If in case you need to uninstall TinyDB, you can use the following command − pip uninstall tinydb TinyDB – Insert Data We have created the instance of the TinyDB and passed a JSON file to it where our data will be stored. It is now time to insert the items in our database. The data should be in the form of a Python dictionary. Syntax To insert an item, you can use insert() method whose syntax is given below − db.insert({”type1”: ”value1”, ”type2”: ”value2”, ”typeN”: ”valueN”}) We can also create a dictionary first and then use the insert() method to insert the data into our database. data_item = {”type1”: ”value1”, ”type2”: ”value2”, ”typeN”: ”valueN” } db.insert(data_item) After running the above command, the insert() method will return the ID of the newly created object. And, our JSON file will look like the one shown below − {“_default”: {“1”: {“type1”: “value1”, “type2”: “value2”, “typeN”: “valueN”}}} Look at the above table entries: ”default”

TinyDB – Extend TinyDB

TinyDB – Extend TinyDB ”; Previous Next It is possible to extend TinyDB and modify its behaviour. There are four ways to do so − Custom middleware Custom storages Hooks and overrides Subclassing TinyDB and table In this chapter, let”s understand each of these methods in detail. Custom Middleware Sometimes the user does not want to write a new storage module. In such cases, the user can modify the behaviour of an existing storage module. Let”s see an example in which we will build a custom middleware filtering out the empty items − First, let”s see the data that will go through the custom middleware − { ”_default”: { 1: {”key1”: ”value1”}, 2: {”key2”: ”value2”}, ……………, N: {”keyN”: ”valueN”} }, Now, let”s see how we can implement the custom middleware − class RemoveEmptyItemsMiddleware(Middleware): def __init__(self, storage_cls): super(self).__init__(storage_cls) def read(self): data = self.storage.read() for _default in data: st_name = data for doc_id in table: item = st_name if item == {}: del st_name return data def close(self): self.storage.close() Custom Storage As discussed earlier, TinyDB comes with two types of storages: in-memory and JSON file storage. Along with that, TinyDB also provides an option to add our own custom storage. In the following example, let”s see how we can add a YAML storage using PyYAML − import yaml class YAMLStorage(Storage): def __init__(self, db.json): self. db.json = db.json To read the file − def read(self): with open(self.db.json) as handle: try: info = yaml.safe_load(handle.read()) return info except yaml.YAMLError: return None To write the file − def write(self, info): with open(self.db.json, ”w+”) as handle: yaml.dump(info, handle) To close the file − def close(self): pass Hooks and Overrides Sometimes, both custom storage and custom middleware cannot work in the way you want. In such cases, user can use predefined hooks and overrides to modify the behaviour of TinyDB. As an example, we will be configuring the name of the default table as follows − TinyDB.default_table_name = ”student_detail” We can also assign cache capacity as follows − TinyDB.table_class.default_query_cache_capacity = 50 Subclassing TinyDB and Table This is the last way we can use to modify the behaviour of TinyDB. As an example, we will be creating a subclass that can be used with hooks and overrides to override the default classes. Class ExtendTable(Table): TinyDB.table_class = student_detail Print Page Previous Next Advertisements ”;

TinyDB – Upserting Data

TinyDB – Upserting Data ”; Previous Next We discussed the update and insert queries, but sometimes, we a need a mix of both update and insert. In such cases, TinyDB provides a unique upsert query. This query helps us to insert and update data at a time as per the condition. It works in two ways − If data is available, then it chooses the update operation. If data is not available, then it chooses the insert operation. Syntax The syntax of upsert query is as follows − db.upsert({ ”key” : ”value”, ”logged – in” : True}, regular expression) Let”s take a couple of examples to demonstrate how you can use this upsert query in TinyDB. We will use the same student database that we have used in all the previous chapters. Example 1 Let”s see how we can use the upsert query to change the address of a student to “surat”, whose roll numbe is 2. In this case, we we have a matching user, hence it will update with the address to have logged-in set to True − from tinydb import TinyDB, Query db = TinyDB(”student.json”) db.upsert({”address”:”Surat”}, Query().roll_number==2) It will produce the following output, which means record number “2” got updated. [2] Use the following code to verify whether record number “2” got updated or not − db.get(doc_id=2) It will show the updated data − { ”roll_number”: 2, ”st_name”: ”Ram”, ”mark”: [250, 280], ”subject”: [”TinyDB”, ”MySQL”], ”address”: ”Surat” } Example 2 Let”s see how we can use the upsert query for unavailable data in our table − from tinydb import TinyDB, Query db = TinyDB(”student.json”) db.upsert({”E-mail”:”[email protected]”,”logged-in”: True}, Query().roll_number==2) It will show the following output, which means the document with the ID “2” got updated. [2] Use the following code to verify the updated values − db.get(doc_id=2) It will produce the following output − { “roll_number”:2, “st_name”:”Ram”, “mark”:[ 250, 280 ], “subject”:[ “TinyDB”, “MySQL” ], “address”:”Surat”, “logged-in”:true, “E-mail”:”[email protected]” } Notice that we used the upsert query to create a new key (E-mail) which was non-existent and supplied it with the value “[email protected]“. Print Page Previous Next Advertisements ”;

TinyDB – Document ID

TinyDB – Document ID ”; Previous Next TinyDB uses document ID, represented by doc_id, to access as well as modify the value of documents in a database. Here we will see how we can use this document_id for various operations. Display Data using Document ID We can use doc_id in get() method to display the data from a database. Its syntax is as follows − db.get(doc_id = value) Check for a Document in a Database We can use doc_id in contains() method to check if a document is present in a database or not. Its syntax is given below − db.contains(doc_id = value) Update All Documents We can use doc_id in update() method to update all the documents in a database with the given document IDs. Here is its syntax − db.update({key : value}, doc_ids = […]) Remove a Document We can use doc_id in remove() method to remove a specific document or all the documents in a database with the given document IDs. Its syntax is given below − db.remove(doc_ids = […]) Let”s take a few examples to demonstrate how you can use doc_id in TinyDB with these methods. We will use the same student database that we have used in all the previous chapters. Example 1 Let”s see how we can use doc_id to get the data of a specific document from a database − from tinydb import TinyDB db = TinyDB(”student.json”) db.get(doc_id = 5) It will fetch the data from the document with the doc_id “5”. { ”roll_number”: 5, ”st_name”: ”karan”, ”mark”: 275, ”subject”: ”oracle”, ”address”: ”benglore” } Example 2 Let”s see how we can use doc_id to check if the database contains a document with a specific ID − from tinydb import TinyDB db = TinyDB(”student.json”) db.contains(doc_id = 15) Based on the availability of the document, it will return either True or False. In this case, our database does not have a document with the doc_id “15”. Hence, it returns False. False Example 3 Let”s see how we can use doc_id to update the documents of our database − from tinydb import TinyDB db = TinyDB(”student.json”) db.update({”mark”:”280”}, doc_ids = [4]) Here, we updated the “marks” field of the document with the doc_id “4”. To check the updated data, use the following query − print(db.get(doc_id=4)) It will display the updated data of the document with the doc_id “4” − { ”roll_number”: 4, ”st_name”: ”lakan”, ”mark”: ”280”, ”subject”: ”MySQL”, ”address”: ”mumbai” } Example 4 Let”s see how we can use doc_id to remove specific documents from our database − from tinydb import TinyDB db = TinyDB(”student.json”) db.remove(doc_ids = [3,4]) Here, we removed two documents with doc_ids “3” and “4”. To verify, use the following get() queries − db.get(doc_id=3) db.get(doc_id=4) It will show the following output − None None It means that we have successfully removed the documents with doc_ids “3” and “4”. Print Page Previous Next Advertisements ”;

TinyDB – Storage Types

TinyDB – Storage Types ”; Previous Next TinyDB has two types of storage: JSON and in-memory. TinyDB, by default, stores the data in JSON files. While creating a database, you need to specify the path where to store the JSON file on your computer. Storing Data in a JSON File First, let”s see how we can use a JSON file to store the data − from tinydb import TinyDB, where db = TinyDB(”path/to/file_name.json”) Example 1 In this example, we are showing how you can insert multiple documents into a JSON file − from tinydb import TinyDB db = TinyDB(”storage.json”) db.insert_multiple([ { “roll_number”:1, “st_name”:”elen”, “mark”:250, “subject”:”TinyDB”, “address”:”delhi” }, { “roll_number”:2, “st_name”:”Ram”, “mark”:[ 250, 280 ], “subject”:[ “TinyDB”, “MySQL” ], “address”:”delhi” }, { “roll_number”:3, “st_name”:”kevin”, “mark”:[ 180, 200 ], “subject”:[ “oracle”, “sql” ], “address”:”keral” }, { “roll_number”:4, “st_name”:”lakhan”, “mark”:200, “subject”:”MySQL”, “address”:”mumbai” }, { “roll_number”:5, “st_name”:”karan”, “mark”:275, “subject”:”oracle”, “address”:”benglore” } ]) Here, we inserted 5 documents inside “storage.json”. To verify the records, use the following query − db.all() It will show the contents of “storage.json” file − [ { “roll_number”:1, “st_name”:”elen”, “mark”:250, “subject”:”TinyDB”, “address”:”delhi” }, { “roll_number”:2, “st_name”:”Ram”, “mark”:[ 250, 280 ], “subject”:[ “TinyDB”, “MySQL” ], “address”:”delhi” }, { “roll_number”:3, “st_name”:”kevin”, “mark”:[ 180, 200 ], “subject”:[ “oracle”, “sql” ], “address”:”keral” }, { “roll_number”:4, “st_name”:”lakhan”, “mark”:200, “subject”:”MySQL”, “address”:”mumbai” }, { “roll_number”:5, “st_name”:”karan”, “mark”:275, “subject”:”oracle”, “address”:”benglore” } ] Using in-memory to Store Data Now, let”s see how we can use “in-memory” to store the data − from tinydb import TinyDB from tinydb.storages import MemoryStorage object = TinyDB(storage = MemoryStorage) TinyDB.DEFAULT_STORAGE = MemoryStorage Example 2 The following example shows how you can insert multiple documents in default storage memory − from tinydb import TinyDB from tinydb.storages import MemoryStorage object = TinyDB(storage = MemoryStorage) TinyDB.DEFAULT_STORAGE = MemoryStorage object.insert_multiple([ { “roll_number”:1, “st_name”:”elen”, “mark”:250, “subject”:”TinyDB”, “address”:”delhi” }, { “roll_number”:2, “st_name”:”Ram”, “mark”:[ 250, 280 ], “subject”:[ “TinyDB”, “MySQL” ], “address”:”delhi” }, { “roll_number”:3, “st_name”:”kevin”, “mark”:[ 180, 200 ], “subject”:[ “oracle”, “sql” ], “address”:”keral” }, { “roll_number”:4, “st_name”:”lakan”, “mark”:200, “subject”:”MySQL”, “address”:”mumbai” }, { “roll_number”:5, “st_name”:”karan”, “mark”:275, “subject”:”oracle”, “address”:”benglore” } ]) To verify whether the documents have been inserted or not, use the following query − object.all() The following output shows the inserted data − [ { “roll_number”:1, “st_name”:”elen”, “mark”:250, “subject”:”TinyDB”, “address”:”delhi” }, { “roll_number”:2, “st_name”:”Ram”, “mark”:[ 250, 280 ], “subject”:[ “TinyDB”, “MySQL” ], “address”:”delhi” }, { “roll_number”:3, “st_name”:”kevin”, “mark”:[ 180, 200 ], “subject”:[ “oracle”, “sql” ], “address”:”keral” }, { “roll_number”:4, “st_name”:”lakan”, “mark”:200, “subject”:”MySQL”, “address”:”mumbai” }, { “roll_number”:5, “st_name”:”karan”, “mark”:275, “subject”:”oracle”, “address”:”benglore” } ] Print Page Previous Next Advertisements ”;

TinyDB – Middleware

TinyDB – Middleware ”; Previous Next TinyDB middleware helps us to customize database storage behavior by wrapping around the existing storage. This middleware improves the performance of the database. Caching Middleware This middleware, as its name implies, improves the speed of a database by reducing the disk I/O. The working of CachingMiddleware is as follows − First, it catches all the read operations. Then it writes the data to the disk after a configured number of write operations. Syntax The syntax to use CachingMiddleware is as follows − from tinydb.storages import JSONStorage from tinydb.middlewares import CachingMiddleware db = TinyDB(”middleware.json”, storage = CachingMiddleware(JSONStorage)) db.close() Example The following example shows how you can perform a basic middleware procedure in a database. from tinydb import TinyDB from tinydb.storages import JSONStorage from tinydb.middlewares import CachingMiddleware object = TinyDB(”storage.json”, storage=CachingMiddleware(JSONStorage)) object.all() Output It will produce the following output − [ { “roll_number”:1, “st_name”:”elen”, “mark”:250, “subject”:”TinyDB”, “address”:”delhi” }, { “roll_number”:2, “st_name”:”Ram”, “mark”:[ 250, 280 ], “subject”:[ “TinyDB”, “MySQL” ], “address”:”delhi” }, { “roll_number”:3, “st_name”:”kevin”, “mark”:[ 180, 200 ], “subject”:[ “oracle”, “sql” ], “address”:”keral” }, { “roll_number”:4, “st_name”:”lakan”, “mark”:200, “subject”:”MySQL”, “address”:”mumbai” }, { “roll_number”:5, “st_name”:”karan”, “mark”:275, “subject”:”oracle”, “address”:”benglore” } ] Close the database to make sure that all the data is safely written. db.close() Print Page Previous Next Advertisements ”;

TinyDB – Caching Query

TinyDB – Caching Query ”; Previous Next Catching query is an advanced feature of TinyDB with the help of which it caches the query result for performance optimization. In this way, when we run the same query again, TinyDB doesn”t need to read the data from the storage. We can pass the cache_size to the table function to optimize the query cache size. Syntax The syntax of TinyDB query caching is shown below − table = db.table(”table_name”, cache_size=value) Example TinyDB creates cache size memory in given table. from tinydb import TinyDB db = TinyDB(”student.json”) objects = db.table(”Student_Detail”, cache_size = 50) objects.all() It will produce the following output. Observe that cache size does not affect the table values. [{ ”roll_number”: 1, ”st_name”: ”elen”, ”mark”: 250, ”subject”: ”TinyDB”, ”address”: ”delhi” }] We can set unlimited cache size by putting “cache_size = None”. objects = db.table(”Student_Detail”, cache_size = None) We can also disable the cache size by putting “cache_size = 0”. objects = db.table(”Student_Detail”, cache_size = 0) To clear the cache size, use the following query − db.clear_cache() Print Page Previous Next Advertisements ”;