@ronin/compiler 0.11.0 → 0.11.1

Sign up to get free protection for your applications and to get access to all the features.
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,25 @@ var expand = (obj) => {
131
131
  var getProperty = (obj, path) => {
132
132
  return path.split(".").reduce((acc, key) => acc?.[key], obj);
133
133
  };
134
+ var setProperty = (obj, path, value) => {
135
+ if (!obj) return setProperty({}, path, value);
136
+ const segments = path.split(/[.[\]]/g).filter((x) => !!x.trim());
137
+ const _set = (node) => {
138
+ if (segments.length > 1) {
139
+ const key = segments.shift();
140
+ const nextIsNum = !Number.isNaN(Number.parseInt(segments[0]));
141
+ if (typeof node[key] !== "object" || node[key] === null) {
142
+ node[key] = nextIsNum ? [] : {};
143
+ }
144
+ _set(node[key]);
145
+ } else {
146
+ node[segments[0]] = value;
147
+ }
148
+ };
149
+ const cloned = structuredClone(obj);
150
+ _set(cloned);
151
+ return cloned;
152
+ };
134
153
  var splitQuery = (query) => {
135
154
  const queryType = Object.keys(query)[0];
136
155
  const queryModel = Object.keys(query[queryType])[0];
@@ -1131,11 +1150,10 @@ var handleIncluding = (models, model, statementParams, instruction) => {
1131
1150
  relatedTableSelector = `(${subSelect.main.statement})`;
1132
1151
  }
1133
1152
  statement += `${joinType} JOIN ${relatedTableSelector} as ${tableAlias}`;
1134
- model.tableAlias = model.table;
1153
+ model.tableAlias = model.tableAlias || model.table;
1135
1154
  if (joinType === "LEFT") {
1136
1155
  if (!single) {
1137
1156
  tableSubQuery = `SELECT * FROM "${model.table}" LIMIT 1`;
1138
- model.tableAlias = `sub_${model.table}`;
1139
1157
  }
1140
1158
  const subStatement = composeConditions(
1141
1159
  models,
@@ -1203,7 +1221,11 @@ var handleSelecting = (models, model, statementParams, instructions, options) =>
1203
1221
  isJoining = true;
1204
1222
  const { queryModel, queryInstructions } = splitQuery(symbol.value);
1205
1223
  const subQueryModel = getModelBySlug(models, queryModel);
1206
- const tableName = composeIncludedTableAlias(key);
1224
+ const tableAlias = composeIncludedTableAlias(key);
1225
+ const single = queryModel !== subQueryModel.pluralSlug;
1226
+ if (!single) {
1227
+ model.tableAlias = `sub_${model.table}`;
1228
+ }
1207
1229
  const queryModelFields = queryInstructions?.selecting ? subQueryModel.fields.filter((field) => {
1208
1230
  return queryInstructions.selecting?.includes(field.slug);
1209
1231
  }) : subQueryModel.fields;
@@ -1211,11 +1233,11 @@ var handleSelecting = (models, model, statementParams, instructions, options) =>
1211
1233
  loadedFields.push({ ...field, parentField: key });
1212
1234
  if (options?.expandColumns) {
1213
1235
  const newValue2 = parseFieldExpression(
1214
- { ...subQueryModel, tableAlias: tableName },
1236
+ { ...subQueryModel, tableAlias },
1215
1237
  "including",
1216
1238
  `${QUERY_SYMBOLS.FIELD}${field.slug}`
1217
1239
  );
1218
- instructions.including[`${tableName}.${field.slug}`] = newValue2;
1240
+ instructions.including[`${tableAlias}.${field.slug}`] = newValue2;
1219
1241
  }
1220
1242
  }
1221
1243
  continue;
@@ -1578,23 +1600,34 @@ var Transaction = class {
1578
1600
  this.models = modelListWithPresets;
1579
1601
  return [...dependencyStatements, ...mainStatements];
1580
1602
  };
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);
1603
+ formatRows(fields, rows, single) {
1604
+ const records = [];
1605
+ for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
1606
+ const row = rows[rowIndex];
1607
+ for (let valueIndex = 0; valueIndex < row.length; valueIndex++) {
1608
+ const value = row[valueIndex];
1609
+ const field = fields[valueIndex];
1610
+ let newSlug = field.slug;
1611
+ let newValue = value;
1612
+ if (field.type === "json") {
1613
+ newValue = JSON.parse(value);
1614
+ } else if (field.type === "boolean") {
1615
+ newValue = Boolean(value);
1616
+ }
1617
+ const parentFieldSlug = field.parentField;
1618
+ let usableRowIndex = rowIndex;
1619
+ if (parentFieldSlug) {
1620
+ if (rows.length === 1) {
1621
+ newSlug = `${parentFieldSlug}.${field.slug}`;
1622
+ } else {
1623
+ newSlug = `${parentFieldSlug}[${rowIndex}].${field.slug}`;
1624
+ usableRowIndex = 0;
1625
+ }
1626
+ }
1627
+ records[usableRowIndex] = setProperty(records[usableRowIndex], newSlug, newValue);
1594
1628
  }
1595
- record[newSlug] = newValue;
1596
1629
  }
1597
- return expand(record);
1630
+ return single ? records[0] : records;
1598
1631
  }
1599
1632
  /**
1600
1633
  * Format the results returned from the database into RONIN records.
@@ -1629,11 +1662,11 @@ var Transaction = class {
1629
1662
  }
1630
1663
  const single = queryModel !== model.pluralSlug;
1631
1664
  if (single) {
1632
- return { record: this.formatRow(fields, rows[0]) };
1665
+ return { record: rows[0] ? this.formatRows(fields, rows, single) : null };
1633
1666
  }
1634
1667
  const pageSize = queryInstructions?.limitedTo;
1635
1668
  const output = {
1636
- records: rows.map((row) => this.formatRow(fields, row))
1669
+ records: this.formatRows(fields, rows, single)
1637
1670
  };
1638
1671
  if (pageSize && output.records.length > 0) {
1639
1672
  if (queryInstructions?.before || queryInstructions?.after) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ronin/compiler",
3
- "version": "0.11.0",
3
+ "version": "0.11.1",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {