@ronin/compiler 0.17.21 → 0.17.22-leo-ron-1140-experimental-431

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
@@ -115,6 +115,11 @@ type AddInstructions = Omit<CombinedInstructions, 'with' | 'using'> & {
115
115
  };
116
116
  type RemoveInstructions = Omit<CombinedInstructions, 'to'>;
117
117
  type CountInstructions = Omit<CombinedInstructions, 'to'>;
118
+ type ListQuery = {
119
+ models?: null;
120
+ } | {
121
+ model: string;
122
+ };
118
123
  type CreateQuery = {
119
124
  model: string | PublicModel;
120
125
  to?: PublicModel;
@@ -152,6 +157,7 @@ type Query = {
152
157
  add?: AddQuery;
153
158
  remove?: RemoveQuery;
154
159
  count?: CountQuery | CountAllQuery;
160
+ list?: ListQuery;
155
161
  create?: CreateQuery;
156
162
  alter?: AlterQuery;
157
163
  drop?: DropQuery;
@@ -258,6 +264,8 @@ type InternalModelField = ModelField & {
258
264
  mountingPath: string;
259
265
  /** A custom value that was provided in the query, which is not stored in the DB. */
260
266
  mountedValue?: unknown;
267
+ /** If this is set, the field is used during formatting, but not exposed afterward. */
268
+ excluded?: boolean;
261
269
  };
262
270
  type ModelIndexField<T extends ModelEntityList<ModelField> = ModelEntityList<ModelField>> = {
263
271
  /** 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({
@@ -1752,6 +1789,12 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query,
1752
1789
  let slug = entity === "model" && action === "create" ? null : query[queryType] && "model" in query[queryType] ? query[queryType].model : null;
1753
1790
  let modelSlug = slug;
1754
1791
  let jsonValue;
1792
+ if ("list" in query && query.list) {
1793
+ if (slug) {
1794
+ return { get: { model: { with: { slug } } } };
1795
+ }
1796
+ return { get: { models: {} } };
1797
+ }
1755
1798
  if ("create" in query && query.create) {
1756
1799
  const init = query.create.model;
1757
1800
  jsonValue = "to" in query.create ? { slug: init, ...query.create.to } : init;
@@ -2309,6 +2352,12 @@ var Transaction = class {
2309
2352
  );
2310
2353
  }
2311
2354
  }
2355
+ for (const field of selectedFields) {
2356
+ if (!field.excluded) continue;
2357
+ for (const record of result.records) {
2358
+ deleteProperty(record, field.slug);
2359
+ }
2360
+ }
2312
2361
  return result;
2313
2362
  }
2314
2363
  /**
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-leo-ron-1140-experimental-431",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {