forked from mongoosejs/sample-apps
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from stargate/vkarpov15/typescript-reviews-astra
support astra in typescript-express-reviews sample app
- Loading branch information
Showing
7 changed files
with
93 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Flag to specify whether the remote endpoint is Astra or not. | ||
# true - if connecting to an AstraDB | ||
# false - if not connecting to an AstraDB and connecting to a local/remote jsonapi instead | ||
IS_ASTRA= | ||
|
||
#Fill the JSON API related details only when IS_ASTRA is set to 'false' | ||
#Local JSON API URL for example: http://127.0.0.1:8181/v1/ecommerce_test where 'ecommerce_test' is the keyspace name | ||
JSON_API_URL=http://127.0.0.1:8181/v1/ecommerce_test | ||
#Auth URL for example: http://127.0.0.1:8081/v1/auth | ||
JSON_API_AUTH_URL=http://127.0.0.1:8081/v1/auth | ||
#Auth username and password | ||
JSON_API_AUTH_USERNAME=cassandra | ||
JSON_API_AUTH_PASSWORD=cassandra | ||
|
||
#Fill the ASTRA DB related details only when IS_ASTRA is set to 'true' | ||
#Astra DB Id | ||
ASTRA_DBID= | ||
#Astra DB Region | ||
ASTRA_REGION= | ||
#Astra DB Keyspace | ||
ASTRA_KEYSPACE= | ||
#Astra DB Application Token | ||
ASTRA_APPLICATION_TOKEN= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,33 @@ | ||
import assert from 'assert'; | ||
import { createAstraUri } from 'stargate-mongoose'; | ||
import mongoose from './mongoose'; | ||
|
||
const isAstra = process.env.IS_ASTRA ?? ''; | ||
|
||
const stargateJSONAPIURL = process.env.STARGATE_JSON_API_URL ?? ''; | ||
const username = process.env.STARGATE_JSON_USERNAME ?? ''; | ||
const password = process.env.STARGATE_JSON_PASSWORD ?? ''; | ||
const authUrl = process.env.STARGATE_JSON_AUTH_URL ?? ''; | ||
if (!stargateJSONAPIURL) { | ||
throw new Error('Must set STARGATE_JSON_API_URL environment variable'); | ||
} | ||
if (!username) { | ||
throw new Error('Must set STARGATE_JSON_USERNAME environment variable'); | ||
} | ||
if (!password) { | ||
throw new Error('Must set STARGATE_JSON_PASSWORD environment variable'); | ||
} | ||
if (!authUrl) { | ||
throw new Error('Must set STARGATE_JSON_AUTH_URL environment variable'); | ||
} | ||
|
||
const astraDbId = process.env.ASTRA_DBID ?? ''; | ||
const astraRegion = process.env.ASTRA_REGION ?? ''; | ||
const astraKeyspace = process.env.ASTRA_KEYSPACE ?? ''; | ||
const astraApplicationToken = process.env.ASTRA_APPLICATION_TOKEN ?? ''; | ||
|
||
export default async function connect() { | ||
console.log('Connecting to', process.env.STARGATE_JSON_API_URL); | ||
await mongoose.connect( | ||
stargateJSONAPIURL, | ||
{ username, password, authUrl } as mongoose.ConnectOptions | ||
); | ||
if (isAstra) { | ||
const uri = createAstraUri(astraDbId, astraRegion, astraKeyspace, astraApplicationToken); | ||
console.log('Connecting to', uri); | ||
await mongoose.connect( | ||
uri, | ||
{ isAstra: true } as mongoose.ConnectOptions | ||
); | ||
} else { | ||
console.log('Connecting to', stargateJSONAPIURL); | ||
await mongoose.connect( | ||
stargateJSONAPIURL, | ||
{ username, password, authUrl } as mongoose.ConnectOptions | ||
); | ||
} | ||
} | ||
|
||
|