Overview
This article walks through setting up an InvokeDB table and connecting it to an angular application. Click here to see a full example of the To Do application.
InvokeDB is a simple NoSQL database with built in API endpoints when tables are created. We can use this to store our to do list items.
Prerequisites
- Create an InvokeDB account
- Install the Angular CLI
Guide
Set up ToDo table in InvokeDB
- Login to your account at https://invokedb.com
- Click the
+
icon and selectEmpty Table
- Name the table
ToDo
- Edit the table so that you have 2 string columns with the following names
name
isComplete
The table is now setup, you can view available API endpoints by clicking the API
button in the top right of the table.
Your table should look like this
Calling the ToDo table from Angular
The samples below will show how to send a request to the Get
and Search
endpoints for the To Do table.
Retrieve your api token from https://db.invokedb.com/account
Set the following 2 variables
const BASE_URL = 'https://api.invokedb.com/v1';
const API_KEY = 'your api key';
InvokeDB requires the api key to be sent as a Bearer token. So we'll create a helper method to return an Authorization header.
getHeaders() {
return new HttpHeaders().set('Authorization', `Bearer ${API_KEY}`);
}
Create a getRows()
method to call InvokeDB
getRows(table: string, params: any, filter?: any) {
const { skip, limit } = params;
const urlQuery = `table=${table}&skip=${skip}&limit=${limit}`;
const headers = this.getHeaders();
return filter
? this.http.post(`${BASE_URL}/search?${urlQuery}`, filter, { headers })
: this.http.get(`${BASE_URL}/get?${urlQuery}`, { headers });
}
Create the getItems()
method
getItems(showCompleted = true) {
const params = {
skip: 0,
limit: 200
};
let filter;
if (!showCompleted) {
filter = {
isComplete: {
value: 'no',
type: 'equals'
}
};
}
return this.invokedb
.getRows('todo', params, filter)
.pipe(map((res: any) => res.data));
}
Assuming the above code was put into a service, you can now use the following code to query the todo items stored in the table.
this.svc
.getItems(this.showCompleted)
.subscribe(items => (this.items = items));
Top comments (0)