@ronin/compiler 0.13.14 → 0.14.0

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/dist/index.d.ts CHANGED
@@ -372,8 +372,8 @@ declare class Transaction {
372
372
  statements: Array<Statement>;
373
373
  models: Array<Model>;
374
374
  constructor(queries: Array<Query>, options?: TransactionOptions);
375
- formatResults<Record>(results: Array<Array<RawRow>>, raw?: true): Array<Result<Record>>;
376
375
  formatResults<Record>(results: Array<Array<ObjectRow>>, raw?: false): Array<Result<Record>>;
376
+ formatResults<Record>(results: Array<Array<RawRow>>, raw?: true): Array<Result<Record>>;
377
377
  }
378
378
 
379
379
  declare const CLEAN_ROOT_MODEL: PublicModel;
package/dist/index.js CHANGED
@@ -125,7 +125,6 @@ var getProperty = (obj, path) => {
125
125
  return path.split(".").reduce((acc, key) => acc?.[key], obj);
126
126
  };
127
127
  var setProperty = (obj, path, value) => {
128
- if (!obj) return setProperty({}, path, value);
129
128
  const segments = path.split(/[.[\]]/g).filter((x) => !!x.trim());
130
129
  const _set = (node) => {
131
130
  if (segments.length > 1) {
@@ -139,9 +138,7 @@ var setProperty = (obj, path, value) => {
139
138
  node[segments[0]] = value;
140
139
  }
141
140
  };
142
- const cloned = structuredClone(obj);
143
- _set(cloned);
144
- return cloned;
141
+ _set(obj);
145
142
  };
146
143
  var splitQuery = (query) => {
147
144
  const queryType = Object.keys(query)[0];
@@ -302,7 +299,7 @@ var handleFor = (model, instructions) => {
302
299
  };
303
300
 
304
301
  // src/instructions/including.ts
305
- var handleIncluding = (models, model, statementParams, instruction) => {
302
+ var handleIncluding = (models, model, statementParams, single, instruction) => {
306
303
  let statement = "";
307
304
  let tableSubQuery;
308
305
  for (const ephemeralFieldSlug in instruction) {
@@ -315,10 +312,10 @@ var handleIncluding = (models, model, statementParams, instruction) => {
315
312
  let joinType = "LEFT";
316
313
  let relatedTableSelector = `"${relatedModel.table}"`;
317
314
  const tableAlias = composeIncludedTableAlias(ephemeralFieldSlug);
318
- const single = queryModel !== relatedModel.pluralSlug;
315
+ const subSingle = queryModel !== relatedModel.pluralSlug;
319
316
  if (!modifiableQueryInstructions?.with) {
320
317
  joinType = "CROSS";
321
- if (single) {
318
+ if (subSingle) {
322
319
  if (!modifiableQueryInstructions) modifiableQueryInstructions = {};
323
320
  modifiableQueryInstructions.limitedTo = 1;
324
321
  }
@@ -350,7 +347,9 @@ var handleIncluding = (models, model, statementParams, instruction) => {
350
347
  );
351
348
  statement += ` ON (${subStatement})`;
352
349
  }
353
- if (!single) tableSubQuery = `SELECT * FROM "${model.table}" LIMIT 1`;
350
+ if (single && !subSingle) {
351
+ tableSubQuery = `SELECT * FROM "${model.table}" LIMIT 1`;
352
+ }
354
353
  }
355
354
  return { statement, tableSubQuery };
356
355
  };
@@ -392,7 +391,7 @@ var handleOrderedBy = (model, instruction) => {
392
391
  };
393
392
 
394
393
  // src/instructions/selecting.ts
395
- var handleSelecting = (models, model, statementParams, instructions, options) => {
394
+ var handleSelecting = (models, model, statementParams, single, instructions, options) => {
396
395
  let loadedFields = [];
397
396
  let expandColumns = false;
398
397
  let statement = "*";
@@ -408,8 +407,8 @@ var handleSelecting = (models, model, statementParams, instructions, options) =>
408
407
  isJoining = true;
409
408
  expandColumns = Boolean(options?.expandColumns || queryInstructions?.selecting);
410
409
  const tableAlias = composeIncludedTableAlias(key);
411
- const single = queryModel !== subQueryModel.pluralSlug;
412
- if (!single) {
410
+ const subSingle = queryModel !== subQueryModel.pluralSlug;
411
+ if (single && !subSingle) {
413
412
  model.tableAlias = `sub_${model.table}`;
414
413
  }
415
414
  const queryModelFields = queryInstructions?.selecting ? subQueryModel.fields.filter((field) => {
@@ -421,7 +420,13 @@ var handleSelecting = (models, model, statementParams, instructions, options) =>
421
420
  })
422
421
  );
423
422
  for (const field of queryModelFields) {
424
- loadedFields.push({ ...field, parentField: key });
423
+ loadedFields.push({
424
+ ...field,
425
+ parentField: {
426
+ slug: key,
427
+ single: subSingle
428
+ }
429
+ });
425
430
  if (expandColumns) {
426
431
  const newValue2 = parseFieldExpression(
427
432
  { ...subQueryModel, tableAlias },
@@ -608,6 +613,7 @@ var compileQueryInput = (defaultQuery, models, statementParams, options) => {
608
613
  models,
609
614
  model,
610
615
  statementParams,
616
+ single,
611
617
  {
612
618
  selecting: instructions?.selecting,
613
619
  including: instructions?.including
@@ -638,6 +644,7 @@ var compileQueryInput = (defaultQuery, models, statementParams, options) => {
638
644
  models,
639
645
  model,
640
646
  statementParams,
647
+ single,
641
648
  instructions?.including
642
649
  );
643
650
  if (tableSubQuery) {
@@ -1903,38 +1910,46 @@ var Transaction = class {
1903
1910
  };
1904
1911
  #formatRows(fields, rows, single, isMeta) {
1905
1912
  const records = [];
1906
- for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
1907
- const row = rows[rowIndex];
1908
- for (let valueIndex = 0; valueIndex < row.length; valueIndex++) {
1909
- const value = row[valueIndex];
1910
- const field = fields[valueIndex];
1913
+ for (const row of rows) {
1914
+ const record = fields.reduce((acc, field, fieldIndex) => {
1911
1915
  let newSlug = field.slug;
1912
- let newValue = value;
1916
+ let newValue = row[fieldIndex];
1917
+ if (field.parentField) {
1918
+ const arrayKey = field.parentField.single ? "" : "[0]";
1919
+ newSlug = `${field.parentField.slug}${arrayKey}.${field.slug}`;
1920
+ }
1913
1921
  if (field.type === "json") {
1914
- newValue = JSON.parse(value);
1922
+ newValue = JSON.parse(newValue);
1915
1923
  } else if (field.type === "boolean") {
1916
- newValue = Boolean(value);
1917
- }
1918
- const parentFieldSlug = field.parentField;
1919
- let usableRowIndex = rowIndex;
1920
- if (parentFieldSlug) {
1921
- if (rows.length === 1) {
1922
- newSlug = `${parentFieldSlug}.${field.slug}`;
1923
- } else {
1924
- newSlug = `${parentFieldSlug}[${rowIndex}].${field.slug}`;
1925
- usableRowIndex = 0;
1926
- }
1924
+ newValue = Boolean(newValue);
1927
1925
  }
1928
1926
  if (isMeta && PLURAL_MODEL_ENTITIES_VALUES.includes(newSlug)) {
1929
1927
  newValue = newValue ? Object.entries(newValue).map(([slug, attributes]) => {
1930
1928
  return { slug, ...attributes };
1931
1929
  }) : [];
1932
1930
  }
1933
- records[usableRowIndex] = setProperty(
1934
- records[usableRowIndex],
1935
- newSlug,
1936
- newValue
1937
- );
1931
+ setProperty(acc, newSlug, newValue);
1932
+ return acc;
1933
+ }, {});
1934
+ const existingRecord = record.id ? records.find((existingRecord2) => {
1935
+ return existingRecord2.id === record.id;
1936
+ }) : null;
1937
+ if (!existingRecord) {
1938
+ records.push(record);
1939
+ continue;
1940
+ }
1941
+ 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]);
1946
+ },
1947
+ []
1948
+ );
1949
+ for (const parentField of joinFields) {
1950
+ const currentValue = existingRecord[parentField];
1951
+ const newValue = record[parentField];
1952
+ currentValue.push(...newValue);
1938
1953
  }
1939
1954
  }
1940
1955
  return single ? records[0] : records;
@@ -1979,13 +1994,13 @@ var Transaction = class {
1979
1994
  const single = queryModel !== model.pluralSlug;
1980
1995
  if (single) {
1981
1996
  return {
1982
- record: rows[0] ? this.#formatRows(rawModelFields, rows, single, isMeta) : null,
1997
+ record: rows[0] ? this.#formatRows(rawModelFields, rows, true, isMeta) : null,
1983
1998
  modelFields
1984
1999
  };
1985
2000
  }
1986
2001
  const pageSize = queryInstructions?.limitedTo;
1987
2002
  const output = {
1988
- records: this.#formatRows(rawModelFields, rows, single, isMeta),
2003
+ records: this.#formatRows(rawModelFields, rows, false, isMeta),
1989
2004
  modelFields
1990
2005
  };
1991
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",
3
+ "version": "0.14.0",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {