cds-mysql is a database adapter for the SAP Cloud Application Programming Model (CAP) framework, enabling applications to connect to and interact with MySQL and MariaDB databases. It is heavily inspired by the cds-pg module. The current stable version is 7.9.0, supporting Node.js 18 and npm 9 or higher. This module allows CAP applications to leverage MySQL/MariaDB for data persistence, offering features such as fundamental CRUD operations, deep insert for associations/compositions, Fiori draft support, temporal aspects (without time-travel queries), incremental IDs, CSV-based initial data provisioning, full-text search, schema migration optimization, media attachment support, localized data, and multi-tenancy including experimental `@sap/cds-mtxs` integration. It differentiates itself by providing comprehensive CAP feature support for MySQL, aiming for seamless integration with the CAP runtime.
npm install cds-mysqlVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to install cds-mysql, configure it in `package.json` or `.cdsrc.json`, and set up database credentials using environment variables for a local CAP project.
Add a `deploy` script to your `package.json` (e.g., `"deploy": "cds deploy --to mysql"`) and run `npm run deploy` before `npx cds-serve`. The `cds deploy` command generates and applies DDL.
Carefully review the official documentation for `@sap/cds-mtxs` and `cds-mysql` before using experimental multi-tenancy features in production. Pin `cds-mysql` to a specific version if stability is critical.
If time-travel query capabilities are required, consider alternative database solutions or implement custom handlers to manually construct SQL queries for historical data retrieval.
Ensure your CAP project's `db/data` folder contains CSV files for localized data (e.g., `MyEntity_texts.csv`) and that `cds deploy` is executed to generate and populate the corresponding `localized_*` tables.
Ensure `package.json` or `.cdsrc.json` correctly specifies `"kind": "mysql"` for the `db` service. Run `npx cds deploy --to mysql` to create the database schema based on your CDS model.
Verify the `CDS_REQUIRES_DB_CREDENTIALS_HOST` in your `.env` file. Grant the MySQL user permissions to connect from `%` (any host) or the specific IP address of your application server (e.g., `GRANT ALL PRIVILEGES ON your_db.* TO 'your_user'@'%' IDENTIFIED BY 'your_password';`). Ensure `bind-address` in MySQL config allows external connections if not `127.0.0.1`.
Create the database manually on your MySQL server (e.g., `CREATE DATABASE your_database_name;`) or adjust the `CDS_REQUIRES_DB_CREDENTIALS_DATABASE` variable to an existing database.
Run `npm install mysql2` in your project directory to install the necessary database driver.