Data in the Astra DBaaS can be accessed using four different APIs (so far).
- GraphQL API
- REST API
- Cassandra Query Language (CQL) API
- Mapper API
This reference project shows how you can use these APIs from Node.js.
All Astra APIs tutorial:
Contributors: Kiyu Gabriel
The most files are in the root of the project:
- table_multi_api.js: examples of CRUD operations using each API
- schema.cql: example Cassandra schema that works with the tests
- config.js: your settings
- utility.js: functions to deal with some of the differences in the way the APIs like their data
- KeyspaceMapper.js: class that wraps the regular Node.js driver Mapper functionality to create one model per table in the keyspace
- TableMapper.js: support file for KeyspaceMapper.js
There are also test files to illustrate how the examples could be used:
- test/multi_table_api.js: test the functionality of the table_multi_api.js
- test/KeyspaceMapper.js: test the functionality of the KeyspaceMapper
- test/sample_data.js: sample data for table_multi_api.js
Setup and Running
- This requires a recent version of Node.js; I was using v14.7
- You will need to install Mocha
npm install mocha
- You'll also need an Astra database (can be set up for free)
If you'd like to see the tests in action, you'll need a test database.
- Create a free-forever Cassandra Database with DataStax Astra: click here to get started 🚀
- Set up a database, keyspace, username and password (in the samples, I named the database and keyspace "stuff", username "stuff_user" and password "stuff_password")
- Put all of this information into your
- Download the secure connect bundle from the summary page after your database has launched. You'll need this later. Save this file update the
config.jsfile with the path to it.
- Also grab the REST API URL from the Summary page - put base of this URL in the config.js. The base of the REST API URL and the GraphQL API URL are the same, just trim off the "/api/rest" part.
- Use the CQL Console to create the tables defined in the
- In the root of this project, run
npm testto exercise all the APIs.