@rebasepro/common 0.29.0-canary.g0e3a0e7 → 0.29.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,13 +1,9 @@
1
1
  import { DataDriver, RebaseData, RebaseSdkData } from "@rebasepro/types";
2
2
  /**
3
- * Derive the response key an aggregate comes back under.
4
- *
5
- * `sum(total)` → `sum_total`, `count()` → `count`. Written once, here, because
6
- * the REST parser derives the same alias from `?select=sum(total)` and the two
7
- * have to agree — a caller reading `row.sum_total` off an SDK result and off an
8
- * HTTP response is reading the same key or the SDK is broken.
3
+ * The key an aggregate comes back under — `@rebasepro/types`' derivation,
4
+ * which the REST parser and the HTTP client use too.
9
5
  */
10
- export declare function aggregateAlias(fn: string, field?: string): string;
6
+ export { aggregateAlias } from "@rebasepro/types";
11
7
  export interface EntityDataOptions {
12
8
  /**
13
9
  * Look up a collection's config by slug, to derive row addresses from its
@@ -7,7 +7,7 @@ import { etagOf } from "./row-etag.es.js";
7
7
  import { QueryBuilder } from "./query_builder.es.js";
8
8
  import { collectAllPages, paginateFind, resolveFindWindow } from "./paginate.es.js";
9
9
  import { deserializeFilter } from "./filter-dialect.es.js";
10
- import { DEFAULT_VECTOR_LIST_LIMIT, EntityReference, EntityRelation, RebaseApiError, RebaseClientError, isUnsupported, sortKeyToString, unsupportedMethod } from "@rebasepro/types";
10
+ import { DEFAULT_VECTOR_LIST_LIMIT, EntityReference, EntityRelation, RebaseApiError, RebaseClientError, aggregateAlias, isUnsupported, sortKeyToString, toDriverAggregate, toDriverAggregateGroup, unsupportedMethod } from "@rebasepro/types";
11
11
  import { toSnakeCase, toWireKey } from "@rebasepro/utils";
12
12
  //#region src/data/buildRebaseData.ts
13
13
  /**
@@ -20,25 +20,6 @@ var noRealtime = (slug) => `Realtime is not available for "${slug}": its data so
20
20
  /** What a client says when its data source cannot count. */
21
21
  var noCount = (slug) => `Counting is not available for "${slug}": its data source does not support it.`;
22
22
  /**
23
- * Derive the response key an aggregate comes back under.
24
- *
25
- * `sum(total)` → `sum_total`, `count()` → `count`. Written once, here, because
26
- * the REST parser derives the same alias from `?select=sum(total)` and the two
27
- * have to agree — a caller reading `row.sum_total` off an SDK result and off an
28
- * HTTP response is reading the same key or the SDK is broken.
29
- */
30
- function aggregateAlias(fn, field) {
31
- return field ? `${fn}_${field}` : fn;
32
- }
33
- function toDriverAggregate(select) {
34
- const field = select.field;
35
- return {
36
- fn: select.fn,
37
- field,
38
- alias: aggregateAlias(select.fn, field)
39
- };
40
- }
41
- /**
42
23
  * What a client says when its data source cannot aggregate.
43
24
  *
44
25
  * A stub rather than a fallback that fetches and reduces in JavaScript: that
@@ -50,6 +31,8 @@ var noTrash = (slug) => `The trash is not available for "${slug}": its data sour
50
31
  /** What `softDelete: true` names the field a delete stamps; a restore clears it. */
51
32
  var DEFAULT_SOFT_DELETE_FIELD = "deletedAt";
52
33
  var noAggregate = (slug) => `Aggregates are not available for "${slug}": its data source does not implement them.`;
34
+ /** What a client says when its data source has no materialized views to refresh. */
35
+ var noRefresh = (slug) => `"${slug}" cannot be refreshed: its data source has no materialized views.`;
53
36
  function createPrimaryKeyResolver(options) {
54
37
  const cache = /* @__PURE__ */ new Map();
55
38
  const warned = /* @__PURE__ */ new Set();
@@ -175,6 +158,10 @@ function rowToEntity(row, slug, primaryKeys = [], toViewModel) {
175
158
  ...etag ? { etag } : {}
176
159
  };
177
160
  }
161
+ /** {@link rowToEntity} over a page of rows, the primary keys looked up once for all of them. */
162
+ function rowsToEntities(rows, slug, primaryKeys, toViewModel) {
163
+ return rows.map((row) => rowToEntity(row, slug, primaryKeys, toViewModel));
164
+ }
178
165
  /**
179
166
  * The relation envelope `toFlatRow` writes where a relation was:
180
167
  * `{ id, path, __type: "relation", data: { id, path, values } }`. It is the
@@ -274,14 +261,14 @@ function createDriverAccessor(driver, slug, getPks = () => [], toViewModel, stor
274
261
  }) : void 0
275
262
  };
276
263
  };
277
- const { fetched, counted } = driver.readTogether ? await driver.readTogether(read) : await read(driver);
264
+ const { fetched, counted } = counting && driver.readTogether ? await driver.readTogether(read) : await read(driver);
278
265
  const rows = probing ? fetched.slice(0, limit) : fetched;
279
266
  const total = counted ?? rows.length + offset;
280
267
  const hasMore = probing ? fetched.length > limit : offset + rows.length < total;
281
268
  const last = rows[rows.length - 1];
282
269
  const nextCursor = hasMore && last && driver.restFetchService?.cursorFor ? driver.restFetchService.cursorFor(storedPath(), last, orderBy) : void 0;
283
270
  return {
284
- data: rows.map((row) => rowToEntity(row, slug, getPks(), toViewModel)),
271
+ data: rowsToEntities(rows, slug, getPks(), toViewModel),
285
272
  meta: {
286
273
  total,
287
274
  limit,
@@ -319,7 +306,7 @@ function createDriverAccessor(driver, slug, getPks = () => [], toViewModel, stor
319
306
  const limit = (params.groupBy?.length ?? 0) > 0 ? params.limit : void 0;
320
307
  const rows = await driver.restFetchService.aggregate(storedPath(), {
321
308
  aggregates: params.select.map(toDriverAggregate),
322
- groupBy: params.groupBy,
309
+ groupBy: params.groupBy?.map((group) => toDriverAggregateGroup(group)),
323
310
  filter: params.where ? deserializeFilter(params.where) : void 0,
324
311
  logical: params.logical,
325
312
  searchString: params.searchString,
@@ -340,6 +327,7 @@ function createDriverAccessor(driver, slug, getPks = () => [], toViewModel, stor
340
327
  });
341
328
  return page;
342
329
  } : void 0,
330
+ refresh: driver.refreshView ? (options) => driver.refreshView(storedPath(), options) : void 0,
343
331
  async create(data, id) {
344
332
  return rowToEntity(await driver.save({
345
333
  path: storedPath(),
@@ -349,12 +337,12 @@ function createDriverAccessor(driver, slug, getPks = () => [], toViewModel, stor
349
337
  }), slug, getPks(), toViewModel);
350
338
  },
351
339
  createMany: driver.saveMany ? async (data, options) => {
352
- return (await driver.saveMany({
340
+ return rowsToEntities(await driver.saveMany({
353
341
  path: storedPath(),
354
342
  rows: data,
355
343
  upsert: options?.upsert,
356
344
  onConflict: options?.onConflict
357
- })).map((row) => rowToEntity(row, slug, getPks(), toViewModel));
345
+ }), slug, getPks(), toViewModel);
358
346
  } : void 0,
359
347
  async update(id, data) {
360
348
  return rowToEntity(await driver.save({
@@ -371,13 +359,13 @@ function createDriverAccessor(driver, slug, getPks = () => [], toViewModel, stor
371
359
  } });
372
360
  },
373
361
  updateMany: driver.updateMany ? async (updates) => {
374
- return (await driver.updateMany({
362
+ return rowsToEntities(await driver.updateMany({
375
363
  path: storedPath(),
376
364
  updates: updates.map((u) => ({
377
365
  id: u.id,
378
366
  values: u.data
379
367
  }))
380
- })).map((row) => rowToEntity(row, slug, getPks(), toViewModel));
368
+ }), slug, getPks(), toViewModel);
381
369
  } : void 0,
382
370
  deleteMany: driver.deleteMany ? async (ids) => {
383
371
  await driver.deleteMany({
@@ -860,6 +848,7 @@ function toSdkCollectionClient(snap, slug = "collection") {
860
848
  distinct: (enabled) => new SdkQueryBuilder(client).distinct(enabled),
861
849
  after: (cursor) => new SdkQueryBuilder(client).after(cursor),
862
850
  aggregate: snap.aggregate ? (params) => snap.aggregate(params) : unsupportedMethod(noAggregate(slug)),
851
+ refresh: snap.refresh ? (options) => snap.refresh(options) : unsupportedMethod(noRefresh(slug)),
863
852
  trash: toSdkTrashClient(snap, slug, () => client)
864
853
  };
865
854
  return client;
@@ -928,7 +917,7 @@ function toEntityAccessor(sdk, slug, getPks = () => [], toViewModel, viewModelSo
928
917
  async find(params) {
929
918
  const res = await sdk.find(params);
930
919
  return {
931
- data: res.data.map((row) => rowToEntity(row, slug, getPks(), toViewModel)),
920
+ data: rowsToEntities(res.data, slug, getPks(), toViewModel),
932
921
  meta: res.meta
933
922
  };
934
923
  },
@@ -940,7 +929,7 @@ function toEntityAccessor(sdk, slug, getPks = () => [], toViewModel, viewModelSo
940
929
  return rowToEntity(await sdk.create(data, id), slug, getPks(), toViewModel);
941
930
  },
942
931
  createMany: sdk.createMany ? async (data, options) => {
943
- return (await sdk.createMany(data, options)).map((row) => rowToEntity(row, slug, getPks(), toViewModel));
932
+ return rowsToEntities(await sdk.createMany(data, options), slug, getPks(), toViewModel);
944
933
  } : void 0,
945
934
  async update(id, data, options) {
946
935
  const row = options?.ifMatch ? await sdk.update(id, data, { ifMatch: options.ifMatch }) : await sdk.update(id, data);
@@ -952,6 +941,7 @@ function toEntityAccessor(sdk, slug, getPks = () => [], toViewModel, viewModelSo
952
941
  },
953
942
  count: isUnsupported(sdk.count) ? void 0 : (params) => sdk.count(params),
954
943
  aggregate: isUnsupported(sdk.aggregate) ? void 0 : (params) => sdk.aggregate(params),
944
+ refresh: isUnsupported(sdk.refresh) ? void 0 : (options) => sdk.refresh(options),
955
945
  listen: isUnsupported(sdk.listen) ? void 0 : (params, onUpdate, onError) => {
956
946
  let built = /* @__PURE__ */ new WeakMap();
957
947
  let builtWith;
@@ -981,7 +971,7 @@ function toEntityAccessor(sdk, slug, getPks = () => [], toViewModel, viewModelSo
981
971
  async find(params) {
982
972
  const res = await sdk.trash.find(params);
983
973
  return {
984
- data: res.data.map((row) => rowToEntity(row, slug, getPks(), toViewModel)),
974
+ data: rowsToEntities(res.data, slug, getPks(), toViewModel),
985
975
  meta: res.meta
986
976
  };
987
977
  },