clivly 0.9.5 → 0.9.7

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 (78) hide show
  1. package/README.md +22 -3
  2. package/core-auth-adapter.cjs +1 -1
  3. package/core-auth-adapter.mjs +1 -1
  4. package/core-drizzle.cjs +1 -1
  5. package/core-drizzle.d.cts +1 -1
  6. package/core-drizzle.d.mts +1 -1
  7. package/core-drizzle.mjs +1 -1
  8. package/core-entity-config.cjs +1 -1
  9. package/core-entity-config.mjs +1 -1
  10. package/core-mapping-form.cjs +1 -1
  11. package/core-mapping-form.mjs +1 -1
  12. package/core-mappings-config.cjs +1 -1
  13. package/core-mappings-config.mjs +1 -1
  14. package/core-sync-engine.cjs +1 -1
  15. package/core-sync-engine.mjs +1 -1
  16. package/core-view-compiler.cjs +1 -1
  17. package/core-view-compiler.mjs +1 -1
  18. package/core.cjs +14 -9
  19. package/core.d.cts +2 -2
  20. package/core.d.mts +2 -2
  21. package/core.mjs +9 -9
  22. package/{drizzle-CiK8MMEZ.d.mts → drizzle-BmDDgAIF.d.mts} +3 -1
  23. package/{drizzle-bfjVzYR6.cjs → drizzle-DcB7OfBp.cjs} +9 -1
  24. package/{drizzle-DEtRrqtc.d.cts → drizzle-ixVV4pPN.d.cts} +3 -1
  25. package/{drizzle-3kvhkwO7.mjs → drizzle-wbcuG-cy.mjs} +9 -1
  26. package/drizzle.cjs +61 -10
  27. package/drizzle.d.cts +9 -3
  28. package/drizzle.d.mts +9 -3
  29. package/drizzle.mjs +61 -10
  30. package/{entity-config-B74SPOUZ.mjs → entity-config-C8IIkrmX.mjs} +34 -5
  31. package/{entity-config-7sGCk_nw.cjs → entity-config-CPM-Tp-d.cjs} +34 -5
  32. package/index.cjs +41 -9
  33. package/index.mjs +41 -9
  34. package/internal-BLKMnIIZ.cjs +259 -0
  35. package/internal-BpVh_J77.mjs +212 -0
  36. package/{internal-o2M7FGlR.d.mts → internal-CvlFk3KC.d.mts} +82 -2
  37. package/{internal-wCXSELDN.d.cts → internal-DGUOLQAq.d.cts} +82 -2
  38. package/internal-drizzle.cjs +1 -1
  39. package/internal-drizzle.d.cts +1 -1
  40. package/internal-drizzle.d.mts +1 -1
  41. package/internal-drizzle.mjs +1 -1
  42. package/internal-entity-config.cjs +1 -1
  43. package/internal-entity-config.mjs +1 -1
  44. package/internal-entity-heuristics.cjs +1 -1
  45. package/internal-entity-heuristics.mjs +1 -1
  46. package/internal-mappings-config.cjs +1 -1
  47. package/internal-mappings-config.mjs +1 -1
  48. package/internal-sync-engine.cjs +1 -1
  49. package/internal-sync-engine.mjs +1 -1
  50. package/internal-view-compiler.cjs +1 -1
  51. package/internal-view-compiler.mjs +1 -1
  52. package/internal.cjs +13 -7
  53. package/internal.d.cts +2 -2
  54. package/internal.d.mts +2 -2
  55. package/internal.mjs +8 -8
  56. package/{mappings-config-CLyEg0sf.cjs → mappings-config-CBwL3FVh.cjs} +1 -1
  57. package/{mappings-config-DQS_s4dg.mjs → mappings-config-DUKBe6OS.mjs} +1 -1
  58. package/{namespace-probe-Cn7voAd0.d.mts → namespace-probe-BQ4imnV7.d.mts} +85 -2
  59. package/{namespace-probe-BoGI18dQ.d.cts → namespace-probe-DZIGdvlm.d.cts} +85 -2
  60. package/package.json +1 -1
  61. package/{projection-Dz6_i1-w.cjs → projection-DEwL-D8R.cjs} +6 -0
  62. package/{projection-CSGJbZlZ.mjs → projection-hnPSgH6G.mjs} +1 -1
  63. package/{projection-sql-BgnfiGqF.mjs → projection-sql-9zzTVGvf.mjs} +34 -2
  64. package/{projection-sql-Cl1qIn8m.cjs → projection-sql-iHy-Cx6V.cjs} +45 -1
  65. package/{sdk-CM1UO-I7.cjs → sdk-BL9BCzyD.cjs} +354 -20
  66. package/{sdk-DYBDR939.mjs → sdk-CubnsyD9.mjs} +354 -20
  67. package/sdk.cjs +2 -2
  68. package/sdk.d.cts +2 -2
  69. package/sdk.d.mts +2 -2
  70. package/sdk.mjs +2 -2
  71. package/{sync-engine-C4A5jgA4.cjs → sync-engine-BLgwLpR6.cjs} +1 -1
  72. package/{sync-engine-Dt5QQSKO.mjs → sync-engine-B_IzoUSx.mjs} +1 -1
  73. package/{view-compiler-B5nDal14.cjs → view-compiler-BHI5E4QI.cjs} +2 -2
  74. package/{view-compiler-CSYwEWf5.mjs → view-compiler-D_6gfXFr.mjs} +2 -2
  75. package/vite.d.cts +1 -1
  76. package/vite.d.mts +1 -1
  77. package/internal-CykXpEGk.mjs +0 -68
  78. package/internal-DqduZU3l.cjs +0 -97
