@seedts/adapter-postgresql
v0.1.1
Published
PostgreSQL adapter for SeedTS
Maintainers
Readme
@seedts/adapter-postgresql
PostgreSQL adapter for SeedTS - enables database seeding with PostgreSQL using the pg library.
Installation
npm install @seedts/adapter-postgresql pg
# or
pnpm add @seedts/adapter-postgresql pg
# or
yarn add @seedts/adapter-postgresql pgUsage
import { Pool } from 'pg';
import { PostgreSQLAdapter } from '@seedts/adapter-postgresql';
import { Seed, Action, Entity, Attribute } from '@seedts/jsx-runtime';
// Create PostgreSQL connection pool
const pool = new Pool({
host: 'localhost',
port: 5432,
database: 'mydb',
user: 'postgres',
password: 'password',
});
// Create adapter
const adapter = new PostgreSQLAdapter({
pool,
tableName: 'users',
schema: 'public', // optional, defaults to 'public'
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
PostgreSQLAdapterConfig
| Option | Type | Required | Default | Description |
|--------|------|----------|---------|-------------|
| pool | Pool | Yes | - | PostgreSQL connection pool from pg library |
| tableName | string | Yes | - | Name of the table to seed |
| schema | string | No | 'public' | PostgreSQL schema name |
| idColumn | string | No | 'id' | Name of the primary key column |
Features
- ✅ Transaction support
- ✅ Schema support
- ✅ RETURNING clause for inserted records
- ✅ Parameterized queries (SQL injection safe)
- ✅ Connection pooling
- ✅ Auto-increment ID handling
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 with RESTART IDENTITY CASCADE.
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, // Wraps all seeds in a transaction
verbose: true,
});
// Clean up
await adapter.close();License
MIT
