@budibase/backend-core 2.32.7 → 2.32.9

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 (37) hide show
  1. package/dist/index.js +729 -615
  2. package/dist/index.js.map +4 -4
  3. package/dist/index.js.meta.json +1 -1
  4. package/dist/package.json +4 -4
  5. package/dist/plugins.js.meta.json +1 -1
  6. package/dist/src/cache/appMetadata.js +3 -3
  7. package/dist/src/cache/appMetadata.js.map +1 -1
  8. package/dist/src/context/mainContext.d.ts +3 -1
  9. package/dist/src/context/mainContext.js +19 -0
  10. package/dist/src/context/mainContext.js.map +1 -1
  11. package/dist/src/context/types.d.ts +2 -1
  12. package/dist/src/db/couch/DatabaseImpl.d.ts +1 -0
  13. package/dist/src/db/couch/DatabaseImpl.js +35 -5
  14. package/dist/src/db/couch/DatabaseImpl.js.map +1 -1
  15. package/dist/src/db/lucene.js +0 -1
  16. package/dist/src/db/lucene.js.map +1 -1
  17. package/dist/src/docIds/params.d.ts +2 -3
  18. package/dist/src/docIds/params.js +6 -3
  19. package/dist/src/docIds/params.js.map +1 -1
  20. package/dist/src/features/index.js +3 -2
  21. package/dist/src/features/index.js.map +1 -1
  22. package/dist/src/index.d.ts +2 -0
  23. package/dist/src/sql/sql.js +45 -9
  24. package/dist/src/sql/sql.js.map +1 -1
  25. package/dist/src/users/users.d.ts +1 -2
  26. package/dist/src/users/users.js +0 -24
  27. package/dist/src/users/users.js.map +1 -1
  28. package/package.json +4 -4
  29. package/src/cache/appMetadata.ts +3 -3
  30. package/src/context/mainContext.ts +18 -1
  31. package/src/context/types.ts +2 -1
  32. package/src/db/couch/DatabaseImpl.ts +35 -6
  33. package/src/db/lucene.ts +0 -1
  34. package/src/docIds/params.ts +8 -6
  35. package/src/features/index.ts +3 -1
  36. package/src/sql/sql.ts +46 -6
  37. package/src/users/users.ts +0 -29
package/src/sql/sql.ts CHANGED
@@ -11,10 +11,12 @@ import {
11
11
  } from "./utils"
12
12
  import SqlTableQueryBuilder from "./sqlTable"
13
13
  import {
14
+ Aggregation,
14
15
  AnySearchFilter,
15
16
  ArrayOperator,
16
17
  BasicOperator,
17
18
  BBReferenceFieldMetadata,
19
+ CalculationType,
18
20
  FieldSchema,
19
21
  FieldType,
20
22
  INTERNAL_TABLE_SOURCE_ID,
@@ -824,8 +826,40 @@ class InternalBuilder {
824
826
  return query.countDistinct(`${aliased}.${primary[0]} as total`)
825
827
  }
826
828
 
829
+ addAggregations(
830
+ query: Knex.QueryBuilder,
831
+ aggregations: Aggregation[]
832
+ ): Knex.QueryBuilder {
833
+ const fields = this.query.resource?.fields || []
834
+ if (fields.length > 0) {
835
+ query = query.groupBy(fields.map(field => `${this.table.name}.${field}`))
836
+ }
837
+ for (const aggregation of aggregations) {
838
+ const op = aggregation.calculationType
839
+ const field = `${this.table.name}.${aggregation.field} as ${aggregation.name}`
840
+ switch (op) {
841
+ case CalculationType.COUNT:
842
+ query = query.count(field)
843
+ break
844
+ case CalculationType.SUM:
845
+ query = query.sum(field)
846
+ break
847
+ case CalculationType.AVG:
848
+ query = query.avg(field)
849
+ break
850
+ case CalculationType.MIN:
851
+ query = query.min(field)
852
+ break
853
+ case CalculationType.MAX:
854
+ query = query.max(field)
855
+ break
856
+ }
857
+ }
858
+ return query
859
+ }
860
+
827
861
  addSorting(query: Knex.QueryBuilder): Knex.QueryBuilder {
828
- let { sort } = this.query
862
+ let { sort, resource } = this.query
829
863
  const primaryKey = this.table.primary
830
864
  const tableName = getTableName(this.table)
831
865
  const aliases = this.query.tableAliases
@@ -862,7 +896,8 @@ class InternalBuilder {
862
896
 
863
897
  // add sorting by the primary key if the result isn't already sorted by it,
864
898
  // to make sure result is deterministic
865
- if (!sort || sort[primaryKey[0]] === undefined) {
899
+ const hasAggregations = (resource?.aggregations?.length ?? 0) > 0
900
+ if (!hasAggregations && (!sort || sort[primaryKey[0]] === undefined)) {
866
901
  query = query.orderBy(`${aliased}.${primaryKey[0]}`)
867
902
  }
868
903
  return query
@@ -1246,10 +1281,15 @@ class InternalBuilder {
1246
1281
  }
1247
1282
  }
1248
1283
 
1249
- // if counting, use distinct count, else select
1250
- query = !counting
1251
- ? query.select(this.generateSelectStatement())
1252
- : this.addDistinctCount(query)
1284
+ const aggregations = this.query.resource?.aggregations || []
1285
+ if (counting) {
1286
+ query = this.addDistinctCount(query)
1287
+ } else if (aggregations.length > 0) {
1288
+ query = this.addAggregations(query, aggregations)
1289
+ } else {
1290
+ query = query.select(this.generateSelectStatement())
1291
+ }
1292
+
1253
1293
  // have to add after as well (this breaks MS-SQL)
1254
1294
  if (!counting) {
1255
1295
  query = this.addSorting(query)
@@ -17,11 +17,8 @@ import {
17
17
  ContextUser,
18
18
  CouchFindOptions,
19
19
  DatabaseQueryOpts,
20
- SearchFilters,
21
20
  SearchUsersRequest,
22
21
  User,
23
- BasicOperator,
24
- ArrayOperator,
25
22
  } from "@budibase/types"
26
23
  import * as context from "../context"
27
24
  import { getGlobalDB } from "../context"
@@ -45,32 +42,6 @@ function removeUserPassword(users: User | User[]) {
45
42
  return users
46
43
  }
47
44
 
48
- export function isSupportedUserSearch(query: SearchFilters) {
49
- const allowed = [
50
- { op: BasicOperator.STRING, key: "email" },
51
- { op: BasicOperator.EQUAL, key: "_id" },
52
- { op: ArrayOperator.ONE_OF, key: "_id" },
53
- ]
54
- for (let [key, operation] of Object.entries(query)) {
55
- if (typeof operation !== "object") {
56
- return false
57
- }
58
- const fields = Object.keys(operation || {})
59
- // this filter doesn't contain options - ignore
60
- if (fields.length === 0) {
61
- continue
62
- }
63
- const allowedOperation = allowed.find(
64
- allow =>
65
- allow.op === key && fields.length === 1 && fields[0] === allow.key
66
- )
67
- if (!allowedOperation) {
68
- return false
69
- }
70
- }
71
- return true
72
- }
73
-
74
45
  export async function bulkGetGlobalUsersById(
75
46
  userIds: string[],
76
47
  opts?: GetOpts