@nestledjs/api 0.1.6 → 0.1.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestledjs/api",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "generators": "./generators.json",
5
5
  "type": "commonjs",
6
6
  "main": "./src/index.js",
@@ -125,26 +125,22 @@ function generateRelationHandling(model, operation) {
125
125
  // Generate the improved iteration logic
126
126
  code += ' for (const [relationName, config] of Object.entries(relationMappings)) {\n';
127
127
  code += ' if (config.ids !== undefined && config.ids !== null) {\n';
128
+ code += ' const ids = Array.isArray(config.ids) ? config.ids.map(id => ({ id })) : [{ id: config.ids }];\n';
129
+ code += ' \n';
130
+ code += ' if (config.isList) {\n';
128
131
 
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';
132
+ if (operation === 'update') {
133
+ code += ' // List relationships: use set for updates on virtual relations, connect for foreign key relations\n';
134
+ code += ' const relationOperation = config.isVirtual ? \'set\' : \'connect\';\n';
135
+ } else {
136
+ code += ' // List relationships: always use connect for creates\n';
137
+ code += ' const relationOperation = \'connect\';\n';
138
+ }
139
+
140
+ code += ' data[relationName] = { [relationOperation]: ids };\n';
139
141
  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';
142
+ code += ' // Single relationship - always use connect\n';
143
+ code += ' data[relationName] = { connect: { id: config.ids } };\n';
148
144
  code += ' }\n';
149
145
  code += ' }\n';
150
146
  code += ' }\n';
@@ -179,8 +175,8 @@ export class ApiCrudDataAccessService {
179
175
  const count = await this.data['<%= model.modelPropertyName %>'].count({
180
176
  ...this.data.filter(input)
181
177
  });
182
- const take = input?.take || 10
183
- const skip = input?.skip || 0
178
+ const take = input?.take ?? 10
179
+ const skip = input?.skip ?? 0
184
180
  const page = Math.floor(skip / take)
185
181
  return {
186
182
  take,