Shared MySQL functionality for the Drizzle Adapter ecosystem.
The @drizzle-adapter/mysql-core
package provides shared MySQL functionality used by MySQL-compatible adapters in the Drizzle Adapter ecosystem. This package is an internal dependency used by:
This package standardizes MySQL-specific functionality across different MySQL-compatible adapters:
- Common MySQL data type definitions
- Shared MySQL query building logic
- Unified MySQL error handling
- Common MySQL utility functions
# This package is typically not installed directly but as a dependency of MySQL adapters
pnpm install @drizzle-adapter/mysql-core
If you're developing a new MySQL-compatible adapter for the Drizzle Adapter ecosystem, you should use this package to ensure consistency with other MySQL adapters:
import { MySQLDrizzleDataTypes } from '@drizzle-adapter/mysql-core';
export class YourMySQLAdapter implements DrizzleAdapterInterface {
public getDataTypes(): MySQLDrizzleDataTypes {
return new MySQLDrizzleDataTypes();
}
// ... rest of your adapter implementation
}
The package provides standardized MySQL data type definitions:
const dataTypes = new MySQLDrizzleDataTypes();
const table = dataTypes.dbTable('example', {
// Numeric types
id: dataTypes.dbBigInt('id', { mode: 'number' }).primaryKey().autoincrement(),
count: dataTypes.dbInt('count'),
price: dataTypes.dbDecimal('price', { precision: 10, scale: 2 }),
// String types
name: dataTypes.dbVarChar('name', { length: 255 }),
description: dataTypes.dbText('description'),
// Date/Time types
createdAt: dataTypes.dbTimestamp('created_at').defaultNow(),
updatedAt: dataTypes.dbTimestamp('updated_at').onUpdateNow(),
// Other types
status: dataTypes.dbEnum('status', ['active', 'inactive']),
metadata: dataTypes.dbJson('metadata'),
isEnabled: dataTypes.dbBoolean('is_enabled')
});
The package provides unified error handling for MySQL-specific errors:
import { handleMySQLError } from '@drizzle-adapter/mysql-core';
try {
// Your database operation
} catch (error) {
throw handleMySQLError(error);
}
The package includes utilities for building MySQL-specific queries:
import {
buildInsertQuery,
buildUpdateQuery,
buildDeleteQuery,
buildSelectQuery
} from '@drizzle-adapter/mysql-core';
// Build INSERT query
const insertQuery = buildInsertQuery(table, data);
// Build UPDATE query with JSON operations
const updateQuery = buildUpdateQuery(table, {
metadata: sql`JSON_SET(metadata, '$.key', 'value')`
});
// Build DELETE query with conditions
const deleteQuery = buildDeleteQuery(table, conditions);
// Build SELECT query with joins
const selectQuery = buildSelectQuery({
table,
joins: [...],
conditions: [...],
orderBy: [...]
});
The package handles MySQL-specific type mapping:
import { mapMySQLType } from '@drizzle-adapter/mysql-core';
// Map JavaScript types to MySQL types
const mysqlType = mapMySQLType(value);
// Map MySQL types to JavaScript types
const jsValue = mapToJavaScript(mysqlValue, mysqlType);
This package is maintained as part of the Drizzle Adapter ecosystem. If you'd like to contribute:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
- @drizzle-adapter/core - Core interfaces and types
- @drizzle-adapter/mysql2 - MySQL adapter using mysql2
- @drizzle-adapter/tidb-serverless - TiDB serverless adapter