Earlier in 2023 we announced that we're working on Mongoose support for Apache Cassandra via DataStax's new JSON API. The big caveat with that announcement was that stargate-mongoose only supported running the Stargate JSON API locally. We've been hard at work on adding support for Astra, DataStax's cloud DBaaS for Cassandra, to stargate-mongoose. This is a huge step forward for making stargate-mongoose production ready, because now you don't have to run Stargate yourself in order to use Mongoose with Cassandra.

Astra support is coming along nicely, and we're now ready to accept beta testers. If you're interested in trying out Mongoose and JSON API on Astra, please send an email to json-preview@datastax.com. Please keep in mind that initially this will be available in only one region, and there is no SLA associated with your data. As we move out of Early Access and into General Availability, all Astra regions will be available, all Astra general SLAs will be applicable, and any specifics relative the stargate-mongoose and the JSON API will be communicated.

Connecting Mongoose to Astra

Keep in mind that, right now, JSON API support in Astra is in a private preview. In order to get access, you need to create an Astra account and email json-preview@datastax.com. That being said, once you have access, log in to Astra and create a new database.

image

Once you have created a new database, create a new application token and gather the required credentials:

  1. ASTRA_DB_ID
  2. ASTRA_DB_REGION
  3. ASTRA_DB_KEYSPACE
  4. ASTRA_DB_APPLICATION_TOKEN

The "Configure your environment" section has all the required fields except the application token.

image

Combine these credentials into a connection string as follows:

https://${ASTRA_DB_ID}-${ASTRA_DB_REGION}.apps.astra.datastax.com/api/json/v1/${ASTRA_DB_KEYSPACE}?applicationToken=${ASTRA_DB_APPLICATION_TOKEN}

Or, you can use the below tool to generate the connection string.

Once you've created the connection string, you can connect to Astra with Mongoose by passing the connection string to mongoose.connect() as follows. The isAstra and createNamespaceOnConnect options are required.

const mongoose = require('mongoose');
const { driver } = require('stargate-mongoose');

mongoose.setDriver(driver);

mongoose.connect(astraConnectionString, {
  isAstra: true,
  createNamespaceOnConnect: false
});

Or you can try out one of our sample apps. Just follow the instructions for connecting to Astra in the sample app's README, like the instructions in the Netlify ecommerce sample app.

Introducing create-stargate-mongoose-app

In order to help you get started with Mongoose and Astra, we've built create-stargate-mongoose-app. This tool lets you create a new stargate-mongoose project based on one of our sample apps:

  1. netlify-functions-ecommerce: an ecommerce store that runs on Netlify and uses Stripe for payments
  2. discord-bot: a basic discord bot that makes queries and inserts documents based on discord commands
  3. typescript-express-reviews: a TypeScript and Express app that stores reviews for vehicles

For example, to create a my-discord-bot directory based on the discord-bot project, you can run the following command:

npx create-stargate-mongoose-app my-discord-bot --sample discord-bot

create-stargate-mongoose-app will copy the latest version of the discord bot sample app into the my-discord-bot directory and run npm install for you, so you'll have an Astra-backed discord bot ready to go.

Moving On

Adding Astra support is a major step forward for stargate-mongoose, because now you can get a Cassandra-backed Mongoose app running with just a few clicks in a UI. This means that users no longer have to run Stargate themselves to use Mongoose with Cassandra. And as a Mongoose developer, this means you'll be able to leverage Cassandra's scalability and durability, particularly in write-heavy applications. Want to try it out? Astra support is currently in private preview, but you can request access by emailing json-preview@datastax.com.

Found a typo or error? Open up a pull request! This post is available as markdown on Github
comments powered by Disqus