@ronin/compiler 0.15.0 → 0.16.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
@@ -391,7 +391,9 @@ type MultipleRecordResult<T = ResultRecord> = {
391
391
  type AmountResult = {
392
392
  amount: number;
393
393
  };
394
- type Result<T = ResultRecord> = SingleRecordResult<T> | MultipleRecordResult<T> | AmountResult;
394
+ type RegularResult<T = ResultRecord> = SingleRecordResult<T> | MultipleRecordResult<T> | AmountResult;
395
+ type ExpandedResult<T = ResultRecord> = Record<Model['slug'], RegularResult<T>>;
396
+ type Result<T = ResultRecord> = RegularResult<T> | ExpandedResult<T>;
395
397
 
396
398
  interface TransactionOptions {
397
399
  /** A list of models that already exist in the database. */
@@ -407,8 +409,8 @@ declare class Transaction {
407
409
  statements: Array<Statement>;
408
410
  models: Array<Model>;
409
411
  constructor(queries: Array<Query>, options?: TransactionOptions);
410
- formatResults<Record>(results: Array<Array<ObjectRow>>, raw?: false): Array<Result<Record>>;
411
- formatResults<Record>(results: Array<Array<RawRow>>, raw?: true): Array<Result<Record>>;
412
+ formatResults<RecordType>(results: Array<Array<ObjectRow>>, raw?: false): Array<Result<RecordType>>;
413
+ formatResults<RecordType>(results: Array<Array<RawRow>>, raw?: true): Array<Result<RecordType>>;
412
414
  }
413
415
 
414
416
  declare const CLEAN_ROOT_MODEL: PublicModel;
package/dist/index.js CHANGED
@@ -566,8 +566,14 @@ var handleTo = (models, model, statementParams, queryType, dependencyStatements,
566
566
  // src/instructions/using.ts
567
567
  var handleUsing = (model, instructions) => {
568
568
  const normalizedUsing = Array.isArray(instructions.using) ? Object.fromEntries(instructions.using.map((presetSlug) => [presetSlug, null])) : instructions.using;
569
+ if ("links" in normalizedUsing) {
570
+ for (const field of model.fields) {
571
+ if (field.type !== "link" || field.kind === "many") continue;
572
+ normalizedUsing[field.slug] = null;
573
+ }
574
+ }
569
575
  for (const presetSlug in normalizedUsing) {
570
- if (!Object.hasOwn(normalizedUsing, presetSlug)) continue;
576
+ if (!Object.hasOwn(normalizedUsing, presetSlug) || presetSlug === "links") continue;
571
577
  const arg = normalizedUsing[presetSlug];
572
578
  const preset = model.presets?.find((preset2) => preset2.slug === presetSlug);
573
579
  if (!preset) {
@@ -1509,6 +1515,7 @@ var ROOT_MODEL = {
1509
1515
  { slug: "presets", type: "json", defaultValue: "{}" }
1510
1516
  ]
1511
1517
  };
1518
+ var ROOT_MODEL_WITH_ATTRIBUTES = addDefaultModelAttributes(ROOT_MODEL, true);
1512
1519
  var getSystemModels = (models, model) => {
1513
1520
  const addedModels = [];
1514
1521
  for (const field of model.fields || []) {
@@ -1990,14 +1997,14 @@ var Transaction = class {
1990
1997
  * @returns The composed SQL statements.
1991
1998
  */
1992
1999
  #compileQueries = (queries, models, options) => {
1993
- const modelsWithAttributes = [ROOT_MODEL, ...models].map((model) => {
2000
+ const modelsWithAttributes = models.map((model) => {
1994
2001
  return addDefaultModelAttributes(model, true);
1995
2002
  });
1996
2003
  const modelsWithFields = [
1997
2004
  ...modelsWithAttributes.flatMap((model) => {
1998
2005
  return getSystemModels(modelsWithAttributes, model);
1999
2006
  }),
2000
- ...modelsWithAttributes
2007
+ ...[ROOT_MODEL_WITH_ATTRIBUTES, ...modelsWithAttributes]
2001
2008
  ].map((model) => {
2002
2009
  return addDefaultModelFields(model, true);
2003
2010
  });
@@ -2005,7 +2012,19 @@ var Transaction = class {
2005
2012
  return addDefaultModelPresets(modelsWithFields, model);
2006
2013
  });
2007
2014
  const statements = [];
2008
- for (const query of queries) {
2015
+ const expandedQueries = queries.flatMap((query, expansionIndex) => {
2016
+ const { queryType, queryModel, queryInstructions } = splitQuery(query);
2017
+ if (queryModel === "all") {
2018
+ return modelsWithAttributes.map((model) => {
2019
+ const query2 = {
2020
+ [queryType]: { [model.pluralSlug]: queryInstructions }
2021
+ };
2022
+ return { query: query2, expansionIndex };
2023
+ });
2024
+ }
2025
+ return { query };
2026
+ });
2027
+ for (const { query, expansionIndex } of expandedQueries) {
2009
2028
  const { dependencies, main, selectedFields } = compileQueryInput(
2010
2029
  query,
2011
2030
  modelsWithPresets,
@@ -2019,7 +2038,8 @@ var Transaction = class {
2019
2038
  ...subStatements.map((statement) => ({
2020
2039
  ...statement,
2021
2040
  query,
2022
- selectedFields
2041
+ selectedFields,
2042
+ expansionIndex
2023
2043
  }))
2024
2044
  );
2025
2045
  }
@@ -2119,10 +2139,19 @@ var Transaction = class {
2119
2139
  return Object.values(row);
2120
2140
  });
2121
2141
  });
2122
- const formattedResults = normalizedResults.map(
2123
- (rows, index) => {
2124
- const { returning, query, selectedFields } = this.#internalStatements[index];
2125
- if (!returning) return null;
2142
+ return normalizedResults.reduce(
2143
+ (finalResults, rows, index) => {
2144
+ const { returning, query, selectedFields, expansionIndex } = this.#internalStatements[index];
2145
+ if (!returning) return finalResults;
2146
+ const addResult = (result2) => {
2147
+ if (typeof expansionIndex !== "undefined") {
2148
+ if (!finalResults[expansionIndex]) finalResults[expansionIndex] = {};
2149
+ finalResults[expansionIndex][queryModel] = result2;
2150
+ } else {
2151
+ finalResults.push(result2);
2152
+ }
2153
+ return finalResults;
2154
+ };
2126
2155
  const { queryType, queryModel, queryInstructions } = splitQuery(query);
2127
2156
  const model = getModelBySlug(this.models, queryModel);
2128
2157
  const isMeta = queryModel === "model" || queryModel === "models";
@@ -2130,32 +2159,32 @@ var Transaction = class {
2130
2159
  model.fields.map((field) => [field.slug, field.type])
2131
2160
  );
2132
2161
  if (queryType === "count") {
2133
- return { amount: rows[0][0] };
2162
+ return addResult({ amount: rows[0][0] });
2134
2163
  }
2135
2164
  const single = queryModel !== model.pluralSlug;
2136
2165
  if (single) {
2137
- return {
2166
+ return addResult({
2138
2167
  record: rows[0] ? this.#formatRows(selectedFields, rows, true, isMeta) : null,
2139
2168
  modelFields
2140
- };
2169
+ });
2141
2170
  }
2142
2171
  const pageSize = queryInstructions?.limitedTo;
2143
- const output = {
2172
+ const result = {
2144
2173
  records: this.#formatRows(selectedFields, rows, false, isMeta),
2145
2174
  modelFields
2146
2175
  };
2147
- if (pageSize && output.records.length > 0) {
2148
- if (output.records.length > pageSize) {
2176
+ if (pageSize && result.records.length > 0) {
2177
+ if (result.records.length > pageSize) {
2149
2178
  if (queryInstructions?.before) {
2150
- output.records.shift();
2179
+ result.records.shift();
2151
2180
  } else {
2152
- output.records.pop();
2181
+ result.records.pop();
2153
2182
  }
2154
2183
  const direction = queryInstructions?.before ? "moreBefore" : "moreAfter";
2155
- const lastRecord = output.records.at(
2184
+ const lastRecord = result.records.at(
2156
2185
  direction === "moreAfter" ? -1 : 0
2157
2186
  );
2158
- output[direction] = generatePaginationCursor(
2187
+ result[direction] = generatePaginationCursor(
2159
2188
  model,
2160
2189
  queryInstructions.orderedBy,
2161
2190
  lastRecord
@@ -2163,20 +2192,20 @@ var Transaction = class {
2163
2192
  }
2164
2193
  if (queryInstructions?.before || queryInstructions?.after) {
2165
2194
  const direction = queryInstructions?.before ? "moreAfter" : "moreBefore";
2166
- const firstRecord = output.records.at(
2195
+ const firstRecord = result.records.at(
2167
2196
  direction === "moreAfter" ? -1 : 0
2168
2197
  );
2169
- output[direction] = generatePaginationCursor(
2198
+ result[direction] = generatePaginationCursor(
2170
2199
  model,
2171
2200
  queryInstructions.orderedBy,
2172
2201
  firstRecord
2173
2202
  );
2174
2203
  }
2175
2204
  }
2176
- return output;
2177
- }
2205
+ return addResult(result);
2206
+ },
2207
+ []
2178
2208
  );
2179
- return formattedResults.filter((result) => result !== null);
2180
2209
  }
2181
2210
  };
2182
2211
  var CLEAN_ROOT_MODEL = omit(ROOT_MODEL, ["system"]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ronin/compiler",
3
- "version": "0.15.0",
3
+ "version": "0.16.0",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {