@budibase/backend-core 2.29.0 → 2.29.2

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.js CHANGED
@@ -54131,8 +54131,8 @@ var require_lib9 = __commonJS({
54131
54131
  var src_exports = {};
54132
54132
  __export(src_exports, {
54133
54133
  APP_DEV: () => APP_DEV,
54134
- APP_DEV_PREFIX: () => APP_DEV_PREFIX,
54135
- APP_PREFIX: () => APP_PREFIX,
54134
+ APP_DEV_PREFIX: () => APP_DEV_PREFIX2,
54135
+ APP_PREFIX: () => APP_PREFIX2,
54136
54136
  AutomationViewMode: () => AutomationViewMode,
54137
54137
  BUDIBASE_DATASOURCE_TYPE: () => BUDIBASE_DATASOURCE_TYPE,
54138
54138
  BadRequestError: () => BadRequestError,
@@ -54659,8 +54659,8 @@ var BREAKDOWN_QUOTA_NAMES = [
54659
54659
  var constants_exports2 = {};
54660
54660
  __export(constants_exports2, {
54661
54661
  APP_DEV: () => APP_DEV,
54662
- APP_DEV_PREFIX: () => APP_DEV_PREFIX,
54663
- APP_PREFIX: () => APP_PREFIX,
54662
+ APP_DEV_PREFIX: () => APP_DEV_PREFIX2,
54663
+ APP_PREFIX: () => APP_PREFIX2,
54664
54664
  AutomationViewMode: () => AutomationViewMode,
54665
54665
  BUDIBASE_DATASOURCE_TYPE: () => BUDIBASE_DATASOURCE_TYPE,
54666
54666
  Config: () => Config,
@@ -54687,69 +54687,6 @@ __export(constants_exports2, {
54687
54687
  ViewName: () => ViewName
54688
54688
  });
54689
54689
 
54690
- // src/constants/db.ts
54691
- var AutomationViewMode = /* @__PURE__ */ ((AutomationViewMode2) => {
54692
- AutomationViewMode2["ALL"] = "all";
54693
- AutomationViewMode2["AUTOMATION"] = "automation";
54694
- AutomationViewMode2["STATUS"] = "status";
54695
- return AutomationViewMode2;
54696
- })(AutomationViewMode || {});
54697
- var ViewName = /* @__PURE__ */ ((ViewName3) => {
54698
- ViewName3["USER_BY_APP"] = "by_app";
54699
- ViewName3["USER_BY_EMAIL"] = "by_email2";
54700
- ViewName3["BY_API_KEY"] = "by_api_key";
54701
- ViewName3["LINK"] = "by_link";
54702
- ViewName3["ROUTING"] = "screen_routes";
54703
- ViewName3["AUTOMATION_LOGS"] = "automation_logs";
54704
- ViewName3["ACCOUNT_BY_EMAIL"] = "account_by_email";
54705
- ViewName3["PLATFORM_USERS_LOWERCASE"] = "platform_users_lowercase_2";
54706
- ViewName3["USER_BY_GROUP"] = "user_by_group";
54707
- ViewName3["APP_BACKUP_BY_TRIGGER"] = "by_trigger";
54708
- return ViewName3;
54709
- })(ViewName || {});
54710
- var DeprecatedViews = {
54711
- ["by_email2" /* USER_BY_EMAIL */]: [
54712
- // removed due to inaccuracy in view doc filter logic
54713
- "by_email"
54714
- ]
54715
- };
54716
- var StaticDatabases = {
54717
- GLOBAL: {
54718
- name: "global-db",
54719
- docs: {
54720
- apiKeys: "apikeys",
54721
- usageQuota: "usage_quota",
54722
- licenseInfo: "license_info",
54723
- environmentVariables: "environmentvariables"
54724
- }
54725
- },
54726
- // contains information about tenancy and so on
54727
- PLATFORM_INFO: {
54728
- name: "global-info",
54729
- docs: {
54730
- tenants: "tenants",
54731
- install: "install"
54732
- }
54733
- },
54734
- AUDIT_LOGS: {
54735
- name: "audit-logs"
54736
- },
54737
- SCIM_LOGS: {
54738
- name: "scim-logs"
54739
- }
54740
- };
54741
- var APP_PREFIX = prefixed("app" /* APP */);
54742
- var APP_DEV = prefixed("app_dev" /* APP_DEV */);
54743
- var APP_DEV_PREFIX = APP_DEV;
54744
- var SQS_DATASOURCE_INTERNAL = "internal";
54745
- var BUDIBASE_DATASOURCE_TYPE = "budibase";
54746
- var SQLITE_DESIGN_DOC_ID = "_design/sqlite";
54747
- var DEFAULT_JOBS_TABLE_ID = "ta_bb_jobs";
54748
- var DEFAULT_INVENTORY_TABLE_ID = "ta_bb_inventory";
54749
- var DEFAULT_EXPENSES_TABLE_ID = "ta_bb_expenses";
54750
- var DEFAULT_EMPLOYEE_TABLE_ID = "ta_bb_employee";
54751
- var DEFAULT_BB_DATASOURCE_ID = "datasource_internal_bb_default";
54752
-
54753
54690
  // ../shared-core/src/constants/api.ts
54754
54691
  var Header = /* @__PURE__ */ ((Header2) => {
54755
54692
  Header2["API_KEY"] = "x-budibase-api-key";
@@ -54802,6 +54739,20 @@ var SWITCHABLE_TYPES = {
54802
54739
  ["number" /* NUMBER */]: ["number" /* NUMBER */, "boolean" /* BOOLEAN */]
54803
54740
  };
54804
54741
 
54742
+ // ../shared-core/src/constants/rows.ts
54743
+ var CONSTANT_INTERNAL_ROW_COLS = [
54744
+ "_id",
54745
+ "_rev",
54746
+ "type",
54747
+ "createdAt",
54748
+ "updatedAt",
54749
+ "tableId"
54750
+ ];
54751
+ var CONSTANT_EXTERNAL_ROW_COLS = ["_id", "_rev", "tableId"];
54752
+ function isInternalColumnName(name) {
54753
+ return CONSTANT_INTERNAL_ROW_COLS.includes(name);
54754
+ }
54755
+
54805
54756
  // ../shared-core/src/constants/index.ts
54806
54757
  var OperatorOptions = {
54807
54758
  Equals: {
@@ -54871,6 +54822,7 @@ var SqlNumberTypeRangeMap = {
54871
54822
  min: -8388608
54872
54823
  }
54873
54824
  };
54825
+ var DEFAULT_BB_DATASOURCE_ID = "datasource_internal_bb_default";
54874
54826
 
54875
54827
  // ../shared-core/src/filters.ts
54876
54828
  var filters_exports = {};
@@ -55217,10 +55169,15 @@ var search = (docs, query) => {
55217
55169
  if (query.sort) {
55218
55170
  result = sort(result, query.sort, query.sortOrder || "ascending" /* ASCENDING */);
55219
55171
  }
55172
+ let totalRows = result.length;
55220
55173
  if (query.limit) {
55221
55174
  result = limit(result, query.limit.toString());
55222
55175
  }
55223
- return result;
55176
+ const response = { rows: result };
55177
+ if (query.countRows) {
55178
+ response.totalRows = totalRows;
55179
+ }
55180
+ return response;
55224
55181
  };
55225
55182
  var runQuery = (docs, query) => {
55226
55183
  if (!docs || !Array.isArray(docs)) {
@@ -55545,31 +55502,31 @@ __export(applications_exports, {
55545
55502
  getDevAppID: () => getDevAppID,
55546
55503
  getProdAppID: () => getProdAppID
55547
55504
  });
55548
- var APP_PREFIX2 = prefixed("app" /* APP */);
55549
- var APP_DEV_PREFIX2 = prefixed("app_dev" /* APP_DEV */);
55505
+ var APP_PREFIX = prefixed("app" /* APP */);
55506
+ var APP_DEV_PREFIX = prefixed("app_dev" /* APP_DEV */);
55550
55507
  function getDevAppID(appId) {
55551
55508
  if (!appId) {
55552
55509
  throw new Error("No app ID provided");
55553
55510
  }
55554
- if (appId.startsWith(APP_DEV_PREFIX2)) {
55511
+ if (appId.startsWith(APP_DEV_PREFIX)) {
55555
55512
  return appId;
55556
55513
  }
55557
- const split = appId.split(APP_PREFIX2);
55514
+ const split = appId.split(APP_PREFIX);
55558
55515
  split.shift();
55559
- const rest = split.join(APP_PREFIX2);
55560
- return `${APP_DEV_PREFIX2}${rest}`;
55516
+ const rest = split.join(APP_PREFIX);
55517
+ return `${APP_DEV_PREFIX}${rest}`;
55561
55518
  }
55562
55519
  function getProdAppID(appId) {
55563
55520
  if (!appId) {
55564
55521
  throw new Error("No app ID provided");
55565
55522
  }
55566
- if (!appId.startsWith(APP_DEV_PREFIX2)) {
55523
+ if (!appId.startsWith(APP_DEV_PREFIX)) {
55567
55524
  return appId;
55568
55525
  }
55569
- const split = appId.split(APP_DEV_PREFIX2);
55526
+ const split = appId.split(APP_DEV_PREFIX);
55570
55527
  split.shift();
55571
- const rest = split.join(APP_DEV_PREFIX2);
55572
- return `${APP_PREFIX2}${rest}`;
55528
+ const rest = split.join(APP_DEV_PREFIX);
55529
+ return `${APP_PREFIX}${rest}`;
55573
55530
  }
55574
55531
 
55575
55532
  // ../shared-core/src/sdk/documents/users.ts
@@ -55724,6 +55681,68 @@ var allowSortColumnByType = {
55724
55681
  ["bb_reference_single" /* BB_REFERENCE_SINGLE */]: false
55725
55682
  };
55726
55683
 
55684
+ // src/constants/db.ts
55685
+ var AutomationViewMode = /* @__PURE__ */ ((AutomationViewMode2) => {
55686
+ AutomationViewMode2["ALL"] = "all";
55687
+ AutomationViewMode2["AUTOMATION"] = "automation";
55688
+ AutomationViewMode2["STATUS"] = "status";
55689
+ return AutomationViewMode2;
55690
+ })(AutomationViewMode || {});
55691
+ var ViewName = /* @__PURE__ */ ((ViewName3) => {
55692
+ ViewName3["USER_BY_APP"] = "by_app";
55693
+ ViewName3["USER_BY_EMAIL"] = "by_email2";
55694
+ ViewName3["BY_API_KEY"] = "by_api_key";
55695
+ ViewName3["LINK"] = "by_link";
55696
+ ViewName3["ROUTING"] = "screen_routes";
55697
+ ViewName3["AUTOMATION_LOGS"] = "automation_logs";
55698
+ ViewName3["ACCOUNT_BY_EMAIL"] = "account_by_email";
55699
+ ViewName3["PLATFORM_USERS_LOWERCASE"] = "platform_users_lowercase_2";
55700
+ ViewName3["USER_BY_GROUP"] = "user_by_group";
55701
+ ViewName3["APP_BACKUP_BY_TRIGGER"] = "by_trigger";
55702
+ return ViewName3;
55703
+ })(ViewName || {});
55704
+ var DeprecatedViews = {
55705
+ ["by_email2" /* USER_BY_EMAIL */]: [
55706
+ // removed due to inaccuracy in view doc filter logic
55707
+ "by_email"
55708
+ ]
55709
+ };
55710
+ var StaticDatabases = {
55711
+ GLOBAL: {
55712
+ name: "global-db",
55713
+ docs: {
55714
+ apiKeys: "apikeys",
55715
+ usageQuota: "usage_quota",
55716
+ licenseInfo: "license_info",
55717
+ environmentVariables: "environmentvariables"
55718
+ }
55719
+ },
55720
+ // contains information about tenancy and so on
55721
+ PLATFORM_INFO: {
55722
+ name: "global-info",
55723
+ docs: {
55724
+ tenants: "tenants",
55725
+ install: "install"
55726
+ }
55727
+ },
55728
+ AUDIT_LOGS: {
55729
+ name: "audit-logs"
55730
+ },
55731
+ SCIM_LOGS: {
55732
+ name: "scim-logs"
55733
+ }
55734
+ };
55735
+ var APP_PREFIX2 = prefixed("app" /* APP */);
55736
+ var APP_DEV = prefixed("app_dev" /* APP_DEV */);
55737
+ var APP_DEV_PREFIX2 = APP_DEV;
55738
+ var SQS_DATASOURCE_INTERNAL = "internal";
55739
+ var BUDIBASE_DATASOURCE_TYPE = "budibase";
55740
+ var SQLITE_DESIGN_DOC_ID = "_design/sqlite";
55741
+ var DEFAULT_JOBS_TABLE_ID = "ta_bb_jobs";
55742
+ var DEFAULT_INVENTORY_TABLE_ID = "ta_bb_inventory";
55743
+ var DEFAULT_EXPENSES_TABLE_ID = "ta_bb_expenses";
55744
+ var DEFAULT_EMPLOYEE_TABLE_ID = "ta_bb_employee";
55745
+
55727
55746
  // src/constants/misc.ts
55728
55747
  var UserStatus = /* @__PURE__ */ ((UserStatus3) => {
55729
55748
  UserStatus3["ACTIVE"] = "active";
@@ -56071,13 +56090,13 @@ function isDevAppID(appId) {
56071
56090
  if (!appId) {
56072
56091
  throw NO_APP_ERROR;
56073
56092
  }
56074
- return appId.startsWith(APP_DEV_PREFIX);
56093
+ return appId.startsWith(APP_DEV_PREFIX2);
56075
56094
  }
56076
56095
  function isProdAppID(appId) {
56077
56096
  if (!appId) {
56078
56097
  throw NO_APP_ERROR;
56079
56098
  }
56080
- return appId.startsWith(APP_PREFIX) && !isDevAppID(appId);
56099
+ return appId.startsWith(APP_PREFIX2) && !isDevAppID(appId);
56081
56100
  }
56082
56101
  function isDevApp(app) {
56083
56102
  if (!app) {
@@ -56086,23 +56105,23 @@ function isDevApp(app) {
56086
56105
  return isDevAppID(app.appId);
56087
56106
  }
56088
56107
  function getDevelopmentAppID(appId) {
56089
- if (!appId || appId.startsWith(APP_DEV_PREFIX)) {
56108
+ if (!appId || appId.startsWith(APP_DEV_PREFIX2)) {
56090
56109
  return appId;
56091
56110
  }
56092
- const split = appId.split(APP_PREFIX);
56111
+ const split = appId.split(APP_PREFIX2);
56093
56112
  split.shift();
56094
- const rest = split.join(APP_PREFIX);
56095
- return `${APP_DEV_PREFIX}${rest}`;
56113
+ const rest = split.join(APP_PREFIX2);
56114
+ return `${APP_DEV_PREFIX2}${rest}`;
56096
56115
  }
56097
56116
  var getDevAppID2 = getDevelopmentAppID;
56098
56117
  function getProdAppID2(appId) {
56099
- if (!appId || !appId.startsWith(APP_DEV_PREFIX)) {
56118
+ if (!appId || !appId.startsWith(APP_DEV_PREFIX2)) {
56100
56119
  return appId;
56101
56120
  }
56102
- const split = appId.split(APP_DEV_PREFIX);
56121
+ const split = appId.split(APP_DEV_PREFIX2);
56103
56122
  split.shift();
56104
- const rest = split.join(APP_DEV_PREFIX);
56105
- return `${APP_PREFIX}${rest}`;
56123
+ const rest = split.join(APP_DEV_PREFIX2);
56124
+ return `${APP_PREFIX2}${rest}`;
56106
56125
  }
56107
56126
  function extractAppUUID(id) {
56108
56127
  const split = id?.split("_") || [];
@@ -56728,20 +56747,6 @@ var DatabaseImpl = class _DatabaseImpl {
56728
56747
  }
56729
56748
  };
56730
56749
 
56731
- // src/db/constants.ts
56732
- var CONSTANT_INTERNAL_ROW_COLS = [
56733
- "_id",
56734
- "_rev",
56735
- "type",
56736
- "createdAt",
56737
- "updatedAt",
56738
- "tableId"
56739
- ];
56740
- var CONSTANT_EXTERNAL_ROW_COLS = ["_id", "_rev", "tableId"];
56741
- function isInternalColumnName(name) {
56742
- return CONSTANT_INTERNAL_ROW_COLS.includes(name);
56743
- }
56744
-
56745
56750
  // src/db/db.ts
56746
56751
  function getDB(dbName, opts) {
56747
56752
  return new DDInstrumentedDatabase(new DatabaseImpl(dbName, opts));
@@ -57348,8 +57353,8 @@ var import_uuid2 = require("uuid");
57348
57353
  var db_exports = {};
57349
57354
  __export(db_exports, {
57350
57355
  APP_DEV: () => APP_DEV,
57351
- APP_DEV_PREFIX: () => APP_DEV_PREFIX,
57352
- APP_PREFIX: () => APP_PREFIX,
57356
+ APP_DEV_PREFIX: () => APP_DEV_PREFIX2,
57357
+ APP_PREFIX: () => APP_PREFIX2,
57353
57358
  AutomationViewMode: () => AutomationViewMode,
57354
57359
  BUDIBASE_DATASOURCE_TYPE: () => BUDIBASE_DATASOURCE_TYPE,
57355
57360
  CONSTANT_EXTERNAL_ROW_COLS: () => CONSTANT_EXTERNAL_ROW_COLS,
@@ -57543,7 +57548,7 @@ __export(docIds_exports, {
57543
57548
 
57544
57549
  // src/docIds/ids.ts
57545
57550
  var generateAppID = (tenantId) => {
57546
- let id = APP_PREFIX;
57551
+ let id = APP_PREFIX2;
57547
57552
  if (tenantId) {
57548
57553
  id += `${tenantId}${SEPARATOR}`;
57549
57554
  }
@@ -58703,7 +58708,7 @@ function sanitizeKey(input) {
58703
58708
  return sanitize(sanitizeBucket(input)).replace(/\\/g, "/");
58704
58709
  }
58705
58710
  function sanitizeBucket(input) {
58706
- return input.replace(new RegExp(APP_DEV_PREFIX, "g"), APP_PREFIX);
58711
+ return input.replace(new RegExp(APP_DEV_PREFIX2, "g"), APP_PREFIX2);
58707
58712
  }
58708
58713
  function ObjectStore(bucket, opts = { presigning: false }) {
58709
58714
  const config = {
@@ -67107,15 +67112,15 @@ function buildExternalTableId(datasourceId, tableName) {
67107
67112
  return `${datasourceId}${DOUBLE_SEPARATOR}${tableName}`;
67108
67113
  }
67109
67114
  function breakExternalTableId(tableId) {
67110
- if (!tableId) {
67111
- return {};
67112
- }
67113
67115
  const parts = tableId.split(DOUBLE_SEPARATOR);
67114
67116
  let datasourceId = parts.shift();
67115
67117
  let tableName = parts.join(DOUBLE_SEPARATOR);
67116
67118
  if (tableName.includes(ENCODED_SPACE)) {
67117
67119
  tableName = decodeURIComponent(tableName);
67118
67120
  }
67121
+ if (!datasourceId || !tableName) {
67122
+ throw new Error("Unable to get datasource/table name from table ID");
67123
+ }
67119
67124
  return { datasourceId, tableName };
67120
67125
  }
67121
67126
  function generateRowIdField(keyProps = []) {
@@ -67291,8 +67296,10 @@ function generateSchema(schema, table, tables, oldTable = null, renamed) {
67291
67296
  }
67292
67297
  const { tableName } = breakExternalTableId(column.tableId);
67293
67298
  const relatedTable = tables[tableName];
67294
- if (!relatedTable) {
67295
- throw new Error("Referenced table doesn't exist");
67299
+ if (!relatedTable || !relatedTable.primary) {
67300
+ throw new Error(
67301
+ "Referenced table doesn't exist or has no primary keys"
67302
+ );
67296
67303
  }
67297
67304
  const relatedPrimary = relatedTable.primary[0];
67298
67305
  const externalType = relatedTable.schema[relatedPrimary].externalType;
@@ -67496,7 +67503,7 @@ function parseFilters(filters) {
67496
67503
  }
67497
67504
  function generateSelectStatement(json, knex3) {
67498
67505
  const { resource, meta } = json;
67499
- if (!resource) {
67506
+ if (!resource || !resource.fields || resource.fields.length === 0) {
67500
67507
  return "*";
67501
67508
  }
67502
67509
  const schema = meta?.table?.schema;
@@ -67743,12 +67750,26 @@ var InternalBuilder = class {
67743
67750
  }
67744
67751
  return query;
67745
67752
  }
67753
+ addDistinctCount(query, json) {
67754
+ const table = json.meta.table;
67755
+ const primary = table.primary;
67756
+ const aliases = json.tableAliases;
67757
+ const aliased = table.name && aliases?.[table.name] ? aliases[table.name] : table.name;
67758
+ if (!primary) {
67759
+ throw new Error("SQL counting requires primary key to be supplied");
67760
+ }
67761
+ return query.countDistinct(`${aliased}.${primary[0]} as total`);
67762
+ }
67746
67763
  addSorting(query, json) {
67747
- let { sort: sort2, paginate } = json;
67764
+ let { sort: sort2 } = json;
67748
67765
  const table = json.meta.table;
67766
+ const primaryKey = table.primary;
67749
67767
  const tableName = getTableName(table);
67750
67768
  const aliases = json.tableAliases;
67751
67769
  const aliased = tableName && aliases?.[tableName] ? aliases[tableName] : table?.name;
67770
+ if (!Array.isArray(primaryKey)) {
67771
+ throw new Error("Sorting requires primary key to be specified for table");
67772
+ }
67752
67773
  if (sort2 && Object.keys(sort2 || {}).length > 0) {
67753
67774
  for (let [key, value] of Object.entries(sort2)) {
67754
67775
  const direction = value.direction === "ascending" /* ASCENDING */ ? "asc" : "desc";
@@ -67758,8 +67779,9 @@ var InternalBuilder = class {
67758
67779
  }
67759
67780
  query = query.orderBy(`${aliased}.${key}`, direction, nulls);
67760
67781
  }
67761
- } else if (this.client === "mssql" /* MS_SQL */ && paginate?.limit) {
67762
- query = query.orderBy(`${aliased}.${table?.primary[0]}`);
67782
+ }
67783
+ if (!sort2 || sort2[primaryKey[0]] === void 0) {
67784
+ query = query.orderBy(`${aliased}.${primaryKey[0]}`);
67763
67785
  }
67764
67786
  return query;
67765
67787
  }
@@ -67828,18 +67850,17 @@ var InternalBuilder = class {
67828
67850
  });
67829
67851
  }
67830
67852
  }
67831
- return query.limit(BASE_LIMIT);
67853
+ return query;
67832
67854
  }
67833
67855
  knexWithAlias(knex3, endpoint, aliases) {
67834
67856
  const tableName = endpoint.entityId;
67835
67857
  const tableAlias = aliases?.[tableName];
67836
- const query = knex3(
67858
+ return knex3(
67837
67859
  this.tableNameWithSchema(tableName, {
67838
67860
  alias: tableAlias,
67839
67861
  schema: endpoint.schema
67840
67862
  })
67841
67863
  );
67842
- return query;
67843
67864
  }
67844
67865
  create(knex3, json, opts) {
67845
67866
  const { endpoint, body: body2 } = json;
@@ -67877,24 +67898,21 @@ var InternalBuilder = class {
67877
67898
  if (!primary) {
67878
67899
  throw new Error("Primary key is required for upsert");
67879
67900
  }
67880
- return query.insert(parsedBody).onConflict(primary).merge();
67901
+ const ret = query.insert(parsedBody).onConflict(primary).merge();
67902
+ return ret;
67881
67903
  } else if (this.client === "mssql" /* MS_SQL */) {
67882
67904
  return query.insert(parsedBody);
67883
67905
  }
67884
67906
  return query.upsert(parsedBody);
67885
67907
  }
67886
- read(knex3, json, limit2) {
67887
- let { endpoint, resource, filters, paginate, relationships, tableAliases } = json;
67908
+ read(knex3, json, opts = {}) {
67909
+ let { endpoint, filters, paginate, relationships, tableAliases } = json;
67910
+ const { limits } = opts;
67911
+ const counting = endpoint.operation === "COUNT" /* COUNT */;
67888
67912
  const tableName = endpoint.entityId;
67889
- if (!resource) {
67890
- resource = { fields: [] };
67891
- }
67892
- let selectStatement = "*";
67893
- if (resource.fields && resource.fields.length > 0) {
67894
- selectStatement = generateSelectStatement(json, knex3);
67895
- }
67896
- let foundLimit = limit2 || BASE_LIMIT;
67913
+ let query = this.knexWithAlias(knex3, endpoint, tableAliases);
67897
67914
  let foundOffset = null;
67915
+ let foundLimit = limits?.query || limits?.base;
67898
67916
  if (paginate && paginate.page && paginate.limit) {
67899
67917
  const page = paginate.page <= 1 ? 0 : paginate.page - 1;
67900
67918
  const offset = page * paginate.limit;
@@ -67906,20 +67924,28 @@ var InternalBuilder = class {
67906
67924
  } else if (paginate && paginate.limit) {
67907
67925
  foundLimit = paginate.limit;
67908
67926
  }
67909
- let query = this.knexWithAlias(knex3, endpoint, tableAliases);
67910
- query = query.limit(foundLimit);
67911
- if (foundOffset) {
67912
- query = query.offset(foundOffset);
67927
+ if (!counting) {
67928
+ if (foundLimit != null) {
67929
+ query = query.limit(foundLimit);
67930
+ }
67931
+ if (foundOffset != null) {
67932
+ query = query.offset(foundOffset);
67933
+ }
67934
+ query = this.addSorting(query, json);
67913
67935
  }
67914
67936
  query = this.addFilters(query, filters, json.meta.table, {
67915
67937
  aliases: tableAliases
67916
67938
  });
67917
- query = this.addSorting(query, json);
67918
67939
  const alias = tableAliases?.[tableName] || tableName;
67919
67940
  let preQuery = knex3({
67941
+ // the typescript definition for the knex constructor doesn't support this
67942
+ // syntax, but it is the only way to alias a pre-query result as part of
67943
+ // a query - there is an alias dictionary type, but it assumes it can only
67944
+ // be a table name, not a pre-query
67920
67945
  [alias]: query
67921
- }).select(selectStatement);
67922
- if (this.client !== "mssql" /* MS_SQL */) {
67946
+ });
67947
+ preQuery = !counting ? preQuery.select(generateSelectStatement(json, knex3)) : this.addDistinctCount(preQuery, json);
67948
+ if (this.client !== "mssql" /* MS_SQL */ && !counting) {
67923
67949
  preQuery = this.addSorting(preQuery, json);
67924
67950
  }
67925
67951
  query = this.addRelationships(
@@ -67929,6 +67955,9 @@ var InternalBuilder = class {
67929
67955
  endpoint.schema,
67930
67956
  tableAliases
67931
67957
  );
67958
+ if (limits?.base) {
67959
+ query = query.limit(limits.base);
67960
+ }
67932
67961
  return this.addFilters(query, filters, json.meta.table, {
67933
67962
  relationship: true,
67934
67963
  aliases: tableAliases
@@ -67966,6 +67995,18 @@ var SqlQueryBuilder = class extends sqlTable_default {
67966
67995
  super(client);
67967
67996
  this.limit = limit2;
67968
67997
  }
67998
+ convertToNative(query, opts = {}) {
67999
+ const sqlClient = this.getSqlClient();
68000
+ if (opts?.disableBindings) {
68001
+ return { sql: query.toString() };
68002
+ } else {
68003
+ let native = getNativeSql(query);
68004
+ if (sqlClient === "sqlite3" /* SQL_LITE */) {
68005
+ native = convertBooleans(native);
68006
+ }
68007
+ return native;
68008
+ }
68009
+ }
67969
68010
  /**
67970
68011
  * @param json The JSON query DSL which is to be converted to SQL.
67971
68012
  * @param opts extra options which are to be passed into the query builder, e.g. disableReturning
@@ -67988,7 +68029,15 @@ var SqlQueryBuilder = class extends sqlTable_default {
67988
68029
  query = builder.create(client, json, opts);
67989
68030
  break;
67990
68031
  case "READ" /* READ */:
67991
- query = builder.read(client, json, this.limit);
68032
+ query = builder.read(client, json, {
68033
+ limits: {
68034
+ query: this.limit,
68035
+ base: BASE_LIMIT
68036
+ }
68037
+ });
68038
+ break;
68039
+ case "COUNT" /* COUNT */:
68040
+ query = builder.read(client, json);
67992
68041
  break;
67993
68042
  case "UPDATE" /* UPDATE */:
67994
68043
  query = builder.update(client, json, opts);
@@ -68009,15 +68058,7 @@ var SqlQueryBuilder = class extends sqlTable_default {
68009
68058
  default:
68010
68059
  throw `Operation type is not supported by SQL query builder`;
68011
68060
  }
68012
- if (opts?.disableBindings) {
68013
- return { sql: query.toString() };
68014
- } else {
68015
- let native = getNativeSql(query);
68016
- if (sqlClient === "sqlite3" /* SQL_LITE */) {
68017
- native = convertBooleans(native);
68018
- }
68019
- return native;
68020
- }
68061
+ return this.convertToNative(query, opts);
68021
68062
  }
68022
68063
  async getReturningRow(queryFn, json) {
68023
68064
  if (!json.extra || !json.extra.idFilter) {
@@ -68084,6 +68125,9 @@ var SqlQueryBuilder = class extends sqlTable_default {
68084
68125
  await this.getReturningRow(queryFn, this.checkLookupKeys(id, json))
68085
68126
  );
68086
68127
  }
68128
+ if (operation === "COUNT" /* COUNT */) {
68129
+ return results;
68130
+ }
68087
68131
  if (operation !== "READ" /* READ */) {
68088
68132
  return row;
68089
68133
  }