@diagramers/cli 1.0.16 → 1.0.18
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 +431 -32
- package/dist/commands/api.d.ts.map +1 -1
- package/dist/commands/api.js +34 -0
- package/dist/commands/api.js.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/services/relation-generator.d.ts +3 -0
- package/dist/services/relation-generator.d.ts.map +1 -0
- package/dist/services/relation-generator.js +212 -0
- package/dist/services/relation-generator.js.map +1 -0
- package/dist/services/table-generator.d.ts +2 -0
- package/dist/services/table-generator.d.ts.map +1 -0
- package/dist/services/table-generator.js +137 -0
- package/dist/services/table-generator.js.map +1 -0
- package/package.json +1 -1
- package/src/commands/api.ts +34 -0
- package/src/index.ts +2 -0
- package/src/services/relation-generator.ts +203 -0
- package/src/services/table-generator.ts +114 -0
@@ -0,0 +1,114 @@
|
|
1
|
+
import * as fs from 'fs';
|
2
|
+
import * as path from 'path';
|
3
|
+
|
4
|
+
export async function generateTable(tableName: string): Promise<void> {
|
5
|
+
// Validate table name
|
6
|
+
if (!tableName || !/^[a-zA-Z][a-zA-Z0-9_-]*$/.test(tableName)) {
|
7
|
+
throw new Error('Invalid table name. Use only letters, numbers, hyphens, and underscores. Must start with a letter.');
|
8
|
+
}
|
9
|
+
|
10
|
+
const tableNameCapitalized = tableName.charAt(0).toUpperCase() + tableName.slice(1);
|
11
|
+
const currentDir = process.cwd();
|
12
|
+
|
13
|
+
// Check for required API project structure
|
14
|
+
const requiredDirs = [
|
15
|
+
'src/entities',
|
16
|
+
'src/schemas'
|
17
|
+
];
|
18
|
+
const missingDirs = requiredDirs.filter(dir => !fs.existsSync(path.join(currentDir, dir)));
|
19
|
+
if (missingDirs.length > 0) {
|
20
|
+
throw new Error(`This command should be run from a diagramers API project. Missing directories: ${missingDirs.join(', ')}`);
|
21
|
+
}
|
22
|
+
|
23
|
+
console.log('📝 Creating entity...');
|
24
|
+
const entityContent = generateEntityContent(tableName, tableNameCapitalized);
|
25
|
+
fs.writeFileSync(path.join(currentDir, 'src/entities', `${tableName}.ts`), entityContent);
|
26
|
+
|
27
|
+
console.log('📋 Creating schema...');
|
28
|
+
const schemaContent = generateSchemaContent(tableName, tableNameCapitalized);
|
29
|
+
fs.writeFileSync(path.join(currentDir, 'src/schemas', `${tableName}.ts`), schemaContent);
|
30
|
+
|
31
|
+
// Update dbcontext to include the new entity
|
32
|
+
updateDbContext(currentDir, tableName, tableNameCapitalized);
|
33
|
+
|
34
|
+
console.log('✅ Table generation completed!');
|
35
|
+
console.log(`📊 Entity: src/entities/${tableName}.ts`);
|
36
|
+
console.log(`📋 Schema: src/schemas/${tableName}.ts`);
|
37
|
+
console.log('🔄 dbcontext.ts updated');
|
38
|
+
}
|
39
|
+
|
40
|
+
function generateEntityContent(tableName: string, tableNameCapitalized: string): string {
|
41
|
+
return `import * as mongoose from 'mongoose';
|
42
|
+
import { ObjectId } from "bson";
|
43
|
+
|
44
|
+
export interface I${tableNameCapitalized} extends mongoose.Document {
|
45
|
+
_id: ObjectId,
|
46
|
+
name: string,
|
47
|
+
description?: string,
|
48
|
+
status: number,
|
49
|
+
createdAt: Date,
|
50
|
+
updatedAt: Date
|
51
|
+
}`;
|
52
|
+
}
|
53
|
+
|
54
|
+
function generateSchemaContent(tableName: string, tableNameCapitalized: string): string {
|
55
|
+
return `import * as mongoose from 'mongoose';
|
56
|
+
import { I${tableNameCapitalized} } from '../entities/${tableName}';
|
57
|
+
|
58
|
+
export const ${tableName}Schema = new mongoose.Schema(
|
59
|
+
{
|
60
|
+
name: {
|
61
|
+
type: mongoose.SchemaTypes.String,
|
62
|
+
required: true,
|
63
|
+
},
|
64
|
+
description: {
|
65
|
+
type: mongoose.SchemaTypes.String,
|
66
|
+
required: false,
|
67
|
+
},
|
68
|
+
status: {
|
69
|
+
type: mongoose.SchemaTypes.Number,
|
70
|
+
default: 1,
|
71
|
+
}
|
72
|
+
},
|
73
|
+
{ timestamps: true, suppressReservedKeysWarning: true },
|
74
|
+
);
|
75
|
+
|
76
|
+
export const ${tableNameCapitalized}Entity = mongoose.model<I${tableNameCapitalized}>('${tableName}', ${tableName}Schema);`;
|
77
|
+
}
|
78
|
+
|
79
|
+
function updateDbContext(currentDir: string, tableName: string, tableNameCapitalized: string): void {
|
80
|
+
const dbcontextPath = path.join(currentDir, 'src/helpers/dbcontext.ts');
|
81
|
+
if (!fs.existsSync(dbcontextPath)) {
|
82
|
+
console.log('⚠️ dbcontext.ts not found, skipping dbcontext update');
|
83
|
+
return;
|
84
|
+
}
|
85
|
+
|
86
|
+
let dbcontextContent = fs.readFileSync(dbcontextPath, 'utf8');
|
87
|
+
|
88
|
+
// Add import
|
89
|
+
const importStatement = `import { ${tableNameCapitalized}Entity } from '../schemas/${tableName}';`;
|
90
|
+
if (!dbcontextContent.includes(importStatement)) {
|
91
|
+
// Find the last import statement and add after it
|
92
|
+
const importRegex = /import.*from.*['"];?\s*$/gm;
|
93
|
+
const matches = [...dbcontextContent.matchAll(importRegex)];
|
94
|
+
if (matches.length > 0) {
|
95
|
+
const lastImport = matches[matches.length - 1];
|
96
|
+
const insertIndex = lastImport.index! + lastImport[0].length;
|
97
|
+
dbcontextContent = dbcontextContent.slice(0, insertIndex) + '\n' + importStatement + dbcontextContent.slice(insertIndex);
|
98
|
+
}
|
99
|
+
}
|
100
|
+
|
101
|
+
// Add to dbcontext object
|
102
|
+
const dbcontextObjectRegex = /export\s+default\s*\{([^}]*)\}/s;
|
103
|
+
const match = dbcontextContent.match(dbcontextObjectRegex);
|
104
|
+
if (match) {
|
105
|
+
const objectContent = match[1];
|
106
|
+
if (!objectContent.includes(`${tableNameCapitalized}Entity`)) {
|
107
|
+
const newObjectContent = objectContent.trim() + `,\n ${tableNameCapitalized}Entity`;
|
108
|
+
dbcontextContent = dbcontextContent.replace(dbcontextObjectRegex, `export default {$1${newObjectContent}\n}`);
|
109
|
+
}
|
110
|
+
}
|
111
|
+
|
112
|
+
fs.writeFileSync(dbcontextPath, dbcontextContent);
|
113
|
+
console.log('📊 Updated dbcontext.ts');
|
114
|
+
}
|