dzql 0.5.7 → 0.5.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/bin/cli.js CHANGED
@@ -404,13 +404,12 @@ async function runMigrateNew(args) {
404
404
  }
405
405
 
406
406
  // Generate migration template
407
+ // Note: No BEGIN/COMMIT - postgres.js handles transactions automatically
407
408
  const template = `-- ============================================================================
408
409
  -- Migration ${paddedNumber}: ${migrationName.replace(/_/g, ' ')}
409
410
  -- Generated: ${new Date().toISOString().split('T')[0]}
410
411
  -- ============================================================================
411
412
 
412
- BEGIN;
413
-
414
413
  -- Part 1: Schema Changes
415
414
  -- ALTER TABLE example ADD COLUMN IF NOT EXISTS new_field TEXT;
416
415
 
@@ -440,8 +439,6 @@ BEGIN;
440
439
  -- ('my_custom_function'::regproc, 'Description of function')
441
440
  -- ON CONFLICT DO NOTHING;
442
441
 
443
- COMMIT;
444
-
445
442
  -- ============================================================================
446
443
  -- Rollback (for migrate:down support)
447
444
  -- ============================================================================
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dzql",
3
- "version": "0.5.7",
3
+ "version": "0.5.8",
4
4
  "description": "PostgreSQL-powered framework with zero boilerplate CRUD operations and real-time WebSocket synchronization",
5
5
  "type": "module",
6
6
  "main": "src/server/index.js",
@@ -167,12 +167,9 @@ $$ LANGUAGE plpgsql STABLE SECURITY DEFINER;`;
167
167
  return 'FALSE';
168
168
  }
169
169
 
170
- const startField = fieldRef.field;
171
170
  const targetTable = tableRef.table;
172
171
  const targetField = tableRef.targetField;
173
172
 
174
- const startValue = `(${recordVar}->>'${startField}')::int`;
175
-
176
173
  // Build WHERE clause
177
174
  const whereClauses = [];
178
175
 
@@ -181,8 +178,10 @@ $$ LANGUAGE plpgsql STABLE SECURITY DEFINER;`;
181
178
  for (const filterCondition of tableRef.filter) {
182
179
  const field = filterCondition.field;
183
180
  if (filterCondition.value.type === 'param') {
184
- // Parameter reference: org_id=$
185
- whereClauses.push(`${targetTable}.${field} = ${startValue}`);
181
+ // Parameter reference: org_id=$ means use the filter field name as the param key
182
+ // e.g., acts_for[organisation_id=$] -> (p_params->>'organisation_id')::int
183
+ const paramValue = `(${recordVar}->>'${field}')::int`;
184
+ whereClauses.push(`${targetTable}.${field} = ${paramValue}`);
186
185
  } else {
187
186
  // Literal value
188
187
  whereClauses.push(`${targetTable}.${field} = '${filterCondition.value}'`);
@@ -301,7 +300,7 @@ $$ LANGUAGE plpgsql SECURITY DEFINER;`;
301
300
  const relIncludes = typeof relConfig === 'object' ? relConfig.include : null;
302
301
 
303
302
  // Build filter condition
304
- let filterSQL = this._generateRelationFilter(relFilter, relEntity);
303
+ let filterSQL = this._generateRelationFilter(relFilter, relEntity, relConfig);
305
304
 
306
305
  // Build nested includes if any
307
306
  let nestedSelect = 'row_to_json(rel.*)';
@@ -334,11 +333,17 @@ $$ LANGUAGE plpgsql SECURITY DEFINER;`;
334
333
  /**
335
334
  * Generate filter for relation subquery
336
335
  * @private
336
+ * @param {string|null} filter - Optional filter expression
337
+ * @param {string} relEntity - Related entity table name
338
+ * @param {Object} relConfig - Full relation config (may contain foreignKey)
337
339
  */
338
- _generateRelationFilter(filter, relEntity) {
340
+ _generateRelationFilter(filter, relEntity, relConfig) {
339
341
  if (!filter) {
340
- // Default: foreign key to root
341
- return `rel.${this.rootEntity}_id = root.id`;
342
+ // Use explicit foreignKey from config, or default to root_id
343
+ const fk = (typeof relConfig === 'object' && relConfig.foreignKey)
344
+ ? relConfig.foreignKey
345
+ : `${this.rootEntity}_id`;
346
+ return `rel.${fk} = root.id`;
342
347
  }
343
348
 
344
349
  // Parse filter expression like "venue_id=$venue_id"