forge-sql-orm 2.0.9 → 2.0.10
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 +53 -5
- package/dist/ForgeSQLORM.js +43 -2
- package/dist/ForgeSQLORM.js.map +1 -1
- package/dist/ForgeSQLORM.mjs +44 -3
- package/dist/ForgeSQLORM.mjs.map +1 -1
- package/dist/core/ForgeSQLQueryBuilder.d.ts +32 -0
- package/dist/core/ForgeSQLQueryBuilder.d.ts.map +1 -1
- package/dist/core/SystemTables.d.ts +8 -6
- package/dist/core/SystemTables.d.ts.map +1 -1
- package/dist/webtriggers/applyMigrationsWebTrigger.d.ts +23 -0
- package/dist/webtriggers/applyMigrationsWebTrigger.d.ts.map +1 -1
- package/dist/webtriggers/dropMigrationWebTrigger.d.ts +20 -4
- package/dist/webtriggers/dropMigrationWebTrigger.d.ts.map +1 -1
- package/dist/webtriggers/fetchSchemaWebTrigger.d.ts +28 -0
- package/dist/webtriggers/fetchSchemaWebTrigger.d.ts.map +1 -0
- package/dist/webtriggers/index.d.ts +1 -0
- package/dist/webtriggers/index.d.ts.map +1 -1
- package/dist-cli/cli.js +5 -0
- package/dist-cli/cli.js.map +1 -1
- package/dist-cli/cli.mjs +8 -3
- package/dist-cli/cli.mjs.map +1 -1
- package/package.json +1 -1
- package/src/core/ForgeSQLQueryBuilder.ts +32 -0
- package/src/core/SystemTables.ts +6 -3
- package/src/webtriggers/applyMigrationsWebTrigger.ts +23 -0
- package/src/webtriggers/dropMigrationWebTrigger.ts +20 -4
- package/src/webtriggers/fetchSchemaWebTrigger.ts +96 -0
- package/src/webtriggers/index.ts +1 -0
|
@@ -4,12 +4,28 @@ import { generateDropTableStatements as generateStatements } from "../utils/sqlU
|
|
|
4
4
|
import { getHttpResponse, TriggerResponse } from "./index";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* ⚠️
|
|
8
|
-
*
|
|
7
|
+
* ⚠️ DEVELOPMENT ONLY WEB TRIGGER ⚠️
|
|
8
|
+
*
|
|
9
|
+
* This web trigger is designed for development environments only and will permanently delete all data in the specified tables.
|
|
10
|
+
* It generates and executes SQL statements to drop tables and their associated constraints.
|
|
11
|
+
*
|
|
12
|
+
* @warning This trigger should NEVER be used in production environments because:
|
|
13
|
+
* - It permanently deletes all data in the specified tables
|
|
14
|
+
* - The operation cannot be undone
|
|
15
|
+
* - It may affect application functionality
|
|
16
|
+
* - It could lead to data loss and system instability
|
|
9
17
|
*
|
|
10
|
-
* Generates SQL statements to drop tables and executes them
|
|
11
18
|
* @param tables - Array of table schemas to drop
|
|
12
|
-
* @returns
|
|
19
|
+
* @returns {Promise<TriggerResponse<string>>} A response containing:
|
|
20
|
+
* - On success: 200 status with warning message about permanent deletion
|
|
21
|
+
* - On failure: 500 status with error message
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* // Example usage in development only
|
|
26
|
+
* await dropSchemaMigrations([users, orders]);
|
|
27
|
+
* // ⚠️ Warning: This will permanently delete all data in users and orders tables
|
|
28
|
+
* ```
|
|
13
29
|
*/
|
|
14
30
|
export async function dropSchemaMigrations(
|
|
15
31
|
tables: AnyMySqlTable[],
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { sql } from "@forge/sql";
|
|
2
|
+
import { getHttpResponse, TriggerResponse } from "./index";
|
|
3
|
+
import { forgeSystemTables } from "../core/SystemTables";
|
|
4
|
+
import { getTableName } from "drizzle-orm/table";
|
|
5
|
+
|
|
6
|
+
interface CreateTableRow {
|
|
7
|
+
Table: string;
|
|
8
|
+
"Create Table": string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* ⚠️ DEVELOPMENT ONLY WEB TRIGGER ⚠️
|
|
13
|
+
*
|
|
14
|
+
* This web trigger retrieves the current database schema from Atlassian Forge SQL.
|
|
15
|
+
* It generates SQL statements that can be used to recreate the database structure.
|
|
16
|
+
*
|
|
17
|
+
* @warning This trigger should ONLY be used in development environments. It:
|
|
18
|
+
* - Exposes your database structure
|
|
19
|
+
* - Disables foreign key checks temporarily
|
|
20
|
+
* - Generates SQL that could potentially be used maliciously
|
|
21
|
+
* - May expose sensitive table names and structures
|
|
22
|
+
*
|
|
23
|
+
* @returns {Promise<TriggerResponse<string>>} A response containing SQL statements to recreate the database schema
|
|
24
|
+
* - On success: Returns 200 status with SQL statements
|
|
25
|
+
* - On failure: Returns 500 status with error message
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* // The response will contain SQL statements like:
|
|
30
|
+
* // SET foreign_key_checks = 0;
|
|
31
|
+
* // CREATE TABLE IF NOT EXISTS users (...);
|
|
32
|
+
* // CREATE TABLE IF NOT EXISTS orders (...);
|
|
33
|
+
* // SET foreign_key_checks = 1;
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export async function fetchSchemaWebTrigger(): Promise<TriggerResponse<string>> {
|
|
37
|
+
try {
|
|
38
|
+
const tables = await getTables();
|
|
39
|
+
const createTableStatements = await generateCreateTableStatements(tables);
|
|
40
|
+
const sqlStatements = wrapWithForeignKeyChecks(createTableStatements);
|
|
41
|
+
|
|
42
|
+
return getHttpResponse<string>(200, sqlStatements.join(";\n"));
|
|
43
|
+
} catch (error: unknown) {
|
|
44
|
+
console.error(JSON.stringify(error));
|
|
45
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
|
|
46
|
+
return getHttpResponse<string>(500, errorMessage);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Retrieves all tables from the database
|
|
52
|
+
*/
|
|
53
|
+
async function getTables(): Promise<string[]> {
|
|
54
|
+
const tables = await sql.executeDDL<string>("SHOW TABLES");
|
|
55
|
+
return tables.rows.flatMap((tableInfo) => Object.values(tableInfo));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Generates CREATE TABLE statements for each table
|
|
60
|
+
*/
|
|
61
|
+
async function generateCreateTableStatements(tables: string[]): Promise<string[]> {
|
|
62
|
+
const statements: string[] = [];
|
|
63
|
+
|
|
64
|
+
for (const table of tables) {
|
|
65
|
+
const createTableResult = await sql.executeDDL<CreateTableRow>(`SHOW CREATE TABLE ${table}`);
|
|
66
|
+
|
|
67
|
+
const createTableStatements = createTableResult.rows
|
|
68
|
+
.filter((row) => !isSystemTable(row.Table))
|
|
69
|
+
.map((row) => formatCreateTableStatement(row["Create Table"]));
|
|
70
|
+
|
|
71
|
+
statements.push(...createTableStatements);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return statements;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Checks if the table is a system table
|
|
79
|
+
*/
|
|
80
|
+
function isSystemTable(tableName: string): boolean {
|
|
81
|
+
return forgeSystemTables.some((st) => getTableName(st) === tableName);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Formats the CREATE TABLE statement
|
|
86
|
+
*/
|
|
87
|
+
function formatCreateTableStatement(statement: string): string {
|
|
88
|
+
return statement.replace(/"/g, "").replace("CREATE TABLE", "CREATE TABLE IF NOT EXISTS");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Wraps the SQL statements with foreign key check controls
|
|
93
|
+
*/
|
|
94
|
+
function wrapWithForeignKeyChecks(statements: string[]): string[] {
|
|
95
|
+
return ["SET foreign_key_checks = 0", ...statements, "SET foreign_key_checks = 1"];
|
|
96
|
+
}
|