@rebasepro/server-postgres 0.12.1-canary.g4e7bcbf → 0.12.1-canary.g6861540

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 (47) hide show
  1. package/dist/auth/services.d.ts +6 -1
  2. package/dist/backup/backup-service.d.ts +10 -1
  3. package/dist/backup/pg-tools.d.ts +47 -0
  4. package/dist/{backup-service-DLb2drIH.js → backup-service-CD8o_1Sl.js} +136 -10
  5. package/dist/backup-service-CD8o_1Sl.js.map +1 -0
  6. package/dist/{ensure-collection-policies-Dv21KDMJ.js → ensure-collection-policies-BrUVgjz3.js} +2 -2
  7. package/dist/{ensure-collection-policies-Dv21KDMJ.js.map → ensure-collection-policies-BrUVgjz3.js.map} +1 -1
  8. package/dist/{ensure-collection-tables-CT6xHB1d.js → ensure-collection-tables-Da2oGkX2.js} +2 -2
  9. package/dist/{ensure-collection-tables-CT6xHB1d.js.map → ensure-collection-tables-Da2oGkX2.js.map} +1 -1
  10. package/dist/index.es.js +256 -88
  11. package/dist/index.es.js.map +1 -1
  12. package/dist/schema/introspect-db-constraints.d.ts +57 -0
  13. package/dist/schema/introspect-db-logic.d.ts +94 -5
  14. package/dist/schema/introspect-db-queries.d.ts +119 -0
  15. package/dist/schema/introspect-db-structure.d.ts +263 -0
  16. package/dist/schema/introspect-db-types.d.ts +11 -0
  17. package/dist/services/RelationService.d.ts +24 -1
  18. package/dist/services/channel-bus/index.d.ts +1 -7
  19. package/dist/services/collection-helpers.d.ts +24 -1
  20. package/dist/{src-BkdpiQdw.js → src-CzbghKwf.js} +43 -5
  21. package/dist/src-CzbghKwf.js.map +1 -0
  22. package/dist/utils/connection-string.d.ts +29 -0
  23. package/dist/utils/drizzle-conditions.d.ts +5 -4
  24. package/package.json +6 -6
  25. package/src/auth/services.ts +6 -3
  26. package/src/backup/backup-cli.ts +41 -2
  27. package/src/backup/backup-service.ts +38 -5
  28. package/src/backup/pg-tools.ts +96 -3
  29. package/src/cli.ts +11 -4
  30. package/src/collections/validate-relations.ts +15 -0
  31. package/src/data-transformer.ts +9 -3
  32. package/src/schema/generate-drizzle-schema-logic.ts +7 -1
  33. package/src/schema/introspect-db-constraints.ts +385 -0
  34. package/src/schema/introspect-db-inference.ts +18 -8
  35. package/src/schema/introspect-db-logic.ts +364 -68
  36. package/src/schema/introspect-db-queries.ts +326 -0
  37. package/src/schema/introspect-db-structure.ts +670 -0
  38. package/src/schema/introspect-db-types.ts +56 -0
  39. package/src/schema/introspect-db.ts +37 -80
  40. package/src/services/PersistService.ts +20 -6
  41. package/src/services/RelationService.ts +211 -45
  42. package/src/services/channel-bus/index.ts +0 -9
  43. package/src/services/collection-helpers.ts +40 -1
  44. package/src/utils/connection-string.ts +58 -0
  45. package/src/utils/drizzle-conditions.ts +31 -6
  46. package/dist/backup-service-DLb2drIH.js.map +0 -1
  47. package/dist/src-BkdpiQdw.js.map +0 -1
@@ -1,5 +1,5 @@
1
1
  import { PgTable, AnyPgColumn } from "drizzle-orm/pg-core";
2
- import { CollectionConfig } from "@rebasepro/types";
2
+ import { CollectionConfig, ResolvedHasMany, ResolvedHasOne } from "@rebasepro/types";
3
3
  import { PostgresCollectionRegistry } from "../collections/PostgresCollectionRegistry";
4
4
  export { buildCompositeId, parseIdValues, COMPOSITE_ID_SEPARATOR } from "@rebasepro/common";
5
5
  export type { PrimaryKeyInfo } from "@rebasepro/common";
@@ -55,6 +55,29 @@ export declare function getPrimaryKeys(collection: CollectionConfig, registry: P
55
55
  * wrong and which collection it is wrong about.
56
56
  */
57
57
  export declare function requirePrimaryKeys(collection: CollectionConfig, registry: PostgresCollectionRegistry): PrimaryKeyInfo[];
58
+ /**
59
+ * The column on the *source* table that a `hasOne`/`hasMany` link points at.
60
+ *
61
+ * `sourceKey` is authored when the two sides join on a natural key — an
62
+ * external identity id, a SKU — and left off when they join on the row id,
63
+ * which is the overwhelming majority. That makes `undefined` the only optional
64
+ * field on a resolved relation, so it gets exactly one reader: this function.
65
+ * Every consumer that needs the column asks here, and none of them re-derives
66
+ * "or else the primary key" for itself. That is the whole point — the fallback
67
+ * chains this codebase removed from relation resolution were dangerous because
68
+ * they were *duplicated* and could disagree, not because they existed.
69
+ */
70
+ export declare function sourceKeyField(relation: ResolvedHasOne | ResolvedHasMany, sourceCollection: CollectionConfig, registry: PostgresCollectionRegistry): string;
71
+ /**
72
+ * Whether this link joins on something other than the source's primary key.
73
+ *
74
+ * Callers that hold a parent *id* — which is most of them, since an id is what
75
+ * a URL carries — must translate it to the source key's value before it can be
76
+ * compared with the target's foreign key. Those that hold the parent *row*, or
77
+ * that build a correlated subquery over the source table, can read the column
78
+ * directly and skip the lookup.
79
+ */
80
+ export declare function joinsOnNaturalKey(relation: ResolvedHasOne | ResolvedHasMany, sourceCollection: CollectionConfig, registry: PostgresCollectionRegistry): boolean;
58
81
  /**
59
82
  * Collections whose key the *browser* cannot resolve, and what it will do
60
83
  * instead.
@@ -1598,8 +1598,7 @@ function enumToObjectEntries(enumValues) {
1598
1598
  function resolveRelation(relation, sourceCollection, propertyKey) {
1599
1599
  const target = relation.target;
1600
1600
  if (typeof target !== "function") throw new Error(`Relation${relation.relationName ? ` '${relation.relationName}'` : ""} on '${sourceCollection.slug}' has no \`target\`. Give it a thunk: \`target: () => otherCollection\`.`);
1601
- const targetCollection = target();
1602
- if (!targetCollection?.slug) throw new Error(`Relation${relation.relationName ? ` '${relation.relationName}'` : ""} on '${sourceCollection.slug}' has a \`target\` that did not resolve to a collection.`);
1601
+ const targetCollection = callTarget(relation, sourceCollection, propertyKey, target);
1603
1602
  const relationName = relation.relationName ?? propertyKey ?? toSnakeCase(targetCollection.slug);
1604
1603
  const shared = {
1605
1604
  relationName,
@@ -1626,7 +1625,8 @@ function resolveRelation(relation, sourceCollection, propertyKey) {
1626
1625
  cardinality: "one",
1627
1626
  writable: true,
1628
1627
  shared: false,
1629
- foreignKeyOnTarget: relation.foreignKeyOnTarget ?? generateForeignKeyName(sourceName)
1628
+ foreignKeyOnTarget: relation.foreignKeyOnTarget ?? generateForeignKeyName(sourceName),
1629
+ sourceKey: relation.sourceKey
1630
1630
  };
1631
1631
  case "hasMany": return {
1632
1632
  ...shared,
@@ -1634,7 +1634,8 @@ function resolveRelation(relation, sourceCollection, propertyKey) {
1634
1634
  cardinality: "many",
1635
1635
  writable: true,
1636
1636
  shared: false,
1637
- foreignKeyOnTarget: relation.foreignKeyOnTarget ?? generateForeignKeyName(sourceName)
1637
+ foreignKeyOnTarget: relation.foreignKeyOnTarget ?? generateForeignKeyName(sourceName),
1638
+ sourceKey: relation.sourceKey
1638
1639
  };
1639
1640
  case "manyToMany": {
1640
1641
  const sourceTable = getTableName(sourceCollection);
@@ -1663,6 +1664,43 @@ function resolveRelation(relation, sourceCollection, propertyKey) {
1663
1664
  default: throw new Error(`Unknown relation kind: ${JSON.stringify(relation)}`);
1664
1665
  }
1665
1666
  }
1667
+ /** How this relation is addressed in an error message, before it has a resolved name. */
1668
+ function describe(relation, sourceCollection, propertyKey) {
1669
+ const name = relation.relationName ?? propertyKey;
1670
+ return `Relation${name ? ` '${name}'` : ""} on '${sourceCollection.slug}'`;
1671
+ }
1672
+ /**
1673
+ * Call the `target` thunk, and translate the two ways an import cycle breaks it
1674
+ * into an error that names the cause.
1675
+ *
1676
+ * The thunk exists to defer the reference until every module has finished
1677
+ * evaluating, and for a cycle that closes at import time it does. What it cannot
1678
+ * defer is a cycle that leaves the binding permanently unusable, and there are
1679
+ * two shapes of that:
1680
+ *
1681
+ * - **ESM/TDZ.** `const` and `class` bindings in a not-yet-evaluated module are
1682
+ * in the temporal dead zone, so reading one throws `ReferenceError: x is not
1683
+ * defined`. The stack points at the thunk — a one-line arrow function that is
1684
+ * obviously fine — and says nothing about the cycle that made it throw.
1685
+ * - **CJS interop.** The half-initialised module object has no `default` yet,
1686
+ * the import resolves to `undefined`, and the thunk returns it without
1687
+ * complaint. That one used to surface here as "did not resolve to a
1688
+ * collection", which is true and unhelpful.
1689
+ *
1690
+ * Both mean the same thing, and the fix for both is the same: break the cycle,
1691
+ * or move the relation into the collection that does not close it.
1692
+ */
1693
+ function callTarget(relation, sourceCollection, propertyKey, target) {
1694
+ let targetCollection;
1695
+ try {
1696
+ targetCollection = target();
1697
+ } catch (error) {
1698
+ 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 });
1699
+ throw error;
1700
+ }
1701
+ 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`."));
1702
+ return targetCollection;
1703
+ }
1666
1704
  //#endregion
1667
1705
  //#region ../common/src/util/relations.ts
1668
1706
  /**
@@ -4230,4 +4268,4 @@ function buildSdkData(driver) {
4230
4268
  //#endregion
4231
4269
  export { DEFAULT_ONE_OF_TYPE as A, updateDateAutoValues as C, mergeDeep as D, getPolicyNamesForRule as E, hasForeignKeyOnTarget as M, isManyToMany as N, camelCase as O, Vector as P, normalizeToEntityRelation as S, legacyForeignKeyName as T, buildCompositeId as _, getJunctionSecurityRules as a, createRelationRef as b, policyToPostgres as c, findRelation as d, getColumnName as f, resolveCollectionRelations as g, getTableVarName as h, getJunctionCollectionConfig as i, DEFAULT_ONE_OF_VALUE as j, toSnakeCase as k, securityRuleToConditions as l, getTableName as m, CollectionRegistry as n, resolveJunctionSpecs as o, getEnumVarName as p, resolveStringColumnLength as r, getEffectiveSecurityRules as s, buildSdkData as t, findAnonymousGrants as u, getDeclaredPrimaryKeys as v, generateForeignKeyName as w, createRelationRefWithData as x, parseIdValues as y };
4232
4270
 
4233
- //# sourceMappingURL=src-BkdpiQdw.js.map
4271
+ //# sourceMappingURL=src-CzbghKwf.js.map