@ooneex/migrations 0.16.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +166 -0
  2. package/package.json +5 -5
package/README.md CHANGED
@@ -1 +1,167 @@
1
1
  # @ooneex/migrations
2
+
3
+ Database migration runner with versioned schema changes, rollback support, execution logging, and container-aware lifecycle management.
4
+
5
+ ![Bun](https://img.shields.io/badge/Bun-Compatible-orange?style=flat-square&logo=bun)
6
+ ![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue?style=flat-square&logo=typescript)
7
+ ![MIT License](https://img.shields.io/badge/License-MIT-yellow?style=flat-square)
8
+
9
+ ## Features
10
+
11
+ ✅ **Versioned Migrations** - Timestamp-based migration versioning with automatic ordering
12
+
13
+ ✅ **Up/Down Methods** - Each migration defines `up` and `down` for applying and rolling back changes
14
+
15
+ ✅ **Dependency Support** - Migrations can declare dependencies on other migrations
16
+
17
+ ✅ **Transaction Safety** - Each migration runs inside a database transaction
18
+
19
+ ✅ **Migration Tracking** - Tracks executed migrations in a dedicated database table
20
+
21
+ ✅ **Code Generation** - Create new migration files from a template with `migrationCreate`
22
+
23
+ ✅ **Container Integration** - Decorator-based registration with dependency injection
24
+
25
+ ✅ **Terminal Logging** - Progress and error reporting via the TerminalLogger
26
+
27
+ ## Installation
28
+
29
+ ```bash
30
+ bun add @ooneex/migrations
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ ### Creating a Migration
36
+
37
+ ```typescript
38
+ import { migrationCreate } from '@ooneex/migrations';
39
+
40
+ // Creates a new migration file in the migrations/ directory
41
+ const filePath = await migrationCreate();
42
+ console.log(`Created: ${filePath}`);
43
+ ```
44
+
45
+ ### Writing a Migration
46
+
47
+ ```typescript
48
+ import { decorator } from '@ooneex/migrations';
49
+ import type { IMigration, MigrationClassType } from '@ooneex/migrations';
50
+ import type { SQL, TransactionSQL } from 'bun';
51
+
52
+ @decorator.migration()
53
+ class Migration20240115103045 implements IMigration {
54
+ public async up(tx: TransactionSQL, sql: SQL): Promise<void> {
55
+ await tx`CREATE TABLE users (
56
+ id SERIAL PRIMARY KEY,
57
+ email VARCHAR(255) NOT NULL UNIQUE,
58
+ created_at TIMESTAMP DEFAULT NOW()
59
+ )`;
60
+ }
61
+
62
+ public async down(tx: TransactionSQL, sql: SQL): Promise<void> {
63
+ await tx`DROP TABLE IF EXISTS users`;
64
+ }
65
+
66
+ public getVersion(): string {
67
+ return '20240115103045';
68
+ }
69
+
70
+ public getDependencies(): MigrationClassType[] {
71
+ return [];
72
+ }
73
+ }
74
+ ```
75
+
76
+ ### Running Migrations
77
+
78
+ ```typescript
79
+ import { migrationUp } from '@ooneex/migrations';
80
+
81
+ // Uses DATABASE_URL environment variable by default
82
+ await migrationUp();
83
+
84
+ // Or with custom config
85
+ await migrationUp({
86
+ databaseUrl: 'postgres://user:pass@localhost:5432/mydb',
87
+ tableName: 'schema_migrations',
88
+ });
89
+ ```
90
+
91
+ ## API Reference
92
+
93
+ ### Functions
94
+
95
+ #### `migrationCreate(config?)`
96
+
97
+ Creates a new migration file from a template.
98
+
99
+ **Parameters:**
100
+ - `config.dir` - Directory for migration files (default: `"migrations"`)
101
+
102
+ **Returns:** `Promise<string>` - Path to the created migration file
103
+
104
+ #### `migrationUp(config?)`
105
+
106
+ Runs all pending migrations in version order.
107
+
108
+ **Parameters:**
109
+ - `config.databaseUrl` - Database connection URL (default: `DATABASE_URL` env var)
110
+ - `config.tableName` - Name of the migrations tracking table (default: `"migrations"`)
111
+
112
+ #### `generateMigrationVersion()`
113
+
114
+ Generates a timestamp-based version string for new migrations.
115
+
116
+ #### `getMigrations()`
117
+
118
+ Returns all registered migrations sorted by version.
119
+
120
+ #### `createMigrationTable(sql, tableName)`
121
+
122
+ Creates the migration tracking table if it does not exist.
123
+
124
+ ### Interfaces
125
+
126
+ #### `IMigration`
127
+
128
+ ```typescript
129
+ interface IMigration {
130
+ up: (tx: TransactionSQL, sql: SQL) => Promise<void>;
131
+ down: (tx: TransactionSQL, sql: SQL) => Promise<void>;
132
+ getVersion: () => string;
133
+ getDependencies: () => Promise<MigrationClassType[]> | MigrationClassType[];
134
+ }
135
+ ```
136
+
137
+ ### Decorators
138
+
139
+ #### `@decorator.migration()`
140
+
141
+ Registers a class as a migration with the dependency injection container.
142
+
143
+ ## License
144
+
145
+ This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.
146
+
147
+ ## Contributing
148
+
149
+ Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
150
+
151
+ ### Development Setup
152
+
153
+ 1. Clone the repository
154
+ 2. Install dependencies: `bun install`
155
+ 3. Run tests: `bun run test`
156
+ 4. Build the project: `bun run build`
157
+
158
+ ### Guidelines
159
+
160
+ - Write tests for new features
161
+ - Follow the existing code style
162
+ - Update documentation for API changes
163
+ - Ensure all tests pass before submitting PR
164
+
165
+ ---
166
+
167
+ Made with ❤️ by the Ooneex team
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ooneex/migrations",
3
- "description": "Database migration runner framework with logging and container integration",
4
- "version": "0.16.0",
3
+ "description": "Database migration runner with versioned schema changes, rollback support, execution logging, and container-aware lifecycle management",
4
+ "version": "1.0.0",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist",
@@ -28,11 +28,11 @@
28
28
  "npm:publish": "bun publish --tolerate-republish --access public"
29
29
  },
30
30
  "dependencies": {
31
- "@ooneex/container": "0.0.17",
32
- "@ooneex/logger": "0.15.0"
31
+ "@ooneex/container": "0.0.19",
32
+ "@ooneex/logger": "0.17.2"
33
33
  },
34
34
  "devDependencies": {
35
- "@ooneex/exception": "0.0.16"
35
+ "@ooneex/exception": "0.0.18"
36
36
  },
37
37
  "keywords": [
38
38
  "bun",