@ronin/compiler 0.17.21 → 0.17.22

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
@@ -258,6 +258,8 @@ type InternalModelField = ModelField & {
258
258
  mountingPath: string;
259
259
  /** A custom value that was provided in the query, which is not stored in the DB. */
260
260
  mountedValue?: unknown;
261
+ /** If this is set, the field is used during formatting, but not exposed afterward. */
262
+ excluded?: boolean;
261
263
  };
262
264
  type ModelIndexField<T extends ModelEntityList<ModelField> = ModelEntityList<ModelField>> = {
263
265
  /** The collating sequence used for text placed inside the field. */
package/dist/index.js CHANGED
@@ -141,8 +141,11 @@ var omit = (obj, properties) => Object.fromEntries(
141
141
  var getProperty = (obj, path) => {
142
142
  return path.split(".").reduce((acc, key) => acc?.[key], obj);
143
143
  };
144
+ var getPathSegments = (path) => {
145
+ return path.split(/[.[\]]/g).filter((segment) => segment.trim().length > 0);
146
+ };
144
147
  var setProperty = (obj, path, value) => {
145
- const segments = path.split(/[.[\]]/g).filter((x) => !!x.trim());
148
+ const segments = getPathSegments(path);
146
149
  const _set = (node) => {
147
150
  if (segments.length > 1) {
148
151
  const key = segments.shift();
@@ -157,6 +160,20 @@ var setProperty = (obj, path, value) => {
157
160
  };
158
161
  _set(obj);
159
162
  };
163
+ var deleteProperty = (obj, path) => {
164
+ const segments = getPathSegments(path);
165
+ const _delete = (node, segs) => {
166
+ const key = segs[0];
167
+ if (segs.length === 1) {
168
+ delete node[key];
169
+ } else if (node[key] && typeof node[key] === "object" && node[key] !== null) {
170
+ const shouldCleanup = _delete(node[key], segs.slice(1));
171
+ if (shouldCleanup) delete node[key];
172
+ }
173
+ return Object.keys(node).length === 0;
174
+ };
175
+ _delete(obj, segments);
176
+ };
160
177
  var splitQuery = (query) => {
161
178
  const queryType = Object.keys(query)[0];
162
179
  const queryModel = Object.keys(query[queryType])[0];
@@ -379,7 +396,7 @@ var handleOrderedBy = (model, instruction) => {
379
396
  };
380
397
 
381
398
  // src/instructions/selecting.ts
382
- var handleSelecting = (models, model, statementParams, single, instructions, options = { inlineDefaults: false }) => {
399
+ var handleSelecting = (models, model, statementParams, queryType, single, instructions, options = { inlineDefaults: false }) => {
383
400
  let isJoining = false;
384
401
  const selectedFields = filterSelectedFields(
385
402
  model,
@@ -403,9 +420,9 @@ var handleSelecting = (models, model, statementParams, single, instructions, opt
403
420
  for (const [key, value] of Object.entries(flatObject)) {
404
421
  const symbol2 = getQuerySymbol(value);
405
422
  if (symbol2?.type === "query") {
406
- const { queryType, queryModel, queryInstructions } = splitQuery(symbol2.value);
423
+ const { queryType: queryType2, queryModel, queryInstructions } = splitQuery(symbol2.value);
407
424
  const subQueryModel = getModelBySlug(models, queryModel);
408
- if (queryType === "count") {
425
+ if (queryType2 === "count") {
409
426
  const subSelect = compileQueryInput(symbol2.value, models, statementParams, {
410
427
  parentModel: { ...model, tableAlias: model.table },
411
428
  inlineDefaults: options.inlineDefaults
@@ -427,10 +444,13 @@ var handleSelecting = (models, model, statementParams, single, instructions, opt
427
444
  models,
428
445
  { ...subQueryModel, tableAlias: `including_${subMountingPath}` },
429
446
  statementParams,
447
+ queryType2,
430
448
  subSingle,
431
449
  {
432
450
  selecting: queryInstructions?.selecting,
433
- including: queryInstructions?.including
451
+ including: queryInstructions?.including,
452
+ orderedBy: queryInstructions?.orderedBy,
453
+ limitedTo: queryInstructions?.limitedTo
434
454
  },
435
455
  { ...options, mountingPath: subMountingPath }
436
456
  );
@@ -454,6 +474,20 @@ var handleSelecting = (models, model, statementParams, single, instructions, opt
454
474
  });
455
475
  }
456
476
  }
477
+ if (queryType === "get" && !single && typeof instructions.limitedTo !== "undefined") {
478
+ const orderedFields = Object.values(instructions.orderedBy || {}).flat().map((fieldSlug) => {
479
+ return getFieldFromModel(model, fieldSlug, { instructionName: "orderedBy" });
480
+ });
481
+ for (const orderedField of orderedFields) {
482
+ const { field } = orderedField;
483
+ if (selectedFields.some(({ slug }) => slug === field.slug)) continue;
484
+ selectedFields.push({
485
+ slug: field.slug,
486
+ mountingPath: field.slug,
487
+ excluded: true
488
+ });
489
+ }
490
+ }
457
491
  const columns = selectedFields.map((selectedField) => {
458
492
  if (selectedField.mountedValue) {
459
493
  return `${selectedField.mountedValue} as "${selectedField.slug}"`;
@@ -1077,14 +1111,29 @@ var compileQueryInput = (defaultQuery, models, statementParams, options) => {
1077
1111
  }
1078
1112
  });
1079
1113
  }
1114
+ if (!single && (queryType === "get" && instructions?.limitedTo || queryType === "count" && (instructions?.before || instructions?.after))) {
1115
+ instructions = instructions || {};
1116
+ instructions.orderedBy = instructions.orderedBy || {};
1117
+ instructions.orderedBy.ascending = instructions.orderedBy.ascending || [];
1118
+ instructions.orderedBy.descending = instructions.orderedBy.descending || [];
1119
+ if (![
1120
+ ...instructions.orderedBy.ascending,
1121
+ ...instructions.orderedBy.descending
1122
+ ].includes("ronin.createdAt")) {
1123
+ instructions.orderedBy.descending.push("ronin.createdAt");
1124
+ }
1125
+ }
1080
1126
  const { columns, isJoining, selectedFields } = handleSelecting(
1081
1127
  models,
1082
1128
  model,
1083
1129
  statementParams,
1130
+ queryType,
1084
1131
  single,
1085
1132
  {
1086
1133
  selecting: instructions?.selecting,
1087
- including: instructions?.including
1134
+ including: instructions?.including,
1135
+ orderedBy: instructions?.orderedBy,
1136
+ limitedTo: instructions?.limitedTo
1088
1137
  },
1089
1138
  // biome-ignore lint/complexity/useSimplifiedLogicExpression: This is needed.
1090
1139
  { inlineDefaults: options?.inlineDefaults || false }
@@ -1156,18 +1205,6 @@ var compileQueryInput = (defaultQuery, models, statementParams, options) => {
1156
1205
  );
1157
1206
  if (withStatement.length > 0) conditions.push(withStatement);
1158
1207
  }
1159
- if (!single && (queryType === "get" && instructions?.limitedTo || queryType === "count" && (instructions?.before || instructions?.after))) {
1160
- instructions = instructions || {};
1161
- instructions.orderedBy = instructions.orderedBy || {};
1162
- instructions.orderedBy.ascending = instructions.orderedBy.ascending || [];
1163
- instructions.orderedBy.descending = instructions.orderedBy.descending || [];
1164
- if (![
1165
- ...instructions.orderedBy.ascending,
1166
- ...instructions.orderedBy.descending
1167
- ].includes("ronin.createdAt")) {
1168
- instructions.orderedBy.descending.push("ronin.createdAt");
1169
- }
1170
- }
1171
1208
  if (instructions && (typeof instructions.before !== "undefined" || typeof instructions.after !== "undefined")) {
1172
1209
  if (single) {
1173
1210
  throw new RoninError({
@@ -2309,6 +2346,12 @@ var Transaction = class {
2309
2346
  );
2310
2347
  }
2311
2348
  }
2349
+ for (const field of selectedFields) {
2350
+ if (!field.excluded) continue;
2351
+ for (const record of result.records) {
2352
+ deleteProperty(record, field.slug);
2353
+ }
2354
+ }
2312
2355
  return result;
2313
2356
  }
2314
2357
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ronin/compiler",
3
- "version": "0.17.21",
3
+ "version": "0.17.22",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {