@ronin/compiler 0.15.0-leo-ron-1099-1-experimental-362 → 0.15.0-leo-ron-1099-1-experimental-364

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
@@ -1515,6 +1515,7 @@ var ROOT_MODEL = {
1515
1515
  { slug: "presets", type: "json", defaultValue: "{}" }
1516
1516
  ]
1517
1517
  };
1518
+ var ROOT_MODEL_WITH_ATTRIBUTES = addDefaultModelAttributes(ROOT_MODEL, true);
1518
1519
  var getSystemModels = (models, model) => {
1519
1520
  const addedModels = [];
1520
1521
  for (const field of model.fields || []) {
@@ -1996,14 +1997,14 @@ var Transaction = class {
1996
1997
  * @returns The composed SQL statements.
1997
1998
  */
1998
1999
  #compileQueries = (queries, models, options) => {
1999
- const modelsWithAttributes = [ROOT_MODEL, ...models].map((model) => {
2000
+ const modelsWithAttributes = models.map((model) => {
2000
2001
  return addDefaultModelAttributes(model, true);
2001
2002
  });
2002
2003
  const modelsWithFields = [
2003
2004
  ...modelsWithAttributes.flatMap((model) => {
2004
2005
  return getSystemModels(modelsWithAttributes, model);
2005
2006
  }),
2006
- ...modelsWithAttributes
2007
+ ...[ROOT_MODEL_WITH_ATTRIBUTES, ...modelsWithAttributes]
2007
2008
  ].map((model) => {
2008
2009
  return addDefaultModelFields(model, true);
2009
2010
  });
@@ -2011,7 +2012,19 @@ var Transaction = class {
2011
2012
  return addDefaultModelPresets(modelsWithFields, model);
2012
2013
  });
2013
2014
  const statements = [];
2014
- 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) {
2015
2028
  const { dependencies, main, selectedFields } = compileQueryInput(
2016
2029
  query,
2017
2030
  modelsWithPresets,
@@ -2025,7 +2038,8 @@ var Transaction = class {
2025
2038
  ...subStatements.map((statement) => ({
2026
2039
  ...statement,
2027
2040
  query,
2028
- selectedFields
2041
+ selectedFields,
2042
+ expansionIndex
2029
2043
  }))
2030
2044
  );
2031
2045
  }
@@ -2125,10 +2139,19 @@ var Transaction = class {
2125
2139
  return Object.values(row);
2126
2140
  });
2127
2141
  });
2128
- const formattedResults = normalizedResults.map(
2129
- (rows, index) => {
2130
- const { returning, query, selectedFields } = this.#internalStatements[index];
2131
- 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
+ };
2132
2155
  const { queryType, queryModel, queryInstructions } = splitQuery(query);
2133
2156
  const model = getModelBySlug(this.models, queryModel);
2134
2157
  const isMeta = queryModel === "model" || queryModel === "models";
@@ -2136,32 +2159,32 @@ var Transaction = class {
2136
2159
  model.fields.map((field) => [field.slug, field.type])
2137
2160
  );
2138
2161
  if (queryType === "count") {
2139
- return { amount: rows[0][0] };
2162
+ return addResult({ amount: rows[0][0] });
2140
2163
  }
2141
2164
  const single = queryModel !== model.pluralSlug;
2142
2165
  if (single) {
2143
- return {
2166
+ return addResult({
2144
2167
  record: rows[0] ? this.#formatRows(selectedFields, rows, true, isMeta) : null,
2145
2168
  modelFields
2146
- };
2169
+ });
2147
2170
  }
2148
2171
  const pageSize = queryInstructions?.limitedTo;
2149
- const output = {
2172
+ const result = {
2150
2173
  records: this.#formatRows(selectedFields, rows, false, isMeta),
2151
2174
  modelFields
2152
2175
  };
2153
- if (pageSize && output.records.length > 0) {
2154
- if (output.records.length > pageSize) {
2176
+ if (pageSize && result.records.length > 0) {
2177
+ if (result.records.length > pageSize) {
2155
2178
  if (queryInstructions?.before) {
2156
- output.records.shift();
2179
+ result.records.shift();
2157
2180
  } else {
2158
- output.records.pop();
2181
+ result.records.pop();
2159
2182
  }
2160
2183
  const direction = queryInstructions?.before ? "moreBefore" : "moreAfter";
2161
- const lastRecord = output.records.at(
2184
+ const lastRecord = result.records.at(
2162
2185
  direction === "moreAfter" ? -1 : 0
2163
2186
  );
2164
- output[direction] = generatePaginationCursor(
2187
+ result[direction] = generatePaginationCursor(
2165
2188
  model,
2166
2189
  queryInstructions.orderedBy,
2167
2190
  lastRecord
@@ -2169,20 +2192,20 @@ var Transaction = class {
2169
2192
  }
2170
2193
  if (queryInstructions?.before || queryInstructions?.after) {
2171
2194
  const direction = queryInstructions?.before ? "moreAfter" : "moreBefore";
2172
- const firstRecord = output.records.at(
2195
+ const firstRecord = result.records.at(
2173
2196
  direction === "moreAfter" ? -1 : 0
2174
2197
  );
2175
- output[direction] = generatePaginationCursor(
2198
+ result[direction] = generatePaginationCursor(
2176
2199
  model,
2177
2200
  queryInstructions.orderedBy,
2178
2201
  firstRecord
2179
2202
  );
2180
2203
  }
2181
2204
  }
2182
- return output;
2183
- }
2205
+ return addResult(result);
2206
+ },
2207
+ []
2184
2208
  );
2185
- return formattedResults.filter((result) => result !== null);
2186
2209
  }
2187
2210
  };
2188
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-leo-ron-1099-1-experimental-362",
3
+ "version": "0.15.0-leo-ron-1099-1-experimental-364",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {