@rebasepro/server-mongo 0.20.0 → 0.21.0

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.es.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { MongoClient, ObjectId } from "mongodb";
2
2
  import { ANONYMOUS_USER_ID, EntityReference, ListLimitError, getCollectionDataPath, isAnonymousUid, resolveClientListLimit } from "@rebasepro/types";
3
- import { buildPropertyCallbacks, buildSdkData, callbackRefusal, checkOperation, normalizeDriverOrderBy, normalizeEmail, securityRuleToConditions, toCallbackError, toFilterTuples, updateDateAutoValues } from "@rebasepro/common";
3
+ import { buildPropertyCallbacks, buildSdkData, callbackRefusal, checkOperation, normalizeDriverOrderBy, normalizeEmail, requireCallbackClient, securityRuleToConditions, toCallbackError, toFilterTuples, updateDateAutoValues } from "@rebasepro/common";
4
4
  import { ApiError, logger } from "@rebasepro/server";
5
5
  import { mergeDeep } from "@rebasepro/utils";
6
6
  //#region \0rolldown/runtime.js
@@ -1221,6 +1221,24 @@ function buildMongoFilterFromSecurityRules(collection, user, targetOperation) {
1221
1221
  //#endregion
1222
1222
  //#region src/services/MongoDriver.ts
1223
1223
  /**
1224
+ * The context a collection callback is handed, built once for both drivers in
1225
+ * this file. It used to be five hand-written literals cast `as
1226
+ * RebaseCallContext`, which let `client` claim a `data` the server singleton
1227
+ * does not have and hid that the client may not be attached at all. Same
1228
+ * contract as `PostgresBackendDriver.buildCallContext`.
1229
+ */
1230
+ function callContext(driver, user, data, client) {
1231
+ return {
1232
+ user,
1233
+ driver,
1234
+ data,
1235
+ get client() {
1236
+ return requireCallbackClient(client);
1237
+ },
1238
+ storageSource: client?.storage
1239
+ };
1240
+ }
1241
+ /**
1224
1242
  * MongoDB DataDriver Delegate
1225
1243
  *
1226
1244
  * Implements the DataDriver interface for Rebase.
@@ -1236,6 +1254,7 @@ var MongoDriver = class {
1236
1254
  historyService;
1237
1255
  user;
1238
1256
  data;
1257
+ /** The server singleton, attached by `initializeRebaseBackend` after boot. */
1239
1258
  client;
1240
1259
  constructor(db, realtimeService, historyService, registry, user) {
1241
1260
  this.db = db;
@@ -1292,13 +1311,7 @@ var MongoDriver = class {
1292
1311
  });
1293
1312
  const { collection: resolvedCollection, callbacks, globalCallbacks, propertyCallbacks } = this.resolveCollectionCallbacks(collection, path);
1294
1313
  if (globalCallbacks?.afterRead || callbacks?.afterRead || propertyCallbacks?.afterRead) {
1295
- const contextForCallback = {
1296
- user: this.user,
1297
- driver: this,
1298
- data: this.data,
1299
- client: this.client,
1300
- storageSource: this.client?.storage
1301
- };
1314
+ const contextForCallback = callContext(this, this.user, this.data, this.client);
1302
1315
  return Promise.all(rows.map(async (row) => {
1303
1316
  let fetched = row;
1304
1317
  if (globalCallbacks?.afterRead) fetched = await globalCallbacks.afterRead({
@@ -1359,13 +1372,7 @@ var MongoDriver = class {
1359
1372
  let row = await this.dataService.fetchOne(path, id, databaseId);
1360
1373
  const { collection: resolvedCollection, callbacks, globalCallbacks, propertyCallbacks } = this.resolveCollectionCallbacks(collection, path);
1361
1374
  if (row && (globalCallbacks?.afterRead || callbacks?.afterRead || propertyCallbacks?.afterRead)) {
1362
- const contextForCallback = {
1363
- user: this.user,
1364
- driver: this,
1365
- data: this.data,
1366
- client: this.client,
1367
- storageSource: this.client?.storage
1368
- };
1375
+ const contextForCallback = callContext(this, this.user, this.data, this.client);
1369
1376
  let processedRow = row;
1370
1377
  if (globalCallbacks?.afterRead) processedRow = await globalCallbacks.afterRead({
1371
1378
  collection: resolvedCollection,
@@ -1418,13 +1425,7 @@ var MongoDriver = class {
1418
1425
  async save({ path, id, values, collection, status }) {
1419
1426
  const { collection: resolvedCollection, callbacks, globalCallbacks, propertyCallbacks } = this.resolveCollectionCallbacks(collection, path);
1420
1427
  let updatedValues = values;
1421
- const contextForCallback = {
1422
- user: this.user,
1423
- driver: this,
1424
- data: this.data,
1425
- client: this.client,
1426
- storageSource: this.client?.storage
1427
- };
1428
+ const contextForCallback = callContext(this, this.user, this.data, this.client);
1428
1429
  let previousValuesForHistory;
1429
1430
  if (status === "existing" && id) {
1430
1431
  const existing = await this.dataService.fetchOne(path, id, resolvedCollection?.databaseId);
@@ -1573,13 +1574,7 @@ var MongoDriver = class {
1573
1574
  id: row.id,
1574
1575
  ...row.values ?? {}
1575
1576
  };
1576
- const contextForCallback = {
1577
- user: this.user,
1578
- driver: this,
1579
- data: this.data,
1580
- client: this.client,
1581
- storageSource: this.client?.storage
1582
- };
1577
+ const contextForCallback = callContext(this, this.user, this.data, this.client);
1583
1578
  try {
1584
1579
  if (globalCallbacks?.beforeDelete || callbacks?.beforeDelete || propertyCallbacks?.beforeDelete) {
1585
1580
  let preventDefault = false;
@@ -1736,13 +1731,7 @@ var AuthenticatedMongoDriver = class {
1736
1731
  });
1737
1732
  const { callbacks, globalCallbacks, propertyCallbacks } = this.delegate.resolveCollectionCallbacks(props.collection, props.path);
1738
1733
  if (globalCallbacks?.afterRead || callbacks?.afterRead || propertyCallbacks?.afterRead) {
1739
- const contextForCallback = {
1740
- user: this.user,
1741
- driver: this,
1742
- data: this.data,
1743
- client: this.delegate.client,
1744
- storageSource: this.delegate.client?.storage
1745
- };
1734
+ const contextForCallback = callContext(this, this.user, this.data, this.delegate.client);
1746
1735
  return Promise.all(rows.map(async (row) => {
1747
1736
  let fetched = row;
1748
1737
  if (globalCallbacks?.afterRead) fetched = await globalCallbacks.afterRead({