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