A library management system is software that maintains a library's records. It captures data specific to the books on hand, which books have been rented, late fees, member information, and more.
In this code pattern, we show you how to build a library management system using CockroachDB hosted on Red Hat Marketplace. CockroachDB is an ultra resilient, distributed SQL that can easily scale-out serializable transactions for your apps and services. Its cloud-native architecture makes it easier to scale and also guarantees consistent transactions across multiple regions and multiple clouds.
When you have completed this code pattern, you will understand how to:
- Install CockroachDB Operator from Red Hat Marketplace on an OpenShift Cluster
- Create a CockroachDB cluster instance
- Create a user and database in CockroachDB
- Store and query unstructured JSON data from a third-party API in CockroachDB
- Manage CockroachDB cluster from the Cluster Overview page
- User performs an operation like borrowing a book or returning a book.
- Application updates the appropriate CockroachDB table accordingly.
- Application fetches the updated data from the table.
- Application displays the updated data that was feteched from the table.
- Clone the repo
- Install the CockroachDB Operator from Red Hat Marketplace on OpenShift Cluster
- Create a Database in CockroachDB
- Port forward CockroachDB
- Run the application
- Explore the library management system
Clone the library-management-system-using-cockroachdb-hosted-on-rhm
repo locally. In a terminal, run:
git clone https://github.com/IBM/library-management-system-using-cockroachdb-hosted-on-rhm
Note: If you have already followed the Get started using a CockroachDB operator hosted on Red Hat Marketplace Tutorial, you can skip this section.
-
Follow the steps to deploy the CockroachDB Operator from Red Hat Marketplace on a OpenShift Cluster:
-
Once you successfully set up CockroachDB Operator on OpenShift Cluster, you can create a database.
-
Create a database called
library
in CockroachDB. -
In your terminal, run the following command to spin up a CockroachDB client:
$ kubectl run -it --rm cockroach-client \
--image=cockroachdb/cockroach \
--restart=Never \
--command -- \
./cockroach sql --insecure --host=example-cockroachdb-public.cockroachdb-test
- This should run the CockroachDB client and take you to a
SQL Command Prompt
as shown. If you don't see a command prompt, try pressing Enter.
root@example-cockroachdb-public.cockroachdb-test:26257/defaultdb>
- Create a database called
library
by running the following command:
root@example-cockroachdb-public.cockroachdb-test:26257/defaultdb> CREATE DATABASE library;
- You can come out of the SQL Prompt by the
\q
command:
root@example-cockroachdb-public.cockroachdb-test:26257/defaultdb> \q
Once the CockroachDB Operator is set up successfully on your OpenShift Cluster, you need to port forward the CockroachDB database instance from OpenShift to establish connection in our application locally.
- In your terminal, run the following command to port forward the
26257
port from the CockroachDB database instance.
NOTE: You must be logged in with your
OC login
credentials before running the following commands.
$ kubectl port-forward example-cockroachdb-0 26257
Forwarding from 127.0.0.1:26257 -> 26257
Forwarding from [::1]:26257 -> 26257
-
Go to the cloned repo from step 1. In your terminal, run the following commands to install the required Python libraries and run the application.
- Install the required Python libraries by running the following command:
$ pip install -r requirements.txt
- Run the application as follows:
$ python app.py
The application will be listening on <http://localhost:8090>
-
Visit http://localhost:8090 on your browser.
-
There are three tabs in the system: Display Books, Borrow Books, and Return Books.
-
In the Display Books tab, you can see three books listed. The book details include:
- Book Name
- Book Author
- Book Availibility
- Book Amount
NOTE: These details are initialized by the Python script.
-
Click the Borrow Books tab which shows which books you can borrow the from the store. Borrower details include:
- Borrower Name
- Borrower Email
- Available books that can be borrowed
-
Enter your name, email address, and book quantities that you wish to borrow and click Submit as shown.
-
After you've successfully borrowed a book, you can see the number of availibile books decreasing.
-
Click on the Return Books tab. You can return the borrowed books from here.
-
Enter your email address for who borrowed the books, and click Search.
-
Your name, the books that you have borrowed, and the total amount due will be displayed. Click on pay and return to return the books as shown.
- After you successfully return the books, you will get an aleart like the following:
Notice that the availibility of the books increased again in the Display Books
section.
You can verify the table in the ClockroachDB instance through the CockroachDB client from terminal.
- In your terminal, run the following command to spin up a CockroachDB client:
$ kubectl run -it --rm cockroach-client \
--image=cockroachdb/cockroach \
--restart=Never \
--command -- \
./cockroach sql --insecure --host=example-cockroachdb-public.cockroachdb-test
This should run the CockroachDB client and take you to a SQL Command Prompt
as shown. If you don't see a command prompt, press Enter.
root@example-cockroachdb-public.cockroachdb-test:26257/defaultdb>
-
From the CockroachDB client, run the following commands to view the
user
,database
andtable
that were created by the Library Management Application:- View
users
by running theSHOW users;
command as follows:
root@example-cockroachdb-public.cockroachdb-test:26257/defaultdb> SHOW users; user_name `-------------` cpuser maxroach root (3 rows) Time: 3.037641ms
- View
databases
by running theSHOW databases;
command as follows:
root@example-cockroachdb-public.cockroachdb-test:26257/defaultdb> SHOW databases; database_name `-----------------` bank defaultdb library postgres system (5 rows) Time: 2.890031ms
- To view the tables present in the
library
database, run theUSE library;
command to switch tolibrary
database, and run\d
command to view thetables
as follows:
root@example-cockroachdb-public.cockroachdb-test:26257/defaultdb> USE library; SET Time: 11.83841ms root@example-cockroachdb-public.cockroachdb-test:26257/library> \d table_name `----------------------` books borrowers (2 rows) Time: 3.684617ms
- Finally, to view the tables
books
andborrowers
, run theSELECT
command as follows:
root@example-cockroachdb-public.cockroachdb-test:26257/defaultdb> SELECT * FROM books;
id | book_name | book_author | book_price | book_availability -----+-------------------------+-------------+------------+-------------------- 1 | Harry Potter | Jk Rowling | 2 | 30 2 | Start with Why | Simon Sinek | 1.5 | 20 3 | Programming with Python | John Smith | 1.5 | 25
root@example-cockroachdb-public.cockroachdb-test:26257/defaultdb> SELECT * FROM borrowers;
id | borrower_name | borrower_email | book_id | total_price | book_quantity ---------------------+---------------+-----------------+---------+-------------+---------------- 572547941512544257 | Manoj | manoj@gmail.com | 1,2,3 | 12.5 | 4,1,2
- View
This code pattern is licensed under the Apache License, Version 2. Separate third-party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the Developer Certificate of Origin, Version 1.1 and the Apache License, Version 2.