|
1 | 1 | # Quickstart for using Couchbase with Next.js |
2 | 2 |
|
3 | | - |
4 | 3 | ## Deployment Instructions |
5 | | -1. Create a new Capella database (or use existing one) at [cloud.couchbase.com](https://cloud.couchbase.com/). |
6 | | -2. Create a bucket called `user_profile`, and a collection called `profile` (within the `_default` scope). |
7 | | - - **Note:** the `build` step will attempt to create the collection within your `CB_BUCKET` on deployment. |
8 | | -3. Click Deploy: |
| 4 | + |
| 5 | +The deployment instructions are specifically for [Vercel](https://vercel.com/). If you are deploying on other platforms, you need to adapt the instructions for running the project [locally](#run-the-code-locally). |
| 6 | + |
| 7 | +### Prerequisites |
| 8 | + |
| 9 | +To deploy this project on Vercel, you will need: |
| 10 | + |
| 11 | +- [Vercel Account](https://vercel.com/signup) |
| 12 | +- Access to a [Couchbase Capella](https://cloud.couchbase.com/) database running the [Query Service](https://docs.couchbase.com/server/current/learn/services-and-indexes/services/query-service.html). Alternatively, you have the option to sign up for a free Capella database in the integration flow. |
| 13 | +- If you are connecting to an existing Capella account, create a bucket called `user_profile`, and a collection called `profile` (within the `_default` scope). |
| 14 | + - **Note:** the `build` step will attempt to create the collection and load the sample data within your `CB_BUCKET` on deployment. |
| 15 | + |
| 16 | +### Steps |
| 17 | + |
| 18 | +1. Click Deploy: |
9 | 19 |
|
10 | 20 | <a href="https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fcouchbase-examples%2Fnextjs-quickstart&project-name=couchbase-nextjs-quickstart&repository-name=couchbase-nextjs-quickstart&developer-id=oac_5eS7l7O4wvTE47rCKEYSFLQT&integration-ids=oac_5eS7l7O4wvTE47rCKEYSFLQT"><img src="https://vercel.com/button" alt="Deploy with Vercel" width="140px"/></a> |
11 | 21 |
|
12 | | -4. Follow the steps when prompted. The integration step will open the Couchbase Capella UI and allow you to select resources to connect. |
13 | | - - If you don't have a Capella account, you can sign up at this point, but you'll have to re-deploy the application a second time once the database has finished provisioning. Read more in #2 of the [Pitfalls and FAQ section](#common-pitfalls-and-faqs) below. |
14 | | -5. Explore the newly deployed application: create, update, and delete some user profiles. A few sample profiles will be added automatically, and you can load the sample data manually by following [these instructions](#sample-data). |
| 22 | +2. Follow the steps when prompted. The integration step will open the Couchbase Capella UI and allow you to select resources to connect. |
| 23 | + - If you sign-up for a new Capella account, you may have to re-deploy the application once the database has finished provisioning. Read more in #2 of the [Pitfalls and FAQ section](#common-pitfalls-and-faqs) below. |
| 24 | +3. Explore the newly deployed application: create, update, and delete some user profiles. A few sample profiles will be added automatically, and you can load the sample data manually by following [these instructions](#sample-data). |
15 | 25 |
|
16 | 26 | ### Common Pitfalls and FAQs |
| 27 | + |
17 | 28 | 1. **Deployment Failed** |
18 | | - - This usually occurs when the environment variables are missing or not yet set (such as when waiting for a new database to be provisioned). Inspect the build logs for more information, and ensure the environment variables have been set. |
19 | | - - When **deploying a new trial database**, you'll need to manually create the correct bucket/collection, update the variables in your Vercel console, and re-deploy to pick up the new values. |
20 | | - - **Note:** A re-deploy is _required_ after any changes to the environment variables, as this is the only way to apply the changes. **This can be done from the "deployments" tab on the project's dashboard (click the three dots on the row for your failed deployment and click "redeploy").** |
| 29 | + |
| 30 | +- This usually occurs when the environment variables are missing or not yet set (such as when waiting for a new database to be provisioned). Inspect the build logs for more information, and ensure the environment variables have been set. |
| 31 | +- When **deploying a new trial database**, you'll need to wait until the environment variables are set in your Vercel console, and re-deploy to pick up the new values. |
| 32 | +- **Note:** A re-deploy is _required_ after any changes to the environment variables, as this is the only way to apply the changes. **This can be done from the "deployments" tab on the project's dashboard (click the three dots on the row for your failed deployment and click "redeploy").** |
| 33 | + |
21 | 34 | 2. **Integrated with a new trial database** |
22 | | - - Although the integration supports the creation of new trial databases, this project requires a different bucket/scope/collection than the default trial database. If you've deployed this template with a trial, please be sure to add a bucket named `user_profile`, a `_default` scope, and a collection named `profile`, and update the `CB_BUCKET` to `user_profile`. |
23 | | - - Be sure to re-deploy after updating the environment variables. |
24 | | - - This is related to the previous, and issues will manifest as deployment failures. |
| 35 | + |
| 36 | +- If you've deployed this template with a trial database, note that the `profile` collection will be created as a part of the `travel-sample` bucket during the initial build. |
| 37 | +- Be sure to re-deploy after updating the environment variables if the initial build fails. |
| 38 | +- This is related to the previous, and issues will manifest as deployment failures. |
| 39 | + |
25 | 40 | 3. **Infinite Loading State OR `Query failed: parsing error` OR `Query failed: bucket not found`** |
26 | | - - No data received from the database. Be sure you are using a bucket named `user_profile`, a `_default` scope, and a collection named `profile`. |
27 | | - - **Note:** while you can _technically_ change the bucket name (via the environment variables), the scope and collections are hardcoded in `/api/user.js`. If you'd like to modify these, you'll need to adjust the queries used in the API layer. |
| 41 | + |
| 42 | +- No data received from the database. Be sure you are using the right bucket in your `CB_BUCKET` environment variable, a `_default`scope, and a collection named`profile`. |
| 43 | + |
28 | 44 | 4. **504 Gateway Timeout after deploying** |
29 | | - - This error usually occurs when the application cannot reach the database. Ensure that the environment variables are correct, IP addresses are allowed, and the Database User credentials match those in the environment variables. |
30 | 45 |
|
| 46 | +- This error usually occurs when the application cannot reach the database. Ensure that the environment variables are correct, IP addresses are allowed, and the Database User credentials match those in the environment variables. |
31 | 47 |
|
32 | 48 | ## Run the Code Locally |
| 49 | + |
33 | 50 | ### Prerequisites |
| 51 | + |
34 | 52 | To run this project locally, you will need: |
| 53 | + |
35 | 54 | - A Couchbase Capella database or Couchbase 7+ database running locally |
36 | 55 | - Node.js & NPM |
37 | 56 | - Next.js |
38 | 57 | - Code Editor |
39 | 58 |
|
40 | 59 | ### Get Started |
| 60 | + |
41 | 61 | Clone the source code: |
| 62 | + |
42 | 63 | ```sh |
43 | 64 | git clone https://github.com/couchbase-examples/nextjs-quickstart.git |
44 | 65 | ``` |
45 | 66 |
|
46 | 67 | Install required dependencies: |
| 68 | + |
47 | 69 | ```sh |
48 | 70 | npm install |
49 | 71 | ``` |
50 | 72 |
|
51 | 73 | **If you are using Capella**, you'll have to manually create a bucket named `user_profile` and a collection named `profile`. See the documentation on [managing buckets](https://docs.couchbase.com/cloud/clusters/data-service/manage-buckets.html) and [creating a collection](https://docs.couchbase.com/cloud/clusters/data-service/scopes-collections.html#create-a-collection) for more information. Note that this collection should be created on the `_default` scope. |
52 | 74 |
|
53 | 75 | ### Update environment variables appropriately |
54 | | -We've included a `.env.local.example` file with blank values for you to copy into a file called `.env.local` and fill in the values. We've also included a `.env.default` file for testing and running in GitPod. In most cases, you can ignore the default config file. |
| 76 | + |
| 77 | +We've included a `.env.local.example` file with blank values for you to copy into a file called `.env.local` and fill in the values. We've also included a `.env.default` file for testing and running in GitPod. In most cases, you can ignore the default config file. |
| 78 | + |
55 | 79 | - `CB_USERNAME` - The username of an authorized user on your database. Follow [these instructions](https://docs.couchbase.com/cloud/clusters/manage-database-users.html#create-database-credentials) to create database credentials on Capella. |
56 | 80 | - `CB_PASSWORD` - The password that corresponds to the user specified above. |
57 | 81 | - `CB_CONNECT_STRING` - The Couchbase connection string. Use the connection string specified on the 'Connect' tab within Capella (formatted like `couchbases://cb.<xxxxxx>.cloud.couchbase.com`) or `couchbase://localhost` for a local/Docker database. |
58 | 82 | - `CB_BUCKET` - The bucket you'd like to connect to. Set this to `user_profiles` for this template. |
59 | 83 |
|
60 | 84 | ### Set up and Run The Application |
| 85 | + |
61 | 86 | If you have Couchbase running locally, we can create the bucket and collection by running the following command: |
| 87 | + |
62 | 88 | ```sh |
63 | 89 | npm run init-db:local |
64 | 90 | ``` |
65 | 91 |
|
66 | 92 | If you'd like to add the sample data, run: |
| 93 | + |
67 | 94 | ```sh |
68 | 95 | npm run load-sample-data |
69 | 96 | ``` |
| 97 | + |
70 | 98 | **Note:** this will also attempt to create a `profile` collection. |
71 | 99 |
|
72 | 100 | **Extra Step for Capella Databases**: if you've manually set up your bucket and collection, you'll need to create the necessary indices as well. To accomplish this, run: |
| 101 | + |
73 | 102 | ```sh |
74 | 103 | npm run build-indexes |
75 | 104 | ``` |
| 105 | + |
76 | 106 | This is because the index creation code is contained within the database initialization script, which we don't use for Capella databases. |
77 | 107 |
|
78 | 108 | Now we're ready to run our application: |
| 109 | + |
79 | 110 | ```sh |
80 | 111 | npm run dev |
81 | 112 | ``` |
82 | 113 |
|
83 | 114 | If everything is configured properly, you should be able to navigate to localhost:3000 to see the example application. For troubleshooting and additional setup instructions please refer to the `NextJS_README.md` included in the with-couchbase starter. |
84 | 115 |
|
85 | | - |
86 | 116 | ## Sample Data |
| 117 | + |
87 | 118 | - We've included a `MOCK_DATA.json` file containing 15 documents with various mocked user data. The `build` step will automatically load the sample data, but you may want to also load it manually for local testing. Use `npm run load-sample-data` to insert the documents to your database. Be sure your local environment variables are set correctly! |
88 | | -- This file can also be [imported into Capella manually](https://docs.couchbase.com/cloud/clusters/data-service/import-data-documents.html). |
| 119 | +- This file can also be [imported into Capella manually](https://docs.couchbase.com/cloud/clusters/data-service/import-data-documents.html). |
89 | 120 | - You can also add your own profile data to the database manually by clicking the **+** icon in the UI. |
90 | 121 |
|
91 | | - |
92 | 122 | ## Notes About the Quickstart Code |
| 123 | + |
93 | 124 | - We've included a `.env.default` file which is used for testing and gitpod instances of the project to ensure smooth setup in these environments. |
94 | 125 | - In the completed quickstart code, fetch URLs use a dynamic `origin` variable instead of hard coding `http://localhost:3000` to ensure requests work when running in other environments. |
95 | 126 | - _**NOTE FOR CAPELLA DATABASES:**_ The database initialization code currently only works with local databases. If you are using Capella, you'll need to manually create a bucket called `user_profile` and then within that buckets default scope, a collection called `profile`. [See here for more info on managing buckets in Capella.](https://docs.couchbase.com/cloud/clusters/data-service/manage-buckets.html). After bucket and collection creation, you can use the index creation command: `npm run build-indexes`. Running `npm run init-db:local` will also work to create the required indices. The bucket and collection creation steps will fail with `ECONNREFUSED` but it will still be able to create the index on your Capella database. |
96 | 127 |
|
97 | | - |
98 | 128 | ## Running The Tests |
| 129 | + |
99 | 130 | A suite of integration tests has been included, and can be run by first setting up the database: |
| 131 | + |
100 | 132 | ``` |
101 | 133 | npm run init-db:default |
102 | 134 | ``` |
103 | | -and then using the `npm test` command. |
104 | 135 |
|
| 136 | +and then using the `npm test` command. |
105 | 137 |
|
106 | 138 | ## Try it in Your Browser |
| 139 | + |
107 | 140 | #### Run with GitPod |
108 | | -[](https://gitpod.io/#https://github.com/couchbase-examples/nextjs-quickstart) |
109 | 141 |
|
| 142 | +[](https://gitpod.io/#https://github.com/couchbase-examples/nextjs-quickstart) |
0 commit comments