dzql 0.5.8 → 0.5.9

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": "dzql",
3
- "version": "0.5.8",
3
+ "version": "0.5.9",
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",
@@ -238,13 +238,20 @@ $$ LANGUAGE plpgsql STABLE SECURITY DEFINER;`;
238
238
  // Build relation subqueries
239
239
  const relationSelects = this._generateRelationSelects();
240
240
 
241
+ // Build schema with path mapping (baked in at compile time)
242
+ const pathMapping = this.buildPathMapping();
243
+ const schemaJson = JSON.stringify({
244
+ root: this.rootEntity,
245
+ paths: pathMapping
246
+ });
247
+
241
248
  return `CREATE OR REPLACE FUNCTION get_${this.name}(
242
249
  p_params JSONB,
243
250
  p_user_id INT
244
251
  ) RETURNS JSONB AS $$
245
252
  DECLARE
246
253
  ${paramDeclarations}
247
- v_result JSONB;
254
+ v_data JSONB;
248
255
  BEGIN
249
256
  -- Extract parameters
250
257
  ${paramExtractions}
@@ -258,11 +265,15 @@ ${paramExtractions}
258
265
  SELECT jsonb_build_object(
259
266
  '${this.rootEntity}', row_to_json(root.*)${relationSelects}
260
267
  )
261
- INTO v_result
268
+ INTO v_data
262
269
  FROM ${this.rootEntity} root
263
270
  WHERE ${rootFilter};
264
271
 
265
- RETURN v_result;
272
+ -- Return data with embedded schema for atomic updates
273
+ RETURN jsonb_build_object(
274
+ 'data', v_data,
275
+ 'schema', '${schemaJson}'::jsonb
276
+ );
266
277
  END;
267
278
  $$ LANGUAGE plpgsql SECURITY DEFINER;`;
268
279
  }
package/src/server/ws.js CHANGED
@@ -322,14 +322,28 @@ export function createRPCHandler(customHandlers = {}) {
322
322
  try {
323
323
  // Execute initial query (this also checks permissions)
324
324
  const queryResult = await sql.unsafe(
325
- `SELECT get_${subscribableName}($1, $2) as data`,
325
+ `SELECT get_${subscribableName}($1, $2) as result`,
326
326
  [params, ws.data.user_id]
327
327
  );
328
328
 
329
- const data = queryResult[0]?.data;
330
-
331
- // Get subscribable metadata for schema (path mapping for atomic updates)
332
- const metadata = await getSubscribableMetadata(subscribableName, sql);
329
+ const queryData = queryResult[0]?.result;
330
+
331
+ // Check if compiled function returned embedded schema
332
+ // Compiled: { data, schema } | Interpreted: just the data object
333
+ let data, schema;
334
+ if (queryData && queryData.schema && queryData.data !== undefined) {
335
+ // Compiled mode - schema is embedded
336
+ data = queryData.data;
337
+ schema = queryData.schema;
338
+ } else {
339
+ // Interpreted mode - fetch schema from metadata table
340
+ data = queryData;
341
+ const metadata = await getSubscribableMetadata(subscribableName, sql);
342
+ schema = {
343
+ root: metadata.rootEntity,
344
+ paths: metadata.pathMapping
345
+ };
346
+ }
333
347
 
334
348
  // Register subscription in memory
335
349
  const subscriptionId = registerSubscription(
@@ -343,11 +357,7 @@ export function createRPCHandler(customHandlers = {}) {
343
357
  const result = {
344
358
  subscription_id: subscriptionId,
345
359
  data,
346
- // Include schema for atomic update support
347
- schema: {
348
- root: metadata.rootEntity,
349
- paths: metadata.pathMapping
350
- }
360
+ schema
351
361
  };
352
362
 
353
363
  wsLogger.response(method, result, Date.now() - startTime);