@nestledjs/api 0.1.5 → 0.1.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestledjs/api",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "generators": "./generators.json",
5
5
  "type": "commonjs",
6
6
  "main": "./src/index.js",
@@ -77,7 +77,8 @@ function getForeignKeyRelationFields(model) {
77
77
  foreignKeyFields.push({
78
78
  fieldName: fkFieldName,
79
79
  relationName: relationField.name,
80
- isRequired: !fkField.isOptional
80
+ isRequired: !fkField.isOptional,
81
+ isList: relationField.isList
81
82
  });
82
83
  }
83
84
  });
@@ -106,48 +107,36 @@ function generateRelationHandling(model, operation) {
106
107
  code += ', ...regularFields } = input;\n';
107
108
  code += ' const data: any = regularFields;\n\n';
108
109
 
109
- // Create relation mapping object
110
- code += ' const connectFields = {\n';
110
+ // Create relation mapping object with field type metadata
111
+ code += ' const relationMappings = {\n';
111
112
 
112
113
  // Add virtual relation fields to the mapping
113
114
  virtualRelationFields.forEach(field => {
114
- if (field.isList) {
115
- code += ` ${field.relatedField}: ${field.name},\n`;
116
- } else {
117
- code += ` ${field.relatedField}: ${field.name},\n`;
118
- }
115
+ code += ` ${field.relatedField}: { ids: ${field.name}, isVirtual: true, isList: ${field.isList} },\n`;
119
116
  });
120
117
 
121
118
  // Add foreign key fields to the mapping
122
119
  foreignKeyFields.forEach(field => {
123
- code += ` ${field.relationName}: ${field.fieldName},\n`;
120
+ code += ` ${field.relationName}: { ids: ${field.fieldName}, isVirtual: false, isList: ${field.isList || false} },\n`;
124
121
  });
125
122
 
126
123
  code += ' };\n\n';
127
124
 
128
- // Generate the iteration logic
129
- code += ' for (const [key, ids] of Object.entries(connectFields)) {\n';
130
- code += ' if (ids) {\n';
131
-
132
- // Handle different connection types based on operation and field type
133
- const hasListFields = virtualRelationFields.some(field => field.isList);
134
- const hasSingleFields = virtualRelationFields.some(field => !field.isList) || foreignKeyFields.length > 0;
135
-
136
- if (hasListFields && hasSingleFields) {
137
- // Mixed field types - need to check if it's an array
138
- code += ' if (Array.isArray(ids)) {\n';
139
- code += ` data[key] = { ${operation === 'create' ? 'connect' : 'set'}: ids.map(id => ({ id })) };\n`;
140
- code += ' } else {\n';
141
- code += ' data[key] = { connect: { id: ids } };\n';
142
- code += ' }\n';
143
- } else if (hasListFields) {
144
- // Only list fields
145
- code += ` data[key] = { ${operation === 'create' ? 'connect' : 'set'}: ids.map(id => ({ id })) };\n`;
146
- } else {
147
- // Only single fields
148
- code += ' data[key] = { connect: { id: ids } };\n';
149
- }
150
-
125
+ // Generate the improved iteration logic
126
+ code += ' for (const [relationName, config] of Object.entries(relationMappings)) {\n';
127
+ code += ' if (config.ids !== undefined && config.ids !== null) {\n';
128
+ code += ' // Virtual list relationships use set for updates, connect for creates\n';
129
+ code += ' // All other relationships always use connect\n';
130
+ code += ' const useSet = config.isVirtual && config.isList && operation === \'update\';\n';
131
+ code += ' const relationOperation = useSet ? \'set\' : \'connect\';\n';
132
+ code += ' const ids = Array.isArray(config.ids) ? config.ids.map(id => ({ id })) : [{ id: config.ids }];\n';
133
+ code += ' \n';
134
+ code += ' if (config.isList || useSet) {\n';
135
+ code += ' data[relationName] = { [relationOperation]: ids };\n';
136
+ code += ' } else {\n';
137
+ code += ' // Single relationship - extract the single id\n';
138
+ code += ' data[relationName] = { connect: { id: config.ids } };\n';
139
+ code += ' }\n';
151
140
  code += ' }\n';
152
141
  code += ' }\n';
153
142