CAP MySQL Database Adapter
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.
Common errors
-
Error: SQLITE_ERROR: no such table: MyService_MyEntity
cause The database schema has not been deployed, or the `cds` configuration is pointing to an incorrect database or kind.fixEnsure `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. -
Failed to connect to MySQL: Host 'X.X.X.X' is not allowed to connect to this MySQL server
cause The MySQL user configured in `.env` (or other credentials) does not have privileges to connect from the specified host, or the MySQL server is not configured to accept remote connections.fixVerify 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`. -
ER_BAD_DB_ERROR: Unknown database 'your_database_name'
cause The database specified in `CDS_REQUIRES_DB_CREDENTIALS_DATABASE` environment variable does not exist on the MySQL server.fixCreate 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. -
Error: Cannot find module 'mysql2'
cause The `mysql2` package, which is a required peer dependency for `cds-mysql`, has not been installed.fixRun `npm install mysql2` in your project directory to install the necessary database driver.
Warnings
- gotcha The `cds-mysql` adapter itself does not automatically perform database deployment (DDL generation and execution) when running `npx cds-serve`. Database artifacts (tables, views) must be deployed manually or via a separate script before starting the server. This is a common pitfall for new users expecting automatic schema sync like with SQLite.
- breaking The documentation mentions experimental `@sap/cds-mtxs` support, noting that its behavior may change later. Relying on experimental features for multi-tenancy could lead to breaking changes in future versions without major version bumps.
- gotcha When dealing with `temporal` aspects in CAP, `cds-mysql` supports the aspect itself but does not currently support 'time-travel queries'. Queries against historical data based on timestamps may not work as expected or require manual SQL construction.
- gotcha Similar to other database adapters, `cds-mysql` relies on the proper configuration of `localized data`. If `localized_*` tables are missing or not properly populated, queries involving localized entities may fail with 'no such table' errors.
Install
-
npm install cds-mysql -
yarn add cds-mysql -
pnpm add cds-mysql
Imports
- mysql2
const mysql = require('mysql2/promise');import mysql from 'mysql2/promise';
- Service
import { Service } from '@sap/cds'; - connect
import cds from '@sap/cds'; const db = await cds.connect.to('db');
Quickstart
npm i cds-mysql mysql2
// package.json (or .cdsrc.json)
// Add this to your CAP project's package.json to configure cds-mysql
// This tells CAP to use 'mysql' as the database kind.
{
"requires": {
"db": {
"kind": "mysql"
}
}
}
// .env file in your local CDS project root
// Fill with your database credentials
CDS_REQUIRES_DB_CREDENTIALS_HOST=127.0.0.1
CDS_REQUIRES_DB_CREDENTIALS_PORT=3306
CDS_REQUIRES_DB_CREDENTIALS_DATABASE=your_database_name
CDS_REQUIRES_DB_CREDENTIALS_USER=your_db_user
CDS_REQUIRES_DB_CREDENTIALS_PASSWORD=your_db_password
// To start the CAP server with MySQL:
npx cds-serve