@ronin/compiler 0.10.3-leo-ron-1083-experimental-224 → 0.11.0-leo-ron-1083-experimental-225

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
@@ -5989,8 +5989,11 @@ type ObjectRow = Record<string, unknown>;
5989
5989
  type NativeRecord = Record<string, unknown> & {
5990
5990
  id: string;
5991
5991
  ronin: {
5992
+ locked: boolean;
5992
5993
  createdAt: Date;
5994
+ createdBy: string | null;
5993
5995
  updatedAt: Date;
5996
+ updatedBy: string | null;
5994
5997
  };
5995
5998
  };
5996
5999
  type SingleRecordResult = {
@@ -6072,7 +6075,7 @@ declare class Transaction {
6072
6075
  * @returns The composed SQL statements.
6073
6076
  */
6074
6077
  private compileQueries;
6075
- private formatRow;
6078
+ private formatRows;
6076
6079
  formatResults(results: Array<Array<RawRow>>, raw?: true): Array<Result>;
6077
6080
  formatResults(results: Array<Array<ObjectRow>>, raw?: false): Array<Result>;
6078
6081
  }
package/dist/index.js CHANGED
@@ -131,6 +131,32 @@ var expand = (obj) => {
131
131
  var getProperty = (obj, path) => {
132
132
  return path.split(".").reduce((acc, key) => acc?.[key], obj);
133
133
  };
134
+ var toInt = (value, defaultValue) => {
135
+ const def = defaultValue === void 0 ? 0 : defaultValue;
136
+ if (value === null || value === void 0) return def;
137
+ const result = Number.parseInt(value);
138
+ return Number.isNaN(result) ? def : result;
139
+ };
140
+ var setProperty = (initial, path, value) => {
141
+ if (!initial) return setProperty({}, path, value);
142
+ if (!path || value === void 0) return initial;
143
+ const segments = path.split(/[.[\]]/g).filter((x) => !!x.trim());
144
+ const _set = (node) => {
145
+ if (segments.length > 1) {
146
+ const key = segments.shift();
147
+ const nextIsNum = toInt(segments[0], null) !== null;
148
+ if (typeof node[key] !== "object" || node[key] === null) {
149
+ node[key] = nextIsNum ? [] : {};
150
+ }
151
+ _set(node[key]);
152
+ } else {
153
+ node[segments[0]] = value;
154
+ }
155
+ };
156
+ const cloned = structuredClone(initial);
157
+ _set(cloned);
158
+ return cloned;
159
+ };
134
160
  var splitQuery = (query) => {
135
161
  const queryType = Object.keys(query)[0];
136
162
  const queryModel = Object.keys(query[queryType])[0];
@@ -1131,11 +1157,10 @@ var handleIncluding = (models, model, statementParams, instruction) => {
1131
1157
  relatedTableSelector = `(${subSelect.main.statement})`;
1132
1158
  }
1133
1159
  statement += `${joinType} JOIN ${relatedTableSelector} as ${tableAlias}`;
1134
- model.tableAlias = model.table;
1160
+ model.tableAlias = model.tableAlias || model.table;
1135
1161
  if (joinType === "LEFT") {
1136
1162
  if (!single) {
1137
1163
  tableSubQuery = `SELECT * FROM "${model.table}" LIMIT 1`;
1138
- model.tableAlias = `sub_${model.table}`;
1139
1164
  }
1140
1165
  const subStatement = composeConditions(
1141
1166
  models,
@@ -1203,7 +1228,11 @@ var handleSelecting = (models, model, statementParams, instructions, options) =>
1203
1228
  isJoining = true;
1204
1229
  const { queryModel, queryInstructions } = splitQuery(symbol.value);
1205
1230
  const subQueryModel = getModelBySlug(models, queryModel);
1206
- const tableName = composeIncludedTableAlias(key);
1231
+ const tableAlias = composeIncludedTableAlias(key);
1232
+ const single = queryModel !== subQueryModel.pluralSlug;
1233
+ if (!single) {
1234
+ model.tableAlias = `sub_${model.table}`;
1235
+ }
1207
1236
  const queryModelFields = queryInstructions?.selecting ? subQueryModel.fields.filter((field) => {
1208
1237
  return queryInstructions.selecting?.includes(field.slug);
1209
1238
  }) : subQueryModel.fields;
@@ -1211,11 +1240,11 @@ var handleSelecting = (models, model, statementParams, instructions, options) =>
1211
1240
  loadedFields.push({ ...field, parentField: key });
1212
1241
  if (options?.expandColumns) {
1213
1242
  const newValue2 = parseFieldExpression(
1214
- { ...subQueryModel, tableAlias: tableName },
1243
+ { ...subQueryModel, tableAlias },
1215
1244
  "including",
1216
1245
  `${QUERY_SYMBOLS.FIELD}${field.slug}`
1217
1246
  );
1218
- instructions.including[`${tableName}.${field.slug}`] = newValue2;
1247
+ instructions.including[`${tableAlias}.${field.slug}`] = newValue2;
1219
1248
  }
1220
1249
  }
1221
1250
  continue;
@@ -1578,23 +1607,34 @@ var Transaction = class {
1578
1607
  this.models = modelListWithPresets;
1579
1608
  return [...dependencyStatements, ...mainStatements];
1580
1609
  };
1581
- formatRow(fields, row) {
1582
- const record = {};
1583
- for (let index = 0; index < row.length; index++) {
1584
- const value = row[index];
1585
- const field = fields[index];
1586
- let newSlug = field.slug;
1587
- let newValue = value;
1588
- const parentFieldSlug = field.parentField;
1589
- if (parentFieldSlug) {
1590
- newSlug = `${parentFieldSlug}.${field.slug}`;
1591
- }
1592
- if (field.type === "json") {
1593
- newValue = JSON.parse(value);
1610
+ formatRows(fields, rows, single) {
1611
+ const records = [];
1612
+ for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
1613
+ const row = rows[rowIndex];
1614
+ for (let valueIndex = 0; valueIndex < row.length; valueIndex++) {
1615
+ const value = row[valueIndex];
1616
+ const field = fields[valueIndex];
1617
+ let newSlug = field.slug;
1618
+ let newValue = value;
1619
+ if (field.type === "json") {
1620
+ newValue = JSON.parse(value);
1621
+ } else if (field.type === "boolean") {
1622
+ newValue = Boolean(value);
1623
+ }
1624
+ const parentFieldSlug = field.parentField;
1625
+ if (parentFieldSlug) {
1626
+ if (rows.length === 1) {
1627
+ newSlug = `${parentFieldSlug}.${field.slug}`;
1628
+ } else {
1629
+ const fieldPath = `${parentFieldSlug}[${rowIndex}].${field.slug}`;
1630
+ records[0] = setProperty(records[0], fieldPath, newValue);
1631
+ continue;
1632
+ }
1633
+ }
1634
+ records[rowIndex] = setProperty(records[rowIndex], newSlug, newValue);
1594
1635
  }
1595
- record[newSlug] = newValue;
1596
1636
  }
1597
- return expand(record);
1637
+ return single ? records[0] : records;
1598
1638
  }
1599
1639
  /**
1600
1640
  * Format the results returned from the database into RONIN records.
@@ -1629,11 +1669,11 @@ var Transaction = class {
1629
1669
  }
1630
1670
  const single = queryModel !== model.pluralSlug;
1631
1671
  if (single) {
1632
- return { record: this.formatRow(fields, rows[0]) };
1672
+ return { record: rows[0] ? this.formatRows(fields, rows, single) : null };
1633
1673
  }
1634
1674
  const pageSize = queryInstructions?.limitedTo;
1635
1675
  const output = {
1636
- records: rows.map((row) => this.formatRow(fields, row))
1676
+ records: this.formatRows(fields, rows, single)
1637
1677
  };
1638
1678
  if (pageSize && output.records.length > 0) {
1639
1679
  if (queryInstructions?.before || queryInstructions?.after) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ronin/compiler",
3
- "version": "0.10.3-leo-ron-1083-experimental-224",
3
+ "version": "0.11.0-leo-ron-1083-experimental-225",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {