Automatically generate SequelizeJS minimal application via the command line.
Posquelize is a powerful CLI tool that automates the generation of Sequelize applications from PostgreSQL databases. It connects directly to your PostgreSQL instance, analyzes the database schema, and produces production-ready TypeScript boilerplate with comprehensive type definitions.
- β Complete Model Generation: Creates Sequelize models, repositories, and TypeScript type definitions
- π Comprehensive Migration Support: Generates migrations for tables, functions, domains, views, triggers, indexes, and keys
- π Advanced Type Support: Handles custom user-defined types with automatic conversions
- π Multi-Schema Support: Seamlessly handle multiple database schemas with efficient processing and organization.
- β‘ Visual Documentation: Creates database ER diagrams in DBML
- π Selective Generation: Filter by schemas or tables for targeted code generation
- π Smart Relationship Detection: Identifies and configures table relationships and associations
- π Rich Type Definitions: Generates TypeScript interfaces, enums, and JSONB prototypes
- π― Production-Ready Boilerplate: Creates minimal but complete application structure
- π οΈ Enhanced Model Options: Configurable model properties including timestamps, paranoid mode
- π§ Advanced Migration Control: Granular control over migration generation with selective inclusion/exclusion options
- π JSON/JSONB Support: Automatically detects and generates TypeScript interfaces from column defaults or values, ensuring proper type safety for structured data.
- ποΈ Composite Type Handling: Support for PostgreSQL composite types with automatic TypeScript interface generation
- π Secure Authentication: Interactive password prompts to avoid sensitive data in command history
- π Flexible Output: Configurable output directory and Sequelize directory structure
- π§Ή Clean Generation: Automatic directory cleanup with
--clean - π¨ Template Customization: Support for custom output templates (upcoming)
- βοΈ Configuration Files: Advanced configuration via
posquelize.config.jsfor complex setups - π Programmatic API: Full TypeScript API for integration into build pipelines and custom tools
npm install -g posquelize # for NPM
pnpm add -g posquelize # for PNPM
yarn global add posquelize # for Yarnposquelize -h localhost -u postgres -d myapp_db -xFor advanced usage and a complete list of all available options, run:
posquelize --help| Option | Description | Example |
|---|---|---|
ποΈ -d, --database <name> |
Target database name | myapp_db |
π€ -u, --user <username> |
Database username | postgres |
π -x, --password <password> |
Database password (or omit for prompt) | mypassword |
| Option | Description | Default |
|---|---|---|
π -h, --host <address> |
IP/Hostname for the database | localhost |
π -p, --port <port> |
Database connection port | 5432 |
π -o, --output <directory> |
Output directory path | ./myapp |
π -n, --dirname <directory> |
Sequelize subdirectory name | database |
βοΈ --use-config |
Load posquelize.config.js configuration file from current directory. |
false |
π --schemas <schemas> |
Specific schemas to process (comma-separated) | - |
π --tables <tables> |
Specific tables to generate (comma-separated) | - |
π§Ή --clean |
Clean output directory before generation | false |
π --no-diagram |
Skip DBML ER diagram generation | false |
π --no-migrations |
Skip migration files generation | false |
π¦ --no-repositories |
Skip repository files generation | false |
π·οΈ --no-enums |
Use alternative types (literal / union) instead of enum |
false |
π --no-null-type |
Omit null in type declaration for nullable column |
false |
π¨ --extract-templates |
Extract template files into the current directory for customization | false |
posquelize -h localhost -u postgres -d myapp_db -xposquelize -h localhost -u postgres -d myapp_db -x --schemas public,authposquelize -h localhost -u postgres -d myapp_db -x --tables users,posts,commentsposquelize -h localhost -u postgres -d myapp_db -x -o ./my-sequelize-app --clean-x flag is used without a value, ensuring credentials don't appear in shell history or process lists.
The tool generates a complete application structure with:
- TypeScript Models: Fully typed models with validations
- Migration Scripts: Version-controlled database schema changes
- Type Definitions: Comprehensive TypeScript interfaces and types
- Relationship Maps: Automatically configured associations
- Repository Pattern: Abstraction layer for data access
<output-directory>/
β π .env # Environment variables
β π .gitignore # Git ignore rules
β βοΈ .sequelizerc # Sequelize configuration
β π¦ package.json # Dependencies and scripts
β π README.md # Project documentation
β βοΈ tsconfig.json # TypeScript configuration
ββββsrc/
β π server.ts # Application entry point
ββββ<sequelize-directory>/ # Default to `database`
β π instance.ts # Database connection
ββββbase/ # Base classes
β βββ π ModelBase.ts
β βββ π RepositoryBase.ts
ββββconfig/ # Configuration files
β βββ βοΈ config.js
ββββdiagrams/ # Database documentation
β βββ π database.dbml
β βββ π README.md
βββ models/ # Sequelize model files
β βββ π User.ts
β βββ π Post.ts
β βββ ... # Generated model files
βββ migrations/ # Sequelize migration files
β βββ π 20251101000000-create-users.js
β βββ π 20251101000001-create-posts.js
β βββ ... # Generated migration files
βββ repositories/ # Repository pattern implementations
β βββ π UserRepository.ts
β βββ π PostRepository.ts
β βββ ... # Generated repository files
βββ types/ # TypeScript type definitions
ββββseeders/ # Database seeders
ββββtypings/ # Type definitions
βββπ models.d.ts
To use a configuration file for more complex setups:
-
Create a
posquelize.config.jsfile in your project's root directory.Note: If a config file doesn't exist, will be created in the current directory.
-
Configure your options using the JavaScript configuration object.
-
Run the generator with the
--use-config.
posquelize --use-configmodule.exports = {
connection: { // Database connection configuration
host: 'localhost', // Host name / IP
username: 'postgres', // Username for database
password: '<password>', // The password
database: 'test_db', // The database name
port: 5432, // The port to connect
},
outputDir: __dirname + '/my_app', // Output directory
cleanRootDir: true, // Clean output directory before generation
dirname: 'db', // Sequelize subdirectory name
schemas: [/*'public'*/], // Specific schemas to process
tables: [/*'tags', 'products'*/], // Specific tables to generate
// Migration configuration
migrations: {
indexes: true, // Generate index migrations
seeders: true, // Generate seeder files
functions: true, // Generate function migrations
domains: true, // Generate domain migrations
composites: true, // Generate composite type migrations
tables: true, // Generate table migrations
views: true, // Generate view migrations
triggers: true, // Generate trigger migrations
foreignKeys: true // Generate foreign key migrations
},
diagram: false, // Skip DBML diagram generation
repositories: false, // Skip repository file generation
generator: {
model: {
addNullTypeForNullable: true, // Controls whether nullable typed property
replaceEnumsWithTypes: false, // Replace enum with String Union types
},
// Configurable enums for table columns, (generate enums instead of plain value)
enums: [{
path: 'public.products.status', // schemaName.tableName.columnName
values: {active: 10, inactive: 5, deleted: 0, suspended: 3}, // key:value map
defaultValue: 10, // Default value to be set in init -> options -> column definition
}, {
path: 'public.products.visibility',
values: ['public', 'private'], // list of values
// defaultValue: 'private', // Default Value is set in DDL
}],
},
// Path to directory containing custom templates for code generation
templatesDir: __dirname + '/templates',
};The Posquelize programmatic API allows you to integrate database generation directly into your TypeScript/JavaScript applications. This provides greater flexibility and automation capabilities compared to using the CLI alone.
import { PosquelizeGenerator } from 'posquelize';
// Define your PostgreSQL connection string
// Format: postgresql://<user>:<pass>@<host>:<port>/<database>
const connectionString = 'postgresql://user:pass@localhost:5432/test_db';
// Initialize the generator with connection string and output path
const posquelize = PosquelizeGenerator.create(connectionString, __dirname + '/myapp', {
cleanRootDir: true, // Clean output directory before generation
// other configuration goes here
});
// Execute the generation process
await posquelize.generate();The programmatic API supports all configuration options available in the CLI tool, allowing for fine-grained control over the generation process:
import { PosquelizeGenerator, GeneratorOptions } from 'posquelize';
const options: GeneratorOptions = {
cleanRootDir: true,
dirname: 'database',
schemas: ['public', 'auth'],
tables: ['users', 'posts', 'comments'],
migrations: {
tables: true,
foreignKeys: true,
indexes: false
},
diagram: false,
repositories: true
};
const posquelize = PosquelizeGenerator.create(connectionString, './output', options);
await posquelize.generate();Always wrap the generation process in try-catch blocks to handle potential connection or generation errors:
try {
await posquelize.generate();
console.log('Database generation completed successfully!');
} catch (error) {
console.error('Generation failed:', error.message);
// Handle error appropriately for your application
}- Fork the project repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Implement your changes with proper testing
- Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Submit a Pull Request for review
- Machine: Legion Pro 7 16IAX10H
- Development Environment: Windows 11
- Primary IDE: JetBrains PhpStorm
- Alternative Editor: Zed with AI assistance
- AI Tools: Qwen, Ollama (GLM 4.6, Qwen 3 Coder)
This project builds upon concepts and implementations from Sequelize Models Generator, with significant enhancements for TypeScript support and application generation.
This project draws inspiration from innovative tools in the Sequelize ecosystem:
-
Sequelize UI - A comprehensive web-based solution for generating TypeScript Sequelize code with flexible database configurations and customizable outputs.
-
Sequelize-Auto - A command-line utility that automates the creation of Sequelize models by analyzing existing database structures.
Posquelize is released under the MIT License. See LICENSE file for details.