@ronin/compiler 0.11.0 → 0.11.1-default-value-experimental-231

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
@@ -20,6 +20,7 @@ var RONIN_MODEL_FIELD_REGEX = new RegExp(
20
20
  `${QUERY_SYMBOLS.FIELD}[_a-zA-Z0-9.]+`,
21
21
  "g"
22
22
  );
23
+ var RAW_FIELD_TYPES = ["string", "number", "boolean"];
23
24
  var composeIncludedTableAlias = (fieldSlug) => `including_${fieldSlug}`;
24
25
  var MODEL_ENTITY_ERROR_CODES = {
25
26
  field: "FIELD_NOT_FOUND",
@@ -131,6 +132,25 @@ var expand = (obj) => {
131
132
  var getProperty = (obj, path) => {
132
133
  return path.split(".").reduce((acc, key) => acc?.[key], obj);
133
134
  };
135
+ var setProperty = (obj, path, value) => {
136
+ if (!obj) return setProperty({}, path, value);
137
+ const segments = path.split(/[.[\]]/g).filter((x) => !!x.trim());
138
+ const _set = (node) => {
139
+ if (segments.length > 1) {
140
+ const key = segments.shift();
141
+ const nextIsNum = !Number.isNaN(Number.parseInt(segments[0]));
142
+ if (typeof node[key] !== "object" || node[key] === null) {
143
+ node[key] = nextIsNum ? [] : {};
144
+ }
145
+ _set(node[key]);
146
+ } else {
147
+ node[segments[0]] = value;
148
+ }
149
+ };
150
+ const cloned = structuredClone(obj);
151
+ _set(cloned);
152
+ return cloned;
153
+ };
134
154
  var splitQuery = (query) => {
135
155
  const queryType = Object.keys(query)[0];
136
156
  const queryModel = Object.keys(query[queryType])[0];
@@ -623,7 +643,7 @@ var getFieldStatement = (models, model, field) => {
623
643
  if (field.unique === true) statement += " UNIQUE";
624
644
  if (field.required === true) statement += " NOT NULL";
625
645
  if (typeof field.defaultValue !== "undefined")
626
- statement += ` DEFAULT ${field.defaultValue}`;
646
+ statement += ` DEFAULT ${typeof field.defaultValue === "string" ? `'${field.defaultValue}'` : field.defaultValue}`;
627
647
  if (field.collation) statement += ` COLLATE ${field.collation}`;
628
648
  if (field.increment === true) statement += " AUTOINCREMENT";
629
649
  if (typeof field.check !== "undefined") {
@@ -1131,12 +1151,8 @@ var handleIncluding = (models, model, statementParams, instruction) => {
1131
1151
  relatedTableSelector = `(${subSelect.main.statement})`;
1132
1152
  }
1133
1153
  statement += `${joinType} JOIN ${relatedTableSelector} as ${tableAlias}`;
1134
- model.tableAlias = model.table;
1154
+ model.tableAlias = model.tableAlias || model.table;
1135
1155
  if (joinType === "LEFT") {
1136
- if (!single) {
1137
- tableSubQuery = `SELECT * FROM "${model.table}" LIMIT 1`;
1138
- model.tableAlias = `sub_${model.table}`;
1139
- }
1140
1156
  const subStatement = composeConditions(
1141
1157
  models,
1142
1158
  { ...relatedModel, tableAlias },
@@ -1149,6 +1165,7 @@ var handleIncluding = (models, model, statementParams, instruction) => {
1149
1165
  );
1150
1166
  statement += ` ON (${subStatement})`;
1151
1167
  }
1168
+ if (!single) tableSubQuery = `SELECT * FROM "${model.table}" LIMIT 1`;
1152
1169
  }
1153
1170
  return { statement, tableSubQuery };
1154
1171
  };
@@ -1203,7 +1220,11 @@ var handleSelecting = (models, model, statementParams, instructions, options) =>
1203
1220
  isJoining = true;
1204
1221
  const { queryModel, queryInstructions } = splitQuery(symbol.value);
1205
1222
  const subQueryModel = getModelBySlug(models, queryModel);
1206
- const tableName = composeIncludedTableAlias(key);
1223
+ const tableAlias = composeIncludedTableAlias(key);
1224
+ const single = queryModel !== subQueryModel.pluralSlug;
1225
+ if (!single) {
1226
+ model.tableAlias = `sub_${model.table}`;
1227
+ }
1207
1228
  const queryModelFields = queryInstructions?.selecting ? subQueryModel.fields.filter((field) => {
1208
1229
  return queryInstructions.selecting?.includes(field.slug);
1209
1230
  }) : subQueryModel.fields;
@@ -1211,11 +1232,11 @@ var handleSelecting = (models, model, statementParams, instructions, options) =>
1211
1232
  loadedFields.push({ ...field, parentField: key });
1212
1233
  if (options?.expandColumns) {
1213
1234
  const newValue2 = parseFieldExpression(
1214
- { ...subQueryModel, tableAlias: tableName },
1235
+ { ...subQueryModel, tableAlias },
1215
1236
  "including",
1216
1237
  `${QUERY_SYMBOLS.FIELD}${field.slug}`
1217
1238
  );
1218
- instructions.including[`${tableName}.${field.slug}`] = newValue2;
1239
+ instructions.including[`${tableAlias}.${field.slug}`] = newValue2;
1219
1240
  }
1220
1241
  }
1221
1242
  continue;
@@ -1227,6 +1248,10 @@ var handleSelecting = (models, model, statementParams, instructions, options) =>
1227
1248
  newValue = prepareStatementValue(statementParams, value);
1228
1249
  }
1229
1250
  instructions.including[key] = newValue;
1251
+ loadedFields.push({
1252
+ slug: key,
1253
+ type: RAW_FIELD_TYPES.includes(typeof value) ? typeof value : "string"
1254
+ });
1230
1255
  }
1231
1256
  }
