@ronin/compiler 0.10.1-leo-ron-1083-experimental-208 → 0.10.1-leo-ron-1083-experimental-209

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.
Files changed (2) hide show
  1. package/dist/index.js +43 -3
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -102,6 +102,9 @@ var expand = (obj) => {
102
102
  return res;
103
103
  }, {});
104
104
  };
105
+ var getProperty = (obj, path) => {
106
+ return path.split(".").reduce((acc, key) => acc?.[key], obj);
107
+ };
105
108
  var splitQuery = (query) => {
106
109
  const queryType = Object.keys(query)[0];
107
110
  const queryModel = Object.keys(query[queryType])[0];
@@ -939,9 +942,24 @@ var transformMetaQuery = (models, dependencyStatements, statementParams, query)
939
942
  };
940
943
  };
941
944
 
942
- // src/instructions/before-after.ts
945
+ // src/utils/pagination.ts
943
946
  var CURSOR_SEPARATOR = ",";
944
947
  var CURSOR_NULL_PLACEHOLDER = "RONIN_NULL";
948
+ var generatePaginationCursor = (model, orderedBy, record) => {
949
+ const { ascending = [], descending = [] } = orderedBy || {};
950
+ const keys = [...ascending, ...descending];
951
+ if (keys.length === 0) keys.push("ronin.createdAt");
952
+ const cursors = keys.map((fieldSlug) => {
953
+ const property = getProperty(record, fieldSlug);
954
+ if (property === null || property === void 0) return CURSOR_NULL_PLACEHOLDER;
955
+ const { field } = getFieldFromModel(model, fieldSlug, "orderedBy");
956
+ if (field.type === "date") return new Date(property).getTime();
957
+ return property;
958
+ });
959
+ return cursors.map((cursor) => encodeURIComponent(String(cursor))).join(CURSOR_SEPARATOR);
960
+ };
961
+
962
+ // src/instructions/before-after.ts
945
963
  var handleBeforeOrAfter = (model, statementParams, instructions) => {
946
964
  if (!(instructions.before || instructions.after)) {
947
965
  throw new RoninError({
@@ -1520,17 +1538,39 @@ var Transaction = class {
1520
1538
  });
1521
1539
  return relevantResults.map((result, index) => {
1522
1540
  const query = this.queries.at(-index);
1523
- const { queryModel } = splitQuery(query);
1541
+ const { queryModel, queryInstructions } = splitQuery(query);
1524
1542
  const model = getModelBySlug(this.models, queryModel);
1525
1543
  const single = queryModel !== model.pluralSlug;
1526
1544
  if (single) {
1527
1545
  return { record: this.formatRecord(model, result[0]) };
1528
1546
  }
1529
- return {
1547
+ const pageSize = queryInstructions?.limitedTo;
1548
+ const output = {
1530
1549
  records: result.map((resultItem) => {
1531
1550
  return this.formatRecord(model, resultItem);
1532
1551
  })
1533
1552
  };
1553
+ if (pageSize && output.records.length > 0) {
1554
+ if (queryInstructions?.before || queryInstructions?.after) {
1555
+ const direction = queryInstructions?.before ? "moreAfter" : "moreBefore";
1556
+ const firstRecord = output.records[0];
1557
+ output[direction] = generatePaginationCursor(
1558
+ model,
1559
+ queryInstructions.orderedBy,
1560
+ firstRecord
1561
+ );
1562
+ }
1563
+ if (output.records.length > pageSize) {
1564
+ const direction = queryInstructions?.before ? "moreBefore" : "moreAfter";
1565
+ const lastRecord = output.records.pop();
1566
+ output[direction] = generatePaginationCursor(
1567
+ model,
1568
+ queryInstructions.orderedBy,
1569
+ lastRecord
1570
+ );
1571
+ }
1572
+ }
1573
+ return output;
1534
1574
  });
1535
1575
  }
1536
1576
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ronin/compiler",
3
- "version": "0.10.1-leo-ron-1083-experimental-208",
3
+ "version": "0.10.1-leo-ron-1083-experimental-209",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {