|
1 | 1 | <!--- STARTEXCLUDE --->
|
2 |
| - |
3 | 2 | # Astra DB REST API with Google Cloud Functions in Node.js
|
4 |
| - |
5 |
| -_30 minutes, Intermediate, [Start Building](https://github.com/DataStax-Examples/google-cloud-functions-nodejs#prerequisites)_ |
| 3 | +*30 minutes, Intermediate, [Start Building](https://github.com/DataStax-Examples/google-cloud-functions-nodejs#prerequisites)* |
6 | 4 |
|
7 | 5 | This example shows how to use [Google Cloud Functions](https://cloud.google.com/functions/) with the [Node.js DataStax Cassandra Driver](https://docs.datastax.com/en/developer/nodejs-driver/latest) to set up a basic REST API for a Cassandra database via HTTP Endpoints. The [Serverless Framework](https://serverless.com/) is used to facilitate the setup and deployment of the functions.
|
8 |
| - |
9 | 6 | <!--- ENDEXCLUDE --->
|
10 | 7 |
|
| 8 | + |
11 | 9 | 
|
12 | 10 |
|
13 |
| -## Objectives |
14 | 11 |
|
| 12 | +## Objectives |
15 | 13 | - How to use the DataStax Cassandra Driver with Google Cloud Functions
|
16 | 14 | - How to use the Serverless Framework to set up Google Cloud Functions HTTP Endpoints
|
17 |
| - |
| 15 | + |
18 | 16 | ## How it works
|
19 |
| - |
20 | 17 | The Serverless Framework handles the packaging and deployment of the functions to the Google Cloud resources. Once the functions are deployed, the DataStax Cassandra Driver establishes the connection to the database and returns the results via the Google Cloud HTTP Endpoints which can be accessed to interact with the database.
|
21 | 18 |
|
22 | 19 | ## Get Started
|
23 |
| - |
24 | 20 | To build and play with this app, follow the build instructions that are located here: [https://github.com/DataStax-Examples/google-cloud-functions-nodejs](https://github.com/DataStax-Examples/google-cloud-functions-nodejs#prerequisites)
|
25 | 21 |
|
26 | 22 | <!--- STARTEXCLUDE --->
|
27 |
| - |
28 | 23 | ## Prerequisites
|
29 |
| - |
30 | 24 | Let's do some initial setup by creating a serverless(!) database.
|
31 | 25 |
|
32 | 26 | ### DataStax Astra
|
33 |
| - |
34 | 27 | <!--- enter a unique UTM_CODE for your sample app below --->
|
35 |
| - |
36 | 28 | 1. Create a [DataStax Astra account](https://dtsx.io/3jENJkP) if you don't already have one:
|
37 |
| -  |
| 29 | + |
38 | 30 |
|
39 | 31 | 2. On the home page. Locate the button **`Create Database`**
|
40 |
| -  |
| 32 | + |
41 | 33 |
|
42 | 34 | 3. Locate the **`Get Started`** button to continue
|
43 |
| -  |
| 35 | + |
44 | 36 |
|
45 | 37 | 4. Define a **database name**, **keyspace name** and select a database **region**, then click **create database**.
|
46 |
| -  |
| 38 | + |
47 | 39 |
|
48 |
| -5. Your Astra DB will be ready when the status will change from _`Pending`_ to **`Active`** 💥💥💥 |
49 |
| -  |
| 40 | +5. Your Astra DB will be ready when the status will change from *`Pending`* to **`Active`** 💥💥💥 |
| 41 | + |
50 | 42 |
|
51 | 43 | 6. After your database is provisioned, we need to generate an Application Token for our App. Go to the `Settings` tab in the database home screen.
|
52 |
| -  |
| 44 | + |
53 | 45 |
|
54 | 46 | 7. Select `Admin User` for the role for this Sample App and then generate the token. Download the CSV so that we can use the credentials we need later.
|
55 |
| -  |
| 47 | + |
56 | 48 |
|
57 | 49 | 8. After you have your Application Token, head to the database connect screen and select the driver connection that we need. Go ahead and download the `Secure Bundle` for the driver.
|
58 |
| -  |
| 50 | + |
59 | 51 |
|
60 | 52 | 9. Make note of where to use the `Client Id` and `Client Secret` that is part of the Application Token that we generated earlier.
|
61 |
| -  |
| 53 | + |
62 | 54 |
|
63 | 55 | ### Github
|
64 | 56 |
|
65 | 57 | 1. Click `Use this template` at the top of the [GitHub Repository](https://github.com/DataStax-Examples/google-cloud-functions-nodejs):
|
66 |
| -  |
| 58 | + |
67 | 59 |
|
68 | 60 | 2. Enter a repository name and click 'Create repository from template':
|
69 |
| -  |
| 61 | + |
70 | 62 |
|
71 | 63 | 3. Clone the repository:
|
72 |
| -  |
| 64 | + |
73 | 65 |
|
74 | 66 | ## 🚀 Getting Started Paths:
|
75 |
| - |
76 |
| -_Make sure you've completed the [prerequisites](#prerequisites) before starting this step_ |
77 |
| - |
78 |
| -- [Running on your local machine](#running-on-your-local-machine) |
| 67 | +*Make sure you've completed the [prerequisites](#prerequisites) before starting this step* |
| 68 | + - [Running on your local machine](#running-on-your-local-machine) |
79 | 69 |
|
80 | 70 | ### Running on your local machine
|
81 |
| - |
82 |
| -Before running with this example, head over to the [SETUP-README](https://raw.githubusercontent.com/DataStax-Examples/google-cloud-functions-nodejs/master/SETUP-README.md) for instructions on how to |
83 |
| - |
| 71 | +Before running with this example, head over to the [SETUP-README](https://raw.githubusercontent.com/DataStax-Examples/google-cloud-functions-nodejs/master/SETUP-README.md) for instructions on how to |
84 | 72 | 1. launch an instance in Google Cloud
|
85 | 73 | 2. install and start a Cassandra database
|
86 | 74 | 3. setup your local development environment for Node.js and [serverless](https://serverless.com)
|
87 | 75 |
|
88 | 76 | Once the above is completed, you will have all of the needed pieces in place to run this example.
|
89 | 77 |
|
90 | 78 | 1. Install the DataStax Cassandra Driver
|
91 |
| - |
92 | 79 | ```sh
|
93 | 80 | npm install cassandra-driver
|
94 | 81 | ```
|
95 |
| - |
96 | 82 | 4. Install serverless-google-cloudfunctions plugin
|
97 |
| - |
98 | 83 | ```sh
|
99 | 84 | npm install serverless-google-cloudfunctions
|
100 | 85 | ```
|
101 |
| - |
102 | 86 | 5. Configure `serverless.yml` with your project-id, credentials file, Contact Points ( public IP of GCP instance ), and Local Data Center ( likely `datacenter1` )
|
103 | 87 |
|
104 | 88 | From the project directory, deploy your function. This should output the endpoints that you can use to access the database.
|
105 |
| - |
106 | 89 | ```sh
|
107 | 90 | sls deploy
|
108 | 91 | ```
|
109 |
| - |
110 |
| -- When you are done, don't forget to clean things up with |
111 |
| - |
| 92 | +* When you are done, don't forget to clean things up with |
112 | 93 | ```
|
113 | 94 | sls remove
|
114 | 95 | ```
|
115 | 96 |
|
116 | 97 | #### createCatalog
|
117 |
| - |
118 | 98 | ```sh
|
119 | 99 | curl -X POST https://us-central1-<project-id>.cloudfunctions.net/createCatalog
|
120 |
| -``` |
121 |
| - |
| 100 | +```` |
122 | 101 | expected output:
|
123 |
| - |
124 | 102 | ```sh
|
125 | 103 | "Successfully created shopping.catalog schema"
|
126 | 104 | ```
|
127 |
| - |
128 | 105 | #### addItem
|
129 |
| - |
130 | 106 | Note the `-H "Content-Type:application/json"` is required here.
|
131 |
| - |
132 | 107 | ```sh
|
133 | 108 | curl -X POST -H "Content-Type:application/json" -d '{"item_id": 0, "name": "name_0", "description": "desc_0", "price": 10.1}' https://us-central1-<project-id>.cloudfunctions.net/addItem
|
134 | 109 | ```
|
135 |
| - |
136 | 110 | expected output:
|
137 |
| - |
138 | 111 | ```sh
|
139 | 112 | {"query":"INSERT INTO shopping.catalog (item_id, name, description, price) VALUES (?, ?, ?, ?)","item_id":0,"name":"name_0","description":"desc_0","price":10.1}
|
140 | 113 | ```
|
141 |
| - |
142 | 114 | #### getItem
|
143 |
| - |
144 | 115 | ```sh
|
145 | 116 | curl -X GET https://us-central1-<project-id>.cloudfunctions.net/getItem/0
|
146 | 117 | ```
|
147 |
| - |
148 | 118 | expected output:
|
149 |
| - |
150 | 119 | ```sh
|
151 | 120 | {"query":"SELECT name, description, price FROM shopping.catalog WHERE item_id = ?","item_id":["0"],"name":"name_0","description":"desc_0","price":"10.1"}
|
152 | 121 | ```
|
|
0 commit comments