forge-sql-orm-cli 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.
- package/README.md +202 -0
- package/dist-cli/actions/generate-models.d.ts +19 -0
- package/dist-cli/actions/migrations-create.d.ts +46 -0
- package/dist-cli/actions/migrations-drops.d.ts +6 -0
- package/dist-cli/actions/migrations-update.d.ts +6 -0
- package/dist-cli/cli.d.ts +3 -0
- package/dist-cli/cli.js +862 -0
- package/dist-cli/cli.js.map +1 -0
- package/dist-cli/cli.mjs +862 -0
- package/dist-cli/cli.mjs.map +1 -0
- package/package.json +64 -0
- package/src/.env +7 -0
- package/src/actions/generate-models.ts +267 -0
- package/src/actions/migrations-create.ts +213 -0
- package/src/actions/migrations-drops.ts +139 -0
- package/src/actions/migrations-update.ts +659 -0
- package/src/cli.ts +302 -0
- package/tsconfig.json +17 -0
package/README.md
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# forge-sql-orm-cli
|
|
2
|
+
|
|
3
|
+
A command-line interface tool for managing Atlassian Forge SQL migrations and model generation with Drizzle ORM integration.
|
|
4
|
+
|
|
5
|
+
## About
|
|
6
|
+
|
|
7
|
+
`forge-sql-orm-cli` is a new package that provides a command-line interface for managing database migrations and models in Atlassian Forge SQL applications. It integrates with Drizzle ORM to provide type-safe database operations and schema management.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Generate Drizzle ORM models from your Atlassian Forge SQL database schema
|
|
12
|
+
- Create and manage Atlassian Forge SQL migrations
|
|
13
|
+
- Automatic version tracking for schema changes
|
|
14
|
+
- Support for MySQL databases in Atlassian Forge environment
|
|
15
|
+
- Environment-based configuration
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install -g forge-sql-orm-cli
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Available Commands
|
|
24
|
+
|
|
25
|
+
### Generate Models
|
|
26
|
+
|
|
27
|
+
Generate Drizzle ORM models and version metadata from your Atlassian Forge SQL database schema. This command will create TypeScript files with Drizzle table definitions and schema types compatible with Atlassian Forge SQL.
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
forge-sql-orm-cli generate:model [options]
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Options:
|
|
34
|
+
- `--saveEnv` - Save the configuration to a `.env` file
|
|
35
|
+
- `--host` - Database host (default: localhost)
|
|
36
|
+
- `--port` - Database port (default: 3306)
|
|
37
|
+
- `--user` - Database user
|
|
38
|
+
- `--password` - Database password
|
|
39
|
+
- `--dbName` - Database name
|
|
40
|
+
- `--output` - Output directory for generated models (default: ./database/entities)
|
|
41
|
+
- `--versionField` - Name of the version field (default: version)
|
|
42
|
+
|
|
43
|
+
The generated models will include:
|
|
44
|
+
- Drizzle table definitions compatible with Atlassian Forge SQL
|
|
45
|
+
- TypeScript types for your database schema
|
|
46
|
+
- Version metadata for tracking schema changes
|
|
47
|
+
|
|
48
|
+
### Create Migration
|
|
49
|
+
|
|
50
|
+
Create a new Atlassian Forge SQL migration file based on your current database schema. This will generate SQL statements that can be used to update your Atlassian Forge SQL database schema.
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
forge-sql-orm-cli migrations:create [options]
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Options:
|
|
57
|
+
- `--saveEnv` - Save the configuration to a `.env` file
|
|
58
|
+
- `--host` - Database host (default: localhost)
|
|
59
|
+
- `--port` - Database port (default: 3306)
|
|
60
|
+
- `--user` - Database user
|
|
61
|
+
- `--password` - Database password
|
|
62
|
+
- `--dbName` - Database name
|
|
63
|
+
- `--output` - Output directory for migrations (default: ./database/migration)
|
|
64
|
+
- `--entitiesPath` - Path to entity files (default: ./database/entities)
|
|
65
|
+
- `--force` - Force overwrite existing migrations. Without this parameter, the command will fail if migrations already exist.
|
|
66
|
+
|
|
67
|
+
### Update Migration
|
|
68
|
+
|
|
69
|
+
Update an existing Atlassian Forge SQL migration with the latest schema changes. This command will compare your current database schema with the Drizzle models and generate the necessary SQL statements for Atlassian Forge SQL.
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
forge-sql-orm-cli migrations:update [options]
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Options:
|
|
76
|
+
- `--saveEnv` - Save the configuration to a `.env` file
|
|
77
|
+
- `--host` - Database host (default: localhost)
|
|
78
|
+
- `--port` - Database port (default: 3306)
|
|
79
|
+
- `--user` - Database user
|
|
80
|
+
- `--password` - Database password
|
|
81
|
+
- `--dbName` - Database name
|
|
82
|
+
- `--output` - Output directory for migrations (default: ./database/migration)
|
|
83
|
+
- `--entitiesPath` - Path to entity files (default: ./database/entities)
|
|
84
|
+
|
|
85
|
+
### Drop Migration
|
|
86
|
+
|
|
87
|
+
Create an Atlassian Forge SQL migration to drop tables from the database. This is useful when you need to remove tables or reset your database schema in Atlassian Forge environment.
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
forge-sql-orm-cli migrations:drop [options]
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Options:
|
|
94
|
+
- `--saveEnv` - Save the configuration to a `.env` file
|
|
95
|
+
- `--host` - Database host (default: localhost)
|
|
96
|
+
- `--port` - Database port (default: 3306)
|
|
97
|
+
- `--user` - Database user
|
|
98
|
+
- `--password` - Database password
|
|
99
|
+
- `--dbName` - Database name
|
|
100
|
+
- `--output` - Output directory for migrations (default: ./database/migration)
|
|
101
|
+
- `--entitiesPath` - Path to entity files (default: ./database/entities)
|
|
102
|
+
|
|
103
|
+
## Atlassian Forge SQL Integration
|
|
104
|
+
|
|
105
|
+
This CLI tool is designed to work seamlessly with Atlassian Forge SQL and Drizzle ORM. It provides the following features:
|
|
106
|
+
|
|
107
|
+
1. **Model Generation**
|
|
108
|
+
- Creates Drizzle table definitions compatible with Atlassian Forge SQL
|
|
109
|
+
- Generates TypeScript types for your schema
|
|
110
|
+
- Supports all Drizzle column types
|
|
111
|
+
- Maintains relationships between tables
|
|
112
|
+
- Ensures compatibility with Atlassian Forge SQL constraints
|
|
113
|
+
|
|
114
|
+
2. **Migration Management**
|
|
115
|
+
- Generates Atlassian Forge SQL-compatible migrations
|
|
116
|
+
- Tracks schema versions
|
|
117
|
+
- Supports incremental updates
|
|
118
|
+
- Handles table creation, modification, and deletion
|
|
119
|
+
- Ensures migrations follow Atlassian Forge SQL best practices
|
|
120
|
+
|
|
121
|
+
3. **Type Safety**
|
|
122
|
+
- Full TypeScript support
|
|
123
|
+
- Type-safe database operations
|
|
124
|
+
- Automatic type generation from schema
|
|
125
|
+
- Validation against Atlassian Forge SQL requirements
|
|
126
|
+
|
|
127
|
+
## Environment Variables
|
|
128
|
+
|
|
129
|
+
The CLI supports loading configuration from environment variables. You can either:
|
|
130
|
+
1. Use the `--saveEnv` flag to save your configuration to a `.env` file
|
|
131
|
+
2. Create a `.env` file manually with the following variables:
|
|
132
|
+
|
|
133
|
+
```env
|
|
134
|
+
FORGE_SQL_ORM_HOST=localhost
|
|
135
|
+
FORGE_SQL_ORM_PORT=3306
|
|
136
|
+
FORGE_SQL_ORM_USER=your_user
|
|
137
|
+
FORGE_SQL_ORM_PASSWORD=your_password
|
|
138
|
+
FORGE_SQL_ORM_DB_NAME=your_database
|
|
139
|
+
FORGE_SQL_ORM_OUTPUT=./database/entities
|
|
140
|
+
FORGE_SQL_ORM_ENTITIES_PATH=./database/entities
|
|
141
|
+
FORGE_SQL_ORM_VERSION_FIELD=version
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Examples
|
|
145
|
+
|
|
146
|
+
### Generate Drizzle Models for Atlassian Forge SQL
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
forge-sql-orm-cli generate:model --host localhost --port 3306 --user root --password secret --dbName myapp --output ./database/entities --saveEnv
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
This will generate Drizzle table definitions and TypeScript types compatible with Atlassian Forge SQL in the specified output directory.
|
|
153
|
+
|
|
154
|
+
### Create Atlassian Forge SQL Migration
|
|
155
|
+
|
|
156
|
+
```bash
|
|
157
|
+
forge-sql-orm-cli migrations:create --host localhost --port 3306 --user root --password secret --dbName myapp --output ./database/migration --entitiesPath ./database/entities --saveEnv
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
This will create a new Atlassian Forge SQL migration file with SQL statements to update your database schema.
|
|
161
|
+
|
|
162
|
+
### Update Atlassian Forge SQL Migration
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
forge-sql-orm-cli migrations:update --host localhost --port 3306 --user root --password secret --dbName myapp --output ./database/migration --entitiesPath ./database/entities --saveEnv
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
This will update an existing migration with the latest schema changes for Atlassian Forge SQL.
|
|
169
|
+
|
|
170
|
+
### Drop Tables with Atlassian Forge SQL Migration
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
forge-sql-orm-cli migrations:drop --host localhost --port 3306 --user root --password secret --dbName myapp --output ./database/migration --entitiesPath ./database/entities --saveEnv
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
This will create a migration to drop specified tables from your Atlassian Forge SQL database.
|
|
177
|
+
|
|
178
|
+
## Development
|
|
179
|
+
|
|
180
|
+
### Running Tests
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
npm test
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Running Tests with Coverage
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
npm run test:coverage
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Linting
|
|
193
|
+
|
|
194
|
+
```bash
|
|
195
|
+
npm run lint
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### Formatting
|
|
199
|
+
|
|
200
|
+
```bash
|
|
201
|
+
npm run format
|
|
202
|
+
```
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
/**
|
|
3
|
+
* Options for model generation
|
|
4
|
+
*/
|
|
5
|
+
interface GenerateModelsOptions {
|
|
6
|
+
host: string;
|
|
7
|
+
port: number;
|
|
8
|
+
user: string;
|
|
9
|
+
password: string;
|
|
10
|
+
dbName: string;
|
|
11
|
+
output: string;
|
|
12
|
+
versionField: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Generates models for all tables in the database using drizzle-kit
|
|
16
|
+
* @param options - Generation options
|
|
17
|
+
*/
|
|
18
|
+
export declare const generateModels: (options: GenerateModelsOptions) => Promise<never>;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
/**
|
|
3
|
+
* Options for migration creation
|
|
4
|
+
*/
|
|
5
|
+
export interface CreateMigrationOptions {
|
|
6
|
+
output: string;
|
|
7
|
+
entitiesPath: string;
|
|
8
|
+
force?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Loads the current migration version from `migrationCount.ts`.
|
|
12
|
+
* @param migrationPath - Path to the migration folder.
|
|
13
|
+
* @returns The latest migration version.
|
|
14
|
+
*/
|
|
15
|
+
export declare const loadMigrationVersion: (migrationPath: string) => Promise<number>;
|
|
16
|
+
/**
|
|
17
|
+
* Cleans SQL statements by removing unnecessary database options.
|
|
18
|
+
* @param sql - The raw SQL statement.
|
|
19
|
+
* @returns The cleaned SQL statement.
|
|
20
|
+
*/
|
|
21
|
+
export declare function cleanSQLStatement(sql: string): string;
|
|
22
|
+
/**
|
|
23
|
+
* Generates a migration file using the provided SQL statements.
|
|
24
|
+
* @param createStatements - Array of SQL statements.
|
|
25
|
+
* @param version - Migration version number.
|
|
26
|
+
* @returns TypeScript migration file content.
|
|
27
|
+
*/
|
|
28
|
+
export declare function generateMigrationFile(createStatements: string[], version: number): string;
|
|
29
|
+
/**
|
|
30
|
+
* Saves the generated migration file along with `migrationCount.ts` and `index.ts`.
|
|
31
|
+
* @param migrationCode - The migration code to be written to the file.
|
|
32
|
+
* @param version - Migration version number.
|
|
33
|
+
* @param outputDir - Directory where the migration files will be saved.
|
|
34
|
+
*/
|
|
35
|
+
export declare function saveMigrationFiles(migrationCode: string, version: number, outputDir: string): void;
|
|
36
|
+
/**
|
|
37
|
+
* Extracts only the relevant SQL statements for migration.
|
|
38
|
+
* @param schema - The full database schema as SQL.
|
|
39
|
+
* @returns Filtered list of SQL statements.
|
|
40
|
+
*/
|
|
41
|
+
export declare const extractCreateStatements: (schema: string) => string[];
|
|
42
|
+
/**
|
|
43
|
+
* Creates a full database migration.
|
|
44
|
+
* @param options - Database connection settings and output paths.
|
|
45
|
+
*/
|
|
46
|
+
export declare const createMigration: (options: CreateMigrationOptions) => Promise<never>;
|