go-duck-cli 1.1.23 → 1.1.24
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/generators/migrations.js +12 -4
- package/index.js +4 -1
- package/package.json +1 -1
package/generators/migrations.js
CHANGED
|
@@ -3,6 +3,14 @@ import path from 'path';
|
|
|
3
3
|
import chalk from 'chalk';
|
|
4
4
|
import { toLiquibaseType } from '../parser/gdl.js';
|
|
5
5
|
|
|
6
|
+
const toSnakeCase = (str) => {
|
|
7
|
+
if (!str) return '';
|
|
8
|
+
return str
|
|
9
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1_$2')
|
|
10
|
+
.replace(/([A-Z])([A-Z][a-z])/g, '$1_$2')
|
|
11
|
+
.toLowerCase();
|
|
12
|
+
};
|
|
13
|
+
|
|
6
14
|
export const generateLiquibaseChangelogs = async (entities, relationships, projectRootDir, delta = null, enums = []) => {
|
|
7
15
|
const migrationsDir = path.join(projectRootDir, 'migrations');
|
|
8
16
|
const sqlDir = path.join(migrationsDir, 'sql');
|
|
@@ -87,11 +95,11 @@ DROP TABLE IF EXISTS api_usage CASCADE;
|
|
|
87
95
|
if (field.required) constraints.push('NOT NULL');
|
|
88
96
|
if (field.unique) constraints.push('UNIQUE');
|
|
89
97
|
|
|
90
|
-
columns += `,\n ${field.name
|
|
98
|
+
columns += `,\n ${toSnakeCase(field.name)} ${sqlType} ${constraints.join(' ')}`;
|
|
91
99
|
}
|
|
92
100
|
|
|
93
101
|
// Auditing / Timestamp columns
|
|
94
|
-
if (entity.
|
|
102
|
+
if (entity.isAudited) {
|
|
95
103
|
columns += `,\n created_by VARCHAR(255),\n created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n last_modified_by VARCHAR(255),\n last_modified_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n last_modified_user_id VARCHAR(255)`;
|
|
96
104
|
} else {
|
|
97
105
|
columns += `,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP`;
|
|
@@ -111,8 +119,8 @@ DROP TABLE IF EXISTS api_usage CASCADE;
|
|
|
111
119
|
if (field.required) constraints.push('NOT NULL');
|
|
112
120
|
if (field.unique) constraints.push('UNIQUE');
|
|
113
121
|
|
|
114
|
-
sqlUp += `ALTER TABLE ${entityName.toLowerCase()} ADD COLUMN IF NOT EXISTS ${field.name
|
|
115
|
-
sqlDown += `ALTER TABLE ${entityName.toLowerCase()} DROP COLUMN IF EXISTS ${field.name
|
|
122
|
+
sqlUp += `ALTER TABLE ${entityName.toLowerCase()} ADD COLUMN IF NOT EXISTS ${toSnakeCase(field.name)} ${sqlType} ${constraints.join(' ')};\n`;
|
|
123
|
+
sqlDown += `ALTER TABLE ${entityName.toLowerCase()} DROP COLUMN IF EXISTS ${toSnakeCase(field.name)};\n`;
|
|
116
124
|
}
|
|
117
125
|
}
|
|
118
126
|
}
|
package/index.js
CHANGED
|
@@ -288,10 +288,13 @@ Handlebars.registerPartial('renderFields', `
|
|
|
288
288
|
{{/each}}
|
|
289
289
|
`);
|
|
290
290
|
|
|
291
|
+
const pkgPath = path.resolve(path.dirname(import.meta.url.replace('file://', '')), 'package.json');
|
|
292
|
+
const pkg = fs.readJsonSync(pkgPath);
|
|
293
|
+
|
|
291
294
|
program
|
|
292
295
|
.name('go-duck-cli')
|
|
293
296
|
.description('A powerful Go code generator for microservices')
|
|
294
|
-
.version('1.0.0');
|
|
297
|
+
.version(pkg.version || '1.0.0');
|
|
295
298
|
|
|
296
299
|
// Helper to load configuration
|
|
297
300
|
const loadConfig = async (configPath) => {
|