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

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/dist/index.js +46 -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,42 @@ 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 { queryType, queryModel, queryInstructions } = splitQuery(query);
1524
1542
  const model = getModelBySlug(this.models, queryModel);
1543
+ if (queryType === "count") {
1544
+ return { amount: result[0]["COUNT(*)"] };
1545
+ }
1525
1546
  const single = queryModel !== model.pluralSlug;
1526
1547
  if (single) {
1527
1548
  return { record: this.formatRecord(model, result[0]) };
1528
1549
  }
1529
- return {
1550
+ const pageSize = queryInstructions?.limitedTo;
1551
+ const output = {
1530
1552
  records: result.map((resultItem) => {
1531
1553
  return this.formatRecord(model, resultItem);
1532
1554
  })
1533
1555
  };
1556
+ if (pageSize && output.records.length > 0) {
1557
+ if (queryInstructions?.before || queryInstructions?.after) {
1558
+ const direction = queryInstructions?.before ? "moreAfter" : "moreBefore";
1559
+ const firstRecord = output.records[0];
1560
+ output[direction] = generatePaginationCursor(
1561
+ model,
1562
+ queryInstructions.orderedBy,
1563
+ firstRecord
1564
+ );
1565
+ }
1566
+ if (output.records.length > pageSize) {
1567
+ const direction = queryInstructions?.before ? "moreBefore" : "moreAfter";
1568
+ const lastRecord = output.records.pop();
1569
+ output[direction] = generatePaginationCursor(
1570
+ model,
1571
+ queryInstructions.orderedBy,
1572
+ lastRecord
1573
+ );
1574
+ }
1575
+ }
1576
+ return output;
1534
1577
  });
1535
1578
  }
1536
1579
  };
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-210",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {