@rebasepro/server-postgres 0.14.1 → 0.14.2-canary.g27a129e

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 (29) hide show
  1. package/dist/{auth-users-columns-C-FDnL_e.js → auth-users-columns-JJ8ngvy5.js} +59 -17
  2. package/dist/auth-users-columns-JJ8ngvy5.js.map +1 -0
  3. package/dist/{backup-service-BH0Dzo_h.js → backup-service-czK-OAuG.js} +2 -2
  4. package/dist/{backup-service-BH0Dzo_h.js.map → backup-service-czK-OAuG.js.map} +1 -1
  5. package/dist/{ensure-collection-policies-DoHwhVf8.js → ensure-collection-policies-D5PtQLyR.js} +3 -3
  6. package/dist/{ensure-collection-policies-DoHwhVf8.js.map → ensure-collection-policies-D5PtQLyR.js.map} +1 -1
  7. package/dist/{ensure-collection-tables-DT2eq859.js → ensure-collection-tables-BHUjQ-z4.js} +3 -3
  8. package/dist/{ensure-collection-tables-DT2eq859.js.map → ensure-collection-tables-BHUjQ-z4.js.map} +1 -1
  9. package/dist/index.es.js +485 -19
  10. package/dist/index.es.js.map +1 -1
  11. package/dist/{rls-bootstrap-sql-69hYT8nr.js → rls-bootstrap-sql-B5Sajku6.js} +2 -2
  12. package/dist/{rls-bootstrap-sql-69hYT8nr.js.map → rls-bootstrap-sql-B5Sajku6.js.map} +1 -1
  13. package/dist/{rls-enforcement-gUNDfm7l.js → rls-enforcement-BDBfuTD4.js} +4 -3
  14. package/dist/rls-enforcement-BDBfuTD4.js.map +1 -0
  15. package/dist/services/FetchService.d.ts +22 -0
  16. package/dist/{src-DCdn3Val.js → src-BBFsDaeA.js} +60 -2
  17. package/dist/src-BBFsDaeA.js.map +1 -0
  18. package/dist/utils/drizzle-conditions.d.ts +168 -1
  19. package/dist/utils/pg-error-utils.d.ts +27 -0
  20. package/dist/{websocket-D2jXv0Ds.js → websocket-BVgDVO-V.js} +2 -2
  21. package/dist/{websocket-D2jXv0Ds.js.map → websocket-BVgDVO-V.js.map} +1 -1
  22. package/package.json +6 -6
  23. package/src/services/FetchService.ts +155 -15
  24. package/src/services/PersistService.ts +23 -2
  25. package/src/utils/drizzle-conditions.ts +594 -1
  26. package/src/utils/pg-error-utils.ts +49 -4
  27. package/dist/auth-users-columns-C-FDnL_e.js.map +0 -1
  28. package/dist/rls-enforcement-gUNDfm7l.js.map +0 -1
  29. package/dist/src-DCdn3Val.js.map +0 -1
@@ -3,7 +3,7 @@ import "process";
3
3
  __createRequire(import.meta.url);
4
4
  import { l as __require, s as __commonJSMin } from "./connection-BuZ97wsr.js";
5
5
  import { a as policy, i as ANONYMOUS_USER_IDS, r as ANONYMOUS_USER_ID } from "./data_driver-ULAyJEi9.js";
6
- import { a as rewriteLegacyRlsFunctions, c as isPostgresCollectionConfig, d as getDataSourceCapabilities, f as ALL_WHERE_FILTER_OPS, g as toCanonicalOp, h as REST_TO_CANONICAL, i as RLS_UID_SQL, l as isRelationalCollectionConfig, m as NULL_OPS, p as CANONICAL_TO_REST, r as RLS_ROLES_SQL, s as getDeclaredSubcollections } from "./src-DCdn3Val.js";
6
+ import { _ as isRelationAggregateSort, a as rewriteLegacyRlsFunctions, b as toCanonicalOp, c as isPostgresCollectionConfig, d as getDataSourceCapabilities, f as ALL_WHERE_FILTER_OPS, h as REST_TO_CANONICAL, i as RLS_UID_SQL, l as isRelationalCollectionConfig, m as NULL_OPS, p as CANONICAL_TO_REST, r as RLS_ROLES_SQL, s as getDeclaredSubcollections, y as sortKeyToString } from "./src-BBFsDaeA.js";
7
7
  import { createHash } from "node:crypto";
8
8
  //#region ../types/src/types/entities.ts
9
9
  /**
@@ -1779,7 +1779,7 @@ function resolveRelation(relation, sourceCollection, propertyKey) {
1779
1779
  const relationName = relation.relationName ?? propertyKey ?? toSnakeCase(targetCollection.slug);
1780
1780
  const shared = {
1781
1781
  relationName,
1782
- target,
1782
+ target: () => unwrapModuleNamespace(target()),
1783
1783
  targetSlug: targetCollection.slug,
1784
1784
  onUpdate: relation.onUpdate,
1785
1785
  onDelete: relation.onDelete,
@@ -1847,35 +1847,67 @@ function describe(relation, sourceCollection, propertyKey) {
1847
1847
  return `Relation${name ? ` '${name}'` : ""} on '${sourceCollection.slug}'`;
1848
1848
  }
1849
1849
  /**
1850
- * Call the `target` thunk, and translate the two ways an import cycle breaks it
1851
- * into an error that names the cause.
1850
+ * A module namespace, unwrapped to the collection it exports.
1851
+ *
1852
+ * A cycle transpiled to CommonJS does not hand the importing module the
1853
+ * *default export* — it hands it the module object, `{ __esModule: true,
1854
+ * default: … }`, captured before the exporting module finished evaluating. The
1855
+ * `default` slot fills in later, so by the time a lazy `target` thunk runs the
1856
+ * collection is sitting right there, one level down. Returning the namespace is
1857
+ * never a thing a thunk means to do, and there is exactly one reading of it.
1858
+ *
1859
+ * Only unwrapped when the inner value is itself a collection: a `default` that
1860
+ * is not one is a genuinely wrong thunk, and it should reach the error below
1861
+ * rather than be quietly swapped in.
1862
+ */
1863
+ function unwrapModuleNamespace(value) {
1864
+ if (!value || typeof value !== "object") return value;
1865
+ if (value.slug) return value;
1866
+ const inner = value.default;
1867
+ return inner && typeof inner === "object" && inner.slug ? inner : value;
1868
+ }
1869
+ /**
1870
+ * Call the `target` thunk, and translate the ways an import cycle breaks it into
1871
+ * an error that names the cause — or, where the value is recoverable, into the
1872
+ * collection the thunk meant.
1852
1873
  *
1853
1874
  * The thunk exists to defer the reference until every module has finished
1854
- * evaluating, and for a cycle that closes at import time it does. What it cannot
1855
- * defer is a cycle that leaves the binding permanently unusable, and there are
1856
- * two shapes of that:
1875
+ * evaluating, and for a cycle that closes at import time it does. Two cycles
1876
+ * leave the binding permanently unusable:
1857
1877
  *
1858
1878
  * - **ESM/TDZ.** `const` and `class` bindings in a not-yet-evaluated module are
1859
1879
  * in the temporal dead zone, so reading one throws `ReferenceError: x is not
1860
1880
  * defined`. The stack points at the thunk — a one-line arrow function that is
1861
1881
  * obviously fine — and says nothing about the cycle that made it throw.
1862
- * - **CJS interop.** The half-initialised module object has no `default` yet,
1863
- * the import resolves to `undefined`, and the thunk returns it without
1864
- * complaint. That one used to surface here as "did not resolve to a
1882
+ * - **CJS interop, unresolved.** The half-initialised module object has no
1883
+ * `default` yet, the import resolves to `undefined`, and the thunk returns it
1884
+ * without complaint. That one used to surface here as "did not resolve to a
1865
1885
  * collection", which is true and unhelpful.
1866
1886
  *
1867
1887
  * Both mean the same thing, and the fix for both is the same: break the cycle,
1868
1888
  * or move the relation into the collection that does not close it.
1889
+ *
1890
+ * A third shape is *not* an error, and used to be reported as one. A loader that
1891
+ * transpiles ESM to CJS — jiti, which is what `rebase generate-sdk` and
1892
+ * `rebase build` load collections with — gives the module entered second in a
1893
+ * cycle a namespace object rather than the default export, and never replaces it
1894
+ * with a live binding. The thunk then returns `{ __esModule: true, default: … }`
1895
+ * holding the fully-initialised collection. Native ESM resolves the same thunk
1896
+ * to the collection directly, so this was a loader artefact reported as an
1897
+ * authoring mistake, and the advice it gave — make the target a lazy thunk — was
1898
+ * already satisfied by the code it was rejecting. Bidirectional relations make
1899
+ * these cycles unavoidable, and the lazy thunk is this framework's own answer to
1900
+ * them, so {@link unwrapModuleNamespace} takes the collection and moves on.
1869
1901
  */
1870
1902
  function callTarget(relation, sourceCollection, propertyKey, target) {
1871
1903
  let targetCollection;
1872
1904
  try {
1873
- targetCollection = target();
1905
+ targetCollection = unwrapModuleNamespace(target());
1874
1906
  } catch (error) {
1875
1907
  if (error instanceof ReferenceError) throw new Error(`${describe(relation, sourceCollection, propertyKey)} targets a collection that is not initialized yet — almost always an import cycle between the two collection files. Break the cycle (move the shared piece into a third module, or import the target lazily) so the target's module finishes evaluating before the registry is built.`, { cause: error });
1876
1908
  throw error;
1877
1909
  }
1878
- if (!targetCollection?.slug) throw new Error(`${describe(relation, sourceCollection, propertyKey)} has a \`target\` that resolved to ${targetCollection === void 0 ? "`undefined`" : "something that is not a collection"}. ` + (targetCollection === void 0 ? "Under CommonJS interop an import cycle resolves the default import to `undefined`, so check whether this collection and its target import each other. Otherwise the thunk is returning the wrong value — it must return the collection itself, not a promise or a module." : "The thunk must return a collection config with a `slug`."));
1910
+ if (!targetCollection?.slug) throw new Error(`${describe(relation, sourceCollection, propertyKey)} has a \`target\` that resolved to ${targetCollection === void 0 ? "`undefined`" : "something that is not a collection"}. ` + (targetCollection === void 0 ? "Under CommonJS interop an import cycle resolves the default import to `undefined`, so check whether this collection and its target import each other. Otherwise the thunk is returning the wrong value — it must return the collection itself, not a promise or a module." : typeof targetCollection.then === "function" ? "The thunk returned a promise — `target: () => import(\"./other\")` is asynchronous. Import the collection at the top of the file and return the binding: `target: () => otherCollection`." : "The thunk must return a collection config with a `slug`."));
1879
1911
  return targetCollection;
1880
1912
  }
1881
1913
  //#endregion
@@ -4026,6 +4058,13 @@ var CollectionRegistry = class {
4026
4058
  * the same value; the two are told apart by whether the first element is
4027
4059
  * itself an array, which no field name ever is.
4028
4060
  *
4061
+ * This is also where a {@link RelationAggregateSort} object stops being an
4062
+ * object. Above this function a sort key may be either spelling; below it,
4063
+ * every key is a string — which is what `OrderByTuple`, the REST parameter, the
4064
+ * driver contract and the cursor all already were. Doing it here means the one
4065
+ * place that already collapses the two *shapes* of a sort also collapses the
4066
+ * two *spellings* of a key, rather than every consumer learning about both.
4067
+ *
4029
4068
  * @returns The keys in order of significance, or `undefined` for no sort. An
4030
4069
  * empty list also returns `undefined` — "sort by nothing" is no sort, and
4031
4070
  * letting `[]` through would have every layer below re-deciding what it meant.
@@ -4033,7 +4072,8 @@ var CollectionRegistry = class {
4033
4072
  function normalizeOrderBy(orderBy) {
4034
4073
  if (!orderBy || orderBy.length === 0) return void 0;
4035
4074
  const list = Array.isArray(orderBy[0]) ? orderBy : [orderBy];
4036
- return list.length > 0 ? list : void 0;
4075
+ if (list.length === 0) return void 0;
4076
+ return list.map(([key, direction]) => [sortKeyToString(key), direction]);
4037
4077
  }
4038
4078
  /**
4039
4079
  * Collapse the driver-level `{orderBy, order}` pair into the list form.
@@ -4080,14 +4120,16 @@ function parseOrderBySpecStrict(raw, order) {
4080
4120
  if (raw === void 0 || raw === null || raw === "") return void 0;
4081
4121
  if (typeof raw === "string") return normalizeDriverOrderBy(raw, order);
4082
4122
  if (!Array.isArray(raw) || raw.length === 0) throw new OrderBySpecError(`${typeof raw} is not a field name or a list of sort keys`);
4083
- if (typeof raw[0] === "string") return [toStrictTuple(raw, 0)];
4123
+ if (typeof raw[0] === "string" || isRelationAggregateSort(raw[0])) return [toStrictTuple(raw, 0)];
4084
4124
  return raw.map(toStrictTuple);
4085
4125
  }
4086
4126
  function toStrictTuple(raw, index) {
4087
- if (!Array.isArray(raw) || typeof raw[0] !== "string" || raw[0].trim() === "") throw new OrderBySpecError(`entry ${index} has no field name`);
4127
+ if (!Array.isArray(raw)) throw new OrderBySpecError(`entry ${index} has no field name`);
4128
+ const key = isRelationAggregateSort(raw[0]) ? sortKeyToString(raw[0]) : raw[0];
4129
+ if (typeof key !== "string" || key.trim() === "") throw new OrderBySpecError(`entry ${index} has no field name`);
4088
4130
  const direction = raw[1];
4089
4131
  if (direction !== void 0 && direction !== "asc" && direction !== "desc") throw new OrderBySpecError(`entry ${index} has direction '${String(direction)}'`);
4090
- return [raw[0], direction ?? "asc"];
4132
+ return [key, direction ?? "asc"];
4091
4133
  }
4092
4134
  //#endregion
4093
4135
  //#region ../common/src/data/query_builder.ts
@@ -5662,4 +5704,4 @@ function isAuthCollection(collection) {
5662
5704
  //#endregion
5663
5705
  export { mergeDeep as $, fieldKeyForColumn as A, parseIdValues as B, getJunctionCollectionConfig as C, policyToPostgres as D, getEffectiveSecurityRules as E, getTableVarName as F, updateDateAutoValues as G, createRelationRef as H, resolveCollectionRelations as I, legacyForeignKeyName as J, firstFreeKey as K, buildCompositeId as L, getColumnName as M, getEnumVarName as N, securityRuleToConditions as O, getTableName as P, isPrototypePollutingKey as Q, getDeclaredPrimaryKeys as R, resolveStringColumnLength as S, resolveJunctionSpecs as T, createRelationRefWithData as U, sortCollectionsBySlug as V, normalizeToEntityRelation as W, toWireKey as X, toPostgresIdentifier as Y, getPolicyNamesForRule as Z, OrderBySpecError as _, SEARCH_STAMP_PREFIX as a, isManyToMany as at, CollectionRegistry as b, assertSearchIsPostgresOnly as c, searchColumnStamps as d, camelCase as et, searchExtensionStatements as f, buildSdkData as g, visibleColumnProjection as h, isAuthCollection as i, hasForeignKeyOnTarget as it, findRelation as j, findAnonymousGrants as k, buildSearchColumnSpec as l, searchIndexStatements as m, authUsersColumnDefinition as n, DEFAULT_ONE_OF_TYPE as nt, SEARCH_TEXT_FN as o, Vector as ot, searchHelperFunctions as p, generateForeignKeyName as q, authUsersColumnSql as r, DEFAULT_ONE_OF_VALUE as rt, SEARCH_UNACCENT_FN as s, AUTH_USERS_COLUMNS as t, toSnakeCase as tt, hiddenColumnsOption as u, normalizeDriverOrderBy as v, getJunctionSecurityRules as w, relationalCollections as x, parseOrderBySpecStrict as y, isAddressableId as z };
5664
5706
 
5665
- //# sourceMappingURL=auth-users-columns-C-FDnL_e.js.map
5707
+ //# sourceMappingURL=auth-users-columns-JJ8ngvy5.js.map