Skip to content

Commit

Permalink
Merge pull request #71 from stargate/vkarpov15/typescript-reviews-astra
Browse files Browse the repository at this point in the history
support astra in typescript-express-reviews sample app
  • Loading branch information
vkarpov15 authored Jan 3, 2024
2 parents ff76450 + 74dcf66 commit dfefbbf
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 24 deletions.
45 changes: 44 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ jobs:
working-directory: discord-bot
- run: npm test
working-directory: discord-bot

test-ecommerce:
runs-on: ${{ matrix.os }}
strategy:
Expand Down Expand Up @@ -87,4 +88,46 @@ jobs:
- run: npm run lint
working-directory: netlify-functions-ecommerce
- run: npm test
working-directory: netlify-functions-ecommerce
working-directory: netlify-functions-ecommerce

test-reviews:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
node: [16]
os: [ubuntu-20.04]
name: Reviews Sample App
steps:
## Set up Stargate
- name: disable and stop mono-xsp4.service
run: |
sudo kill -9 $(sudo lsof -t -i:8084)
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b
with:
repository: stargate/stargate-mongoose
path: stargate-mongoose
ref: main
- name: Set up JSON API
run: |
chmod +x bin/start_json_api.sh
bin/start_json_api.sh
working-directory: stargate-mongoose
- name: Wait for services
run: |
while ! nc -z localhost 8181; do sleep 1; done
while ! nc -z localhost 8081; do sleep 1; done
working-directory: stargate-mongoose
## Set up and run reviews sample app tests
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.0.2
- name: Setup node
uses: actions/setup-node@eeb10cff27034e7acf239c5d29f62154018672fd # v3.1.1
with:
node-version: ${{ matrix.node }}

- run: npm install
working-directory: typescript-express-reviews
- run: npm run build
working-directory: typescript-express-reviews
- run: npm test
working-directory: typescript-express-reviews
4 changes: 0 additions & 4 deletions typescript-express-reviews/.env

This file was deleted.

23 changes: 23 additions & 0 deletions typescript-express-reviews/.env.example
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=
1 change: 1 addition & 0 deletions typescript-express-reviews/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
1 change: 1 addition & 0 deletions typescript-express-reviews/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Make sure you have Node.js 14 or higher and a local Stargate instance running as
## Running This Example

1. Run `npm install`
1. Create a `.env` file using the instructions in `.env.example`
1. Run `npm run seed`
1. Run `npm run build` to build the example
1. Run `npm start`
Expand Down
3 changes: 2 additions & 1 deletion typescript-express-reviews/src/api/Review/findByVehicle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ async function findByVehicle (request: Request, response: Response): Promise<voi
if (request.query?.limit != null) {
limit = parseInt(request.query.limit.toString(), 10) || 5;
}
let skip = 5;
let skip = 0;
if (request.query?.skip != null) {
skip = parseInt(request.query.skip.toString(), 10) || 0;
}

const reviews = await Review.
find({ vehicleId: request.query?.vehicleId }).
sort({ createdAt: -1 }).
Expand Down
40 changes: 22 additions & 18 deletions typescript-express-reviews/src/models/connect.ts
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
);
}
}


0 comments on commit dfefbbf

Please sign in to comment.