@backstage/plugin-catalog-backend 0.17.2 → 0.17.3

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/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # @backstage/plugin-catalog-backend
2
2
 
3
+ ## 0.17.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 86bef79ad1: Allow singleton and flexibly nested EntityFilters
8
+ - Updated dependencies
9
+ - @backstage/backend-common@0.9.9
10
+ - @backstage/catalog-client@0.5.1
11
+
3
12
  ## 0.17.2
4
13
 
5
14
  ### Patch Changes
package/dist/index.cjs.js CHANGED
@@ -2633,13 +2633,16 @@ class CommonDatabase {
2633
2633
  var _a, _b;
2634
2634
  const tx = txOpaque;
2635
2635
  let entitiesQuery = tx("entities");
2636
+ if ((request == null ? void 0 : request.filter) && (request.filter.hasOwnProperty("key") || request.filter.hasOwnProperty("allOf"))) {
2637
+ throw new Error("Filters for the legacy CommonDatabase must obey the { anyOf: [{ allOf: [] }] } format.");
2638
+ }
2636
2639
  for (const singleFilter of (_b = (_a = request == null ? void 0 : request.filter) == null ? void 0 : _a.anyOf) != null ? _b : []) {
2637
2640
  entitiesQuery = entitiesQuery.orWhere(function singleFilterFn() {
2638
- for (const {
2639
- key,
2640
- matchValueIn,
2641
- matchValueExists
2642
- } of singleFilter.allOf) {
2641
+ for (const filter of singleFilter.allOf) {
2642
+ if (filter.hasOwnProperty("anyOf") || filter.hasOwnProperty("allOf")) {
2643
+ throw new Error("Nested filters are not supported in the legacy CommonDatabase");
2644
+ }
2645
+ const {key, matchValueIn, matchValueExists} = filter;
2643
2646
  const matchQuery = tx("entities_search").select("entity_id").where(function keyFilter() {
2644
2647
  this.andWhere({key: key.toLowerCase()});
2645
2648
  if (matchValueExists !== false && matchValueIn) {
@@ -4131,34 +4134,60 @@ function stringifyPagination(input) {
4131
4134
  const base64 = Buffer.from(json, "utf8").toString("base64");
4132
4135
  return base64;
4133
4136
  }
4137
+ function addCondition(queryBuilder, db, {key, matchValueIn, matchValueExists}) {
4138
+ const matchQuery = db("search").select("entity_id").where(function keyFilter() {
4139
+ this.andWhere({key: key.toLowerCase()});
4140
+ if (matchValueExists !== false && matchValueIn) {
4141
+ if (matchValueIn.length === 1) {
4142
+ this.andWhere({value: matchValueIn[0].toLowerCase()});
4143
+ } else if (matchValueIn.length > 1) {
4144
+ this.andWhere("value", "in", matchValueIn.map((v) => v.toLowerCase()));
4145
+ }
4146
+ }
4147
+ });
4148
+ queryBuilder.andWhere("entity_id", matchValueExists === false ? "not in" : "in", matchQuery);
4149
+ }
4150
+ function isEntitiesSearchFilter(filter) {
4151
+ return filter.hasOwnProperty("key");
4152
+ }
4153
+ function isAndEntityFilter(filter) {
4154
+ return filter.hasOwnProperty("allOf");
4155
+ }
4156
+ function isOrEntityFilter(filter) {
4157
+ return filter.hasOwnProperty("anyOf");
4158
+ }
4159
+ function parseFilter(filter, query, db) {
4160
+ var _a, _b;
4161
+ if (isEntitiesSearchFilter(filter)) {
4162
+ return query.where(function filterFunction() {
4163
+ addCondition(this, db, filter);
4164
+ });
4165
+ }
4166
+ if (isOrEntityFilter(filter)) {
4167
+ let cumulativeQuery = query;
4168
+ for (const subFilter of (_a = filter.anyOf) != null ? _a : []) {
4169
+ cumulativeQuery = cumulativeQuery.orWhere((subQuery) => parseFilter(subFilter, subQuery, db));
4170
+ }
4171
+ return cumulativeQuery;
4172
+ }
4173
+ if (isAndEntityFilter(filter)) {
4174
+ let cumulativeQuery = query;
4175
+ for (const subFilter of (_b = filter.allOf) != null ? _b : []) {
4176
+ cumulativeQuery = cumulativeQuery.andWhere((subQuery) => parseFilter(subFilter, subQuery, db));
4177
+ }
4178
+ return cumulativeQuery;
4179
+ }
4180
+ return query;
4181
+ }
4134
4182
  class NextEntitiesCatalog {
4135
4183
  constructor(database) {
4136
4184
  this.database = database;
4137
4185
  }
4138
4186
  async entities(request) {
4139
- var _a, _b;
4140
4187
  const db = this.database;
4141
4188
  let entitiesQuery = db("final_entities");
4142
- for (const singleFilter of (_b = (_a = request == null ? void 0 : request.filter) == null ? void 0 : _a.anyOf) != null ? _b : []) {
4143
- entitiesQuery = entitiesQuery.orWhere(function singleFilterFn() {
4144
- for (const {
4145
- key,
4146
- matchValueIn,
4147
- matchValueExists
4148
- } of singleFilter.allOf) {
4149
- const matchQuery = db("search").select("entity_id").where(function keyFilter() {
4150
- this.andWhere({key: key.toLowerCase()});
4151
- if (matchValueExists !== false && matchValueIn) {
4152
- if (matchValueIn.length === 1) {
4153
- this.andWhere({value: matchValueIn[0].toLowerCase()});
4154
- } else if (matchValueIn.length > 1) {
4155
- this.andWhere("value", "in", matchValueIn.map((v) => v.toLowerCase()));
4156
- }
4157
- }
4158
- });
4159
- this.andWhere("entity_id", matchValueExists === false ? "not in" : "in", matchQuery);
4160
- }
4161
- });
4189
+ if (request == null ? void 0 : request.filter) {
4190
+ entitiesQuery = parseFilter(request.filter, entitiesQuery, db);
4162
4191
  }
4163
4192
  entitiesQuery = entitiesQuery.select("final_entities.*").whereNotNull("final_entities.final_entity").orderBy("entity_id", "asc");
4164
4193
  const {limit, offset} = parsePagination(request == null ? void 0 : request.pagination);