@ronin/compiler 0.14.8 → 0.14.9-leo-ron-1099-1-experimental-342

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/README.md CHANGED
@@ -127,26 +127,7 @@ new Transaction(queries, {
127
127
  // Instead of returning an array of parameters for every statement (which allows for
128
128
  // preventing SQL injections), all parameters are inlined directly into the SQL strings.
129
129
  // This option should only be used if the generated SQL will be manually verified.
130
- inlineParams: true,
131
-
132
- // By default, in the generated SQL statements, the compiler does not alias columns if
133
- // multiple different tables with the same column names are being joined. Only the table
134
- // names themselves are aliased.
135
- //
136
- // This ensures the cleanest possible SQL statements in conjunction with the default
137
- // behavior of SQL databases, where the result of a statement is a list (array) of
138
- // values, which are inherently not prone to conflicts.
139
- //
140
- // If the driver being used instead returns an object for every row, the driver must
141
- // ensure the uniqueness of every key in that object, which means prefixing duplicated
142
- // column names with the name of the respective table, if multiple tables are joined
143
- // (example for an object key: "table_name.column_name").
144
- //
145
- // Drivers that return objects for rows offer this behavior as an option that is
146
- // usually called "expand columns". If the driver being used does not offer such an
147
- // option, you can instead activate the option in the compiler, which results in longer
148
- // SQL statements because all column names are aliased.
149
- expandColumns: true
130
+ inlineParams: true
150
131
  });
