@nestledjs/api 0.1.4 → 0.1.6
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
|
@@ -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,45 @@ 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
|
|
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
|
-
|
|
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 [
|
|
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
|
-
}
|
|
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';
|
|
150
128
|
|
|
129
|
+
// Improved logic based on relationship type and operation
|
|
130
|
+
code += ' if (config.isVirtual) {\n';
|
|
131
|
+
code += ' // Virtual relationships (many-to-many or one-to-many reverse)\n';
|
|
132
|
+
code += ' if (config.isList) {\n';
|
|
133
|
+
code += ' // Many-to-many or one-to-many from the "many" side - use set for updates\n';
|
|
134
|
+
code += ` data[relationName] = { ${operation === 'create' ? 'connect' : 'set'}: Array.isArray(config.ids) ? config.ids.map(id => ({ id })) : [{ id: config.ids }] };\n`;
|
|
135
|
+
code += ' } else {\n';
|
|
136
|
+
code += ' // Single virtual relationship - use connect\n';
|
|
137
|
+
code += ' data[relationName] = { connect: { id: config.ids } };\n';
|
|
138
|
+
code += ' }\n';
|
|
139
|
+
code += ' } else {\n';
|
|
140
|
+
code += ' // Foreign key relationships (belongs-to) - always use connect for single values\n';
|
|
141
|
+
code += ' if (config.isList) {\n';
|
|
142
|
+
code += ' // This case should be rare, but handle arrays if they exist\n';
|
|
143
|
+
code += ' data[relationName] = { connect: Array.isArray(config.ids) ? config.ids.map(id => ({ id })) : [{ id: config.ids }] };\n';
|
|
144
|
+
code += ' } else {\n';
|
|
145
|
+
code += ' // Single foreign key relationship - use connect\n';
|
|
146
|
+
code += ' data[relationName] = { connect: { id: config.ids } };\n';
|
|
147
|
+
code += ' }\n';
|
|
148
|
+
code += ' }\n';
|
|
151
149
|
code += ' }\n';
|
|
152
150
|
code += ' }\n';
|
|
153
151
|
|
|
@@ -14,7 +14,7 @@ const internals_1 = require("@prisma/internals");
|
|
|
14
14
|
const utils_1 = require("@nestledjs/utils");
|
|
15
15
|
const get_npm_scope_1 = require("@nx/js/src/utils/package-json/get-npm-scope");
|
|
16
16
|
// STEP 2: DEFINE PURE HELPER & CONTENT GENERATION FUNCTIONS
|
|
17
|
-
// These functions are side-effect
|
|
17
|
+
// These functions are side-effect-free and can be tested independently.
|
|
18
18
|
function parseCrudAuth(comment) {
|
|
19
19
|
try {
|
|
20
20
|
const match = comment.match(/@crudAuth:\s*(\{.*\})/);
|