@@ -0,0 +1,259 @@
1
+ const require_projection = require("./projection-DEwL-D8R.cjs");
2
+ const require_entity_config = require("./entity-config-CPM-Tp-d.cjs");
3
+ //#region ../core/src/projection-draft.ts
4
+ function emptyProjectionDraft(baseTable) {
5
+ return {
6
+ baseTable,
7
+ entityKey: "",
8
+ fieldBindings: {},
9
+ identity: { kind: "base_pk" },
10
+ joins: []
11
+ };
12
+ }
13
+ /**
14
+ * The ON conditions implied by a foreign key between the two tables, so the
15
+ * builder can pre-fill a join instead of making the user hand-pick columns.
16
+ * Returns [] when no FK relates them — the user then picks manually.
17
+ */
18
+ function suggestJoinConditions(baseTable, joinTable, schema, joinKey) {
19
+ const join = schema.find((t) => t.name === joinTable);
20
+ const conditions = [];
21
+ for (const column of join?.columnsMeta ?? []) if (column.references?.table === baseTable) conditions.push({
22
+ left: {
23
+ table: joinKey,
24
+ column: column.name
25
+ },
26
+ op: "eq",
27
+ right: {
28
+ table: require_projection.BASE_REF,
29
+ column: column.references.column
30
+ }
31
+ });
32
+ return conditions;
33
+ }
34
+ /**
35
+ * Add a join on `joinTable`, keyed and pre-filled.
36
+ *
37
+ * The key is the table's own name where that is free, because it is what the
38
+ * user already calls the table; a collision falls back to `_2`, `_3` and so on.
39
+ * Uniqueness is not cosmetic — the key is the SQL alias and the name every ref
40
+ * addresses the table by, so two joins sharing one would make every ref
41
+ * ambiguous. `base` is reserved for the base table and can never be taken.
42
+ */
43
+ function addDraftJoin(draft, joinTable, schema) {
44
+ const taken = /* @__PURE__ */ new Set([require_projection.BASE_REF, ...draft.joins.map((j) => j.key)]);
45
+ let key = joinTable;
46
+ let suffix = 2;
47
+ while (taken.has(key)) {
48
+ key = `${joinTable}_${suffix}`;
49
+ suffix += 1;
50
+ }
51
+ return {
52
+ ...draft,
53
+ joins: [...draft.joins, {
54
+ key,
55
+ table: joinTable,
56
+ type: "left",
57
+ on: suggestJoinConditions(draft.baseTable, joinTable, schema, key)
58
+ }]
59
+ };
60
+ }
61
+ /**
62
+ * Remove a join, and everything that addressed it.
63
+ *
64
+ * Refs to a removed join are refs to a table the projection no longer declares.
65
+ * Left in place they fail validation as "unknown ref table", which blames the
66
+ * field — or, worse, the identity — for the join's removal. Identity falls back
67
+ * to the base primary key, the same default a new draft starts from.
68
+ */
69
+ function removeDraftJoin(draft, joinKey) {
70
+ const fieldBindings = Object.fromEntries(Object.entries(draft.fieldBindings).filter(([, binding]) => binding.from.table !== joinKey));
71
+ const identityDependsOnJoin = require_projection.identityRefs(draft.identity, "").some((ref) => ref.table === joinKey);
72
+ return {
73
+ ...draft,
74
+ joins: draft.joins.filter((join) => join.key !== joinKey),
75
+ fieldBindings,
76
+ identity: identityDependsOnJoin ? { kind: "base_pk" } : draft.identity
77
+ };
78
+ }
79
+ /**
80
+ * Every table a field or identity may currently read from, in ref order: the
81
+ * base table first, then each join.
82
+ *
83
+ * Returns [] when the base table is not in the discovered schema — there is
84
+ * nothing truthful to offer, and inventing columns would let someone build a
85
+ * projection that cannot compile.
86
+ */
87
+ function draftRefOptions(draft, schema) {
88
+ const byName = new Map(schema.map((table) => [table.name, table]));
89
+ const base = byName.get(draft.baseTable);
90
+ if (!base) return [];
91
+ const options = [{
92
+ ref: require_projection.BASE_REF,
93
+ label: `${base.name} (${require_projection.BASE_REF})`,
94
+ columns: base.columns
95
+ }];
96
+ for (const join of draft.joins) {
97
+ const table = byName.get(join.table);
98
+ if (table) options.push({
99
+ ref: join.key,
100
+ label: `${table.name} (${join.key})`,
101
+ columns: table.columns
102
+ });
103
+ }
104
+ return options;
105
+ }
106
+ /** A draft becomes a projection once it has a valid key and ≥1 binding. */
107
+ function draftToProjection(draft) {
108
+ if (!require_projection.PROJECTION_KEY_PATTERN.test(draft.entityKey)) return null;
109
+ if (Object.keys(draft.fieldBindings).length === 0) return null;
110
+ return {
111
+ version: 1,
112
+ kind: "projection",
113
+ entityKey: draft.entityKey,
114
+ baseTable: draft.baseTable,
115
+ joins: draft.joins,
116
+ fieldBindings: draft.fieldBindings,
117
+ identity: draft.identity
118
+ };
119
+ }
120
+ const PROBE_KEY = "draft";
121
+ /**
122
+ * A draft rendered as a projection so the semantic checks can run against it
123
+ * while it is being written, not only once it is finished.
124
+ *
125
+ * `entityKey` is stood in for rather than taken from the draft: it is the one
126
+ * field `validateProjection` never looks at, and a half-typed key would
127
+ * otherwise have to be valid before any join or identity could be checked. The
128
+ * missing or malformed key is reported separately, by `draftErrors`.
129
+ */
130
+ function probeProjection(draft) {
131
+ return {
132
+ version: 1,
133
+ kind: "projection",
134
+ entityKey: PROBE_KEY,
135
+ baseTable: draft.baseTable,
136
+ joins: draft.joins,
137
+ fieldBindings: draft.fieldBindings,
138
+ identity: draft.identity
139
+ };
140
+ }
141
+ /**
142
+ * `validateProjection` appends its own `projection.` segment to the base path it
143
+ * is given, because in a config the base is the entity that owns the projection
144
+ * (`entities.accounts` → `entities.accounts.projection.identity`). A draft has
145
+ * no owning entity, so passing "projection" as the base produced
146
+ * `projection.projection.identity` — a path no caller could match against, and
147
+ * the reason `draftErrors` was only ever consumed as a flat list of messages.
148
+ *
149
+ * Passing an empty base and dropping the leading separator leaves one
150
+ * convention: every path here starts `projection.` exactly once, whether it came
151
+ * from this module or from the validator.
152
+ */
153
+ function semanticErrors(draft, schema) {
154
+ return require_entity_config.validateProjection(probeProjection(draft), schema, "").map((error) => ({
155
+ ...error,
156
+ path: error.path.startsWith(".") ? error.path.slice(1) : error.path
157
+ }));
158
+ }
159
+ /**
160
+ * Every reason this draft cannot be saved, one per cause and each anchored to
161
+ * the section that owns it.
162
+ *
163
+ * One lumped "Name the object and map a field." was all this used to return for
164
+ * an incomplete draft, at the bare path "projection". Two consequences, both
165
+ * bad: a builder had nowhere to attach the message, and — because it returned
166
+ * before validating — a fanning join under a `base_pk` identity stayed silent
167
+ * until the draft was otherwise complete, which is well after the point where
168
+ * the user chose the join.
169
+ */
170
+ function draftErrors(draft, schema) {
171
+ const errors = [];
172
+ if (draft.entityKey === "") errors.push({
173
+ path: "projection.entityKey",
174
+ message: "Key is required."
175
+ });
176
+ else if (!require_projection.PROJECTION_KEY_PATTERN.test(draft.entityKey)) errors.push({
177
+ path: "projection.entityKey",
178
+ message: `Key ${require_projection.PROJECTION_KEY_MESSAGE}.`
179
+ });
180
+ if (Object.keys(draft.fieldBindings).length === 0) errors.push({
181
+ path: "projection.fieldBindings",
182
+ message: "Map at least one field."
183
+ });
184
+ if (draft.baseTable === "") {
185
+ errors.push({
186
+ path: "projection.baseTable",
187
+ message: "Pick a base table."
188
+ });
189
+ return errors;
190
+ }
191
+ errors.push(...semanticErrors(draft, schema));
192
+ return errors;
193
+ }
194
+ /**
195
+ * Per-join fan-out, for the builder's inline cardinality hints.
196
+ *
197
+ * Answered from the joins alone. Joins are chosen before the key and the fields
198
+ * are filled in, and a fan-out warning is precisely what should steer the
199
+ * identity choice while it is being made — requiring a complete draft put the
200
+ * hint after the decision it exists to inform.
201
+ */
202
+ function describeCardinality(draft, schema) {
203
+ const projection = probeProjection(draft);
204
+ const tablesByName = new Map(schema.map((t) => [t.name, t]));
205
+ const fanning = new Set(require_entity_config.fanOutJoins(projection, tablesByName).map((j) => j.key));
206
+ return projection.joins.map((join) => ({
207
+ joinKey: join.key,
208
+ fansOut: fanning.has(join.key)
209
+ }));
210
+ }
211
+ //#endregion
212
+ Object.defineProperty(exports, "addDraftJoin", {
213
+ enumerable: true,
214
+ get: function() {
215
+ return addDraftJoin;
216
+ }
217
+ });
218
+ Object.defineProperty(exports, "describeCardinality", {
219
+ enumerable: true,
220
+ get: function() {
221
+ return describeCardinality;
222
+ }
223
+ });
224
+ Object.defineProperty(exports, "draftErrors", {
225
+ enumerable: true,
226
+ get: function() {
227
+ return draftErrors;
228
+ }
229
+ });
230
+ Object.defineProperty(exports, "draftRefOptions", {
231
+ enumerable: true,
232
+ get: function() {
233
+ return draftRefOptions;
234
+ }
235
+ });
236
+ Object.defineProperty(exports, "draftToProjection", {
237
+ enumerable: true,
238
+ get: function() {
239
+ return draftToProjection;
240
+ }
241
+ });
242
+ Object.defineProperty(exports, "emptyProjectionDraft", {
243
+ enumerable: true,
244
+ get: function() {
245
+ return emptyProjectionDraft;
246
+ }
247
+ });
248
+ Object.defineProperty(exports, "removeDraftJoin", {
249
+ enumerable: true,
250
+ get: function() {
251
+ return removeDraftJoin;
252
+ }
253
+ });
254
+ Object.defineProperty(exports, "suggestJoinConditions", {
255
+ enumerable: true,
256
+ get: function() {
257
+ return suggestJoinConditions;
258
+ }
259
+ });
@@ -0,0 +1,212 @@
1
+ import { i as identityRefs, n as PROJECTION_KEY_MESSAGE, r as PROJECTION_KEY_PATTERN, t as BASE_REF } from "./projection-hnPSgH6G.mjs";
2
+ import { h as fanOutJoins, m as validateProjection } from "./entity-config-C8IIkrmX.mjs";
3
+ //#region ../core/src/projection-draft.ts
4
+ function emptyProjectionDraft(baseTable) {
5
+ return {
6
+ baseTable,
7
+ entityKey: "",
8
+ fieldBindings: {},
9
+ identity: { kind: "base_pk" },
10
+ joins: []
11
+ };
12
+ }
13
+ /**
14
+ * The ON conditions implied by a foreign key between the two tables, so the
15
+ * builder can pre-fill a join instead of making the user hand-pick columns.
16
+ * Returns [] when no FK relates them — the user then picks manually.
17
+ */
18
+ function suggestJoinConditions(baseTable, joinTable, schema, joinKey) {
19
+ const join = schema.find((t) => t.name === joinTable);
20
+ const conditions = [];
21
+ for (const column of join?.columnsMeta ?? []) if (column.references?.table === baseTable) conditions.push({
22
+ left: {
23
+ table: joinKey,
24
+ column: column.name
25
+ },
26
+ op: "eq",
27
+ right: {
28
+ table: BASE_REF,
29
+ column: column.references.column
30
+ }
31
+ });
32
+ return conditions;
33
+ }
34
+ /**
35
+ * Add a join on `joinTable`, keyed and pre-filled.
36
+ *
37
+ * The key is the table's own name where that is free, because it is what the
38
+ * user already calls the table; a collision falls back to `_2`, `_3` and so on.
39
+ * Uniqueness is not cosmetic — the key is the SQL alias and the name every ref
40
+ * addresses the table by, so two joins sharing one would make every ref
41
+ * ambiguous. `base` is reserved for the base table and can never be taken.
42
+ */
43
+ function addDraftJoin(draft, joinTable, schema) {
44
+ const taken = /* @__PURE__ */ new Set([BASE_REF, ...draft.joins.map((j) => j.key)]);
45
+ let key = joinTable;
46
+ let suffix = 2;
47
+ while (taken.has(key)) {
48
+ key = `${joinTable}_${suffix}`;
49
+ suffix += 1;
50
+ }
51
+ return {
52
+ ...draft,
53
+ joins: [...draft.joins, {
54
+ key,
55
+ table: joinTable,
56
+ type: "left",
57
+ on: suggestJoinConditions(draft.baseTable, joinTable, schema, key)
58
+ }]
59
+ };
60
+ }
61
+ /**
62
+ * Remove a join, and everything that addressed it.
63
+ *
64
+ * Refs to a removed join are refs to a table the projection no longer declares.
65
+ * Left in place they fail validation as "unknown ref table", which blames the
66
+ * field — or, worse, the identity — for the join's removal. Identity falls back
67
+ * to the base primary key, the same default a new draft starts from.
68
+ */
69
+ function removeDraftJoin(draft, joinKey) {
70
+ const fieldBindings = Object.fromEntries(Object.entries(draft.fieldBindings).filter(([, binding]) => binding.from.table !== joinKey));
71
+ const identityDependsOnJoin = identityRefs(draft.identity, "").some((ref) => ref.table === joinKey);
72
+ return {
73
+ ...draft,
74
+ joins: draft.joins.filter((join) => join.key !== joinKey),
75
+ fieldBindings,
76
+ identity: identityDependsOnJoin ? { kind: "base_pk" } : draft.identity
77
+ };
78
+ }
79
+ /**
80
+ * Every table a field or identity may currently read from, in ref order: the
81
+ * base table first, then each join.
82
+ *
83
+ * Returns [] when the base table is not in the discovered schema — there is
84
+ * nothing truthful to offer, and inventing columns would let someone build a
85
+ * projection that cannot compile.
86
+ */
87
+ function draftRefOptions(draft, schema) {
88
+ const byName = new Map(schema.map((table) => [table.name, table]));
89
+ const base = byName.get(draft.baseTable);
90
+ if (!base) return [];
91
+ const options = [{
92
+ ref: BASE_REF,
93
+ label: `${base.name} (${BASE_REF})`,
94
+ columns: base.columns
95
+ }];
96
+ for (const join of draft.joins) {
97
+ const table = byName.get(join.table);
98
+ if (table) options.push({
99
+ ref: join.key,
100
+ label: `${table.name} (${join.key})`,
101
+ columns: table.columns
102
+ });
103
+ }
104
+ return options;
105
+ }
106
+ /** A draft becomes a projection once it has a valid key and ≥1 binding. */
107
+ function draftToProjection(draft) {
108
+ if (!PROJECTION_KEY_PATTERN.test(draft.entityKey)) return null;
109
+ if (Object.keys(draft.fieldBindings).length === 0) return null;
110
+ return {
111
+ version: 1,
112
+ kind: "projection",
113
+ entityKey: draft.entityKey,
114
+ baseTable: draft.baseTable,
115
+ joins: draft.joins,
116
+ fieldBindings: draft.fieldBindings,
117
+ identity: draft.identity
118
+ };
119
+ }
120
+ const PROBE_KEY = "draft";
121
+ /**
122
+ * A draft rendered as a projection so the semantic checks can run against it
123
+ * while it is being written, not only once it is finished.
124
+ *
125
+ * `entityKey` is stood in for rather than taken from the draft: it is the one
126
+ * field `validateProjection` never looks at, and a half-typed key would
127
+ * otherwise have to be valid before any join or identity could be checked. The
128
+ * missing or malformed key is reported separately, by `draftErrors`.
129
+ */
130
+ function probeProjection(draft) {
131
+ return {
132
+ version: 1,
133
+ kind: "projection",
134
+ entityKey: PROBE_KEY,
135
+ baseTable: draft.baseTable,
136
+ joins: draft.joins,
137
+ fieldBindings: draft.fieldBindings,
138
+ identity: draft.identity
139
+ };
140
+ }
141
+ /**
142
+ * `validateProjection` appends its own `projection.` segment to the base path it
143
+ * is given, because in a config the base is the entity that owns the projection
144
+ * (`entities.accounts` → `entities.accounts.projection.identity`). A draft has
145
+ * no owning entity, so passing "projection" as the base produced
146
+ * `projection.projection.identity` — a path no caller could match against, and
147
+ * the reason `draftErrors` was only ever consumed as a flat list of messages.
148
+ *
149
+ * Passing an empty base and dropping the leading separator leaves one
150
+ * convention: every path here starts `projection.` exactly once, whether it came
151
+ * from this module or from the validator.
152
+ */
153
+ function semanticErrors(draft, schema) {
154
+ return validateProjection(probeProjection(draft), schema, "").map((error) => ({
155
+ ...error,
156
+ path: error.path.startsWith(".") ? error.path.slice(1) : error.path
157
+ }));
158
+ }
159
+ /**
160
+ * Every reason this draft cannot be saved, one per cause and each anchored to
161
+ * the section that owns it.
162
+ *
163
+ * One lumped "Name the object and map a field." was all this used to return for
164
+ * an incomplete draft, at the bare path "projection". Two consequences, both
165
+ * bad: a builder had nowhere to attach the message, and — because it returned
166
+ * before validating — a fanning join under a `base_pk` identity stayed silent
167
+ * until the draft was otherwise complete, which is well after the point where
168
+ * the user chose the join.
169
+ */
170
+ function draftErrors(draft, schema) {
171
+ const errors = [];
172
+ if (draft.entityKey === "") errors.push({
173
+ path: "projection.entityKey",
174
+ message: "Key is required."
175
+ });
176
+ else if (!PROJECTION_KEY_PATTERN.test(draft.entityKey)) errors.push({
177
+ path: "projection.entityKey",
178
+ message: `Key ${PROJECTION_KEY_MESSAGE}.`
179
+ });
180
+ if (Object.keys(draft.fieldBindings).length === 0) errors.push({
181
+ path: "projection.fieldBindings",
182
+ message: "Map at least one field."
183
+ });
184
+ if (draft.baseTable === "") {
185
+ errors.push({
186
+ path: "projection.baseTable",
187
+ message: "Pick a base table."
188
+ });
189
+ return errors;
190
+ }
191
+ errors.push(...semanticErrors(draft, schema));
192
+ return errors;
193
+ }
194
+ /**
195
+ * Per-join fan-out, for the builder's inline cardinality hints.
196
+ *
197
+ * Answered from the joins alone. Joins are chosen before the key and the fields
198
+ * are filled in, and a fan-out warning is precisely what should steer the
199
+ * identity choice while it is being made — requiring a complete draft put the
200
+ * hint after the decision it exists to inform.
201
+ */
202
+ function describeCardinality(draft, schema) {
203
+ const projection = probeProjection(draft);
204
+ const tablesByName = new Map(schema.map((t) => [t.name, t]));
205
+ const fanning = new Set(fanOutJoins(projection, tablesByName).map((j) => j.key));
206
+ return projection.joins.map((join) => ({
207
+ joinKey: join.key,
208
+ fansOut: fanning.has(join.key)
209
+ }));
210
+ }
211
+ //#endregion
212
+ export { draftToProjection as a, suggestJoinConditions as c, draftRefOptions as i, describeCardinality as n, emptyProjectionDraft as o, draftErrors as r, removeDraftJoin as s, addDraftJoin as t };
@@ -27,10 +27,62 @@ declare function emptyProjectionDraft(baseTable: string): ProjectionDraft;
27
27
  * Returns [] when no FK relates them — the user then picks manually.
