Skip to content

Commit 511757c

Browse files
committed
Updated Readme
1 parent 960a5b2 commit 511757c

File tree

1 file changed

+159
-1
lines changed

1 file changed

+159
-1
lines changed

README.md

Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,159 @@
1-
# ruby-couchbase-orm-quickstart
1+
# Quickstart in Couchbase with Ruby Couchbase ORM
2+
3+
## Build a REST API with Ruby Couchbase ORM
4+
5+
Often, the first step developers take after creating their database is to create a REST API capable of performing Create, Read, Update, and Delete (CRUD) operations for that database. This repository is designed to teach you and provide you with a starter project (in Ruby on Rails) to generate such a REST API. Once you have installed the travel-sample bucket in your database, you can run this application, which is a REST API with Swagger documentation, to learn:
6+
7+
- How to create, read, update, and delete documents using Key-value operations (KV operations). Key-value operations are unique to Couchbase and provide super-fast (think microseconds) queries.
8+
- How to write simple parameterized SQL++ queries using the built-in travel-sample bucket.
9+
10+
You can find the full documentation for the tutorial on the [Couchbase Developer Portal](https://www.couchbase.com/developers)
11+
12+
## Prerequisites
13+
14+
To run this prebuilt project, you will need:
15+
16+
- [Couchbase Capella cluster](https://www.couchbase.com/products/capella/) with [travel-sample bucket](https://docs.couchbase.com/ruby-sdk/current/ref/travel-app-data-model.html) loaded.
17+
- To run this tutorial using a [self-managed Couchbase cluster](https://docs.couchbase.com/capella/current/getting-started/self-managed-cluster.html), please refer to the appendix.
18+
- [Ruby 3.3.0](https://www.ruby-lang.org/en/documentation/installation/) is installed on the local machine.
19+
- Basic knowledge of [Ruby](https://www.ruby-lang.org/en/documentation/), [Ruby on Rails](https://rubyonrails.org/), and [RSpec](https://rspec.info/).
20+
21+
## Loading Travel Sample Bucket
22+
23+
If travel-sample is not loaded in your Capella cluster, you can load it by following the instructions for your Capella Cluster:
24+
25+
- Load travel-sample bucket in Couchbase Capella
26+
27+
## App Setup
28+
29+
We will walk through the different steps required to get the application running.
30+
31+
### Cloning Repo
32+
33+
```sh
34+
git clone https://github.com/couchbase-examples/ruby-couchbase-orm-quickstart.git
35+
```
36+
37+
### Install Dependencies
38+
39+
Any dependencies will be installed by running the bundle install command, which installs any dependencies required for the project.
40+
41+
```sh
42+
bundle install
43+
```
44+
45+
### Setup Database Configuration
46+
47+
To learn more about connecting to your Capella cluster, please follow the instructions.
48+
49+
Specifically, you need to do the following:
50+
51+
Open the `config/couchbase.yml` file and update the connection string, username, password, and bucket name for the development and test environments.
52+
53+
```yml
54+
common: &common
55+
connection_string: couchbase://localhost
56+
username: Administrator
57+
password: password
58+
59+
development:
60+
<<: *common
61+
bucket: travel-sample
62+
63+
test:
64+
<<: *common
65+
bucket: travel-sample
66+
```
67+
68+
> Note: The connection string expects the `couchbases://` or `couchbase://` part.
69+
70+
## Running The Application
71+
72+
### Directly on machine
73+
74+
At this point, we have installed the dependencies, loaded the travel-sample data and configured the application with the credentials. The application is now ready and you can run it.
75+
76+
The application will run on a port specified by Rails. You can find the port in the terminal after running the application. You will find the Swagger documentation at [http://localhost:3000/api-docs](http://localhost:3000/api-docs) of the API if you go to the URL in your browser.
77+
78+
```sh
79+
rails server
80+
```
81+
82+
### Using Docker
83+
84+
- Build the Docker image
85+
86+
```sh
87+
docker build -t ruby-couchbase-orm-quickstart .
88+
```
89+
90+
- Run the Docker image
91+
92+
```sh
93+
docker run -p 3000:3000 ruby-couchbase-orm-quickstart -e DB_CONN_STR=<connection_string> -e DB_USERNAME=<user_with_read_write_permission_to_travel-sample_bucket> -e DB_PASSWORD=<password_for_user>
94+
```
95+
96+
## Verifying the Application
97+
98+
Once the application starts, you can see the details of the application on the terminal.
99+
100+
Show Image
101+
102+
The application will run on the port specified by Rails on your local machine (eg: http://localhost:3000). You will find the interactive Swagger documentation of the API if you go to the URL in your browser. Swagger documentation is used in this demo to showcase the different API endpoints and how they can be invoked. More details on the Swagger documentation can be found in the appendix.
103+
104+
Show Image
105+
106+
## Running The Tests
107+
108+
The application comes with a set of integration tests that can be run to verify the functionality of the application. The tests are written using RSpec, a popular testing framework for Ruby.
109+
110+
To run the tests, you can use the following command:
111+
112+
```sh
113+
bundle exec rspec test/integration
114+
```
115+
116+
# Appendix
117+
118+
## Data Model
119+
120+
For this quickstart, we use three collections, airport, airline and routes that contain sample airports, airlines and airline routes respectively. The route collection connects the airports and airlines as seen in the figure below. We use these connections in the quickstart to generate airports that are directly connected and airlines connecting to a destination airport. Note that these are just examples to highlight how you can use SQL++ queries to join the collections.
121+
122+
![Data Model](image_link)
123+
124+
## Extending API by Adding New Entity
125+
126+
If you would like to add another entity to the APIs, these are the steps to follow:
127+
128+
1. Create a new model: Create a new model for the entity in the `app/models` folder. This model should contain the schema for the entity.
129+
2. Create the new route: In Rails, you can create a new route by adding a new resource in the `config/routes.rb` file. This file should contain the logic for the new entity's CRUD operations.
130+
3. Create the new controller: Create a new controller for the entity in the `app/controllers` folder. This controller should contain the logic for the new entity's CRUD operations.
131+
4. Add the integration tests: Add integration tests for the new entity in the `test/integration` folder. This ensures that the new entity's CRUD operations are tested. The test file should be named `<entity>_spec.rb`.
132+
133+
Following these steps ensures a systematic and organized approach to expanding the API functionality with a new entity.
134+
135+
## Running Self-Managed Couchbase Cluster
136+
137+
If you are running this quickstart with a self-managed Couchbase cluster, you need to load the travel-sample data bucket in your cluster and generate the credentials for the bucket by creating a user.
138+
139+
You need to update the connection string and the credentials in the `dev.env` file in the root folder.
140+
141+
Note: Couchbase Server must be installed and running prior to running this app.
142+
143+
## Swagger Documentation
144+
145+
Swagger documentation provides a clear view of the API including endpoints, HTTP methods, request parameters, and response objects.
146+
147+
Click on an individual endpoint to expand it and see detailed information. This includes the endpoint's description, possible response status codes, and the request parameters it accepts.
148+
149+
### Trying Out the API
150+
151+
You can try out an API by clicking on the "Try it out" button next to the endpoints.
152+
153+
Parameters: If an endpoint requires parameters, Swagger UI provides input boxes for you to fill in. This could include path parameters, query strings, headers, or the body of a POST/PUT request.
154+
155+
Execution: Once you've inputted all the necessary parameters, you can click the "Execute" button to make a live API call. Swagger UI will send the request to the API and display the response directly in the documentation. This includes the response code, response headers, and response body.
156+
157+
### Models
158+
159+
Swagger documents the structure of request and response bodies using models. These models define the expected data structure using JSON schema and are extremely helpful in understanding what data to send and expect.

0 commit comments

Comments
 (0)