fiberx-backend-toolkit 0.0.18 → 0.0.20
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/dist/code_templates/sequelize_code_template.js +9 -1
- package/dist/config/constants.js +23 -17
- package/dist/database/connectors/base_connector.js +6 -4
- package/dist/database/connectors/sequelize_connector.js +12 -7
- package/dist/database/main.js +26 -15
- package/dist/database/schema/schema_diff_util.js +3 -1
- package/dist/database/schema/schema_normalizer_util.js +3 -1
- package/dist/database/scripts/create_schema_script.js +23 -18
- package/dist/database/scripts/create_seeder_script.js +24 -19
- package/dist/database/scripts/make_migrations_script.js +35 -30
- package/dist/database/scripts/migration_runner_script.js +27 -22
- package/dist/database/scripts/seeder_runner_script.js +27 -22
- package/dist/database/scripts/sequelize_model_generator_script.js +26 -21
- package/dist/index.js +17 -1
- package/dist/middle_ware/cookie_manager_middle_ware.js +21 -10
- package/dist/middle_ware/cors_middle_ware.js +32 -15
- package/dist/middle_ware/https_enforcement_middle_ware.js +14 -6
- package/dist/middle_ware/main.js +20 -8
- package/dist/middle_ware/rate_limiter_middle_ware.js +18 -10
- package/dist/middle_ware/request_logger_middle_ware.js +13 -5
- package/dist/middle_ware/response_formatter_middle_ware.js +22 -8
- package/dist/middle_ware/secure_headers_middle_ware.js +14 -6
- package/dist/types/database_type.js +2 -1
- package/dist/types/main.js +21 -5
- package/dist/types/middle_ware_type.js +2 -1
- package/dist/types/migration_type.js +2 -1
- package/dist/types/schema_type.js +2 -1
- package/dist/types/util_type.js +5 -2
- package/dist/utils/cache_util.js +3 -1
- package/dist/utils/env_manager_util.js +18 -13
- package/dist/utils/input_transformer_util.js +12 -7
- package/dist/utils/input_validator_util.js +22 -17
- package/dist/utils/logger_util.js +15 -10
- package/dist/utils/main.js +22 -9
- package/dist/utils/safe_execute_util.js +18 -16
- package/dist/utils/server_util.js +5 -3
- package/dist/utils/sql_formatter_util.js +3 -1
- package/package.json +2 -2
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SEQUELIZE_MODELS_INDEX_CODE_TEMPLATE = exports.SEQUELIZE_MODEL_CODE_TEMPLATE = exports.SEQUELIZE_UPDATE_EXISTING_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE = exports.SEQUELIZE_CREATE_NEW_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE = exports.SEQUELIZE_SEEDER_TEMPLATE = exports.SEQUELIZE_SCHEMA_CODE_TEMPLATE = void 0;
|
|
1
4
|
const SEQUELIZE_SCHEMA_CODE_TEMPLATE = (schema_name, table_name, model_name, migration_priority) => {
|
|
2
5
|
return `
|
|
3
6
|
import { DataTypes } from "sequelize";
|
|
@@ -39,6 +42,7 @@ const ${schema_name}: SchemaDefinitionInterface = {
|
|
|
39
42
|
export default ${schema_name};
|
|
40
43
|
`;
|
|
41
44
|
};
|
|
45
|
+
exports.SEQUELIZE_SCHEMA_CODE_TEMPLATE = SEQUELIZE_SCHEMA_CODE_TEMPLATE;
|
|
42
46
|
const SEQUELIZE_CREATE_NEW_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE = (schema_table_name, schema_model_name, schema_file_name) => `
|
|
43
47
|
import { QueryInterface, DataTypes, Sequelize } from "sequelize";
|
|
44
48
|
import { LoggerUtil } from "fiberx-backend-toolkit//utils";
|
|
@@ -100,6 +104,7 @@ class Create${schema_model_name}TableMigration {
|
|
|
100
104
|
|
|
101
105
|
export default Create${schema_model_name}TableMigration;
|
|
102
106
|
`;
|
|
107
|
+
exports.SEQUELIZE_CREATE_NEW_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE = SEQUELIZE_CREATE_NEW_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE;
|
|
103
108
|
const SEQUELIZE_UPDATE_EXISTING_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE = (schema_table_name, schema_model_name, diff, schema_file_name) => `
|
|
104
109
|
import { QueryInterface, DataTypes, Sequelize } from "sequelize";
|
|
105
110
|
import { LoggerUtil } from "fiberx-backend-toolkit//utils";
|
|
@@ -269,6 +274,7 @@ class Update${schema_model_name}TableMigration {
|
|
|
269
274
|
|
|
270
275
|
export default Update${schema_model_name}TableMigration;
|
|
271
276
|
`;
|
|
277
|
+
exports.SEQUELIZE_UPDATE_EXISTING_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE = SEQUELIZE_UPDATE_EXISTING_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE;
|
|
272
278
|
const SEQUELIZE_SEEDER_TEMPLATE = (class_name, table_name) => `
|
|
273
279
|
'use strict';
|
|
274
280
|
|
|
@@ -314,6 +320,7 @@ class ${class_name}Seeder {
|
|
|
314
320
|
|
|
315
321
|
export default ${class_name}Seeder;
|
|
316
322
|
`;
|
|
323
|
+
exports.SEQUELIZE_SEEDER_TEMPLATE = SEQUELIZE_SEEDER_TEMPLATE;
|
|
317
324
|
const SEQUELIZE_MODEL_CODE_TEMPLATE = (schema_model_name, schema_file_name, schema_columns) => {
|
|
318
325
|
const attributes = Object.keys(schema_columns)
|
|
319
326
|
.map(col => ` declare ${col}: any;`)
|
|
@@ -354,6 +361,7 @@ ${attributes}
|
|
|
354
361
|
export default ${schema_model_name}
|
|
355
362
|
`;
|
|
356
363
|
};
|
|
364
|
+
exports.SEQUELIZE_MODEL_CODE_TEMPLATE = SEQUELIZE_MODEL_CODE_TEMPLATE;
|
|
357
365
|
const SEQUELIZE_MODELS_INDEX_CODE_TEMPLATE = (model_names, imports) => {
|
|
358
366
|
const exports = model_names.join(",\n ");
|
|
359
367
|
return `
|
|
@@ -379,4 +387,4 @@ export const initModels = (sequelize: Sequelize) => {
|
|
|
379
387
|
export default models;
|
|
380
388
|
`;
|
|
381
389
|
};
|
|
382
|
-
|
|
390
|
+
exports.SEQUELIZE_MODELS_INDEX_CODE_TEMPLATE = SEQUELIZE_MODELS_INDEX_CODE_TEMPLATE;
|
package/dist/config/constants.js
CHANGED
|
@@ -1,23 +1,29 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.REQUEST_RATE_LIMITTER_OPTIONS = exports.CORS_MAX_AGE_IN_MICRO_SECONDS = exports.CORS_MAX_AGE_IN_SECONDS = exports.CORS_ALLOWED_HEADERS = exports.CORS_ALLOWED_METHODS = exports.REQUEST_ID_COOKIE_MAX_AGE = exports.SEQUELIZE_SEEDER_META_TABLE_NAME = exports.SEQUELIZE_META_TABLE_NAME = exports.SEEDERS_DIR = exports.MIGRATIONS_DIR = exports.MODELS_DIR = exports.SCHEMA_SNAPSHOTS_DIR = exports.SCHEMAS_DIR = exports.ENV_VAR_DIR = exports.LOG_DIR = exports.BASE_DIR = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
2
8
|
// Database constants
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
exports.BASE_DIR = process.cwd();
|
|
10
|
+
exports.LOG_DIR = path_1.default.join(exports.BASE_DIR, "logs");
|
|
11
|
+
exports.ENV_VAR_DIR = path_1.default.join(exports.BASE_DIR, "environment_varaiables");
|
|
12
|
+
exports.SCHEMAS_DIR = path_1.default.join(exports.BASE_DIR, "src/database/schemas");
|
|
13
|
+
exports.SCHEMA_SNAPSHOTS_DIR = path_1.default.join(exports.BASE_DIR, "src/database/schema_snapshots");
|
|
14
|
+
exports.MODELS_DIR = path_1.default.join(exports.BASE_DIR, "src/database/models");
|
|
15
|
+
exports.MIGRATIONS_DIR = path_1.default.join(exports.BASE_DIR, "src/database/migrations");
|
|
16
|
+
exports.SEEDERS_DIR = path_1.default.join(exports.BASE_DIR, "src/database/seeders");
|
|
17
|
+
exports.SEQUELIZE_META_TABLE_NAME = "sequelize_database_tables_meta";
|
|
18
|
+
exports.SEQUELIZE_SEEDER_META_TABLE_NAME = "sequelize_database_table_seeder_meta";
|
|
19
|
+
exports.REQUEST_ID_COOKIE_MAX_AGE = (1000 * 60 * 60 * 24 * 7); // 7 days
|
|
14
20
|
// CORS constants
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
21
|
+
exports.CORS_ALLOWED_METHODS = ["GET", "POST", "PATCH", "DELETE", "OPTIONS"];
|
|
22
|
+
exports.CORS_ALLOWED_HEADERS = ["Content-Type", "Authorization", "X-Device-Id", "X-Device-Name", "User-Agent"];
|
|
23
|
+
exports.CORS_MAX_AGE_IN_SECONDS = (600); // default: 10 min
|
|
24
|
+
exports.CORS_MAX_AGE_IN_MICRO_SECONDS = (1000 * 60 * 10); // 10 minutes
|
|
19
25
|
// Rate limitter
|
|
20
|
-
|
|
26
|
+
exports.REQUEST_RATE_LIMITTER_OPTIONS = {
|
|
21
27
|
window_ms: (60 * 1000), // 1 MINS
|
|
22
28
|
max_requests: 50,
|
|
23
29
|
message: "⏳ Too many requests from this IP, please try again later"
|
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const main_1 = require("../../utils/main");
|
|
2
4
|
/**
|
|
3
5
|
* Base connector abstraction for all DB / external service connectors
|
|
4
6
|
*/
|
|
5
7
|
class BaseConnector {
|
|
6
8
|
connector_name;
|
|
7
|
-
env_manager = EnvManagerUtil.getInstance();
|
|
9
|
+
env_manager = main_1.EnvManagerUtil.getInstance();
|
|
8
10
|
logger;
|
|
9
11
|
constructor(connector_name) {
|
|
10
12
|
this.connector_name = connector_name;
|
|
11
|
-
this.logger = new LoggerUtil(connector_name);
|
|
13
|
+
this.logger = new main_1.LoggerUtil(connector_name);
|
|
12
14
|
}
|
|
13
15
|
}
|
|
14
|
-
|
|
16
|
+
exports.default = BaseConnector;
|
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const sequelize_1 = require("sequelize");
|
|
7
|
+
const base_connector_1 = __importDefault(require("./base_connector"));
|
|
8
|
+
const sql_formatter_util_1 = __importDefault(require("../../utils/sql_formatter_util"));
|
|
9
|
+
class SequelizeConnector extends base_connector_1.default {
|
|
5
10
|
static instance;
|
|
6
11
|
static connections = new Map();
|
|
7
12
|
constructor() {
|
|
@@ -18,7 +23,7 @@ class SequelizeConnector extends BaseConnector {
|
|
|
18
23
|
// ----------------------------------
|
|
19
24
|
formatSQLQueryLog = (sql, timing) => {
|
|
20
25
|
try {
|
|
21
|
-
const formatted =
|
|
26
|
+
const formatted = sql_formatter_util_1.default.format(sql);
|
|
22
27
|
if (typeof timing === "number") {
|
|
23
28
|
this.logger.info(`[SQL ${timing}ms] ${formatted}`);
|
|
24
29
|
}
|
|
@@ -51,7 +56,7 @@ class SequelizeConnector extends BaseConnector {
|
|
|
51
56
|
const password = options.password ?? this.env_manager.getEnvVar("DB_PASSWORD");
|
|
52
57
|
const host = options.host ?? this.env_manager.getEnvVar("DB_HOST");
|
|
53
58
|
const port = options.port ?? this.env_manager.getEnvVar("DB_PORT", 5432);
|
|
54
|
-
const sequelize = new Sequelize({
|
|
59
|
+
const sequelize = new sequelize_1.Sequelize({
|
|
55
60
|
database,
|
|
56
61
|
username,
|
|
57
62
|
password,
|
|
@@ -90,4 +95,4 @@ class SequelizeConnector extends BaseConnector {
|
|
|
90
95
|
SequelizeConnector.connections.clear();
|
|
91
96
|
}
|
|
92
97
|
}
|
|
93
|
-
|
|
98
|
+
exports.default = SequelizeConnector;
|
package/dist/database/main.js
CHANGED
|
@@ -1,15 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.SequelizeConnector = exports.BaseConnector = exports.SeqeulizeModelGeneratorScript = exports.SeederRunnerScript = exports.CreateSeederScript = exports.MigrationRunnerScript = exports.MakeMigrationsScript = exports.CreateSchemaScript = exports.SchemaNormalizerUtil = exports.SchemaDiffUtil = void 0;
|
|
7
|
+
const schema_diff_util_1 = __importDefault(require("./schema/schema_diff_util"));
|
|
8
|
+
exports.SchemaDiffUtil = schema_diff_util_1.default;
|
|
9
|
+
const schema_normalizer_util_1 = __importDefault(require("./schema/schema_normalizer_util"));
|
|
10
|
+
exports.SchemaNormalizerUtil = schema_normalizer_util_1.default;
|
|
11
|
+
const create_schema_script_1 = __importDefault(require("./scripts/create_schema_script"));
|
|
12
|
+
exports.CreateSchemaScript = create_schema_script_1.default;
|
|
13
|
+
const make_migrations_script_1 = __importDefault(require("./scripts/make_migrations_script"));
|
|
14
|
+
exports.MakeMigrationsScript = make_migrations_script_1.default;
|
|
15
|
+
const migration_runner_script_1 = __importDefault(require("./scripts/migration_runner_script"));
|
|
16
|
+
exports.MigrationRunnerScript = migration_runner_script_1.default;
|
|
17
|
+
const create_seeder_script_1 = __importDefault(require("./scripts/create_seeder_script"));
|
|
18
|
+
exports.CreateSeederScript = create_seeder_script_1.default;
|
|
19
|
+
const seeder_runner_script_1 = __importDefault(require("./scripts/seeder_runner_script"));
|
|
20
|
+
exports.SeederRunnerScript = seeder_runner_script_1.default;
|
|
21
|
+
const sequelize_model_generator_script_1 = __importDefault(require("./scripts/sequelize_model_generator_script"));
|
|
22
|
+
exports.SeqeulizeModelGeneratorScript = sequelize_model_generator_script_1.default;
|
|
23
|
+
const base_connector_1 = __importDefault(require("./connectors/base_connector"));
|
|
24
|
+
exports.BaseConnector = base_connector_1.default;
|
|
25
|
+
const sequelize_connector_1 = __importDefault(require("./connectors/sequelize_connector"));
|
|
26
|
+
exports.SequelizeConnector = sequelize_connector_1.default;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
3
|
class SchemaDiffUtil {
|
|
2
4
|
static indexKey(i) {
|
|
3
5
|
return JSON.stringify({
|
|
@@ -65,4 +67,4 @@ class SchemaDiffUtil {
|
|
|
65
67
|
return diff;
|
|
66
68
|
}
|
|
67
69
|
}
|
|
68
|
-
|
|
70
|
+
exports.default = SchemaDiffUtil;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
3
|
class SchemaNormalizerUtil {
|
|
2
4
|
static normalizeColumn(column) {
|
|
3
5
|
return {
|
|
@@ -36,4 +38,4 @@ class SchemaNormalizerUtil {
|
|
|
36
38
|
};
|
|
37
39
|
}
|
|
38
40
|
}
|
|
39
|
-
|
|
41
|
+
exports.default = SchemaNormalizerUtil;
|
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const fs_1 = __importDefault(require("fs"));
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const constants_1 = require("../../config/constants");
|
|
9
|
+
const sequelize_code_template_1 = require("../../code_templates/sequelize_code_template");
|
|
10
|
+
const main_1 = require("../../utils/main");
|
|
6
11
|
class CreateSchemaScript {
|
|
7
12
|
name = "create_schema_script";
|
|
8
13
|
raw_input;
|
|
9
14
|
schemas_dir;
|
|
10
|
-
logger = new LoggerUtil(this.name);
|
|
15
|
+
logger = new main_1.LoggerUtil(this.name);
|
|
11
16
|
constructor(raw_input) {
|
|
12
17
|
if (!raw_input) {
|
|
13
18
|
const log_msg = "❌ Please provide a schema name.";
|
|
@@ -15,11 +20,11 @@ class CreateSchemaScript {
|
|
|
15
20
|
throw new Error(log_msg);
|
|
16
21
|
}
|
|
17
22
|
this.raw_input = raw_input;
|
|
18
|
-
this.schemas_dir = SCHEMAS_DIR;
|
|
23
|
+
this.schemas_dir = constants_1.SCHEMAS_DIR;
|
|
19
24
|
}
|
|
20
25
|
// Method to ensure schema does not exist
|
|
21
26
|
ensureSchemaDoesNotExist(file_path) {
|
|
22
|
-
if (
|
|
27
|
+
if (fs_1.default.existsSync(file_path)) {
|
|
23
28
|
const log_msg = `❌ Schema already exists. File: ${file_path}`;
|
|
24
29
|
this.logger.error(log_msg);
|
|
25
30
|
throw new Error(log_msg);
|
|
@@ -27,10 +32,10 @@ class CreateSchemaScript {
|
|
|
27
32
|
}
|
|
28
33
|
// Determine next migration priority from existing schemas
|
|
29
34
|
getNextMigrationPriority() {
|
|
30
|
-
if (!
|
|
35
|
+
if (!fs_1.default.existsSync(this.schemas_dir)) {
|
|
31
36
|
return 1;
|
|
32
37
|
}
|
|
33
|
-
const files =
|
|
38
|
+
const files = fs_1.default.readdirSync(this.schemas_dir);
|
|
34
39
|
return files.length + 1;
|
|
35
40
|
}
|
|
36
41
|
// Pad migration priority (e.g. 1 -> 001)
|
|
@@ -39,9 +44,9 @@ class CreateSchemaScript {
|
|
|
39
44
|
}
|
|
40
45
|
// Method to create schema file with boilerplate data
|
|
41
46
|
createSchemaFile(file_path, schema_name, model_name, table_name, migration_priority) {
|
|
42
|
-
const template = SEQUELIZE_SCHEMA_CODE_TEMPLATE(schema_name, table_name, model_name, migration_priority);
|
|
47
|
+
const template = (0, sequelize_code_template_1.SEQUELIZE_SCHEMA_CODE_TEMPLATE)(schema_name, table_name, model_name, migration_priority);
|
|
43
48
|
try {
|
|
44
|
-
|
|
49
|
+
fs_1.default.writeFileSync(file_path, template, { encoding: "utf-8" });
|
|
45
50
|
}
|
|
46
51
|
catch (error) {
|
|
47
52
|
this.logger.error(`❌ Failed to write schema file: ${file_path}`);
|
|
@@ -50,16 +55,16 @@ class CreateSchemaScript {
|
|
|
50
55
|
}
|
|
51
56
|
// Main Method to run
|
|
52
57
|
run() {
|
|
53
|
-
const file_base_name = InputTransformerUtil.toSchemaFileName(this.raw_input);
|
|
54
|
-
const base_pascal = InputTransformerUtil.toPascalCase(file_base_name);
|
|
58
|
+
const file_base_name = main_1.InputTransformerUtil.toSchemaFileName(this.raw_input);
|
|
59
|
+
const base_pascal = main_1.InputTransformerUtil.toPascalCase(file_base_name);
|
|
55
60
|
const schema_name = base_pascal.toLowerCase().endsWith("schema") ? base_pascal : `${base_pascal}Schema`;
|
|
56
61
|
const model_name = base_pascal.toLowerCase().endsWith("schema") ? base_pascal.replace(/Schema$/i, "") : base_pascal;
|
|
57
|
-
const table_name = InputTransformerUtil.pluralizeSnakeCase(InputTransformerUtil.toSnakeCase(model_name));
|
|
62
|
+
const table_name = main_1.InputTransformerUtil.pluralizeSnakeCase(main_1.InputTransformerUtil.toSnakeCase(model_name));
|
|
58
63
|
const migration_priority = this.getNextMigrationPriority();
|
|
59
64
|
const file_name = `${file_base_name}.ts`;
|
|
60
|
-
const file_path =
|
|
65
|
+
const file_path = path_1.default.join(this.schemas_dir, file_name);
|
|
61
66
|
// Ensure schemas directory exists (create if missing)
|
|
62
|
-
InputValidatorUtil.dirExists(this.schemas_dir, true);
|
|
67
|
+
main_1.InputValidatorUtil.dirExists(this.schemas_dir, true);
|
|
63
68
|
this.ensureSchemaDoesNotExist(file_path);
|
|
64
69
|
this.createSchemaFile(file_path, schema_name, model_name, table_name, migration_priority);
|
|
65
70
|
;
|
|
@@ -70,7 +75,7 @@ class CreateSchemaScript {
|
|
|
70
75
|
this.logger.info(`🗄️ Table Name: ${table_name}`);
|
|
71
76
|
}
|
|
72
77
|
}
|
|
73
|
-
|
|
78
|
+
exports.default = CreateSchemaScript;
|
|
74
79
|
// CLI entry point
|
|
75
80
|
try {
|
|
76
81
|
new CreateSchemaScript(process.argv[2]).run();
|
|
@@ -1,15 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const fs_1 = __importDefault(require("fs"));
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const constants_1 = require("../../config/constants");
|
|
9
|
+
const sequelize_code_template_1 = require("../../code_templates/sequelize_code_template");
|
|
10
|
+
const main_1 = require("../../utils/main");
|
|
6
11
|
class CreateSeederScript {
|
|
7
12
|
module_name = "create_seeder_script";
|
|
8
13
|
schema_input;
|
|
9
14
|
seeder_file_input;
|
|
10
|
-
seeders_dir = SEEDERS_DIR;
|
|
11
|
-
schemas_dir = SCHEMAS_DIR;
|
|
12
|
-
logger = new LoggerUtil(this.module_name);
|
|
15
|
+
seeders_dir = constants_1.SEEDERS_DIR;
|
|
16
|
+
schemas_dir = constants_1.SCHEMAS_DIR;
|
|
17
|
+
logger = new main_1.LoggerUtil(this.module_name);
|
|
13
18
|
constructor(schema_input, seeder_file_input) {
|
|
14
19
|
if (!schema_input || !seeder_file_input) {
|
|
15
20
|
const log_msg = "❌ Please provide a schema name and seeder file name.";
|
|
@@ -21,7 +26,7 @@ class CreateSeederScript {
|
|
|
21
26
|
}
|
|
22
27
|
// Method to ensure seeder file does not exists
|
|
23
28
|
ensureSeederDoesNotExist(file_path) {
|
|
24
|
-
if (
|
|
29
|
+
if (fs_1.default.existsSync(file_path)) {
|
|
25
30
|
const log_msg = `❌ Seeder already exists: ${file_path}`;
|
|
26
31
|
this.logger.error(log_msg);
|
|
27
32
|
throw new Error(log_msg);
|
|
@@ -31,7 +36,7 @@ class CreateSeederScript {
|
|
|
31
36
|
generateSeederFileName(migration_priority) {
|
|
32
37
|
const timestamp = Date.now();
|
|
33
38
|
const formatted_migration_priority = this.formatPriority(migration_priority);
|
|
34
|
-
const seedeer_name = InputTransformerUtil.toSnakeCase(this.seeder_file_input);
|
|
39
|
+
const seedeer_name = main_1.InputTransformerUtil.toSnakeCase(this.seeder_file_input);
|
|
35
40
|
return `${formatted_migration_priority}_${seedeer_name}_${timestamp}.ts`;
|
|
36
41
|
}
|
|
37
42
|
// Method to format migration priority number for file name
|
|
@@ -40,9 +45,9 @@ class CreateSeederScript {
|
|
|
40
45
|
}
|
|
41
46
|
// Method to create seeder file with content
|
|
42
47
|
createSeederFile(file_path, sedder_file_class_name, table_name) {
|
|
43
|
-
const template = SEQUELIZE_SEEDER_TEMPLATE(sedder_file_class_name, table_name);
|
|
48
|
+
const template = (0, sequelize_code_template_1.SEQUELIZE_SEEDER_TEMPLATE)(sedder_file_class_name, table_name);
|
|
44
49
|
try {
|
|
45
|
-
|
|
50
|
+
fs_1.default.writeFileSync(file_path, template, { encoding: "utf-8" });
|
|
46
51
|
}
|
|
47
52
|
catch (error) {
|
|
48
53
|
this.logger.error(`❌ Failed to write seeder file: ${file_path}`);
|
|
@@ -52,10 +57,10 @@ class CreateSeederScript {
|
|
|
52
57
|
// Method to run create seeder file script
|
|
53
58
|
run() {
|
|
54
59
|
// Transform input to schema file name (snake_case)
|
|
55
|
-
const file_base_name = InputTransformerUtil.toSchemaFileName(this.schema_input);
|
|
56
|
-
const schema_file_path =
|
|
60
|
+
const file_base_name = main_1.InputTransformerUtil.toSchemaFileName(this.schema_input);
|
|
61
|
+
const schema_file_path = path_1.default.join(this.schemas_dir, `${file_base_name}.ts`);
|
|
57
62
|
// Check if schema exists
|
|
58
|
-
if (!
|
|
63
|
+
if (!fs_1.default.existsSync(schema_file_path)) {
|
|
59
64
|
const log_msg = `❌ Schema file not found: ${schema_file_path}`;
|
|
60
65
|
this.logger.error(log_msg);
|
|
61
66
|
throw new Error(log_msg);
|
|
@@ -70,10 +75,10 @@ class CreateSeederScript {
|
|
|
70
75
|
// Generate unique seeder file name with optional custom name or timestamp
|
|
71
76
|
const { model_name, table_name, migration_priority } = schema_module;
|
|
72
77
|
const seeder_file_name = this.generateSeederFileName(migration_priority);
|
|
73
|
-
const file_path =
|
|
74
|
-
const seedeer_file_class_name = InputTransformerUtil.toPascalCase(this.seeder_file_input);
|
|
78
|
+
const file_path = path_1.default.join(this.seeders_dir, seeder_file_name);
|
|
79
|
+
const seedeer_file_class_name = main_1.InputTransformerUtil.toPascalCase(this.seeder_file_input);
|
|
75
80
|
// Ensure seeders directory exists
|
|
76
|
-
InputValidatorUtil.dirExists(this.seeders_dir, true);
|
|
81
|
+
main_1.InputValidatorUtil.dirExists(this.seeders_dir, true);
|
|
77
82
|
this.ensureSeederDoesNotExist(file_path);
|
|
78
83
|
this.createSeederFile(file_path, seedeer_file_class_name, table_name);
|
|
79
84
|
this.logger.success(`✅ Seeder created successfully: ${seeder_file_name}`);
|
|
@@ -83,4 +88,4 @@ class CreateSeederScript {
|
|
|
83
88
|
this.logger.info(`🔢 Migration Priority: ${migration_priority}`);
|
|
84
89
|
}
|
|
85
90
|
}
|
|
86
|
-
|
|
91
|
+
exports.default = CreateSeederScript;
|
|
@@ -1,18 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const fs_1 = __importDefault(require("fs"));
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const constants_1 = require("../../config/constants");
|
|
9
|
+
const sequelize_code_template_1 = require("../../code_templates/sequelize_code_template");
|
|
10
|
+
const main_1 = require("../../utils/main");
|
|
11
|
+
const main_2 = require("../../database/main");
|
|
7
12
|
class MakeMigrationsScript {
|
|
8
13
|
name = "make_migrations_script";
|
|
9
|
-
schemas_dir = SCHEMAS_DIR;
|
|
10
|
-
migrations_dir = MIGRATIONS_DIR;
|
|
11
|
-
snapshot_dir = SCHEMA_SNAPSHOTS_DIR;
|
|
12
|
-
logger = new LoggerUtil(this.name);
|
|
14
|
+
schemas_dir = constants_1.SCHEMAS_DIR;
|
|
15
|
+
migrations_dir = constants_1.MIGRATIONS_DIR;
|
|
16
|
+
snapshot_dir = constants_1.SCHEMA_SNAPSHOTS_DIR;
|
|
17
|
+
logger = new main_1.LoggerUtil(this.name);
|
|
13
18
|
constructor() { }
|
|
14
19
|
getSnapshotPath(model_name) {
|
|
15
|
-
return
|
|
20
|
+
return path_1.default.join(this.snapshot_dir, `${main_1.InputTransformerUtil.toSnakeCase(model_name)}.schema_snapshot.json`);
|
|
16
21
|
}
|
|
17
22
|
// Method to handle generating migration file name
|
|
18
23
|
generateMigrationFilename(table_name, type, migration_priority) {
|
|
@@ -25,8 +30,8 @@ class MakeMigrationsScript {
|
|
|
25
30
|
const snapshot_path = this.getSnapshotPath(model_name);
|
|
26
31
|
try {
|
|
27
32
|
// Ensure snapshot directory exists
|
|
28
|
-
InputValidatorUtil.dirExists(this.snapshot_dir, true);
|
|
29
|
-
|
|
33
|
+
main_1.InputValidatorUtil.dirExists(this.snapshot_dir, true);
|
|
34
|
+
fs_1.default.writeFileSync(snapshot_path, JSON.stringify(normalized_schema, null, 2), { encoding: "utf-8" });
|
|
30
35
|
this.logger.success(`📸 Schema snapshot saved: ${snapshot_path}`);
|
|
31
36
|
return true;
|
|
32
37
|
}
|
|
@@ -39,10 +44,10 @@ class MakeMigrationsScript {
|
|
|
39
44
|
// Method to handle create update schema table migration file
|
|
40
45
|
createUpdateSchemaMigrationFile(file_path, schema_file_path, schema_content, schema_diff) {
|
|
41
46
|
const { table_name, model_name } = schema_content;
|
|
42
|
-
const schema_file_name =
|
|
43
|
-
const template = SEQUELIZE_UPDATE_EXISTING_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE(table_name, model_name, schema_diff, schema_file_name);
|
|
47
|
+
const schema_file_name = path_1.default.basename(schema_file_path);
|
|
48
|
+
const template = (0, sequelize_code_template_1.SEQUELIZE_UPDATE_EXISTING_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE)(table_name, model_name, schema_diff, schema_file_name);
|
|
44
49
|
try {
|
|
45
|
-
|
|
50
|
+
fs_1.default.writeFileSync(file_path, template, { encoding: "utf-8" });
|
|
46
51
|
}
|
|
47
52
|
catch (error) {
|
|
48
53
|
this.logger.error(`❌ Failed to write create update schema migration file: ${file_path}`);
|
|
@@ -53,10 +58,10 @@ class MakeMigrationsScript {
|
|
|
53
58
|
// Method to handle create new schema table migration file
|
|
54
59
|
createNewSchemaMigrationFile(file_path, schema_file_path, schema_content) {
|
|
55
60
|
const { table_name, model_name } = schema_content;
|
|
56
|
-
const schema_file_name =
|
|
57
|
-
const template = SEQUELIZE_CREATE_NEW_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE(table_name, model_name, schema_file_name);
|
|
61
|
+
const schema_file_name = path_1.default.basename(schema_file_path);
|
|
62
|
+
const template = (0, sequelize_code_template_1.SEQUELIZE_CREATE_NEW_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE)(table_name, model_name, schema_file_name);
|
|
58
63
|
try {
|
|
59
|
-
|
|
64
|
+
fs_1.default.writeFileSync(file_path, template, { encoding: "utf-8" });
|
|
60
65
|
}
|
|
61
66
|
catch (error) {
|
|
62
67
|
this.logger.error(`❌ Failed to write create new schema migration file: ${file_path}`);
|
|
@@ -72,21 +77,21 @@ class MakeMigrationsScript {
|
|
|
72
77
|
this.logger.error(`❌ Schema has no table_name: ${schema_file_path}`);
|
|
73
78
|
return;
|
|
74
79
|
}
|
|
75
|
-
const normalized_schema = SchemaNormalizerUtil.normalizeSchema(schema_content);
|
|
80
|
+
const normalized_schema = main_2.SchemaNormalizerUtil.normalizeSchema(schema_content);
|
|
76
81
|
const snapshot_path = this.getSnapshotPath(model_name);
|
|
77
|
-
const operation = !
|
|
82
|
+
const operation = !fs_1.default.existsSync(snapshot_path) ? "create" : "update";
|
|
78
83
|
const migration_file_name = this.generateMigrationFilename(table_name, operation, migration_priority);
|
|
79
|
-
const migration_file_path =
|
|
84
|
+
const migration_file_path = path_1.default.join(this.migrations_dir, migration_file_name);
|
|
80
85
|
let migration_file_created = false;
|
|
81
|
-
if (!
|
|
86
|
+
if (!fs_1.default.existsSync(snapshot_path)) {
|
|
82
87
|
this.logger.info(`Creating Migration File for New schema: ${model_name}Schema`);
|
|
83
88
|
migration_file_created = this.createNewSchemaMigrationFile(migration_file_path, schema_file_path, schema_content);
|
|
84
89
|
}
|
|
85
90
|
else {
|
|
86
91
|
// Existing migration → UPDATE
|
|
87
92
|
this.logger.info(`🔄 Found existing migration snapshots: ${snapshot_path} → checking for updates...`);
|
|
88
|
-
const previous_snapshot = JSON.parse(
|
|
89
|
-
const diff = SchemaDiffUtil.getSchemaDifference(previous_snapshot, normalized_schema);
|
|
93
|
+
const previous_snapshot = JSON.parse(fs_1.default.readFileSync(snapshot_path, "utf-8"));
|
|
94
|
+
const diff = main_2.SchemaDiffUtil.getSchemaDifference(previous_snapshot, normalized_schema);
|
|
90
95
|
if (!diff.table_renamed &&
|
|
91
96
|
!diff.columns.added.length &&
|
|
92
97
|
!diff.columns.removed.length &&
|
|
@@ -115,10 +120,10 @@ class MakeMigrationsScript {
|
|
|
115
120
|
run() {
|
|
116
121
|
this.logger.info(`🔍 Scanning schemas in directory: ${this.schemas_dir}`);
|
|
117
122
|
// Ensure directory exists
|
|
118
|
-
InputValidatorUtil.dirExists(this.schemas_dir, true);
|
|
119
|
-
InputValidatorUtil.dirExists(this.migrations_dir, true);
|
|
123
|
+
main_1.InputValidatorUtil.dirExists(this.schemas_dir, true);
|
|
124
|
+
main_1.InputValidatorUtil.dirExists(this.migrations_dir, true);
|
|
120
125
|
// Fetch all TypeScript schema files
|
|
121
|
-
const schema_files =
|
|
126
|
+
const schema_files = fs_1.default.readdirSync(this.schemas_dir).filter(file => file.endsWith(".ts"));
|
|
122
127
|
if (!schema_files.length) {
|
|
123
128
|
this.logger.info("⚠️ No schema files found.");
|
|
124
129
|
return;
|
|
@@ -127,7 +132,7 @@ class MakeMigrationsScript {
|
|
|
127
132
|
let created_count = 0;
|
|
128
133
|
let failed_count = 0;
|
|
129
134
|
for (const file of schema_files) {
|
|
130
|
-
const file_path =
|
|
135
|
+
const file_path = path_1.default.join(this.schemas_dir, file);
|
|
131
136
|
try {
|
|
132
137
|
// Dynamically import the schema
|
|
133
138
|
// Using require + resolve to get absolute path
|
|
@@ -161,7 +166,7 @@ class MakeMigrationsScript {
|
|
|
161
166
|
this.logger.success(`Failed: ${failed_count}`);
|
|
162
167
|
}
|
|
163
168
|
}
|
|
164
|
-
|
|
169
|
+
exports.default = MakeMigrationsScript;
|
|
165
170
|
// CLI entry point
|
|
166
171
|
try {
|
|
167
172
|
new MakeMigrationsScript().run();
|