@ronin/compiler 0.13.14-leo-ron-1099-1-experimental-310 → 0.14.0-leo-ron-1099-1-experimental-311

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.
Files changed (2) hide show
  1. package/dist/index.js +90 -90
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -350,6 +350,16 @@ var handleIncluding = (models, model, statementParams, single, instruction) => {
350
350
  if (single && !subSingle) {
351
351
  tableSubQuery = `SELECT * FROM "${model.table}" LIMIT 1`;
352
352
  }
353
+ if (modifiableQueryInstructions?.including) {
354
+ const subIncluding = handleIncluding(
355
+ models,
356
+ { ...relatedModel, tableAlias },
357
+ statementParams,
358
+ subSingle,
359
+ modifiableQueryInstructions.including
360
+ );
361
+ statement += ` ${subIncluding.statement}`;
362
+ }
353
363
  }
354
364
  return { statement, tableSubQuery };
355
365
  };
@@ -391,11 +401,23 @@ var handleOrderedBy = (model, instruction) => {
391
401
  };
392
402
 
393
403
  // src/instructions/selecting.ts
394
- var handleSelecting = (models, model, statementParams, single, instructions, options) => {
395
- let loadedFields = [];
396
- let expandColumns = false;
397
- let statement = "*";
404
+ var handleSelecting = (models, model, statementParams, single, instructions, options = {}) => {
398
405
  let isJoining = false;
406
+ const selectedFields = (instructions.selecting ? instructions.selecting.map((slug) => {
407
+ const { field } = getFieldFromModel(model, slug, {
408
+ instructionName: "selecting"
409
+ });
410
+ return field;
411
+ }) : model.fields).filter((field) => !(field.type === "link" && field.kind === "many")).map((field) => {
412
+ const newField = { ...field, mountingPath: field.slug };
413
+ if (options.mountingPath) {
414
+ newField.mountingPath = `${options.mountingPath}.${field.slug}`;
415
+ }
416
+ return newField;
417
+ });
418
+ if (instructions.selecting) options.expandColumns = true;
419
+ const joinedSelectedFields = [];
420
+ const joinedColumns = [];
399
421
  if (instructions.including) {
400
422
  const flatObject = flatten(instructions.including);
401
423
  instructions.including = {};
@@ -405,79 +427,65 @@ var handleSelecting = (models, model, statementParams, single, instructions, opt
405
427
  const { queryModel, queryInstructions } = splitQuery(symbol.value);
406
428
  const subQueryModel = getModelBySlug(models, queryModel);
407
429
  isJoining = true;
408
- expandColumns = Boolean(options?.expandColumns || queryInstructions?.selecting);
430
+ if (queryInstructions?.selecting) options.expandColumns = true;
409
431
  const tableAlias = composeIncludedTableAlias(key);
410
432
  const subSingle = queryModel !== subQueryModel.pluralSlug;
411
- if (single && !subSingle) {
412
- model.tableAlias = `sub_${model.table}`;
413
- }
414
- const queryModelFields = queryInstructions?.selecting ? subQueryModel.fields.filter((field) => {
415
- return queryInstructions.selecting?.includes(field.slug);
416
- }) : (
417
- // Exclude link fields with cardinality "many", since those don't exist as columns.
418
- subQueryModel.fields.filter((field) => {
419
- return !(field.type === "link" && field.kind === "many");
420
- })
433
+ if (!model.tableAlias)
434
+ model.tableAlias = single && !subSingle ? `sub_${model.table}` : model.table;
435
+ const subMountingPath = `${options?.mountingPath ? `${options?.mountingPath}.` : ""}${subSingle ? key : `${key}[0]`}`;
436
+ const { columns: nestedColumns, selectedFields: nestedSelectedFields } = handleSelecting(
437
+ models,
438
+ { ...subQueryModel, tableAlias },
439
+ statementParams,
440
+ subSingle,
441
+ {
442
+ selecting: queryInstructions?.selecting,
443
+ including: queryInstructions?.including
444
+ },
445
+ { ...options, mountingPath: subMountingPath }
421
446
  );
422
- for (const field of queryModelFields) {
423
- loadedFields.push({
424
- ...field,
425
- parentField: {
426
- slug: key,
427
- single: subSingle
428
- }
429
- });
430
- if (expandColumns) {
431
- const newValue2 = parseFieldExpression(
432
- { ...subQueryModel, tableAlias },
433
- "including",
434
- `${QUERY_SYMBOLS.FIELD}${field.slug}`
435
- );
436
- instructions.including[`${tableAlias}.${field.slug}`] = newValue2;
437
- }
438
- }
447
+ if (nestedColumns !== "*") joinedColumns.push(nestedColumns);
448
+ joinedSelectedFields.push(...nestedSelectedFields);
439
449
  continue;
440
450
  }
441
- let newValue = value;
451
+ let mountedValue = value;
442
452
  if (symbol?.type === "expression") {
443
- newValue = `(${parseFieldExpression(model, "including", symbol.value)})`;
453
+ mountedValue = `(${parseFieldExpression(model, "including", symbol.value)})`;
444
454
  } else {
445
- newValue = prepareStatementValue(statementParams, value);
455
+ mountedValue = prepareStatementValue(statementParams, value);
446
456
  }
447
- instructions.including[key] = newValue;
448
- loadedFields.push({
457
+ selectedFields.push({
449
458
  slug: key,
450
- type: RAW_FIELD_TYPES.includes(typeof value) ? typeof value : "string"
459
+ mountingPath: key,
460
+ type: RAW_FIELD_TYPES.includes(typeof value) ? typeof value : "string",
461
+ mountedValue
451
462
  });
452
463
  }
453
464
  }
454
- if (expandColumns) {
455
- instructions.selecting = model.fields.filter((field) => !(field.type === "link" && field.kind === "many")).map((field) => field.slug);
456
- }
457
- if (instructions.selecting) {
458
- const usableModel = expandColumns ? { ...model, tableAlias: model.tableAlias || model.table } : model;
459
- const selectedFields = [];
460
- statement = instructions.selecting.map((slug) => {
461
- const { field, fieldSelector } = getFieldFromModel(usableModel, slug, {
462
- instructionName: "selecting"
463
- });
464
- selectedFields.push(field);
465
- return fieldSelector;
466
- }).join(", ");
467
- loadedFields = [...selectedFields, ...loadedFields];
468
- } else {
469
- loadedFields = [
470
- ...model.fields.filter(
471
- (field) => !(field.type === "link" && field.kind === "many")
472
- ),
473
- ...loadedFields
474
- ];
475
- }
476
- if (instructions.including && Object.keys(instructions.including).length > 0) {
477
- statement += ", ";
478
- statement += Object.entries(instructions.including).map(([key, value]) => `${value} as "${key}"`).join(", ");
479
- }
480
- return { columns: statement, isJoining, loadedFields };
465
+ let columns = ["*"];
466
+ const fieldsToExpand = options.expandColumns ? selectedFields : selectedFields.filter(
467
+ (loadedField) => typeof loadedField.mountedValue !== "undefined"
468
+ );
469
+ const extraColumns = fieldsToExpand.map((selectedField) => {
470
+ if (selectedField.mountedValue) {
471
+ return `${selectedField.mountedValue} as "${selectedField.slug}"`;
472
+ }
473
+ const { fieldSelector } = getFieldFromModel(model, selectedField.slug, {
474
+ instructionName: "selecting"
475
+ });
476
+ if (model.tableAlias?.startsWith("including_")) {
477
+ return `${fieldSelector} as "${model.tableAlias}.${selectedField.slug}"`;
478
+ }
479
+ return fieldSelector;
480
+ });
481
+ if (options.expandColumns) {
482
+ columns = extraColumns;
483
+ } else if (extraColumns) {
484
+ columns.push(...extraColumns);
485
+ }
486
+ columns.push(...joinedColumns);
487
+ selectedFields.push(...joinedSelectedFields);
488
+ return { columns: columns.join(", "), isJoining, selectedFields };
481
489
  };
482
490
 
483
491
  // src/instructions/to.ts
@@ -599,7 +607,7 @@ var compileQueryInput = (defaultQuery, models, statementParams, options) => {
599
607
  defaultQuery
600
608
  );
601
609
  if (query === null)
602
- return { dependencies: [], main: dependencyStatements[0], loadedFields: [] };
610
+ return { dependencies: [], main: dependencyStatements[0], selectedFields: [] };
603
611
  const parsedQuery = splitQuery(query);
604
612
  const { queryType, queryModel, queryInstructions } = parsedQuery;
605
613
  const model = getModelBySlug(models, queryModel);
@@ -609,7 +617,7 @@ var compileQueryInput = (defaultQuery, models, statementParams, options) => {
609
617
  if (instructions && Object.hasOwn(instructions, "for")) {
610
618
  instructions = handleFor(model, instructions);
611
619
  }
612
- const { columns, isJoining, loadedFields } = handleSelecting(
620
+ const { columns, isJoining, selectedFields } = handleSelecting(
613
621
  models,
614
622
  model,
615
623
  statementParams,
@@ -741,7 +749,7 @@ var compileQueryInput = (defaultQuery, models, statementParams, options) => {
741
749
  return {
742
750
  dependencies: dependencyStatements,
743
751
  main: mainStatement,
744
- loadedFields
752
+ selectedFields
745
753
  };
746
754
  };
747
755
 
@@ -1299,8 +1307,10 @@ var getFieldSelector = (model, field, fieldPath, writing) => {
1299
1307
  if (field.type === "json" && !writing) {
1300
1308
  const dotParts = fieldPath.split(".");
1301
1309
  const columnName = tablePrefix + dotParts.shift();
1302
- const jsonField = dotParts.join(".");
1303
- return `json_extract(${columnName}, '$.${jsonField}')`;
1310
+ if (dotParts.length > 0) {
1311
+ const jsonField = dotParts.join(".");
1312
+ return `json_extract(${columnName}, '$.${jsonField}')`;
1313
+ }
1304
1314
  }
1305
1315
  return `${tablePrefix}"${fieldPath}"`;
1306
1316
  };
@@ -1901,7 +1911,7 @@ var Transaction = class {
1901
1911
  ...subStatements.map((statement) => ({
1902
1912
  ...statement,
1903
1913
  query,
1904
- fields: result.loadedFields
1914
+ selectedFields: result.selectedFields
1905
1915
  }))
1906
1916
  );
1907
1917
  }
@@ -1912,12 +1922,8 @@ var Transaction = class {
1912
1922
  const records = [];
1913
1923
  for (const row of rows) {
1914
1924
  const record = fields.reduce((acc, field, fieldIndex) => {
1915
- let newSlug = field.slug;
1925
+ const newSlug = field.mountingPath;
1916
1926
  let newValue = row[fieldIndex];
1917
- if (field.parentField) {
1918
- const arrayKey = field.parentField.single ? "" : "[0]";
1919
- newSlug = `${field.parentField.slug}${arrayKey}.${field.slug}`;
1920
- }
1921
1927
  if (field.type === "json") {
1922
1928
  newValue = JSON.parse(newValue);
1923
1929
  } else if (field.type === "boolean") {
@@ -1939,16 +1945,14 @@ var Transaction = class {
1939
1945
  continue;
1940
1946
  }
1941
1947
  const joinFields = fields.reduce(
1942
- (acc, field) => {
1943
- if (!field.parentField) return acc;
1944
- const { single: single2, slug } = field.parentField;
1945
- return single2 || acc.includes(slug) ? acc : acc.concat([slug]);
1948
+ (acc, { mountingPath }) => {
1949
+ return mountingPath.includes("[0]") && !acc.includes(mountingPath.split("[0]")[0]) ? acc.concat([mountingPath.split("[0]")[0]]) : acc;
1946
1950
  },
1947
1951
  []
1948
1952
  );
1949
- for (const parentField of joinFields) {
1950
- const currentValue = existingRecord[parentField];
1951
- const newValue = record[parentField];
1953
+ for (const arrayField of joinFields) {
1954
+ const currentValue = existingRecord[arrayField];
1955
+ const newValue = record[arrayField];
1952
1956
  currentValue.push(...newValue);
1953
1957
  }
1954
1958
  }
@@ -1976,11 +1980,7 @@ var Transaction = class {
1976
1980
  });
1977
1981
  const formattedResults = normalizedResults.map(
1978
1982
  (rows, index) => {
1979
- const {
1980
- returning,
1981
- query,
1982
- fields: rawModelFields
1983
- } = this.#internalStatements[index];
1983
+ const { returning, query, selectedFields } = this.#internalStatements[index];
1984
1984
  if (!returning) return null;
1985
1985
  const { queryType, queryModel, queryInstructions } = splitQuery(query);
1986
1986
  const model = getModelBySlug(this.models, queryModel);
@@ -1994,13 +1994,13 @@ var Transaction = class {
1994
1994
  const single = queryModel !== model.pluralSlug;
1995
1995
  if (single) {
1996
1996
  return {
1997
- record: rows[0] ? this.#formatRows(rawModelFields, rows, true, isMeta) : null,
1997
+ record: rows[0] ? this.#formatRows(selectedFields, rows, true, isMeta) : null,
1998
1998
  modelFields
1999
1999
  };
2000
2000
  }
2001
2001
  const pageSize = queryInstructions?.limitedTo;
2002
2002
  const output = {
2003
- records: this.#formatRows(rawModelFields, rows, false, isMeta),
2003
+ records: this.#formatRows(selectedFields, rows, false, isMeta),
2004
2004
  modelFields
2005
2005
  };
2006
2006
  if (pageSize && output.records.length > 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ronin/compiler",
3
- "version": "0.13.14-leo-ron-1099-1-experimental-310",
3
+ "version": "0.14.0-leo-ron-1099-1-experimental-311",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {