shelving 1.312.0 → 1.312.1

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.
@@ -1,6 +1,6 @@
1
1
  import type { Collection } from "../db/collection/Collection.js";
2
2
  import { DBProvider } from "../db/provider/DBProvider.js";
3
- import type { ImmutableArray } from "../util/array.js";
3
+ import { type ImmutableArray } from "../util/array.js";
4
4
  import type { Data } from "../util/data.js";
5
5
  import type { AnyCaller } from "../util/function.js";
6
6
  import type { Item, Items, ItemsSequence, OptionalItem, OptionalItemSequence } from "../util/item.js";
@@ -1,18 +1,20 @@
1
1
  import { DBProvider } from "../db/provider/DBProvider.js";
2
2
  import { ResponseError } from "../error/ResponseError.js";
3
3
  import { UnsupportedError } from "../error/UnsupportedError.js";
4
+ import { isArray } from "../util/array.js";
4
5
  import { getDelay } from "../util/async.js";
5
6
  import { joinDataPath } from "../util/data.js";
6
7
  import { BLACKHOLE } from "../util/function.js";
7
8
  import { getItem } from "../util/item.js";
8
9
  import { isPlainObject } from "../util/object.js";
9
- import { getQueryFilters, getQueryLimit, getQueryOrders } from "../util/query.js";
10
+ import { getQueryFilters, getQueryLimit, getQueryOrders, limitQueryItems, sortQueryItems, } from "../util/query.js";
10
11
  import { getRandom, getRandomKey } from "../util/random.js";
11
12
  import { getUpdates } from "../util/update.js";
12
13
  import { toData, toFirestoreFields, toFirestoreValue } from "./value.js";
13
14
  // Constants.
14
15
  const WRITE_BATCH = 500;
15
16
  const TRANSACTION_ATTEMPTS = 5;
17
+ const IN_MAX = 30; // Firestore allows at most 30 values in an `IN` filter.
16
18
  // Map `Filter.types` to Firestore REST `FieldFilter` operators.
17
19
  const OPERATORS = {
18
20
  is: "EQUAL",
@@ -82,6 +84,28 @@ function _getStructuredQuery(root, collection, query, select, caller) {
82
84
  structured.select = { fields: [{ fieldPath: "__name__" }] };
83
85
  return structured;
84
86
  }
87
+ /**
88
+ * Split a query with an `in` filter of more than `IN_MAX` values into queries of `IN_MAX` values each.
89
+ * - Returns `undefined` when the query needs no split.
90
+ * - Each query keeps the order and the limit, so the caller must merge, sort and limit the results again.
91
+ */
92
+ function _splitQuery(query) {
93
+ if (!query)
94
+ return;
95
+ for (const [key, value] of Object.entries(query)) {
96
+ if (!isArray(value) || value.length <= IN_MAX || /^[!$]|\[\]$|[<>=]$/.test(key))
97
+ continue;
98
+ const queries = [];
99
+ for (let i = 0; i < value.length; i += IN_MAX)
100
+ queries.push({ ...query, [key]: value.slice(i, i + IN_MAX) });
101
+ return queries.flatMap(q => _splitQuery(q) ?? [q]);
102
+ }
103
+ }
104
+ /** Get the orders Firestore sorts a query by: its own orders, then the document id in the direction of the last order. */
105
+ function _getFirestoreOrders(query) {
106
+ const orders = getQueryOrders(query);
107
+ return [...orders, { key: ["id"], direction: orders[orders.length - 1]?.direction ?? "asc" }];
108
+ }
85
109
  /** Convert an `Updates` object to a Firestore REST `Write` against a document name. */
86
110
  function _getUpdateWrite(name, updates, caller) {
87
111
  const fields = {};
@@ -184,6 +208,13 @@ export class FirestoreProvider extends DBProvider {
184
208
  }
185
209
  /** Run a query returning only the matching document names. */
186
210
  async _getQueryNames(collection, query, caller) {
211
+ const queries = _splitQuery(query);
212
+ if (queries) {
213
+ // A limit needs the full documents to find the first items across the queries.
214
+ if (query && getQueryLimit(query) !== undefined)
215
+ return (await this.getQuery(collection, query)).map(({ id }) => this._getDocumentName(collection, id));
216
+ return (await Promise.all(queries.map(q => this._getQueryNames(collection, q, caller)))).flat();
217
+ }
187
218
  const structuredQuery = _getStructuredQuery(this._root, collection.name, query, true, caller);
188
219
  if (!structuredQuery)
189
220
  return [];
@@ -225,6 +256,12 @@ export class FirestoreProvider extends DBProvider {
225
256
  await this._write([{ delete: this._getDocumentName(collection, id) }]);
226
257
  }
227
258
  async countQuery(collection, query) {
259
+ const queries = _splitQuery(query);
260
+ if (queries) {
261
+ const total = (await Promise.all(queries.map(q => this.countQuery(collection, q)))).reduce((a, b) => a + b, 0);
262
+ const limit = query && getQueryLimit(query);
263
+ return typeof limit === "number" ? Math.min(total, limit) : total;
264
+ }
228
265
  const structuredQuery = _getStructuredQuery(this._root, collection.name, query, false, this.countQuery);
229
266
  if (!structuredQuery)
230
267
  return 0;
@@ -236,6 +273,11 @@ export class FirestoreProvider extends DBProvider {
236
273
  return count ? Number(toData({ count }, this.countQuery).count) : 0;
237
274
  }
238
275
  async getQuery(collection, query) {
276
+ const queries = _splitQuery(query);
277
+ if (queries && query) {
278
+ const items = (await Promise.all(queries.map(q => this.getQuery(collection, q)))).flat();
279
+ return Array.from(limitQueryItems(sortQueryItems(items, _getFirestoreOrders(query)), getQueryLimit(query)));
280
+ }
239
281
  const structuredQuery = _getStructuredQuery(this._root, collection.name, query, false, this.getQuery);
240
282
  if (!structuredQuery)
241
283
  return [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shelving",
3
- "version": "1.312.0",
3
+ "version": "1.312.1",
4
4
  "author": "Dave Houlbrooke <dave@shax.com>",
5
5
  "repository": {
6
6
  "type": "git",
package/util/start.js CHANGED
@@ -60,7 +60,7 @@ export class Starter {
60
60
  throw new UnexpectedError("Unexpected error in stop callback", { cause: thrown, caller: this.stop });
61
61
  }
62
62
  finally {
63
- this._started = true;
63
+ this._started = false;
64
64
  this._stop = undefined;
65
65
  }
66
66
  }