@igniter-js/cli 0.1.0 → 0.1.3

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.
@@ -49,7 +49,17 @@ class PrismaSchemaParser {
49
49
  return [];
50
50
  }
51
51
  const fieldsContent = modelMatch[1].trim();
52
- return this.parseFields(fieldsContent);
52
+ const fields = this.parseFields(fieldsContent);
53
+ // Transform parsed fields to include relationship info
54
+ return fields.map(field => {
55
+ var _a, _b;
56
+ return ({
57
+ ...field,
58
+ isRelation: !!field.relations || this.schemaContent.includes(`model ${field.type} {`),
59
+ isList: field.isList || (((_a = field.relations) === null || _a === void 0 ? void 0 : _a.type) === 'one-to-many' || ((_b = field.relations) === null || _b === void 0 ? void 0 : _b.type) === 'many-to-many'),
60
+ isOptional: field.hasDefault || field.isOptional
61
+ });
62
+ });
53
63
  }
54
64
  /**
55
65
  * Parses field strings into structured field objects
@@ -64,6 +74,7 @@ class PrismaSchemaParser {
64
74
  continue;
65
75
  const field = this.parseFieldLine(trimmedLine);
66
76
  if (field) {
77
+ console.log(field);
67
78
  fields.push(field);
68
79
  }
69
80
  }
@@ -75,7 +86,7 @@ class PrismaSchemaParser {
75
86
  parseFieldLine(line) {
76
87
  this.loadSchema();
77
88
  // Basic field pattern: name type modifiers
78
- const fieldPattern = /^(\w+)\s+(\w+)(\?|\[\])?\s*(@\w+(?:\([^)]*\))?)*$/;
89
+ const fieldPattern = /^(\w+)\s+(\w+)(?:\?|\[\])?\s*(@[^\n]+)?$/;
79
90
  const match = line.match(fieldPattern);
80
91
  if (!match)
81
92
  return null;
@@ -83,31 +94,42 @@ class PrismaSchemaParser {
83
94
  const modifiers = line.slice(match[0].length).trim();
84
95
  const isOptional = line.includes('?');
85
96
  const isList = line.includes('[]');
86
- const hasDefault = modifiers.includes('@default');
87
- // Skip internal Prisma fields
88
- if (['id', 'createdAt', 'updatedAt'].includes(name)) {
89
- return null;
90
- }
91
- // Verifica se é um enum
92
- const isEnum = this.schemaContent.includes(`enum ${type} {`);
97
+ const hasDefault = line.includes('@default');
98
+ // Check if type is a model (potential relation)
99
+ const isModelType = this.schemaContent.includes(`model ${type} {`);
93
100
  // Parse relations
94
101
  let relations = undefined;
95
- if (line.includes('@relation')) {
102
+ let isRelation = false;
103
+ if (line.includes('@relation') || isModelType) {
96
104
  const relationMatch = line.match(/@relation\([^)]*\)/);
97
105
  if (relationMatch) {
98
106
  relations = this.parseRelation(relationMatch[0], isList);
107
+ isRelation = true;
108
+ }
109
+ else if (isModelType) {
110
+ // If it's a model type but doesn't have @relation, create a default relation
111
+ relations = {
112
+ type: isList ? 'one-to-many' : 'one-to-one',
113
+ model: type,
114
+ fields: [name + 'Id'],
115
+ references: ['id']
116
+ };
117
+ isRelation = true;
99
118
  }
100
119
  }
120
+ // Verifica se é um enum
121
+ const isEnum = this.schemaContent.includes(`enum ${type} {`);
101
122
  return {
102
123
  name,
103
124
  type,
104
- zodType: this.getZodType(type, isOptional, isList),
125
+ zodType: this.getZodType(type, isOptional || hasDefault, isList),
105
126
  description: `${name} field`,
106
- isOptional,
127
+ isOptional: isOptional || hasDefault,
107
128
  isList,
108
129
  hasDefault,
109
130
  isEnum,
110
- relations
131
+ relations,
132
+ isRelation
111
133
  };
112
134
  }
113
135
  /**
@@ -115,22 +137,63 @@ class PrismaSchemaParser {
115
137
  */
