forge-sql-orm 2.0.11 → 2.0.13

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.
@@ -0,0 +1,400 @@
1
+ import moment from "moment";
2
+ import { AnyColumn, Column, isTable, SQL, sql, StringChunk } from "drizzle-orm";
3
+ import { AnyMySqlTable, MySqlCustomColumn } from "drizzle-orm/mysql-core/index";
4
+ import { PrimaryKeyBuilder } from "drizzle-orm/mysql-core/primary-keys";
5
+ import { AnyIndexBuilder } from "drizzle-orm/mysql-core/indexes";
6
+ import { CheckBuilder } from "drizzle-orm/mysql-core/checks";
7
+ import { ForeignKeyBuilder } from "drizzle-orm/mysql-core/foreign-keys";
8
+ import { UniqueConstraintBuilder } from "drizzle-orm/mysql-core/unique-constraint";
9
+ import type { SelectedFields } from "drizzle-orm/mysql-core/query-builders/select.types";
10
+ import { MySqlTable } from "drizzle-orm/mysql-core";
11
+ import { getTableName } from "drizzle-orm/table";
12
+ import { isSQLWrapper } from "drizzle-orm/sql/sql";
13
+
14
+ /**
15
+ * Interface representing table metadata information
16
+ */
17
+ export interface MetadataInfo {
18
+ /** The name of the table */
19
+ tableName: string;
20
+ /** Record of column names and their corresponding column definitions */
21
+ columns: Record<string, AnyColumn>;
22
+ /** Array of index builders */
23
+ indexes: AnyIndexBuilder[];
24
+ /** Array of check constraint builders */
25
+ checks: CheckBuilder[];
26
+ /** Array of foreign key builders */
27
+ foreignKeys: ForeignKeyBuilder[];
28
+ /** Array of primary key builders */
29
+ primaryKeys: PrimaryKeyBuilder[];
30
+ /** Array of unique constraint builders */
31
+ uniqueConstraints: UniqueConstraintBuilder[];
32
+ /** Array of all extra builders */
33
+ extras: any[];
34
+ }
35
+
36
+ /**
37
+ * Interface for config builder data
38
+ */
39
+ interface ConfigBuilderData {
40
+ value?: any;
41
+ [key: string]: any;
42
+ }
43
+
44
+ /**
45
+ * Parses a date string into a Date object using the specified format
46
+ * @param value - The date string to parse
47
+ * @param format - The format to use for parsing
48
+ * @returns Date object
49
+ */
50
+ export const parseDateTime = (value: string, format: string): Date => {
51
+ const m = moment(value, format, true);
52
+ if (!m.isValid()) {
53
+ return moment(value).toDate();
54
+ }
55
+ return m.toDate();
56
+ };
57
+
58
+ /**
59
+ * Extracts the alias from a SQL query
60
+ * @param query - The SQL query to extract the alias from
61
+ * @returns The extracted alias or the original query if no alias found
62
+ */
63
+ export function extractAlias(query: string): string {
64
+ const match = query.match(/\bas\s+(['"`]?)([\w*]+)\1$/i);
65
+ return match ? match[2] : query;
66
+ }
67
+
68
+ /**
69
+ * Gets primary keys from the schema.
70
+ * @template T - The type of the table schema
71
+ * @param {T} table - The table schema
72
+ * @returns {[string, AnyColumn][]} Array of primary key name and column pairs
73
+ */
74
+ export function getPrimaryKeys<T extends AnyMySqlTable>(table: T): [string, AnyColumn][] {
75
+ const { columns, primaryKeys } = getTableMetadata(table);
76
+
77
+ // First try to find primary keys in columns
78
+ const columnPrimaryKeys = Object.entries(columns).filter(([, column]) => column.primary) as [
79
+ string,
80
+ AnyColumn,
81
+ ][];
82
+
83
+ if (columnPrimaryKeys.length > 0) {
84
+ return columnPrimaryKeys;
85
+ }
86
+
87
+ // If no primary keys found in columns, check primary key builders
88
+ if (Array.isArray(primaryKeys) && primaryKeys.length > 0) {
89
+ // Collect all primary key columns from all primary key builders
90
+ const primaryKeyColumns = new Set<[string, AnyColumn]>();
91
+
92
+ primaryKeys.forEach((primaryKeyBuilder) => {
93
+ // Get primary key columns from each builder
94
+ Object.entries(columns)
95
+ .filter(([, column]) => {
96
+ // @ts-ignore - PrimaryKeyBuilder has internal columns property
97
+ return primaryKeyBuilder.columns.includes(column);
98
+ })
99
+ .forEach(([name, column]) => {
100
+ primaryKeyColumns.add([name, column]);
101
+ });
102
+ });
103
+
104
+ return Array.from(primaryKeyColumns);
105
+ }
106
+
107
+ return [];
108
+ }
109
+
110
+ /**
111
+ * Processes foreign keys from both foreignKeysSymbol and extraSymbol
112
+ * @param table - The table schema
113
+ * @param foreignKeysSymbol - Symbol for foreign keys
114
+ * @param extraSymbol - Symbol for extra configuration
115
+ * @returns Array of foreign key builders
116
+ */
117
+ function processForeignKeys(
118
+ table: AnyMySqlTable,
119
+ foreignKeysSymbol: symbol | undefined,
120
+ extraSymbol: symbol | undefined,
121
+ ): ForeignKeyBuilder[] {
122
+ const foreignKeys: ForeignKeyBuilder[] = [];
123
+
124
+ // Process foreign keys from foreignKeysSymbol
125
+ if (foreignKeysSymbol) {
126
+ // @ts-ignore
127
+ const fkArray: any[] = table[foreignKeysSymbol];
128
+ if (fkArray) {
129
+ fkArray.forEach((fk) => {
130
+ if (fk.reference) {
131
+ const item = fk.reference(fk);
132
+ foreignKeys.push(item);
133
+ }
134
+ });
135
+ }
136
+ }
137
+
138
+ // Process foreign keys from extraSymbol
139
+ if (extraSymbol) {
140
+ // @ts-ignore
141
+ const extraConfigBuilder = table[extraSymbol];
142
+ if (extraConfigBuilder && typeof extraConfigBuilder === "function") {
143
+ const configBuilderData = extraConfigBuilder(table);
144
+ if (configBuilderData) {
145
+ const configBuilders = Array.isArray(configBuilderData)
146
+ ? configBuilderData
147
+ : Object.values(configBuilderData).map(
148
+ (item) => (item as ConfigBuilderData).value || item,
149
+ );
150
+
151
+ configBuilders.forEach((builder) => {
152
+ if (!builder?.constructor) return;
153
+
154
+ const builderName = builder.constructor.name.toLowerCase();
155
+ if (builderName.includes("foreignkeybuilder")) {
156
+ foreignKeys.push(builder);
157
+ }
158
+ });
159
+ }
160
+ }
161
+ }
162
+
163
+ return foreignKeys;
164
+ }
165
+
166
+ /**
167
+ * Extracts table metadata from the schema.
168
+ * @param {AnyMySqlTable} table - The table schema
169
+ * @returns {MetadataInfo} Object containing table metadata
170
+ */
171
+ export function getTableMetadata(table: AnyMySqlTable): MetadataInfo {
172
+ const symbols = Object.getOwnPropertySymbols(table);
173
+ const nameSymbol = symbols.find((s) => s.toString().includes("Name"));
174
+ const columnsSymbol = symbols.find((s) => s.toString().includes("Columns"));
175
+ const foreignKeysSymbol = symbols.find((s) => s.toString().includes("ForeignKeys)"));
176
+ const extraSymbol = symbols.find((s) => s.toString().includes("ExtraConfigBuilder"));
177
+
178
+ // Initialize builders arrays
179
+ const builders = {
180
+ indexes: [] as AnyIndexBuilder[],
181
+ checks: [] as CheckBuilder[],
182
+ foreignKeys: [] as ForeignKeyBuilder[],
183
+ primaryKeys: [] as PrimaryKeyBuilder[],
184
+ uniqueConstraints: [] as UniqueConstraintBuilder[],
185
+ extras: [] as any[],
186
+ };
187
+
188
+ // Process foreign keys
189
+ builders.foreignKeys = processForeignKeys(table, foreignKeysSymbol, extraSymbol);
190
+
191
+ // Process extra configuration if available
192
+ if (extraSymbol) {
193
+ // @ts-ignore
194
+ const extraConfigBuilder = table[extraSymbol];
195
+ if (extraConfigBuilder && typeof extraConfigBuilder === "function") {
196
+ const configBuilderData = extraConfigBuilder(table);
197
+ if (configBuilderData) {
198
+ // Convert configBuilderData to array if it's an object
199
+ const configBuilders = Array.isArray(configBuilderData)
200
+ ? configBuilderData
201
+ : Object.values(configBuilderData).map(
202
+ (item) => (item as ConfigBuilderData).value || item,
203
+ );
204
+
205
+ // Process each builder
206
+ configBuilders.forEach((builder) => {
207
+ if (!builder?.constructor) return;
208
+
209
+ const builderName = builder.constructor.name.toLowerCase();
210
+
211
+ // Map builder types to their corresponding arrays
212
+ const builderMap = {
213
+ indexbuilder: builders.indexes,
214
+ checkbuilder: builders.checks,
215
+ primarykeybuilder: builders.primaryKeys,
216
+ uniqueconstraintbuilder: builders.uniqueConstraints,
217
+ };
218
+
219
+ // Add builder to appropriate array if it matches any type
220
+ for (const [type, array] of Object.entries(builderMap)) {
221
+ if (builderName.includes(type)) {
222
+ array.push(builder);
223
+ break;
224
+ }
225
+ }
226
+
227
+ // Always add to extras array
228
+ builders.extras.push(builder);
229
+ });
230
+ }
231
+ }
232
+ }
233
+
234
+ return {
235
+ tableName: nameSymbol ? (table as any)[nameSymbol] : "",
236
+ columns: columnsSymbol ? ((table as any)[columnsSymbol] as Record<string, AnyColumn>) : {},
237
+ ...builders,
238
+ };
239
+ }
240
+
241
+ /**
242
+ * Generates SQL statements to drop tables
243
+ * @param tables - Array of table schemas
244
+ * @returns Array of SQL statements for dropping tables
245
+ */
246
+ export function generateDropTableStatements(tables: AnyMySqlTable[]): string[] {
247
+ const dropStatements: string[] = [];
248
+
249
+ tables.forEach((table) => {
250
+ const tableMetadata = getTableMetadata(table);
251
+ if (tableMetadata.tableName) {
252
+ dropStatements.push(`DROP TABLE IF EXISTS \`${tableMetadata.tableName}\`;`);
253
+ }
254
+ });
255
+
256
+ // Add statement to clear migrations table
257
+ dropStatements.push(`DELETE FROM __migrations;`);
258
+
259
+ return dropStatements;
260
+ }
261
+
262
+ type AliasColumnMap = Record<string, AnyColumn>;
263
+
264
+ function mapSelectTableToAlias(table: MySqlTable, aliasMap: AliasColumnMap): any {
265
+ const { columns, tableName } = getTableMetadata(table);
266
+ const selectionsTableFields: Record<string, unknown> = {};
267
+ Object.keys(columns).forEach((name) => {
268
+ const column = columns[name] as AnyColumn;
269
+ const uniqName = `a_${tableName}_${column.name}`;
270
+ const fieldAlias = sql.raw(uniqName);
271
+ selectionsTableFields[name] = sql`${column} as \`${fieldAlias}\``;
272
+ aliasMap[uniqName] = column;
273
+ });
274
+ return selectionsTableFields;
275
+ }
276
+
277
+ function isDrizzleColumn(column: any): boolean {
278
+ return column && typeof column === "object" && "table" in column;
279
+ }
280
+
281
+ export function mapSelectAllFieldsToAlias(
282
+ selections: any,
283
+ name: string,
284
+ fields: any,
285
+ aliasMap: AliasColumnMap,
286
+ ): any {
287
+ if (isTable(fields)) {
288
+ selections[name] = mapSelectTableToAlias(fields as MySqlTable, aliasMap);
289
+ } else if (isDrizzleColumn(fields)) {
290
+ const column = fields as Column;
291
+ const uniqName = `a_${getTableName(column.table)}_${column.name}`;
292
+ let aliasName = sql.raw(uniqName);
293
+ selections[name] = sql`${column} as \`${aliasName}\``;
294
+ aliasMap[uniqName] = column;
295
+ } else if (isSQLWrapper(fields)) {
296
+ selections[name] = fields;
297
+ } else {
298
+ const innerSelections: any = {};
299
+ Object.entries(fields).forEach(([iname, ifields]) => {
300
+ mapSelectAllFieldsToAlias(innerSelections, iname, ifields, aliasMap);
301
+ });
302
+ selections[name] = innerSelections;
303
+ }
304
+ return selections;
305
+ }
306
+ export function mapSelectFieldsWithAlias<TSelection extends SelectedFields>(
307
+ fields: TSelection,
308
+ ): { selections: TSelection; aliasMap: AliasColumnMap } {
309
+ if (!fields) {
310
+ throw new Error("fields is empty");
311
+ }
312
+ const aliasMap: AliasColumnMap = {};
313
+ const selections: any = {};
314
+ Object.entries(fields).forEach(([name, fields]) => {
315
+ mapSelectAllFieldsToAlias(selections, name, fields, aliasMap);
316
+ });
317
+ return { selections, aliasMap };
318
+ }
319
+
320
+ function getAliasFromDrizzleAlias(value: unknown): string | undefined {
321
+ const isSQL =
322
+ value !== null && typeof value === "object" && isSQLWrapper(value) && "queryChunks" in value;
323
+ if (isSQL) {
324
+ const sql = value as SQL;
325
+ const queryChunks = sql.queryChunks;
326
+ if (queryChunks.length > 3) {
327
+ const aliasNameChunk = queryChunks[queryChunks.length - 2];
328
+ if (isSQLWrapper(aliasNameChunk) && "queryChunks" in aliasNameChunk) {
329
+ const aliasNameChunkSql = aliasNameChunk as SQL;
330
+ if (aliasNameChunkSql && aliasNameChunkSql.queryChunks.length === 1) {
331
+ const queryChunksStringChunc = aliasNameChunkSql.queryChunks[0];
332
+ if (queryChunksStringChunc && "value" in queryChunksStringChunc) {
333
+ const values = (queryChunksStringChunc as StringChunk).value;
334
+ if (values && values.length === 1) {
335
+ return values[0];
336
+ }
337
+ }
338
+ }
339
+ }
340
+ }
341
+ }
342
+ return undefined;
343
+ }
344
+
345
+ function transformValue(
346
+ value: unknown,
347
+ alias: string,
348
+ aliasMap: Record<string, AnyColumn>,
349
+ ): unknown {
350
+ const column = aliasMap[alias];
351
+ if (!column) return value;
352
+
353
+ let customColumn = column as MySqlCustomColumn<any>;
354
+ // @ts-ignore
355
+ const fromDriver = customColumn?.mapFrom;
356
+ if (fromDriver && value !== null && value !== undefined) {
357
+ return fromDriver(value);
358
+ }
359
+ return value;
360
+ }
361
+
362
+ function transformObject(
363
+ obj: Record<string, unknown>,
364
+ selections: Record<string, unknown>,
365
+ aliasMap: Record<string, AnyColumn>,
366
+ ): Record<string, unknown> {
367
+ const result: Record<string, unknown> = {};
368
+
369
+ for (const [key, value] of Object.entries(obj)) {
370
+ const selection = selections[key];
371
+ const alias = getAliasFromDrizzleAlias(selection);
372
+ if (alias && aliasMap[alias]) {
373
+ result[key] = transformValue(value, alias, aliasMap);
374
+ } else if (selection && typeof selection === "object" && !isSQLWrapper(selection)) {
375
+ result[key] = transformObject(
376
+ value as Record<string, unknown>,
377
+ selection as Record<string, unknown>,
378
+ aliasMap,
379
+ );
380
+ } else {
381
+ result[key] = value;
382
+ }
383
+ }
384
+
385
+ return result;
386
+ }
387
+
388
+ export function applyFromDriverTransform<T, TSelection>(
389
+ rows: T[],
390
+ selections: TSelection,
391
+ aliasMap: Record<string, AnyColumn>,
392
+ ): T[] {
393
+ return rows.map((row) => {
394
+ return transformObject(
395
+ row as Record<string, unknown>,
396
+ selections as Record<string, unknown>,
397
+ aliasMap,
398
+ ) as T;
399
+ });
400
+ }
@@ -0,0 +1,49 @@
1
+ import { migrationRunner, sql } from "@forge/sql";
2
+ import { MigrationRunner } from "@forge/sql/out/migration";
3
+
4
+ /**
5
+ * Web trigger for applying database schema migrations in Atlassian Forge SQL.
6
+ * This function handles the complete migration process including:
7
+ * - Database provisioning
8
+ * - Migration execution
9
+ * - Migration history tracking
10
+ *
11
+ * @param migration - A function that takes a MigrationRunner instance and returns a Promise of MigrationRunner
12
+ * This function should define the sequence of migrations to be applied
13
+ * @returns {Promise<{
14
+ * headers: { "Content-Type": ["application/json"] },
15
+ * statusCode: number,
16
+ * statusText: string,
17
+ * body: string
18
+ * }>} A response object containing:
19
+ * - headers: Content-Type header set to application/json
20
+ * - statusCode: 200 on success
21
+ * - statusText: "OK" on success
22
+ * - body: Success message or error details
23
+ *
24
+ * @throws {Error} If database provisioning fails
25
+ * @throws {Error} If migration execution fails
26
+ */
27
+ export const applySchemaMigrations = async (
28
+ migration: (migrationRunner: MigrationRunner) => Promise<MigrationRunner>,
29
+ ) => {
30
+ console.log("Provisioning the database");
31
+ await sql._provision();
32
+ console.info("Running schema migrations");
33
+ const migrations = await migration(migrationRunner);
34
+ const successfulMigrations = await migrations.run();
35
+ console.info("Migrations applied:", successfulMigrations);
36
+
37
+ const migrationHistory = (await migrationRunner.list())
38
+ .map((y) => `${y.id}, ${y.name}, ${y.migratedAt.toUTCString()}`)
39
+ .join("\n");
40
+
41
+ console.info("Migrations history:\nid, name, migrated_at\n", migrationHistory);
42
+
43
+ return {
44
+ headers: { "Content-Type": ["application/json"] },
45
+ statusCode: 200,
46
+ statusText: "OK",
47
+ body: "Migrations successfully executed",
48
+ };
49
+ };
@@ -0,0 +1,51 @@
1
+ import { sql } from "@forge/sql";
2
+ import { AnyMySqlTable } from "drizzle-orm/mysql-core";
3
+ import { generateDropTableStatements as generateStatements } from "../utils/sqlUtils";
4
+ import { getHttpResponse, TriggerResponse } from "./index";
5
+
6
+ /**
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
17
+ *
18
+ * @param tables - Array of table schemas to drop
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
+ * ```
29
+ */
30
+ export async function dropSchemaMigrations(
31
+ tables: AnyMySqlTable[],
32
+ ): Promise<TriggerResponse<string>> {
33
+ try {
34
+ // Generate drop statements
35
+ const dropStatements = generateStatements(tables);
36
+
37
+ // Execute each statement
38
+ for (const statement of dropStatements) {
39
+ console.warn(statement);
40
+ await sql.executeDDL(statement);
41
+ }
42
+
43
+ return getHttpResponse<string>(
44
+ 200,
45
+ "⚠️ All data in these tables has been permanently deleted. This operation cannot be undone.",
46
+ );
47
+ } catch (error: unknown) {
48
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
49
+ return getHttpResponse<string>(500, errorMessage);
50
+ }
51
+ }
@@ -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
+ }
@@ -0,0 +1,26 @@
1
+ export * from "./dropMigrationWebTrigger";
2
+ export * from "./applyMigrationsWebTrigger";
3
+ export * from "./fetchSchemaWebTrigger";
4
+
5
+ export interface TriggerResponse<BODY> {
6
+ body?: BODY;
7
+ headers?: Record<string, string[]>;
8
+ statusCode: number;
9
+ statusText?: string;
10
+ }
11
+
12
+ export const getHttpResponse = <Body>(statusCode: number, body: Body): TriggerResponse<Body> => {
13
+ let statusText = "";
14
+ if (statusCode === 200) {
15
+ statusText = "Ok";
16
+ } else {
17
+ statusText = "Bad Request";
18
+ }
19
+
20
+ return {
21
+ headers: { "Content-Type": ["application/json"] },
22
+ statusCode,
23
+ statusText,
24
+ body,
25
+ };
26
+ };