rsnext/examples/with-couchbase/util/couchbase.js
Elliot Scribner f78ebd0895
Fix: changing import syntax slightly to ensure success with create-next-app (#28431)
Just a small change to recently merged PR; for some reason `import couchbase from 'couchbase'` is not working correctly when I use create-next-app to bootstrap with the `with-couchbase` example, despite initially working within my local next.js directory. The wildcard import fixes that issue. Thanks!

## Documentation / Examples

- [x] Make sure the linting passes
2021-08-24 16:48:28 +00:00

65 lines
1.5 KiB
JavaScript

import * as couchbase from 'couchbase'
const COUCHBASE_USER = process.env.COUCHBASE_USER
const COUCHBASE_PASSWORD = process.env.COUCHBASE_PASSWORD
const COUCHBASE_ENDPOINT = process.env.COUCHBASE_ENDPOINT || 'localhost'
const COUCHBASE_BUCKET = process.env.COUCHBASE_BUCKET || 'travel-sample'
let IS_CLOUD_INSTANCE = process.env.IS_CLOUD_INSTANCE || 'false'
if (!COUCHBASE_USER) {
throw new Error(
'Please define the COUCHBASE_USER environment variable inside .env.local'
)
}
if (!COUCHBASE_PASSWORD) {
throw new Error(
'Please define the COUCHBASE_PASSWORD environment variable inside .env.local'
)
}
/**
* Global is used here to maintain a cached connection across hot reloads
* in development. This prevents connections growing exponentially
* during API Route usage.
*/
let cached = global.couchbase
if (!cached) {
cached = global.couchbase = { conn: null }
}
async function createCouchbaseCluster() {
if (cached.conn) {
return cached.conn
}
cached.conn = await couchbase.connect(
'couchbase://' +
COUCHBASE_ENDPOINT +
(IS_CLOUD_INSTANCE === 'true'
? '?ssl=no_verify&console_log_level=5'
: ''),
{
username: COUCHBASE_USER,
password: COUCHBASE_PASSWORD,
}
)
return cached.conn
}
export async function connectToDatabase() {
const cluster = await createCouchbaseCluster()
const bucket = cluster.bucket(COUCHBASE_BUCKET)
const collection = bucket.defaultCollection()
let dbConnection = {
cluster,
bucket,
collection,
}
return dbConnection
}