151
132
  ```
152
133
 
package/dist/index.d.ts CHANGED
@@ -361,7 +361,7 @@ type PublicModel<T extends Array<ModelField> = Array<ModelField>> = Omit<Partial
361
361
 
362
362
  type RawRow = Array<unknown>;
363
363
  type ObjectRow = Record<string, unknown>;
364
- type NativeRecord = Record<string, unknown> & {
364
+ type ResultRecord = Record<string, unknown> & {
365
365
  id: string;
366
366
  ronin: {
367
367
  locked: boolean;
@@ -371,11 +371,11 @@ type NativeRecord = Record<string, unknown> & {
371
371
  updatedBy: string | null;
372
372
  };
373
373
  };
374
- type SingleRecordResult<T = NativeRecord> = {
374
+ type SingleRecordResult<T = ResultRecord> = {
375
375
  record: T | null;
376
376
  modelFields: Record<ModelField['slug'], ModelField['type']>;
377
377
  };
378
- type MultipleRecordResult<T = NativeRecord> = {
378
+ type MultipleRecordResult<T = ResultRecord> = {
379
379
  records: Array<T>;
380
380
  moreAfter?: string;
381
381
  moreBefore?: string;
@@ -384,7 +384,7 @@ type MultipleRecordResult<T = NativeRecord> = {
384
384
  type AmountResult = {
385
385
  amount: number;
386
386
  };
387
- type Result<T = NativeRecord> = SingleRecordResult<T> | MultipleRecordResult<T> | AmountResult;
387
+ type Result<T = ResultRecord> = SingleRecordResult<T> | MultipleRecordResult<T> | AmountResult;
388
388
 
389
389
  interface TransactionOptions {
390
390
  /** A list of models that already exist in the database. */
@@ -394,8 +394,6 @@ interface TransactionOptions {
394
394
  * separating them out into a dedicated `params` array.
395
395
  */
396
396
  inlineParams?: boolean;
397
- /** Alias column names that are duplicated when joining multiple tables. */
398
- expandColumns?: boolean;
399
397
  }
400
398
  declare class Transaction {
401
399
  #private;
@@ -408,4 +406,4 @@ declare class Transaction {
408
406
 
409
407
  declare const CLEAN_ROOT_MODEL: PublicModel;
410
408
 
411
- export { type AddInstructions, type AddQuery, type AddInstructions as AddQueryInstructions, type AlterQuery, type CombinedInstructions, type CountInstructions, type CountQuery, type CountInstructions as CountQueryInstructions, type CreateQuery, type DropQuery, type GetInstructions, type GetQuery, type GetInstructions as GetQueryInstructions, type PublicModel as Model, type ModelField, type ModelIndex, type ModelPreset, type ModelTrigger, QUERY_SYMBOLS, type Query, type QueryInstructionType as QueryInstruction, type QuerySchemaType, type QueryType, CLEAN_ROOT_MODEL as ROOT_MODEL, type RemoveInstructions, type RemoveQuery, type RemoveInstructions as RemoveQueryInstructions, type Result, RoninError, type SetInstructions, type SetQuery, type SetInstructions as SetQueryInstructions, type Statement, type StoredObject, Transaction, type WithInstruction, getQuerySymbol };
409
+ export { type AddInstructions, type AddQuery, type AddInstructions as AddQueryInstructions, type AlterQuery, type CombinedInstructions, type CountInstructions, type CountQuery, type CountInstructions as CountQueryInstructions, type CreateQuery, type DropQuery, type GetInstructions, type GetQuery, type GetInstructions as GetQueryInstructions, type PublicModel as Model, type ModelField, type ModelIndex, type ModelPreset, type ModelTrigger, QUERY_SYMBOLS, type Query, type QueryInstructionType as QueryInstruction, type QuerySchemaType, type QueryType, CLEAN_ROOT_MODEL as ROOT_MODEL, type RemoveInstructions, type RemoveQuery, type RemoveInstructions as RemoveQueryInstructions, type Result, type ResultRecord, RoninError, type SetInstructions, type SetQuery, type SetInstructions as SetQueryInstructions, type Statement, type StoredObject, Transaction, type WithInstruction, getQuerySymbol };
package/dist/index.js CHANGED
@@ -424,7 +424,6 @@ var handleSelecting = (models, model, statementParams, single, instructions, opt
424
424
  }
425
425
  return newField;
426
426
  });
427
- if (instructions.selecting) options.expandColumns = true;
428
427
  const joinedSelectedFields = [];
429
428
  const joinedColumns = [];
430
429
  if (instructions.including) {
@@ -440,7 +439,6 @@ var handleSelecting = (models, model, statementParams, single, instructions, opt
440
439
  const { queryModel, queryInstructions } = splitQuery(symbol2.value);
441
440
  const subQueryModel = getModelBySlug(models, queryModel);
442
441
  isJoining = true;
443
- if (queryInstructions?.selecting) options.expandColumns = true;
444
442
  const subSingle = queryModel !== subQueryModel.pluralSlug;
445
443
  if (!model.tableAlias)
446
444
  model.tableAlias = single && !subSingle ? `sub_${model.table}` : model.table;
@@ -478,11 +476,7 @@ var handleSelecting = (models, model, statementParams, single, instructions, opt
478
476
  });
479
477
  }
480
478
  }
481
- let columns = ["*"];
482
- const fieldsToExpand = options.expandColumns ? selectedFields : selectedFields.filter(
483
- (loadedField) => typeof loadedField.mountedValue !== "undefined"
484
- );
485
- const extraColumns = fieldsToExpand.map((selectedField) => {
479
+ const columns = selectedFields.map((selectedField) => {
486
480
  if (selectedField.mountedValue) {
487
481
  return `${selectedField.mountedValue} as "${selectedField.slug}"`;
488
482
  }
@@ -494,11 +488,6 @@ var handleSelecting = (models, model, statementParams, single, instructions, opt
494
488
  }
495
489
  return fieldSelector;
496
490
  });
497
- if (options.expandColumns) {
498
- columns = extraColumns;
499
- } else if (extraColumns) {
500
- columns.push(...extraColumns);
501
- }
502
491
  columns.push(...joinedColumns);
503
492
  selectedFields.push(...joinedSelectedFields);
504
493
  return { columns: columns.join(", "), isJoining, selectedFields };
@@ -631,6 +620,15 @@ var compileQueryInput = (defaultQuery, models, statementParams, options) => {
631
620
  if (instructions && Object.hasOwn(instructions, "for")) {
632
621
  instructions = handleFor(model, instructions);
633
622
  }
623
+ if (queryType === "count") {
624
+ if (!instructions) instructions = {};
625
+ instructions.selecting = ["amount"];
626
+ instructions.including = Object.assign(instructions?.including || {}, {
627
+ amount: {
628
+ [QUERY_SYMBOLS.EXPRESSION]: "COUNT(*)"
629
+ }
630
+ });
631
+ }
634
632
  const { columns, isJoining, selectedFields } = handleSelecting(
635
633
  models,
636
634
  model,
@@ -645,6 +643,7 @@ var compileQueryInput = (defaultQuery, models, statementParams, options) => {
645
643
  let statement = "";
646
644
  switch (queryType) {
647
645
  case "get":
646
+ case "count":
648
647
  statement += `SELECT ${columns} FROM `;
649
648
  break;
650
649
  case "set":
@@ -656,9 +655,6 @@ var compileQueryInput = (defaultQuery, models, statementParams, options) => {
656
655
  case "remove":
657
656
  statement += "DELETE FROM ";
658
657
  break;
659
- case "count":
660
- statement += `SELECT COUNT(${columns}) FROM `;
661
- break;
662
658
  }
663
659
  let isJoiningMultipleRows = false;
664
660
  if (isJoining) {
@@ -1988,8 +1984,7 @@ var Transaction = class {
1988
1984
  const { dependencies, main, selectedFields } = compileQueryInput(
1989
1985
  query,
1990
1986
  modelsWithPresets,
1991
- options?.inlineParams ? null : [],
1992
- { expandColumns: options?.expandColumns }
1987
+ options?.inlineParams ? null : []
1993
1988
  );
1994
1989
  const preDependencies = dependencies.filter(({ after }) => !after);
1995
1990
  const postDependencies = dependencies.map(({ after, ...rest }) => after ? rest : null).filter((item) => item != null);
@@ -2092,9 +2087,10 @@ var Transaction = class {
2092
2087
  */
2093
2088
  formatResults(results, raw = true) {
2094
2089
  const normalizedResults = raw ? results : results.map((rows) => {
2095
- return rows.map((row) => {
2090
+ return rows.map((row, index) => {
2091
+ const { query } = this.#internalStatements[index];
2096
2092
  if (Array.isArray(row)) return row;
2097
- if (row["COUNT(*)"]) return [row["COUNT(*)"]];
2093
+ if (query.count) return [row.amount];
2098
2094
  return Object.values(row);
2099
2095
  });
2100
2096
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ronin/compiler",
3
- "version": "0.14.8",
3
+ "version": "0.14.9-leo-ron-1099-1-experimental-342",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {