@an5/orm 1.0.8 → 1.0.9

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 (38) hide show
  1. package/README.md +167 -175
  2. package/dist/an5Metadata.js +64 -0
  3. package/dist/an5Orm.d.ts +92 -3
  4. package/dist/an5Orm.js +697 -206
  5. package/dist/errors.d.ts +1 -1
  6. package/dist/errors.js +1 -1
  7. package/dist/generator/src/code-generator.d.ts +14 -13
  8. package/dist/generator/src/code-generator.js +249 -225
  9. package/dist/generator/src/dotnet-generator.d.ts +13 -11
  10. package/dist/generator/src/dotnet-generator.js +872 -546
  11. package/dist/generator/src/index.d.ts +1 -1
  12. package/dist/generator/src/index.js +104 -88
  13. package/dist/generator/src/metadata-generator.d.ts +11 -8
  14. package/dist/generator/src/metadata-generator.js +104 -90
  15. package/dist/generator/src/parser.d.ts +11 -11
  16. package/dist/generator/src/parser.js +206 -206
  17. package/dist/generator/src/python-generator.d.ts +19 -9
  18. package/dist/generator/src/python-generator.js +345 -105
  19. package/dist/generator/src/types.d.ts +27 -27
  20. package/dist/generator/src/types.js +2 -2
  21. package/dist/index.d.ts +3 -2
  22. package/dist/index.js +19 -8
  23. package/dist/logger.js +1 -1
  24. package/dist/migration-core.d.ts +87 -0
  25. package/dist/migration-core.js +692 -0
  26. package/dist/scripts/release-cli.d.ts +2 -2
  27. package/dist/scripts/release-cli.js +115 -115
  28. package/dist/sql-utils.d.ts +3 -1
  29. package/dist/sql-utils.js +94 -24
  30. package/generator/src/code-generator.ts +259 -230
  31. package/generator/src/dotnet-generator.ts +882 -544
  32. package/generator/src/golang-generator.ts +1242 -0
  33. package/generator/src/index.ts +111 -116
  34. package/generator/src/metadata-generator.ts +116 -101
  35. package/generator/src/parser.ts +202 -202
  36. package/generator/src/python-generator.ts +424 -114
  37. package/generator/src/types.ts +29 -29
  38. package/package.json +12 -6
@@ -1,202 +1,202 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import { Model, Field, Relation } from './types';
4
-
5
- // SQL Server type → TypeScript type mapping
6
- const AN5_TO_TS: Record<string, string> = {
7
- // String types
8
- 'NVARCHAR': 'string',
9
- 'VARCHAR': 'string',
10
- 'CHAR': 'string',
11
- 'NCHAR': 'string',
12
- 'TEXT': 'string',
13
- 'NTEXT': 'string',
14
- 'XML': 'string',
15
- // Numeric types
16
- 'INT': 'number',
17
- 'SMALLINT': 'number',
18
- 'TINYINT': 'number',
19
- 'BIGINT': 'number | bigint',
20
- 'FLOAT': 'number',
21
- 'REAL': 'number',
22
- 'DECIMAL': 'number',
23
- 'NUMERIC': 'number',
24
- 'MONEY': 'number',
25
- 'SMALLMONEY': 'number',
26
- // Boolean
27
- 'BIT': 'boolean',
28
- // Date types
29
- 'DATETIME': 'Date',
30
- 'DATETIME2': 'Date',
31
- 'SMALLDATETIME': 'Date',
32
- 'DATE': 'Date',
33
- 'TIME': 'Date',
34
- 'DATETIMEOFFSET': 'Date',
35
- // Binary types
36
- 'VARBINARY': 'Buffer',
37
- 'BINARY': 'Buffer',
38
- 'IMAGE': 'Buffer',
39
- // Other
40
- 'UNIQUEIDENTIFIER': 'string',
41
- 'SQL_VARIANT': 'any',
42
- 'ROWVERSION': 'Buffer',
43
- 'HIERARCHYID': 'string',
44
- 'GEOGRAPHY': 'string',
45
- 'GEOMETRY': 'string',
46
- 'VECTOR': 'number[] | string',
47
- };
48
-
49
- // Parse base type from "NVARCHAR(255)" → "NVARCHAR"
50
- function parseSqlType(raw: string): { base: string; params: string } {
51
- const match = raw.match(/^(\w+)(?:\((.+)\))?$/);
52
- if (!match) return { base: raw, params: '' };
53
- return { base: match[1].toUpperCase(), params: match[2] || '' };
54
- }
55
-
56
- // Map SQL Server type to TypeScript type
57
- export function sqlTypeToTs(sqlType: string): string {
58
- const { base } = parseSqlType(sqlType);
59
- return AN5_TO_TS[base] || 'any';
60
- }
61
-
62
- export class SchemaParser {
63
- private schemaText: string = '';
64
-
65
- constructor(private schemaDir: string) {}
66
-
67
- public async parse(): Promise<Model[]> {
68
- this.loadSchema();
69
- const lines = this.schemaText.split('\n');
70
- const models: Model[] = [];
71
- let currentModel: Model | null = null;
72
-
73
- for (let line of lines) {
74
- line = line.trim();
75
- if (!line || line.startsWith('//')) continue;
76
-
77
- const modelHeaderMatch = line.match(/^model\s+(\w+)\s*\{/);
78
- if (modelHeaderMatch) {
79
- const modelName = modelHeaderMatch[1];
80
- currentModel = {
81
- name: modelName,
82
- tableName: modelName.toLowerCase() + 's',
83
- schemaName: 'dbo',
84
- fields: [],
85
- relations: []
86
- };
87
- models.push(currentModel);
88
- continue;
89
- }
90
-
91
- if (line === '}') {
92
- currentModel = null;
93
- continue;
94
- }
95
-
96
- if (currentModel) {
97
- this.parseModelLine(line, currentModel);
98
- }
99
- }
100
-
101
- this.postProcessRelations(models);
102
- return models;
103
- }
104
-
105
- private loadSchema() {
106
- if (fs.existsSync(this.schemaDir)) {
107
- const files = fs.readdirSync(this.schemaDir).filter(f => f.endsWith('.an5'));
108
- for (const file of files) {
109
- this.schemaText += fs.readFileSync(path.join(this.schemaDir, file), 'utf8') + '\n';
110
- }
111
- } else {
112
- throw new Error(`No schema directory found at ${this.schemaDir}`);
113
- }
114
- }
115
-
116
- private parseModelLine(line: string, model: Model) {
117
- if (line.startsWith('@@map')) {
118
- const mapMatch = line.match(/@@map\("(.+)"\)/);
119
- if (mapMatch) model.tableName = mapMatch[1];
120
- return;
121
- }
122
- if (line.startsWith('@@schema')) {
123
- const schemaMatch = line.match(/@@schema\("(.+)"\)/);
124
- if (schemaMatch) model.schemaName = schemaMatch[1];
125
- return;
126
- }
127
- if (line.startsWith('@@unique')) {
128
- const uniqueMatch = line.match(/@@unique\(\[([\w,\s]+)\]\)/);
129
- if (uniqueMatch) {
130
- const fields = uniqueMatch[1].split(',').map(f => f.trim());
131
- model.compoundUniques = model.compoundUniques || [];
132
- model.compoundUniques.push(fields);
133
- }
134
- return;
135
- }
136
- if (line.startsWith('@@description')) {
137
- const descMatch = line.match(/@@description\("(.+)"\)/);
138
- if (descMatch) model.description = descMatch[1];
139
- return;
140
- }
141
- if (line.startsWith('@@')) return;
142
-
143
- const parts = line.split(/\s+/);
144
- const fieldName = parts[0];
145
- let fieldType = parts[1];
146
- if (!fieldName || !fieldType) return;
147
-
148
- const isArray = fieldType.endsWith('[]');
149
- const isOptional = fieldType.endsWith('?');
150
- const cleanType = fieldType.replace('[]', '').replace('?', '');
151
-
152
- // Parse SQL Server type (e.g., "NVARCHAR(255)" → base="NVARCHAR")
153
- const { base: sqlBase } = parseSqlType(cleanType);
154
-
155
- let tsType = 'any';
156
- let isRelation = false;
157
-
158
- // Check if it's a known SQL Server type
159
- if (AN5_TO_TS[sqlBase]) {
160
- tsType = sqlTypeToTs(cleanType);
161
- } else if (cleanType[0] === cleanType[0].toUpperCase() && !cleanType.includes('(')) {
162
- // Uppercase without parens = likely a relation to another model
163
- tsType = cleanType;
164
- isRelation = true;
165
- }
166
-
167
- if (isRelation) {
168
- let foreignKey = '', localKey = '', relationName = '';
169
- const nameMatch = line.match(/@relation\("(\w+)"/);
170
- if (nameMatch) relationName = nameMatch[1];
171
- const relationMatch = line.match(/@relation\((?:.*fields:\s*\[(\w+)\],)?\s*(?:.*references:\s*\[(\w+)\],?)?.*\)/);
172
- if (relationMatch) {
173
- foreignKey = relationMatch[1] || '';
174
- localKey = relationMatch[2] || '';
175
- }
176
- model.relations.push({ name: fieldName, type: tsType, isArray, isOptional, foreignKey, localKey, relationName });
177
- } else {
178
- const hasDefault = line.includes('@default') || line.includes('@updatedAt') || line.includes('@id');
179
- const isId = line.includes('@id');
180
- let description: string | undefined;
181
- const descMatch = line.match(/@description\("(.+)"\)/);
182
- if (descMatch) description = descMatch[1];
183
- model.fields.push({ name: fieldName, type: tsType, sqlType: cleanType, isOptional, hasDefault, isId, description });
184
- }
185
- }
186
-
187
- private postProcessRelations(models: Model[]) {
188
- for (const model of models) {
189
- for (const rel of model.relations) {
190
- if (!rel.foreignKey || !rel.localKey) {
191
- const targetModel = models.find(m => m.name === rel.type);
192
- if (targetModel) {
193
- let opposite = rel.relationName ?
194
- targetModel.relations.find(r => r.type === model.name && r.relationName === rel.relationName && r.foreignKey && r.localKey) :
195
- targetModel.relations.find(r => r.type === model.name && r.foreignKey && r.localKey);
196
- if (opposite) { rel.foreignKey = opposite.foreignKey; rel.localKey = opposite.localKey; }
197
- }
198
- }
199
- }
200
- }
201
- }
202
- }
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { Model, Field, Relation } from './types';
4
+
5
+ // SQL Server type → TypeScript type mapping
6
+ const AN5_TO_TS: Record<string, string> = {
7
+ // String types
8
+ 'NVARCHAR': 'string',
9
+ 'VARCHAR': 'string',
10
+ 'CHAR': 'string',
11
+ 'NCHAR': 'string',
12
+ 'TEXT': 'string',
13
+ 'NTEXT': 'string',
14
+ 'XML': 'string',
15
+ // Numeric types
16
+ 'INT': 'number',
17
+ 'SMALLINT': 'number',
18
+ 'TINYINT': 'number',
19
+ 'BIGINT': 'number | bigint',
20
+ 'FLOAT': 'number',
21
+ 'REAL': 'number',
22
+ 'DECIMAL': 'number',
23
+ 'NUMERIC': 'number',
24
+ 'MONEY': 'number',
25
+ 'SMALLMONEY': 'number',
26
+ // Boolean
27
+ 'BIT': 'boolean',
28
+ // Date types
29
+ 'DATETIME': 'Date',
30
+ 'DATETIME2': 'Date',
31
+ 'SMALLDATETIME': 'Date',
32
+ 'DATE': 'Date',
33
+ 'TIME': 'Date',
34
+ 'DATETIMEOFFSET': 'Date',
35
+ // Binary types
36
+ 'VARBINARY': 'Buffer',
37
+ 'BINARY': 'Buffer',
38
+ 'IMAGE': 'Buffer',
39
+ // Other
40
+ 'UNIQUEIDENTIFIER': 'string',
41
+ 'SQL_VARIANT': 'any',
42
+ 'ROWVERSION': 'Buffer',
43
+ 'HIERARCHYID': 'string',
44
+ 'GEOGRAPHY': 'string',
45
+ 'GEOMETRY': 'string',
46
+ 'VECTOR': 'number[] | string',
47
+ };
48
+
49
+ // Parse base type from "NVARCHAR(255)" → "NVARCHAR"
50
+ function parseSqlType(raw: string): { base: string; params: string } {
51
+ const match = raw.match(/^(\w+)(?:\((.+)\))?$/);
52
+ if (!match) return { base: raw, params: '' };
53
+ return { base: match[1].toUpperCase(), params: match[2] || '' };
54
+ }
55
+
56
+ // Map SQL Server type to TypeScript type
57
+ export function sqlTypeToTs(sqlType: string): string {
58
+ const { base } = parseSqlType(sqlType);
59
+ return AN5_TO_TS[base] || 'any';
60
+ }
61
+
62
+ export class SchemaParser {
63
+ private schemaText: string = '';
64
+
65
+ constructor(private schemaDir: string) {}
66
+
67
+ public async parse(): Promise<Model[]> {
68
+ this.loadSchema();
69
+ const lines = this.schemaText.split('\n');
70
+ const models: Model[] = [];
71
+ let currentModel: Model | null = null;
72
+
73
+ for (let line of lines) {
74
+ line = line.trim();
75
+ if (!line || line.startsWith('//')) continue;
76
+
77
+ const modelHeaderMatch = line.match(/^model\s+(\w+)\s*\{/);
78
+ if (modelHeaderMatch) {
79
+ const modelName = modelHeaderMatch[1];
80
+ currentModel = {
81
+ name: modelName,
82
+ tableName: modelName.toLowerCase() + 's',
83
+ schemaName: 'dbo',
84
+ fields: [],
85
+ relations: []
86
+ };
87
+ models.push(currentModel);
88
+ continue;
89
+ }
90
+
91
+ if (line === '}') {
92
+ currentModel = null;
93
+ continue;
94
+ }
95
+
96
+ if (currentModel) {
97
+ this.parseModelLine(line, currentModel);
98
+ }
99
+ }
100
+
101
+ this.postProcessRelations(models);
102
+ return models;
103
+ }
104
+
105
+ private loadSchema() {
106
+ if (fs.existsSync(this.schemaDir)) {
107
+ const files = fs.readdirSync(this.schemaDir).filter(f => f.endsWith('.an5'));
108
+ for (const file of files) {
109
+ this.schemaText += fs.readFileSync(path.join(this.schemaDir, file), 'utf8') + '\n';
110
+ }
111
+ } else {
112
+ throw new Error(`No schema directory found at ${this.schemaDir}`);
113
+ }
114
+ }
115
+
116
+ private parseModelLine(line: string, model: Model) {
117
+ if (line.startsWith('@@map')) {
118
+ const mapMatch = line.match(/@@map\("(.+)"\)/);
119
+ if (mapMatch) model.tableName = mapMatch[1];
120
+ return;
121
+ }
122
+ if (line.startsWith('@@schema')) {
123
+ const schemaMatch = line.match(/@@schema\("(.+)"\)/);
124
+ if (schemaMatch) model.schemaName = schemaMatch[1];
125
+ return;
126
+ }
127
+ if (line.startsWith('@@unique')) {
128
+ const uniqueMatch = line.match(/@@unique\(\[([\w,\s]+)\]\)/);
129
+ if (uniqueMatch) {
130
+ const fields = uniqueMatch[1].split(',').map(f => f.trim());
131
+ model.compoundUniques = model.compoundUniques || [];
132
+ model.compoundUniques.push(fields);
133
+ }
134
+ return;
135
+ }
136
+ if (line.startsWith('@@description')) {
137
+ const descMatch = line.match(/@@description\("(.+)"\)/);
138
+ if (descMatch) model.description = descMatch[1];
139
+ return;
140
+ }
141
+ if (line.startsWith('@@')) return;
142
+
143
+ const parts = line.split(/\s+/);
144
+ const fieldName = parts[0];
145
+ let fieldType = parts[1];
146
+ if (!fieldName || !fieldType) return;
147
+
148
+ const isArray = fieldType.endsWith('[]');
149
+ const isOptional = fieldType.endsWith('?');
150
+ const cleanType = fieldType.replace('[]', '').replace('?', '');
151
+
152
+ // Parse SQL Server type (e.g., "NVARCHAR(255)" → base="NVARCHAR")
153
+ const { base: sqlBase } = parseSqlType(cleanType);
154
+
155
+ let tsType = 'any';
156
+ let isRelation = false;
157
+
158
+ // Check if it's a known SQL Server type
159
+ if (AN5_TO_TS[sqlBase]) {
160
+ tsType = sqlTypeToTs(cleanType);
161
+ } else if (cleanType[0] === cleanType[0].toUpperCase() && !cleanType.includes('(')) {
162
+ // Uppercase without parens = likely a relation to another model
163
+ tsType = cleanType;
164
+ isRelation = true;
165
+ }
166
+
167
+ if (isRelation) {
168
+ let foreignKey = '', localKey = '', relationName = '';
169
+ const nameMatch = line.match(/@relation\("(\w+)"/);
170
+ if (nameMatch) relationName = nameMatch[1];
171
+ const relationMatch = line.match(/@relation\((?:.*fields:\s*\[(\w+)\],)?\s*(?:.*references:\s*\[(\w+)\],?)?.*\)/);
172
+ if (relationMatch) {
173
+ foreignKey = relationMatch[1] || '';
174
+ localKey = relationMatch[2] || '';
175
+ }
176
+ model.relations.push({ name: fieldName, type: tsType, isArray, isOptional, foreignKey, localKey, relationName });
177
+ } else {
178
+ const hasDefault = line.includes('@default') || line.includes('@updatedAt') || line.includes('@id');
179
+ const isId = line.includes('@id');
180
+ let description: string | undefined;
181
+ const descMatch = line.match(/@description\("(.+)"\)/);
182
+ if (descMatch) description = descMatch[1];
183
+ model.fields.push({ name: fieldName, type: tsType, sqlType: cleanType, isOptional, hasDefault, isId, description });
184
+ }
185
+ }
186
+
187
+ private postProcessRelations(models: Model[]) {
188
+ for (const model of models) {
189
+ for (const rel of model.relations) {
190
+ if (!rel.foreignKey || !rel.localKey) {
191
+ const targetModel = models.find(m => m.name === rel.type);
192
+ if (targetModel) {
193
+ let opposite = rel.relationName ?
194
+ targetModel.relations.find(r => r.type === model.name && r.relationName === rel.relationName && r.foreignKey && r.localKey) :
195
+ targetModel.relations.find(r => r.type === model.name && r.foreignKey && r.localKey);
196
+ if (opposite) { rel.foreignKey = opposite.foreignKey; rel.localKey = opposite.localKey; }
197
+ }
198
+ }
199
+ }
200
+ }
201
+ }
202
+ }