116
138
  parseRelation(relationString, isList) {
117
139
  var _a;
118
- const relationDetails = (_a = relationString.match(/@relation\(([^)]+)\)/)) === null || _a === void 0 ? void 0 : _a[1].split(',');
119
- if (!relationDetails)
120
- return undefined;
121
- const nameMatch = relationDetails.find(detail => detail.trim().startsWith('name:'));
122
- const fieldsMatch = relationDetails.find(detail => detail.trim().startsWith('fields:'));
123
- const referencesMatch = relationDetails.find(detail => detail.trim().startsWith('references:'));
124
- if (nameMatch && fieldsMatch && referencesMatch) {
125
- const model = nameMatch.split(':')[1].trim().replace(/['"]/g, '');
126
- const fields = fieldsMatch.split(':')[1].trim().replace(/\[|\]/g, '').split(',').map(f => f.trim());
127
- const references = referencesMatch.split(':')[1].trim().replace(/\[|\]/g, '').split(',').map(f => f.trim());
128
- return {
129
- type: fields.length > 1 || references.length > 1 ? 'many-to-many' : (isList ? 'one-to-many' : 'one-to-one'),
130
- model,
131
- fields,
132
- references
133
- };
140
+ try {
141
+ // Extract details from @relation(...) string
142
+ const relationDetails = (_a = relationString.match(/@relation\(([^)]+)\)/)) === null || _a === void 0 ? void 0 : _a[1].trim();
143
+ if (!relationDetails)
144
+ return undefined;
145
+ // Extract fields and references from the relation
146
+ const fieldsMatch = relationDetails.match(/fields:\s*\[\s*([^\]]+)\s*\]/);
147
+ const referencesMatch = relationDetails.match(/references:\s*\[\s*([^\]]+)\s*\]/);
148
+ if (fieldsMatch && referencesMatch) {
149
+ // Get fields and references
150
+ const fields = fieldsMatch[1].split(',').map(f => f.trim().replace(/['"]/g, ''));
151
+ const references = referencesMatch[1].split(',').map(r => r.trim().replace(/['"]/g, ''));
152
+ // Try to find referenced model from the context
153
+ let model = '';
154
+ // Check if there's an explicit model name attribute
155
+ const modelMatch = relationDetails.match(/name:\s*['"]?(\w+)['"]?/);
156
+ if (modelMatch) {
157
+ model = modelMatch[1];
158
+ }
159
+ else {
160
+ // Infer model from schema by looking at field references
161
+ // This is a simplification - in a real implementation you might need to look at
162
+ // the other side of the relation in the schema
163
+ const referenceField = references[0].replace(/['"]/g, '');
164
+ if (referenceField === 'id') {
165
+ // Try to find "@relation(fields: [categoryId], references: [id])"
166
+ // Where categoryId indicates a Category model
167
+ const possibleModelField = fields[0];
168
+ if (possibleModelField && possibleModelField.toLowerCase().endsWith('id')) {
169
+ // Extract model name by removing the 'Id' suffix
170
+ model = possibleModelField.substring(0, possibleModelField.length - 2);
171
+ // Convert first letter to uppercase for proper model name format
172
+ model = model.charAt(0).toUpperCase() + model.slice(1);
173
+ }
174
+ }
175
+ }
176
+ // Determine relation type based on list status and fields/references count
177
+ let relationType;
178
+ if (fields.length > 1 || references.length > 1) {
179
+ relationType = 'many-to-many';
180
+ }
181
+ else if (isList) {
182
+ relationType = 'one-to-many';
183
+ }
184
+ else {
185
+ relationType = 'one-to-one';
186
+ }
187
+ return {
188
+ type: relationType,
189
+ model,
190
+ fields,
191
+ references
192
+ };
193
+ }
194
+ }
195
+ catch (error) {
196
+ console.log(`Error parsing relation: ${error}`);
134
197
  }
135
198
  return undefined;
136
199
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@igniter-js/cli",
3
- "version": "0.1.0",
3
+ "version": "0.1.3",
4
4
  "description": "Igniter CLI for projects using Igniter Framework",
5
5
  "main": "dist/index.js",
6
6
  "homepage": "https://felipebarcelospro.github.io/igniter",