1232
1257
  const expandColumns = isJoining && options?.expandColumns;
@@ -1578,23 +1603,34 @@ var Transaction = class {
1578
1603
  this.models = modelListWithPresets;
1579
1604
  return [...dependencyStatements, ...mainStatements];
1580
1605
  };
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);
1606
+ formatRows(fields, rows, single) {
1607
+ const records = [];
1608
+ for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
1609
+ const row = rows[rowIndex];
1610
+ for (let valueIndex = 0; valueIndex < row.length; valueIndex++) {
1611
+ const value = row[valueIndex];
1612
+ const field = fields[valueIndex];
1613
+ let newSlug = field.slug;
1614
+ let newValue = value;
1615
+ if (field.type === "json") {
1616
+ newValue = JSON.parse(value);
1617
+ } else if (field.type === "boolean") {
1618
+ newValue = Boolean(value);
1619
+ }
1620
+ const parentFieldSlug = field.parentField;
1621
+ let usableRowIndex = rowIndex;
1622
+ if (parentFieldSlug) {
1623
+ if (rows.length === 1) {
1624
+ newSlug = `${parentFieldSlug}.${field.slug}`;
1625
+ } else {
1626
+ newSlug = `${parentFieldSlug}[${rowIndex}].${field.slug}`;
1627
+ usableRowIndex = 0;
1628
+ }
1629
+ }
1630
+ records[usableRowIndex] = setProperty(records[usableRowIndex], newSlug, newValue);
1594
1631
  }
1595
- record[newSlug] = newValue;
1596
1632
  }
1597
- return expand(record);
1633
+ return single ? records[0] : records;
1598
1634
  }
1599
1635
  /**
1600
1636
  * Format the results returned from the database into RONIN records.
@@ -1629,11 +1665,11 @@ var Transaction = class {
1629
1665
  }
1630
1666
  const single = queryModel !== model.pluralSlug;
1631
1667
  if (single) {
1632
- return { record: this.formatRow(fields, rows[0]) };
1668
+ return { record: rows[0] ? this.formatRows(fields, rows, single) : null };
1633
1669
  }
1634
1670
  const pageSize = queryInstructions?.limitedTo;
1635
1671
  const output = {
1636
- records: rows.map((row) => this.formatRow(fields, row))
1672
+ records: this.formatRows(fields, rows, single)
1637
1673
  };
1638
1674
  if (pageSize && output.records.length > 0) {
1639
1675
  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-default-value-experimental-231",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {