Monday, July 1, 2024
Google search engine
HomeDevelopersSembast: NoSQL Database

Sembast: NoSQL Database

This article mainly focuses light on the Sembast database, though there are many other databases that are readily used in Flutter like Hive, Moor, SQFlite but before going over the implementation of it we should have a Rough Idea of Database.

DataBase: Database is an Organized collection of structured or well-managed information basically electronically stored in the electronic device or machine. There are various types of databases present.

Some of the prominent ones are :

Relational databases: In the relational database data/items are organized as a set of tables with columns and rows. it is the most efficient and flexible way to access structured information

NoSQL/Nonrelation databases: It allows unstructured and semi-structured data to be stored and manipulated (in contrast to a relational database, which defines how all data inserted into the database must be composed). NoSQL databases grew popular as web applications became more common and more complex.

Before Letting Readers Dive Into the whereabouts of Sembast database , They will need to get some Insights about what constitute — NoSQL database as sembast database comes under the categaory of NoSQL database

NoSQL Database:

NoSQL database consists of some important aspects as it handles large no of data with ease, works faster even when you make queries to search something, and also has strong data encryption as compared to the SQL database. In SQLite, it is based on a structured approach where we require a schema with tables and columns but in NoSQL, we do not need such kinds of things and you only need to do is simply pass objects to the storage.

Some advantages of NoSQL Database :

  • The right data model: key-value, graph, wide column, or document models
  • Large volumes of structured, semi-structured, and unstructured data
  • Object-oriented programming that is easy to use and flexible
  • Efficient, scale-out architecture instead of expensive, monolithic architecture

This was a general overview of NoSQL database here after reading thing the first thing which will come in your mind is

Which one is better ? Relational(SQL) database or NoSQL

If we compare NoSQL database with Traditional(Relational) database the main thing which comes, as a result, both databases are having different qualities like if you see when we work on a relational database and if we have more raw data and when it starts growing its searching becomes slower while in NoSQL database we don’t face these problems as compared to Relational database.

Role of Database in APP

If I talk about apps, Apps are mainly designed to make suitable user interactions that make users do their work easily, and here storing data becomes a must. if your app is having a database that consists of more space your app becomes slower and to get riddance of this you need to choose your database carefully, However, storing data in the flutter is not the easiest task if you look beyond preferences and here the importance of such kinds of things(Database) for storing data comes into effect.

so here we observed each and everything which we require to know to implement Sembast and now without wasting a second let’s first take a look at what is all about Sembast.

SemBast Database:

As preferred by name Sembast db stands for Simple Embedded Application Store database.

Yet another NoSQL persistent store database solution for single process io applications. The whole document based database resides in a single file and is loaded in memory when opened. Changes are appended right away to the file and the file is automatically compacted when needed.

Sembast also supports encryption using user-defined codec so its new thing to work on. but the thing which makes it different among all NoSQL databases is its working compatibility with de-normalized data as you know that it is just a JSON file and viewing and accessing the content from the database is easy as compared to others. we can understand with an example suppose you are going to work with a database where you need to provide an id for every table and it could become a problem if you need to specify it separately but in Sembast it is quite easy that it creates ids automatically, you also get riddance from boilerplate code which improves your productivity as a developer. This was a description in simple words to make you understand.

Now let’s understand its working and implementation :

Implementation:

You first need to add some dependencies in your project

path_provider: ^latest version
sembast: ^latest version

Like database libraries, SEMBAST needs to be opened before so that we can store and retrieve data because the library can make a connection with a file on the persistent storage where all the data is stored.

How Does Sembast stores the database rows/records?

Semabast follows the store concept, we can imagine that a table which holds data and a key associated with it, and that key is automatically generated and unique for every record. So you don’t need to generate any autoincremented key for this.

and very set of data will look like this

{“key”:1, ”store”:”StoreName”, ”value”:{}}

key: The key is an autogenerated unique entity.

store: It identifies the data table or folder for which the belongs, It could be considered as an equivalent to a table in the relational database.

value: Value is actual data that reside in the database and as Sembast is a NoSQL database so the value should be stored in key-value pair.

here we are going to create a simple example where we will create a list of books that we will create by inserting the data.

Let’s create a model class and add its toJson() and fromJson() methods.

https://gist.github.com/shivanchalaeologic/bb918bdea8f0e94e4684749bc94800b2#file-books_model-dart

DataBase setup:

Here we have created the database and you can easily understand what every piece of code does as it is well explained in this file.

https://gist.github.com/shivanchalaeologic/68e3120dca82e8a5b617df20e07b25b8#file-data_base-dart

After creating the modelclass let’s make a Dao class to deal with the database and it performs the required CRUD operation. you can see in books_dao.dart below :

Firstly we stores the name of our table/folder in a String variable and then used a intMapStoreFactory.store(folderName) to initialise it as a table/folder. then we create the instance of Books Database in line no 9. and below this line there four methods are used

:: insertBooks() : In this method, we are using add() method and passing the database instance into map format.

:: updateBooks(): In this method, we are using update() method to update the record using a finder object. The finder object is a parameter that is used to update the function.

:: delete(): It performs the delete function and work like update() but also removes the data table from the database.

:: getAllBooks(): It is the function from which we get all records from the database.

https://gist.github.com/shivanchalaeologic/459fc3c039aaea09eb6db55b1026617b#file-books_dao-dart

After creating all these files we perform actions in the UI part for which you can find the source code from here:

flutter-devs/sembast_demo
A new Flutter application. This project is a starting point for a Flutter application. A few resources to get you…github.com

In the given video you can see that list is updating on entering values :

Conclusion:

After doing a comparative study of the Sembast NoSQL database and its working with an example we have come to point where we have found that the Sembast NoSQL database is faster, easy to manage, fast readable, a strong encryption technique and able to handle de-normalized data efficiently.

So if you want to handle your project with these qualities you need to go with the Sembast NoSQL database.

In the article, I have explained the basic architecture of Sembast you can modify this code according to your choice, this was a small introduction of Sembast from my side.

If this article has helped you a bit and found interesting please clap!👏

Thanks for reading this article if you find anything that could be improved please let me know, I would love to improve.💙


Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, Twitter, and LinkedIn for any flutter related queries.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences!.


Previous article
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments