@an5/orm 1.0.6 → 1.0.8
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 +175 -147
- package/dist/an5Metadata.d.ts +10 -0
- package/dist/an5Metadata.js +29 -0
- package/dist/an5Orm.d.ts +4 -1
- package/dist/an5Orm.js +214 -409
- package/dist/config.d.ts +2 -0
- package/dist/config.js +24 -0
- package/dist/errors.d.ts +14 -0
- package/dist/errors.js +16 -0
- package/dist/generator/src/code-generator.d.ts +13 -13
- package/dist/generator/src/code-generator.js +225 -225
- package/dist/generator/src/dotnet-generator.d.ts +11 -11
- package/dist/generator/src/dotnet-generator.js +546 -546
- package/dist/generator/src/index.d.ts +1 -1
- package/dist/generator/src/index.js +88 -88
- package/dist/generator/src/metadata-generator.d.ts +8 -8
- package/dist/generator/src/metadata-generator.js +90 -90
- package/dist/generator/src/parser.d.ts +11 -11
- package/dist/generator/src/parser.js +206 -206
- package/dist/generator/src/python-generator.d.ts +9 -9
- package/dist/generator/src/python-generator.js +105 -105
- package/dist/generator/src/types.d.ts +27 -27
- package/dist/generator/src/types.js +2 -2
- package/dist/index.d.ts +3 -0
- package/dist/index.js +5 -1
- package/dist/logger.d.ts +53 -0
- package/dist/logger.js +117 -0
- package/dist/metadata.d.ts +24 -0
- package/dist/metadata.js +15 -0
- package/dist/scripts/release-cli.d.ts +2 -2
- package/dist/scripts/release-cli.js +115 -115
- package/dist/sql-utils.d.ts +29 -0
- package/dist/sql-utils.js +220 -0
- package/generator/src/code-generator.ts +230 -234
- package/generator/src/dotnet-generator.ts +544 -544
- package/generator/src/index.ts +116 -95
- package/generator/src/metadata-generator.ts +101 -99
- package/generator/src/parser.ts +202 -202
- package/generator/src/python-generator.ts +114 -114
- package/generator/src/types.ts +29 -29
- package/package.json +5 -7
- package/dist/cleanup.d.ts +0 -2
- package/dist/cleanup.js +0 -120
- package/dist/cli.js +0 -64
- package/dist/migrate.d.ts +0 -11
- package/dist/migrate.js +0 -301
- package/dist/pull.d.ts +0 -2
- package/dist/pull.js +0 -339
- package/dist/push.d.ts +0 -2
- package/dist/push.js +0 -281
- package/dist/seed.d.ts +0 -2
- package/dist/seed.js +0 -401
package/generator/src/index.ts
CHANGED
|
@@ -1,95 +1,116 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import { SchemaParser } from './parser';
|
|
3
|
-
import { CodeGenerator } from './code-generator';
|
|
4
|
-
import { MetadataGenerator } from './metadata-generator';
|
|
5
|
-
import { PythonGenerator } from './python-generator';
|
|
6
|
-
import { DotnetGenerator } from './dotnet-generator';
|
|
7
|
-
|
|
8
|
-
import fs from 'fs';
|
|
9
|
-
|
|
10
|
-
function clearGeneratedFiles(outputDir: string, extension: string) {
|
|
11
|
-
if (!fs.existsSync(outputDir)) {
|
|
12
|
-
fs.mkdirSync(outputDir, { recursive: true });
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
for (const entry of fs.readdirSync(outputDir)) {
|
|
17
|
-
const fullPath = path.join(outputDir, entry);
|
|
18
|
-
const stat = fs.statSync(fullPath);
|
|
19
|
-
if (stat.isDirectory()) continue;
|
|
20
|
-
if (entry.endsWith(extension)) {
|
|
21
|
-
fs.unlinkSync(fullPath);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
async function main() {
|
|
27
|
-
|
|
28
|
-
let config: any = {};
|
|
29
|
-
|
|
30
|
-
try {
|
|
31
|
-
let configPath = path.join(rootDir, 'an5Orm.config.js');
|
|
32
|
-
if (!fs.existsSync(configPath)) {
|
|
33
|
-
configPath = path.join(rootDir, 'an5Orm.config.cjs');
|
|
34
|
-
}
|
|
35
|
-
if (fs.existsSync(configPath)) {
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { SchemaParser } from './parser';
|
|
3
|
+
import { CodeGenerator } from './code-generator';
|
|
4
|
+
import { MetadataGenerator } from './metadata-generator';
|
|
5
|
+
import { PythonGenerator } from './python-generator';
|
|
6
|
+
import { DotnetGenerator } from './dotnet-generator';
|
|
7
|
+
|
|
8
|
+
import fs from 'fs';
|
|
9
|
+
|
|
10
|
+
function clearGeneratedFiles(outputDir: string, extension: string) {
|
|
11
|
+
if (!fs.existsSync(outputDir)) {
|
|
12
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
for (const entry of fs.readdirSync(outputDir)) {
|
|
17
|
+
const fullPath = path.join(outputDir, entry);
|
|
18
|
+
const stat = fs.statSync(fullPath);
|
|
19
|
+
if (stat.isDirectory()) continue;
|
|
20
|
+
if (entry.endsWith(extension)) {
|
|
21
|
+
fs.unlinkSync(fullPath);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function main() {
|
|
27
|
+
let rootDir = process.cwd();
|
|
28
|
+
let config: any = {};
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
let configPath = path.join(rootDir, 'an5Orm.config.js');
|
|
32
|
+
if (!fs.existsSync(configPath)) {
|
|
33
|
+
configPath = path.join(rootDir, 'an5Orm.config.cjs');
|
|
34
|
+
}
|
|
35
|
+
if (!fs.existsSync(configPath)) {
|
|
36
|
+
configPath = path.join(rootDir, '..', 'an5Orm.config.js');
|
|
37
|
+
}
|
|
38
|
+
if (!fs.existsSync(configPath)) {
|
|
39
|
+
configPath = path.join(rootDir, '..', 'an5Orm.config.cjs');
|
|
40
|
+
}
|
|
41
|
+
if (fs.existsSync(configPath)) {
|
|
42
|
+
config = require(configPath);
|
|
43
|
+
rootDir = path.dirname(configPath);
|
|
44
|
+
} else if (fs.existsSync(path.join(rootDir, '..', 'an5Schema'))) {
|
|
45
|
+
rootDir = path.resolve(rootDir, '..');
|
|
46
|
+
}
|
|
47
|
+
} catch (err) {
|
|
48
|
+
console.warn('⚠️ Could not load an5Orm.config.js/.cjs, using defaults.', err);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const schemaDir = path.resolve(rootDir, config.schemaDir || 'an5Schema');
|
|
52
|
+
const outputTypesDir = path.resolve(rootDir, config.outputs?.typescript?.outputDir || 'an5Client/typescript');
|
|
53
|
+
const outputMetadataPath = path.resolve(rootDir, config.outputs?.typescript?.metadataFile || 'an5Client/typescript/an5Metadata.ts');
|
|
54
|
+
const outputOrmMetadataPath = path.resolve(rootDir, config.outputs?.typescript?.ormMetadataFile || 'an5Orm/an5Metadata.ts');
|
|
55
|
+
const outputPythonMetadataPath = path.resolve(rootDir, config.outputs?.python?.metadataFile || 'an5Client/python/an5_metadata.py');
|
|
56
|
+
const outputDotnetDir = path.resolve(rootDir, config.outputs?.dotnet?.outputDir || 'an5Client/dotnet');
|
|
57
|
+
|
|
58
|
+
console.log('🚀 Starting ORM generation...');
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
clearGeneratedFiles(outputTypesDir, '.ts');
|
|
62
|
+
clearGeneratedFiles(outputDotnetDir, '.cs');
|
|
63
|
+
if (fs.existsSync(outputMetadataPath)) {
|
|
64
|
+
fs.unlinkSync(outputMetadataPath);
|
|
65
|
+
}
|
|
66
|
+
if (fs.existsSync(outputPythonMetadataPath)) {
|
|
67
|
+
fs.unlinkSync(outputPythonMetadataPath);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const parser = new SchemaParser(schemaDir);
|
|
71
|
+
const models = await parser.parse();
|
|
72
|
+
console.log(`📦 Parsed ${models.length} models from schema.`);
|
|
73
|
+
|
|
74
|
+
const codeGen = new CodeGenerator(outputTypesDir);
|
|
75
|
+
codeGen.generate(models);
|
|
76
|
+
console.log(`✨ Generated modular types in ${outputTypesDir}`);
|
|
77
|
+
|
|
78
|
+
const metadataDir = path.dirname(outputMetadataPath);
|
|
79
|
+
if (!fs.existsSync(metadataDir)) {
|
|
80
|
+
fs.mkdirSync(metadataDir, { recursive: true });
|
|
81
|
+
}
|
|
82
|
+
const metadataGen = new MetadataGenerator(outputMetadataPath);
|
|
83
|
+
metadataGen.generate(models);
|
|
84
|
+
console.log(`✨ Generated metadata in ${outputMetadataPath}`);
|
|
85
|
+
|
|
86
|
+
const ormMetadataDir = path.dirname(outputOrmMetadataPath);
|
|
87
|
+
if (!fs.existsSync(ormMetadataDir)) {
|
|
88
|
+
fs.mkdirSync(ormMetadataDir, { recursive: true });
|
|
89
|
+
}
|
|
90
|
+
const ormMetadataGen = new MetadataGenerator(
|
|
91
|
+
outputOrmMetadataPath,
|
|
92
|
+
'import type { RelationDef } from "./metadata";'
|
|
93
|
+
);
|
|
94
|
+
ormMetadataGen.generate(models);
|
|
95
|
+
console.log(`✨ Generated ORM-local metadata in ${outputOrmMetadataPath}`);
|
|
96
|
+
|
|
97
|
+
const pythonDir = path.dirname(outputPythonMetadataPath);
|
|
98
|
+
if (!fs.existsSync(pythonDir)) {
|
|
99
|
+
fs.mkdirSync(pythonDir, { recursive: true });
|
|
100
|
+
}
|
|
101
|
+
const pythonGen = new PythonGenerator(outputPythonMetadataPath);
|
|
102
|
+
pythonGen.generate(models);
|
|
103
|
+
console.log(`✨ Generated Python metadata in ${outputPythonMetadataPath}`);
|
|
104
|
+
|
|
105
|
+
const dotnetGen = new DotnetGenerator(outputDotnetDir);
|
|
106
|
+
dotnetGen.generate(models);
|
|
107
|
+
console.log(`✨ Generated .NET models in ${outputDotnetDir}`);
|
|
108
|
+
|
|
109
|
+
console.log('✅ ORM generation completed successfully.');
|
|
110
|
+
} catch (error) {
|
|
111
|
+
console.error('❌ ORM generation failed:', error);
|
|
112
|
+
process.exit(1);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
main();
|
|
@@ -1,99 +1,101 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import { Model } from './types';
|
|
4
|
-
|
|
5
|
-
export class MetadataGenerator {
|
|
6
|
-
constructor(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
metaContent += '
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
//
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { Model } from './types';
|
|
4
|
+
|
|
5
|
+
export class MetadataGenerator {
|
|
6
|
+
constructor(
|
|
7
|
+
private outputPath: string,
|
|
8
|
+
private relationImport: string = 'import type { RelationDef } from "@an5/orm";'
|
|
9
|
+
) {}
|
|
10
|
+
|
|
11
|
+
public generate(models: Model[]) {
|
|
12
|
+
let metaContent = '// This file is auto-generated. Do not edit directly.\n\n';
|
|
13
|
+
metaContent += `${this.relationImport}\n\n`;
|
|
14
|
+
|
|
15
|
+
metaContent += 'export const modelToTable: Record<string, string> = {\n';
|
|
16
|
+
for (const model of models) {
|
|
17
|
+
const props = this.getAllPropertyVariations(model.name);
|
|
18
|
+
const fullTableName = `[${model.schemaName}].[${model.tableName}]`;
|
|
19
|
+
for (const prop of props) {
|
|
20
|
+
metaContent += ` ${prop}: "${fullTableName}",\n`;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
metaContent += '};\n\n';
|
|
24
|
+
|
|
25
|
+
metaContent += 'export const modelDescriptions: Record<string, string | undefined> = {\n';
|
|
26
|
+
for (const model of models) {
|
|
27
|
+
const props = this.getAllPropertyVariations(model.name);
|
|
28
|
+
for (const prop of props) {
|
|
29
|
+
metaContent += ` ${prop}: ${model.description ? JSON.stringify(model.description) : 'undefined'},\n`;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
metaContent += '};\n\n';
|
|
33
|
+
|
|
34
|
+
metaContent += 'export const modelFields: Record<string, Record<string, { ts: string; sql: string; description?: string }>> = {\n';
|
|
35
|
+
for (const model of models) {
|
|
36
|
+
const props = this.getAllPropertyVariations(model.name);
|
|
37
|
+
const fieldsStr = `{ ${model.fields.map(f => `${f.name}: ${this.formatFieldMetadata(f)}`).join(', ')} }`;
|
|
38
|
+
for (const prop of props) {
|
|
39
|
+
metaContent += ` ${prop}: ${fieldsStr},\n`;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
metaContent += '};\n\n';
|
|
43
|
+
|
|
44
|
+
metaContent += 'export const relationMap: Record<string, Record<string, RelationDef>> = {\n';
|
|
45
|
+
for (const model of models) {
|
|
46
|
+
const props = this.getAllPropertyVariations(model.name);
|
|
47
|
+
let relationsContent = ` {\n`;
|
|
48
|
+
for (const rel of model.relations) {
|
|
49
|
+
relationsContent += ` ${rel.name}: { modelName: "${this.toCamelCase(rel.type)}", relationType: "${rel.isArray ? 'many' : 'one'}", foreignKey: "${rel.foreignKey || 'id'}", localKey: "${rel.localKey || 'id'}" },\n`;
|
|
50
|
+
}
|
|
51
|
+
relationsContent += ` }`;
|
|
52
|
+
|
|
53
|
+
for (const prop of props) {
|
|
54
|
+
metaContent += ` ${prop}: ${relationsContent},\n`;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
metaContent += '};\n';
|
|
58
|
+
|
|
59
|
+
fs.writeFileSync(this.outputPath, metaContent);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private getAllPropertyVariations(modelName: string): string[] {
|
|
63
|
+
const variations = new Set<string>();
|
|
64
|
+
|
|
65
|
+
// 1. Standard camelCase (e.g. User -> user, McpServer -> mcpServer)
|
|
66
|
+
variations.add(this.toCamelCase(modelName));
|
|
67
|
+
|
|
68
|
+
// 2. Handle known acronyms at start (e.g. LLMProvider -> lLMProvider)
|
|
69
|
+
// This is already handled by toCamelCase if it only lowercases the first letter.
|
|
70
|
+
// But we might want 'llmProvider' as well.
|
|
71
|
+
const acronyms = ['LLM', 'AI', 'MCP', 'IT', 'QC', 'HR', 'MR', 'WH', 'SSIS', 'API', 'URL', 'ID', 'JSON'];
|
|
72
|
+
|
|
73
|
+
for (const acronym of acronyms) {
|
|
74
|
+
if (modelName.startsWith(acronym)) {
|
|
75
|
+
// e.g. LLMProvider -> llmProvider
|
|
76
|
+
variations.add(acronym.toLowerCase() + modelName.slice(acronym.length));
|
|
77
|
+
|
|
78
|
+
// e.g. LLMProvider -> lLMProvider (Prisma style)
|
|
79
|
+
variations.add(acronym[0].toLowerCase() + acronym.slice(1) + modelName.slice(acronym.length));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return Array.from(variations);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private formatFieldMetadata(field: Model['fields'][number]): string {
|
|
87
|
+
const entries = [
|
|
88
|
+
`ts: ${JSON.stringify(`${field.type}${field.isOptional ? '?' : ''}`)}`,
|
|
89
|
+
`sql: ${JSON.stringify(field.sqlType)}`,
|
|
90
|
+
];
|
|
91
|
+
if (field.description) {
|
|
92
|
+
entries.push(`description: ${JSON.stringify(field.description)}`);
|
|
93
|
+
}
|
|
94
|
+
return `{ ${entries.join(', ')} }`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private toCamelCase(str: string): string {
|
|
98
|
+
if (!str) return '';
|
|
99
|
+
return str[0].toLowerCase() + str.slice(1);
|
|
100
|
+
}
|
|
101
|
+
}
|