As a front end developer it's a pain to wait for API's to be delivered. What if there is a miracle Database that has the API built in. Well, it's no longer fantasy anymore. Injee is a Database that comes with ready to use CRUD API's for front end developers. By reading this page, you will learn how to use Injee, create a record of books in injee, and you will learn how to manipulate and search the data.
You need to do this only once. Visit https://java.com to download Java for your machine. Once installed on your CMD, or Terminal type java --varsion and it must work.
You can download injee by clicking here. Or in your terminal use:
$ wget https://codeberg.org/injee/injee/releases/download/0.2.0/injee-0.2.0.jar
Navigate to the directory where the injee jar file is downloaded, and run it using:
$ java -jar injee-0.2.0.jar
Let's check if the server is running. We use the API GET http://localhost:4125/ops/health.
In your terminal try:
$ curl -X GET http://localhost:4125/ops/health
Output should be
{ "health": "ok" }
So let's create a repo of books, magically injee has the API POST http://localhost:4125/api/books to create a book. If you want to create a repo of cars, injee has the API POST http://localhost:4125/api/cars API. So let's create a book and store it in injee:
$ curl -X POST http://localhost:4125/api/books \ -H "Content-Type: application/json" \ -d '{"title": "Treasure Island", "author": "Robert Louis Stevenson"}'
Output
{ "title": "Treasure Island", "author": "Robert Louis Stevenson", "id": "722e2b57-59cc-4254-85b5-562858264f75" }
So injee stores the book, and gives out a JSON that has all the values you sent to injee, as well as a UUID, which is assigned to a ney named id.
Now let's create another book:
$ curl -X POST http://localhost:4125/api/books \ -H "Content-Type: application/json" \ -d '{"title": "Adventures of Huckleberry Finn", "author": "Mark Twain"}'
Output
{ "title": "Adventures of Huckleberry Finn", "author": "Mark Twain", "id": "689976e3-082e-4943-9525-a21b47cba325" }
And it works!
Now to list all books we use GET http://localhost:4125/api/books:
$ curl -X GET http://localhost:4125/api/books
Output
[ { "title": "Treasure Island", "author": "Robert Louis Stevenson", "id": "722e2b57-59cc-4254-85b5-562858264f75" }, { "title": "Adventures of Huckleberry Finn", "author": "Mark Twain", "id": "689976e3-082e-4943-9525-a21b47cba325" } ]
We get a nice array of books we have stored.
Now let's fetch just one book, for that we use the API GET http://localhost:4125/api/books/:id:
$ curl -X GET http://localhost:4125/api/books/689976e3-082e-4943-9525-a21b47cba325
Output
{ "title": "Adventures of Huckleberry Finn", "author": "Mark Twain", "id": "689976e3-082e-4943-9525-a21b47cba325" }
So if I prepend id GET http://localhost:4125/api/books/ I get the details of a single book.
To updatea book use PUT along with http://localhost:4125/api/books/:id, followed by parameters for the book:
$ curl -X PUT http://localhost:4125/api/books/689976e3-082e-4943-9525-a21b47cba325 \ -H "Content-Type: application/json" \ -d '{"title": "Adventures of Tom Sawyer"}'
Output
{ "title": "Adventures of Tom Sawyer", "author": "Mark Twain", "id": "689976e3-082e-4943-9525-a21b47cba325" }
So as you can see above, the title of the book has been changed from Adventures of Huckleberry Finn to Adventures of Tom Sawyer.
Now let's list all books:
$ curl -X GET http://localhost:4125/api/books
Output
[ { "title": "Treasure Island", "author": "Robert Louis Stevenson", "id": "722e2b57-59cc-4254-85b5-562858264f75" }, { "title": "Adventures of Tom Sawyer", "author": "Mark Twain", "id": "689976e3-082e-4943-9525-a21b47cba325" } ]
to confirm our update.
Now let's delete a book. For that use DELETE along with http://localhost:4125/api/books/:id:
$ curl -X DELETE http://localhost:4125/api/books/689976e3-082e-4943-9525-a21b47cba325
Output
There will be no output, you should get status 204, if you are trying it in code and receive the response object.
Now let's list all books, and confirm that Adventures of Tom Sawyer has been deleted:
$ curl -X GET http://localhost:4125/api/books
Output
[ { "title": "Treasure Island", "author": "Robert Louis Stevenson", "id": "722e2b57-59cc-4254-85b5-562858264f75" } ]
Now let's create a user:
$ curl -X POST http://localhost:4125/api/users \ -H "Content-Type: application/json" \ -d '{"name": "Karthik"}'
Output
{ "name": "Karthik", "created_at": "2024-07-22T11:18:42Z", "updated_at": "2024-07-22T11:18:42Z", "id": "ad100ab0-7893-421d-9233-353cc8899aa9" }
So now there must be two tables in our db namely books and users, let's list them using the following API:
$ curl -X GET http://localhost:4125/ops/tables
Output
[ "books", "users" ]
Let's add another user record into users table:
$ curl -X POST http://localhost:4125/api/users \ -H "Content-Type: application/json" \ -d '{"name": "Pari"}'
Let's now fetch all users and confirm our addition
$ curl -X GET http://localhost:4125/api/users
[ { "name": "Karthik", "created_at": "2024-07-22T11:18:42Z", "updated_at": "2024-07-22T11:18:42Z", "id": "ad100ab0-7893-421d-9233-353cc8899aa9" }, { "name": "Pari", "created_at": "2024-07-22T11:23:27Z", "updated_at": "2024-07-22T11:23:27Z", "id": "1f06bb65-1f2d-4980-9cfc-cf3d38c9db7e" } ]
Now let's search for a string in users:
$ curl -X GET http://localhost:4125/api/users?q=Pari
[ { "name": "Pari", "created_at": "2024-07-22T11:23:27Z", "updated_at": "2024-07-22T11:23:27Z", "id": "1f06bb65-1f2d-4980-9cfc-cf3d38c9db7e" } ]
Now lets backup our DB into a file named backup.json:
$ curl -X GET http://localhost:4125/ops/save?file=backup.json
Output
{ "message": "saved to file backup.json" }
Finally, to stop injee, in terminal where injee is running hit Ctrl c in terminal where injee is running to stop it.
Let's start injee again:
$ java -jar injee-0.2.0.jar
$ curl -X GET http://localhost:4125/ops/load?file=backup.json
Output
{ "message": "loaded from file backup.json" }
So you have got your original DB back and running. Congrats.
One of the best ways to keep upto date with Injee is to follow its page here https://injee.codeberg.page/ , or follow its RSS here https://codeberg.org/injee.rss
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3