Let me introduce one of my pet projects I had build about 2 years ago. I named it HYDB. Sort of like you can say Hybrid Database (I don't even know why I did select it :/). The main idea of this application was to build basic API endpoints without doing a single coding. For example, you have a very basic IoT application which collects your room temperature and few other information to control you air conditioner. You just need 2-3 API endpoints like:
- Get room environment data like currently (temperature, humidity)
- To power on/off your AC.
May be, that's all. I couldn't get that in my heads to create these two simple API endpoints and deploy it in a server! I was thinking there are many basic applications which are sort of like a collection of 2-3 endpoints. Why do we need an API endpoint actually? The fundamental purpose is to store, process and fetch data. Isn't it? I knew that I'm not the only one who was thinking of it but I really wanted to do something of my own and something better. That was actually the motivation behind HYDB. So how do you can create an API in HYDB? You just need to:
- Define your Data Models. Data model is just a schema. You define your properties here. Each property has their own name and a type. You can also add few more configurations if you want.
- Create Services. These services are the collections of Operations like: Query, Create, Update, Delete.
- Add Operations in Services. These operations stores the logic of your endpoints. Operations are of two types: Query and Mutation (Create, Update and Delete). HYDB has a Script Editor where you can configure you API logic. For Example, a query type operations would be:
{ "dataSource": "temperature_log", // temperature_log is a data model "filters": "temparature >= 26.00 && logged_at = '2011-10-05T14:48:00.000Z'" }
So when you run the operation by calling the API it would get data from temperature_log data model where temparature >= 26.00 && logged_at = '2011-10-05T14:48:00.000Z'. Data model has its own data filter syntax compiler and it's quite similar as any regular programming syntax.
That's it! Okay now let me demonstrate by creating a basic Angular application with HYDB. With that you will get the picture what I was trying to build actually. My application is going to be sort of like this: a simple list and some input fields.
Let's start by defining the main Person interface:
Now let's create the basic structure, populate some items in our table like this:
This will be generated as like this:
But we don't want it like that. We want the data to be stored in a particular database and also have some operation to add/modify data. For this we'll need to have a backend or we can use other backend-as-a-service platforms that can give us the facility to do that. I'm gonna show you how you can do this with HYDB. Create a free account and go to your DbConsole. Let's first define the person data model to store our data. Think it as a table in a SQL like databases.
That's all! now let's create a service. Services are the collection of operations. Think operations as functions of API endpoints. There are two types of operations. If you like to modify data like add/edit/delete you can choose mutation operation and if you are querying then simply select query type operation. There are three types of mutation operations: create, update and delete. I will be creating a mutation operation as I'm gonna add data:
After creating the operation simply click on the operation and navigate to the script editor to define the operation. The dataSource property is going to tell HYDB that on which data model we are gonna applying our mutation. As we will be creating new data items/objects through this so we are creating a create type mutation:
Now here comes the security part. You really don't wanna make your operation open for every applications right? Then create a client and grab the API key.
We are all done! Now let's go back to our application. HYDB has a request structure that we need to follow. You'll get all references in the docs. Let's define our request in the angular app:
Then create a method in your component class where will be sending our new contact data in the server:
and modify your template:
Now test you application. If I click "Add Contact" button after filling some info in the form I will get this response from HYDB:
And that means our data has been added in the database. Now let's create a query operation in HYDB to fetch all our data. I have created one in the services:
Let's navigate to the script editor:
HYDB has some good query features that will help you to get your data easily. For now we just want all the data that's why we are keeping fields and filter property empty. I'll show you to use these soon. Now let's write a method in our application:
This should grab all the data from the database and populate them in the list. Now let's see how to use the filters while querying. We are going to get person by their ID. For this let's create another query operation "get_by_id" and navigate to the script editor:
Look at this part "id == p_id" carefully. By default, HYDB assigns an unique ID for every objects that are created. Here, we are telling that get all the data that matches the id value with the p_id value. Now what we have to do is to define this p_id in our request. That means we are going to feed the value for p_id from our application. We feed these values in the "args" section in our request. So, let's write a method for that in our app,
That's all! Please let me know about your thoughts on my project!