Connecting Astra DB to Strapi using a hook

Strapi is an open-source headless CMS that gives developers the freedom to choose their favorite tools and frameworks and allows editors to manage and distribute their content using their application’s admin panel. Based on a plugin system, its admin panel and API are extensible. Every part is customizable to match any use case. Strapi also has a built-in user system to manage what the administrators and end users can access.

Procedure

  1. Install Strapi locally:

    npx create-strapi-app my-project

    You can view your Strapi project as it is hosted locally at http://localhost:1337/admin.

  2. Install the Strapi hook:

    npm i strapi-hook-astra
  3. Activate the hook by adding the following to `./config/hook.js of the sample Strapi Project:

    module.exports = {
        settings: {
            astra: {
                enabled: true,
                token: 'REPLACE_ME',
                databaseId: 'REPLACE_ME',
                databaseRegion: 'REPLACE_ME',
                keyspace: 'REPLACE_ME',
                collection: 'REPLACE_ME'
            },
        }
    };
    • token: Generate a token from Astra DB.

    • databaseId: Enter your Astra DB database ID from your database URL.

    • databaseRegion: Enter your Astra DB database region

    • keyspace: Enter your Astra DB keyspace name.

    • collection: Enter your Astra DB collection name.

  4. Create a document:

    strapi.services.astra.create(document);
    Parameter Type Explanation Values

    document

    json

    Create a document

    var dataString = '{ "name": "John", "last_name": "Doe" }'

  5. Get document by ID:

    strapi.services.astra.getById(documentId);
    Parameter Type Explanation Values

    documentId

    string

    Get document by documentId

    var documentId = "your_document_id"

  6. Get document by path:

    strapi.services.astra.getByPath();
  7. Search a collection:

    strapi.services.astra.searchCollection(query,pagesize);
    Parameter Type Explanation Values

    query

    string

    Search collection via query

    var query = {"name": { "$eq": "John" }}

    pagesize

    int

    Number of documents to fetch

    int page_size = 3

For more, see the Strapi documentation.