28
28
  */
29
29
  declare function suggestJoinConditions(baseTable: string, joinTable: string, schema: DiscoveredSchemaTable[], joinKey: string): ProjectionJoin["on"];
30
+ /**
31
+ * Add a join on `joinTable`, keyed and pre-filled.
32
+ *
33
+ * The key is the table's own name where that is free, because it is what the
34
+ * user already calls the table; a collision falls back to `_2`, `_3` and so on.
35
+ * Uniqueness is not cosmetic — the key is the SQL alias and the name every ref
36
+ * addresses the table by, so two joins sharing one would make every ref
37
+ * ambiguous. `base` is reserved for the base table and can never be taken.
38
+ */
39
+ declare function addDraftJoin(draft: ProjectionDraft, joinTable: string, schema: DiscoveredSchemaTable[]): ProjectionDraft;
40
+ /**
41
+ * Remove a join, and everything that addressed it.
42
+ *
43
+ * Refs to a removed join are refs to a table the projection no longer declares.
44
+ * Left in place they fail validation as "unknown ref table", which blames the
45
+ * field — or, worse, the identity — for the join's removal. Identity falls back
46
+ * to the base primary key, the same default a new draft starts from.
47
+ */
48
+ declare function removeDraftJoin(draft: ProjectionDraft, joinKey: string): ProjectionDraft;
49
+ /** One selectable table in the builder's column pickers. */
50
+ interface DraftRefOption {
51
+ columns: string[];
52
+ label: string;
53
+ ref: string;
54
+ }
55
+ /**
56
+ * Every table a field or identity may currently read from, in ref order: the
57
+ * base table first, then each join.
58
+ *
59
+ * Returns [] when the base table is not in the discovered schema — there is
60
+ * nothing truthful to offer, and inventing columns would let someone build a
61
+ * projection that cannot compile.
62
+ */
63
+ declare function draftRefOptions(draft: ProjectionDraft, schema: DiscoveredSchemaTable[]): DraftRefOption[];
30
64
  /** A draft becomes a projection once it has a valid key and ≥1 binding. */
