Skip to content

Commit cf14380

Browse files
authored
Merge pull request #153 from couchbase-examples/auto-collection-creation
Adding Logic to Create the Collection with the Sample Data Loading
2 parents 123f079 + 0d1287a commit cf14380

File tree

3 files changed

+56
-14
lines changed

3 files changed

+56
-14
lines changed

README.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,28 @@
44
## Deployment Instructions
55
1. Create a new Capella database (or use existing one) at [cloud.couchbase.com](https://cloud.couchbase.com/).
66
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.
78
3. Click Deploy:
89

910
&nbsp; &nbsp; &nbsp; &nbsp; <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>
1011

11-
4. Follow the steps when prompted.
12-
5. Explore the newly deployed application and add some profiles to populate the database. You can also load the sample data by following [these instructions](#sample-data).
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).
1315

1416
### Common Pitfalls and FAQs
15-
- **Deployment Failed**
17+
1. **Deployment Failed**
1618
- 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.
1719
- 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.
1820
- **Note:** A re-deploy is _required_ after any changes to the environment variables, as this is the only way to apply the changes.
19-
- **Integrated with a new trial database**
21+
2. **Integrated with a new trial database**
2022
- 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`.
2123
- Be sure to re-deploy after updating the environment variables.
2224
- This is related to the previous, and issues will manifest as deployment failures.
23-
- **Infinite Loading State OR `Query failed: parsing error` OR `Query failed: bucket not found`**
25+
3. **Infinite Loading State OR `Query failed: parsing error` OR `Query failed: bucket not found`**
2426
- No data received from the database. Be sure you are using a bucket named `user_profile`, a `_default` scope, and a collection named `profile`.
2527
- **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.
26-
- **504 Gateway Timeout after deploying**
28+
4. **504 Gateway Timeout after deploying**
2729
- 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.
2830

2931

@@ -61,6 +63,12 @@ If you have Couchbase running locally, we can create the bucket and collection b
6163
npm run init-db:local
6264
```
6365

66+
If you'd like to add the sample data, run:
67+
```sh
68+
npm run load-sample-data
69+
```
70+
**Note:** this will also attempt to create a `profile` collection.
71+
6472
**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:
6573
```sh
6674
npm run build-indexes
@@ -76,9 +84,9 @@ If everything is configured properly, you should be able to navigate to localhos
7684

7785

7886
## Sample Data
79-
- We've included a `MOCK_DATA.json` file containing 15 documents with various mocked user data. Use `npm run load-sample-data` to insert the documents to your database. Be sure your local environment variables are set correctly!
87+
- 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!
8088
- This file can also be [imported into Capella manually](https://docs.couchbase.com/cloud/clusters/data-service/import-data-documents.html).
81-
- You can also add your own profile data to the database manually by clicking the + icon in the UI.
89+
- You can also add your own profile data to the database manually by clicking the **+** icon in the UI.
8290

8391

8492
## Notes About the Quickstart Code

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"dev": "next dev",
77
"dev:default": "env-cmd -f .env.default next dev",
8-
"build": "npm run strip-cb-sdk && next build && npm run build-indexes:no-env",
8+
"build": "npm run strip-cb-sdk && next build && npm run load-sample-data:no-env && npm run build-indexes:no-env",
99
"start": "next start",
1010
"lint": "next lint",
1111
"format:check": "prettier --check ./**/*.js",
@@ -16,6 +16,7 @@
1616
"build-indexes": "env-cmd -f .env.local node ./util/buildIndexes.js",
1717
"build-indexes:no-env": "node ./util/buildIndexes.js",
1818
"load-sample-data": "env-cmd -f .env.local node ./util/loadSampleData.js",
19+
"load-sample-data:no-env": "node ./util/loadSampleData.js",
1920
"strip-cb-sdk": "strip --strip-debug ./node_modules/couchbase/build/Release/couchbase_impl.node",
2021
"test": "env-cmd -f .env.default jest"
2122
},

util/loadSampleData.js

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
import { connectToDatabase } from './couchbase.js';
22
import { readFile } from 'fs/promises';
3+
import * as couchbase from 'couchbase';
4+
5+
export const createProfileCollection = async () => {
6+
let { bucket } = await connectToDatabase();
7+
8+
const collectionMgr = bucket.collections();
9+
10+
let newCollectionSpec = new couchbase.CollectionSpec({
11+
name: 'profile',
12+
scopeName: '_default',
13+
});
14+
15+
try {
16+
await collectionMgr.createCollection(newCollectionSpec);
17+
} catch (e) {
18+
throw new Error(e);
19+
}
20+
};
321

422
const loadSampleData = async () => {
523
let { profileCollection } = await connectToDatabase();
@@ -20,11 +38,26 @@ const loadSampleData = async () => {
2038
}
2139
};
2240

23-
loadSampleData()
41+
createProfileCollection()
2442
.then(() => {
25-
console.log('\nSample Data Loaded Successfully');
43+
console.log('Profile Collection Created. Loading Sample Data.');
44+
loadSampleData()
45+
.then(() => {
46+
console.log('\nSample Data Loaded Successfully');
47+
})
48+
.catch((e) => {
49+
console.log(e);
50+
console.error('\nFailed to Load Sample Data: ' + e.message);
51+
});
2652
})
27-
.catch((e) => {
28-
console.error(e);
29-
console.error('\nFailed to Load Sample Data');
53+
.catch(() => {
54+
console.log('Profile Collection Already Exists. Loading Sample Data.');
55+
loadSampleData()
56+
.then(() => {
57+
console.log('\nSample Data Loaded Successfully');
58+
})
59+
.catch((e) => {
60+
console.log(e);
61+
console.error('\nFailed to Load Sample Data: ' + e.message);
62+
});
3063
});

0 commit comments

Comments
 (0)