@seedts/adapter-mysql
v0.1.1
Published
MySQL adapter for SeedTS
Downloads
7
Maintainers
Readme
@seedts/adapter-mysql
MySQL adapter for SeedTS - enables database seeding with MySQL using the mysql2 library.
Installation
npm install @seedts/adapter-mysql mysql2
# or
pnpm add @seedts/adapter-mysql mysql2
# or
yarn add @seedts/adapter-mysql mysql2Usage
import mysql from 'mysql2/promise';
import { MySQLAdapter } from '@seedts/adapter-mysql';
import { Seed, Action, Entity, Attribute } from '@seedts/jsx-runtime';
// Create MySQL connection pool
const pool = mysql.createPool({
host: 'localhost',
port: 3306,
database: 'mydb',
user: 'root',
password: 'password',
waitForConnections: true,
connectionLimit: 10,
});
// Create adapter
const adapter = new MySQLAdapter({
pool,
tableName: 'users',
database: 'mydb', // optional
idColumn: 'id', // optional, defaults to 'id'
});
// Use with SeedTS
export const UsersSeed = () => (
<Seed name="users" adapter={adapter}>
<Action count={10}>
<Entity>
<Attribute name="id" type="number" autoIncrement />
<Attribute
name="email"
type="string"
factory={(ctx) => `user${ctx.index}@example.com`}
/>
<Attribute name="name" type="string" factory={() => 'John Doe'} />
</Entity>
</Action>
</Seed>
);Configuration
MySQLAdapterConfig
| Option | Type | Required | Default | Description |
|--------|------|----------|---------|-------------|
| pool | Pool | Yes | - | MySQL connection pool from mysql2/promise |
| tableName | string | Yes | - | Name of the table to seed |
| database | string | No | - | Database name (optional prefix for queries) |
| idColumn | string | No | 'id' | Name of the primary key column |
Features
- ✅ Transaction support
- ✅ Connection pooling
- ✅ Prepared statements (SQL injection safe)
- ✅ Auto-increment ID handling
- ✅ Proper identifier escaping
Methods
insert(tableName, data)
Insert records and return inserted data with generated IDs.
query(tableName, where?)
Query records with optional where clause.
update(tableName, data)
Update records by ID.
delete(tableName, where?)
Delete records with optional where clause.
truncate(tableName)
Truncate table to remove all records.
Transaction Methods
beginTransaction()- Start a transactioncommit()- Commit the transactionrollback()- Rollback the transaction
Example with Transactions
import { Executor } from '@seedts/core';
const executor = new Executor();
executor.registerSeed(UsersSeed());
// Run with transaction support
await executor.execute({
transaction: true,
verbose: true,
});
// Clean up
await adapter.close();Database Setup
CREATE DATABASE mydb;
USE mydb;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
name VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);License
MIT