31
65
  declare function draftToProjection(draft: ProjectionDraft): ProjectionMapping | null;
66
+ /**
67
+ * Every reason this draft cannot be saved, one per cause and each anchored to
68
+ * the section that owns it.
69
+ *
70
+ * One lumped "Name the object and map a field." was all this used to return for
71
+ * an incomplete draft, at the bare path "projection". Two consequences, both
72
+ * bad: a builder had nowhere to attach the message, and — because it returned
73
+ * before validating — a fanning join under a `base_pk` identity stayed silent
74
+ * until the draft was otherwise complete, which is well after the point where
75
+ * the user chose the join.
76
+ */
32
77
  declare function draftErrors(draft: ProjectionDraft, schema: DiscoveredSchemaTable[]): ConfigValidationError[];
33
- /** Per-join fan-out, for the builder's inline cardinality hints. */
78
+ /**
79
+ * Per-join fan-out, for the builder's inline cardinality hints.
80
+ *
81
+ * Answered from the joins alone. Joins are chosen before the key and the fields
82
+ * are filled in, and a fan-out warning is precisely what should steer the
83
+ * identity choice while it is being made — requiring a complete draft put the
84
+ * hint after the decision it exists to inform.
85
+ */
34
86
  declare function describeCardinality(draft: ProjectionDraft, schema: DiscoveredSchemaTable[]): Array<{
35
87
  fansOut: boolean;
36
88
  joinKey: string;
@@ -60,9 +112,37 @@ declare function quoteIdent(name: string): string;
60
112
  * a pushed row's `id` identical to the one the view would have produced.
61
113
  */
62
114
  declare function compileProjectionIdentity(projection: ProjectionMapping, basePkColumn: string): string;
115
+ declare function compileProjectionSelect(projection: ProjectionMapping, basePkColumn: string): {
116
+ columns: string[];
117
+ selectItems: string[];
118
+ };
63
119
  declare function compileProjectionJoins(projection: ProjectionMapping): string[];
64
120
  /** `FROM <baseTable> AS "base"` — the alias every ref resolves against. */
65
121
  declare function compileProjectionFrom(projection: ProjectionMapping): string;
122
+ /**
123
+ * Hard ceiling on preview rows, regardless of what a caller asks for.
124
+ *
125
+ * A preview runs against a production database from a button a user can click
126
+ * as often as they like, so the cap belongs beside the query rather than in
127
+ * whichever caller happens to be passing a limit today.
128
+ */
129
+ declare const PREVIEW_MAX_ROWS = 50;
130
+ /**
131
+ * One SELECT reading a few rows through an UNSAVED projection, for the derived
132
+ * object builder's preview.
133
+ *
134
+ * Lives here, beside the view compiler, because both sides of the round trip
135
+ * need the identical statement: an embedded org runs it against its own
136
+ * database from `@clivly.com/api`, and a cloud org's host app runs it from
137
+ * `@clivly/sdk`. Two copies would be free to disagree about what the user is
138
+ * being shown, which is the whole value of a preview.
139
+ *
140
+ * Columns are aliased by FIELD NAME, not by ref token. That is the deliberate
141
+ * difference from the push source, which aliases by token because cloud ingest
142
+ * reads `row[token]`; a preview is read by a person, in a table whose headers
143
+ * are the field names they just typed.
144
+ */
145
+ declare function compileProjectionPreview(projection: ProjectionMapping, limit: number): string;
66
146
  //#endregion
67
147
  //#region ../core/src/projection-validate.d.ts
68
148
  /**
@@ -97,4 +177,4 @@ declare function coversUniqueKey(table: DiscoveredSchemaTable, columns: string[]
97
177
  /** Is this table's payload rich enough to validate a projection against? */
98
178
  declare function hasRichMeta(table: DiscoveredSchemaTable): boolean;
99
179
  //#endregion
100
- export { joinFansOut as _, validateProjection as a, compileProjectionJoins as c, describeCardinality as d, draftErrors as f, fanOutJoins as g, suggestJoinConditions as h, primaryKeyColumns as i, quoteIdent as l, emptyProjectionDraft as m, coversUniqueKey as n, compileProjectionFrom as o, draftToProjection as p, hasRichMeta as r, compileProjectionIdentity as s, columnMetaFor as t, ProjectionDraft as u };
180
+ export { fanOutJoins as C, suggestJoinConditions as S, draftErrors as _, validateProjection as a, emptyProjectionDraft as b, compileProjectionIdentity as c, compileProjectionSelect as d, quoteIdent as f, describeCardinality as g, addDraftJoin as h, primaryKeyColumns as i, compileProjectionJoins as l, ProjectionDraft as m, coversUniqueKey as n, PREVIEW_MAX_ROWS as o, DraftRefOption as p, hasRichMeta as r, compileProjectionFrom as s, columnMetaFor as t, compileProjectionPreview as u, draftRefOptions as v, joinFansOut as w, removeDraftJoin as x, draftToProjection as y };
@@ -27,10 +27,62 @@ declare function emptyProjectionDraft(baseTable: string): ProjectionDraft;
27
27
  * Returns [] when no FK relates them — the user then picks manually.
28
28
  */
29
29
  declare function suggestJoinConditions(baseTable: string, joinTable: string, schema: DiscoveredSchemaTable[], joinKey: string): ProjectionJoin["on"];
30
+ /**
31
+ * Add a join on `joinTable`, keyed and pre-filled.
32
+ *
33
+ * The key is the table's own name where that is free, because it is what the
34
+ * user already calls the table; a collision falls back to `_2`, `_3` and so on.
35
+ * Uniqueness is not cosmetic — the key is the SQL alias and the name every ref
36
+ * addresses the table by, so two joins sharing one would make every ref
37
+ * ambiguous. `base` is reserved for the base table and can never be taken.
38
+ */
39
+ declare function addDraftJoin(draft: ProjectionDraft, joinTable: string, schema: DiscoveredSchemaTable[]): ProjectionDraft;
40
+ /**
41
+ * Remove a join, and everything that addressed it.
42
+ *
43
+ * Refs to a removed join are refs to a table the projection no longer declares.
44
+ * Left in place they fail validation as "unknown ref table", which blames the
45
+ * field — or, worse, the identity — for the join's removal. Identity falls back
46
+ * to the base primary key, the same default a new draft starts from.
47
+ */
48
+ declare function removeDraftJoin(draft: ProjectionDraft, joinKey: string): ProjectionDraft;
49
+ /** One selectable table in the builder's column pickers. */
50
+ interface DraftRefOption {
51
+ columns: string[];
52
+ label: string;
53
+ ref: string;
54
+ }
55
+ /**
56
+ * Every table a field or identity may currently read from, in ref order: the
57
+ * base table first, then each join.
58
+ *
59
+ * Returns [] when the base table is not in the discovered schema — there is
60
+ * nothing truthful to offer, and inventing columns would let someone build a
61
+ * projection that cannot compile.
62
+ */
63
+ declare function draftRefOptions(draft: ProjectionDraft, schema: DiscoveredSchemaTable[]): DraftRefOption[];
30
64
  /** A draft becomes a projection once it has a valid key and ≥1 binding. */
31
65
  declare function draftToProjection(draft: ProjectionDraft): ProjectionMapping | null;
66
+ /**
67
+ * Every reason this draft cannot be saved, one per cause and each anchored to
68
+ * the section that owns it.
69
+ *
70
+ * One lumped "Name the object and map a field." was all this used to return for
71
+ * an incomplete draft, at the bare path "projection". Two consequences, both
72
+ * bad: a builder had nowhere to attach the message, and — because it returned
73
+ * before validating — a fanning join under a `base_pk` identity stayed silent
74
+ * until the draft was otherwise complete, which is well after the point where
75
+ * the user chose the join.
76
+ */
32
77
  declare function draftErrors(draft: ProjectionDraft, schema: DiscoveredSchemaTable[]): ConfigValidationError[];
33
- /** Per-join fan-out, for the builder's inline cardinality hints. */
78
+ /**
79
+ * Per-join fan-out, for the builder's inline cardinality hints.
80
+ *
81
+ * Answered from the joins alone. Joins are chosen before the key and the fields
82
+ * are filled in, and a fan-out warning is precisely what should steer the
83
+ * identity choice while it is being made — requiring a complete draft put the
84
+ * hint after the decision it exists to inform.
85
+ */
34
86
  declare function describeCardinality(draft: ProjectionDraft, schema: DiscoveredSchemaTable[]): Array<{
35
87
  fansOut: boolean;
36
88
  joinKey: string;
@@ -60,9 +112,37 @@ declare function quoteIdent(name: string): string;
60
112
  * a pushed row's `id` identical to the one the view would have produced.
61
113
  */
62
114
  declare function compileProjectionIdentity(projection: ProjectionMapping, basePkColumn: string): string;
115
+ declare function compileProjectionSelect(projection: ProjectionMapping, basePkColumn: string): {
116
+ columns: string[];
117
+ selectItems: string[];
118
+ };
63
119
  declare function compileProjectionJoins(projection: ProjectionMapping): string[];
64
120
  /** `FROM <baseTable> AS "base"` — the alias every ref resolves against. */
65
121
  declare function compileProjectionFrom(projection: ProjectionMapping): string;
122
+ /**
123
+ * Hard ceiling on preview rows, regardless of what a caller asks for.
124
+ *
125
+ * A preview runs against a production database from a button a user can click
126
+ * as often as they like, so the cap belongs beside the query rather than in
127
+ * whichever caller happens to be passing a limit today.
128
+ */
129
+ declare const PREVIEW_MAX_ROWS = 50;
130
+ /**
131
+ * One SELECT reading a few rows through an UNSAVED projection, for the derived
132
+ * object builder's preview.
133
+ *
134
+ * Lives here, beside the view compiler, because both sides of the round trip
135
+ * need the identical statement: an embedded org runs it against its own
136
+ * database from `@clivly.com/api`, and a cloud org's host app runs it from
137
+ * `@clivly/sdk`. Two copies would be free to disagree about what the user is
138
+ * being shown, which is the whole value of a preview.
139
+ *
140
+ * Columns are aliased by FIELD NAME, not by ref token. That is the deliberate
141
+ * difference from the push source, which aliases by token because cloud ingest
142
+ * reads `row[token]`; a preview is read by a person, in a table whose headers
143
+ * are the field names they just typed.
144
+ */
145
+ declare function compileProjectionPreview(projection: ProjectionMapping, limit: number): string;
66
146
  //#endregion
67
147
  //#region ../core/src/projection-validate.d.ts
68
148
  /**
@@ -97,4 +177,4 @@ declare function coversUniqueKey(table: DiscoveredSchemaTable, columns: string[]
97
177
  /** Is this table's payload rich enough to validate a projection against? */
98
178
  declare function hasRichMeta(table: DiscoveredSchemaTable): boolean;
99
179
  //#endregion
100
- export { joinFansOut as _, validateProjection as a, compileProjectionJoins as c, describeCardinality as d, draftErrors as f, fanOutJoins as g, suggestJoinConditions as h, primaryKeyColumns as i, quoteIdent as l, emptyProjectionDraft as m, coversUniqueKey as n, compileProjectionFrom as o, draftToProjection as p, hasRichMeta as r, compileProjectionIdentity as s, columnMetaFor as t, ProjectionDraft as u };
180
+ export { fanOutJoins as C, suggestJoinConditions as S, draftErrors as _, validateProjection as a, emptyProjectionDraft as b, compileProjectionIdentity as c, compileProjectionSelect as d, quoteIdent as f, describeCardinality as g, addDraftJoin as h, primaryKeyColumns as i, compileProjectionJoins as l, ProjectionDraft as m, coversUniqueKey as n, PREVIEW_MAX_ROWS as o, DraftRefOption as p, hasRichMeta as r, compileProjectionFrom as s, columnMetaFor as t, compileProjectionPreview as u, draftRefOptions as v, joinFansOut as w, removeDraftJoin as x, draftToProjection as y };