@clivly/core 0.7.4 → 0.9.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.
@@ -1,350 +1,13 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- let zod = require("zod");
3
- //#region src/entity-config.ts
4
- /**
5
- * Entity Config v2 — the declarative contract that turns Clivly's "schema-aware"
6
- * promise into a real capability.
7
- *
8
- * A CRM entity is no longer a flat table name. It is a *derived entity*: a
9
- * filtered slice of a host table (`source` + `filter`), a column allowlist
10
- * (`fields` + `readOnly`), and declared `relationships` to other entities. The
11
- * same host table can back several entities (e.g. `users` → owners + members)
12
- * distinguished by `role`.
13
- *
14
- * This module is the Phase 1 slice: types, schema, `defineClivlyConfig` (a
15
- * structural helper) and `validateEntitiesConfig` (semantic validation against
16
- * the host's discovered schema). It performs NO DB access and generates no SQL —
17
- * the view compiler (Phase 2) and sync engine (Phase 3) consume this contract.
18
- */
19
- /** Canonical CRM concepts a host entity can map onto. */
20
- const CRM_CONCEPTS = [
21
- "contact",
22
- "company",
23
- "deal",
24
- "activity",
25
- "conversation",
26
- "note"
27
- ];
28
- /**
29
- * The canonical `fields` keys the mirror actually persists, per concept — the
30
- * crm_* columns the sync store writes. An entity's `fields` map keys must be one
31
- * of these: a non-canonical key (e.g. `fullName` instead of `name`) validates
32
- * structurally but has no destination column, so the store silently writes an
33
- * empty value. `validateEntitiesConfig` flags those.
34
- *
35
- * This is the single source of truth — the Drizzle store imports it, so the
36
- * two can't drift (the drift is what made the silent-data-loss trap possible).
37
- * Concepts absent here aren't materialized yet, so their field keys are
38
- * unconstrained.
39
- */
40
- const CANONICAL_FIELDS = {
41
- contact: [
42
- "name",
43
- "email",
44
- "title",
45
- "status"
46
- ],
47
- company: [
48
- "name",
49
- "domain",
50
- "industry",
51
- "size"
52
- ],
53
- deal: [
54
- "name",
55
- "value",
56
- "stage"
57
- ]
58
- };
59
- /**
60
- * The concepts the mapping→sync pipeline can actually materialize into `crm_*`
61
- * tables. These are exactly the CANONICAL_FIELDS keys — the Drizzle sync store's
62
- * `persist()` throws for any other concept, so offering one (in the mapping
63
- * editor, in suggestions, or via the upsert API) is a broken affordance.
64
- *
65
- * The broader CRM_CONCEPTS set still validates structurally, so previously
66
- * stored rows for not-yet-materialized concepts keep rendering. When a concept
67
- * gains a sync-store path, add its canonical fields above and it joins this set
68
- * automatically (guarded by the MATERIALIZING_CONCEPTS drift test).
69
- */
70
- const MATERIALIZING_CONCEPTS = [
71
- "contact",
72
- "company",
73
- "deal"
74
- ];
75
- /**
76
- * The org-defined custom-object target. Deliberately NOT a member of
77
- * CRM_CONCEPTS / CANONICAL_FIELDS / MATERIALIZING_CONCEPTS — a custom object has
78
- * no fixed canonical columns; its mapping's fieldMap keys ARE its properties.
79
- * Handled as an explicit branch wherever concepts are switched on, so the
80
- * canonical-concept invariants (and their drift test) stay intact.
81
- */
82
- const CUSTOM_CONCEPT = "custom";
83
- /**
84
- * Operator forms for a single column predicate. All predicates on an entity are
85
- * AND-ed together. `$raw` (see FilterExpr) is the escape hatch for anything the
86
- * operators can't express.
87
- */
88
- const filterOperatorsSchema = zod.z.union([
89
- zod.z.strictObject({ eq: zod.z.union([
90
- zod.z.string(),
91
- zod.z.number(),
92
- zod.z.boolean()
93
- ]) }),
94
- zod.z.strictObject({ ne: zod.z.union([
95
- zod.z.string(),
96
- zod.z.number(),
97
- zod.z.boolean()
98
- ]) }),
99
- zod.z.strictObject({ in: zod.z.array(zod.z.union([
100
- zod.z.string(),
101
- zod.z.number(),
102
- zod.z.boolean()
103
- ])).min(1) }),
104
- zod.z.strictObject({ notIn: zod.z.array(zod.z.union([
105
- zod.z.string(),
106
- zod.z.number(),
107
- zod.z.boolean()
108
- ])).min(1) }),
109
- zod.z.strictObject({ isNull: zod.z.literal(true) }),
110
- zod.z.strictObject({ isNotNull: zod.z.literal(true) })
111
- ]);
112
- const filterValueSchema = zod.z.union([
113
- zod.z.string(),
114
- zod.z.number(),
115
- zod.z.boolean(),
116
- filterOperatorsSchema
117
- ]);
118
- /**
119
- * A raw SQL predicate fragment. Kept as a *string* (not a query-builder callback)
120
- * so the whole config stays JSON-serializable — it can be persisted into
121
- * `crm_entity_mappings` and shipped to the cloud unchanged. Embedded-only in
122
- * practice; not validated against the schema.
123
- */
124
- const rawFilterSchema = zod.z.strictObject({ $raw: zod.z.string().min(1) });
125
- const columnFilterSchema = zod.z.record(zod.z.string(), filterValueSchema);
126
- /** `$raw` is a reserved key; a column literally named `$raw` is not addressable. */
127
- const filterSchema = zod.z.union([rawFilterSchema, columnFilterSchema]);
128
- /**
129
- * How the link between two entities is expressed in the host schema:
130
- * - `fkOn: "company"` — FK lives on the company table (e.g. organizations.owner_id)
131
- * - `fkOn: "contact"` — FK lives on the contact table (e.g. users.organization_id)
132
- * - `through` — a join table sits between them (many-to-many, role often here)
133
- * - `$raw` — opaque SQL join fragment (escape hatch; not schema-validated)
134
- *
135
- * `column` and the join-table keys are `table.column` or bare `column` strings.
136
- */
137
- const relationshipViaSchema = zod.z.union([
138
- zod.z.strictObject({
139
- fkOn: zod.z.enum(["company", "contact"]),
140
- column: zod.z.string().min(1)
141
- }),
142
- zod.z.strictObject({
143
- through: zod.z.string().min(1),
144
- localKey: zod.z.string().min(1),
145
- foreignKey: zod.z.string().min(1)
146
- }),
147
- zod.z.strictObject({ $raw: zod.z.string().min(1) })
148
- ]);
149
- const relationshipSchema = zod.z.strictObject({
150
- /** Key of the related entity in the `entities` map. */
151
- entity: zod.z.string().min(1),
152
- via: relationshipViaSchema
153
- });
154
- /** A named map of relationships, as stored on an entity / in the mappings row. */
155
- const relationshipsSchema = zod.z.record(zod.z.string(), relationshipSchema);
156
- const entitySchema = zod.z.strictObject({
157
- /** Which CRM concept this entity maps onto — a canonical concept or "custom". */
158
- concept: zod.z.union([zod.z.enum(CRM_CONCEPTS), zod.z.literal(CUSTOM_CONCEPT)]),
159
- /** Host table (or view) this entity is derived from. */
160
- source: zod.z.string().min(1),
161
- /**
162
- * Distinguishes multiple entities backed by the same table (e.g. "primary"
163
- * for owners, "secondary" for members). Free text; defaults to "primary".
164
- */
165
- role: zod.z.string().min(1).optional(),
166
- /** Predicate that narrows `source` into this entity's rows. Omit = whole table. */
167
- filter: filterSchema.optional(),
168
- /** clivlyField → sourceColumn. At least one field is required. */
169
- fields: zod.z.record(zod.z.string(), zod.z.string()).refine((f) => Object.keys(f).length > 0, { message: "at least one field must be mapped" }),
170
- /**
171
- * Host-owned identity fields. Sync overwrites them; the CRM UI cannot edit
172
- * them. Must be a subset of `fields` keys.
173
- */
174
- readOnly: zod.z.array(zod.z.string()).optional(),
175
- /** Named relationships to other entities in this config. */
176
- relationships: zod.z.record(zod.z.string(), relationshipSchema).optional(),
177
- /**
178
- * For a "custom" entity, the crm_object_types row this materializes into.
179
- * Required for custom (enforced at the API layer), unused for standard concepts.
180
- */
181
- targetObjectTypeId: zod.z.string().min(1).optional()
182
- });
183
- const entitiesConfigSchema = zod.z.strictObject({ entities: zod.z.record(zod.z.string(), entitySchema).refine((e) => Object.keys(e).length > 0, { message: "at least one entity must be defined" }) });
184
- /** Thrown by `defineClivlyConfig({ strict: true })` on non-canonical field keys. */
185
- var ClivlyConfigError = class extends Error {
186
- issues;
187
- constructor(issues) {
188
- super(`Clivly config has ${issues.length} non-canonical field mapping(s) that would write empty values at sync time:\n${issues.map((issue) => ` - ${issue.path}: ${issue.message}`).join("\n")}`);
189
- this.name = "ClivlyConfigError";
190
- this.issues = issues;
191
- }
192
- };
193
- function nonCanonicalFieldKeys(entity) {
194
- const canonical = CANONICAL_FIELDS[entity.concept];
195
- if (!canonical) return [];
196
- return Object.keys(entity.fields).filter((key) => !canonical.includes(key));
197
- }
198
- function nonCanonicalMessage(entity, field) {
199
- const canonical = CANONICAL_FIELDS[entity.concept] ?? [];
200
- return `"${field}" is not a canonical ${entity.concept} field (expected one of: ${canonical.join(", ")})`;
201
- }
202
- function canonicalFieldIssues(config) {
203
- const issues = [];
204
- for (const [key, entity] of Object.entries(config.entities)) for (const field of nonCanonicalFieldKeys(entity)) issues.push({
205
- path: `entities.${key}.fields.${field}`,
206
- message: nonCanonicalMessage(entity, field)
207
- });
208
- return issues;
209
- }
210
- function defaultWarn(message) {
211
- console.warn(message);
212
- }
213
- /**
214
- * Structural helper for `clivly.config.ts`. Gives full type inference on the
215
- * literal you pass and eagerly parses it, so shape errors surface at config
216
- * load rather than at sync time.
217
- *
218
- * It also catches the silent-data-loss footgun: a non-canonical field key for a
219
- * materialised concept (e.g. `fullName` instead of `name` on a `contact`) has no
220
- * destination column, so the store would write an empty value. By default these
221
- * are reported via `onWarn` (a `console.warn`); pass `{ strict: true }` to throw
222
- * a `ClivlyConfigError` instead. Does NOT validate against the host schema — that
223
- * needs the discovered schema; see `validateEntitiesConfig`.
224
- */
225
- function defineClivlyConfig(config, options) {
226
- const parsed = entitiesConfigSchema.parse(config);
227
- const issues = canonicalFieldIssues(parsed);
228
- if (issues.length > 0) {
229
- if (options?.strict) throw new ClivlyConfigError(issues);
230
- const warn = options?.onWarn ?? defaultWarn;
231
- for (const issue of issues) warn(`⚠ Clivly config: ${issue.message} (${issue.path}) — this field would write an empty value at sync time. Pass { strict: true } to make this throw.`);
232
- }
233
- return parsed;
234
- }
235
- /** Split a `table.column` / bare `column` ref into its parts. */
236
- function parseColumnRef(ref) {
237
- const dot = ref.indexOf(".");
238
- if (dot === -1) return { column: ref };
239
- return {
240
- table: ref.slice(0, dot),
241
- column: ref.slice(dot + 1)
242
- };
243
- }
244
- /** Shared lookup + error-collection state threaded through the validators. */
245
- var SchemaValidator = class {
246
- errors = [];
247
- columnsByTable = /* @__PURE__ */ new Map();
248
- constructor(schema) {
249
- for (const table of schema) this.columnsByTable.set(table.name, new Set(table.columns));
250
- }
251
- tableExists(name) {
252
- return this.columnsByTable.has(name);
253
- }
254
- columnExists(table, column) {
255
- return this.columnsByTable.get(table)?.has(column) ?? false;
256
- }
257
- report(path, message) {
258
- this.errors.push({
259
- path,
260
- message
261
- });
262
- }
263
- /** Report if `column` is absent from `table`. Returns whether it existed. */
264
- requireColumn(path, table, column) {
265
- if (this.columnExists(table, column)) return true;
266
- this.report(path, `column "${column}" does not exist on "${table}"`);
267
- return false;
268
- }
269
- };
270
- function validateFields(v, base, entity) {
271
- for (const [field, column] of Object.entries(entity.fields)) v.requireColumn(`${base}.fields.${field}`, entity.source, column);
272
- for (const field of nonCanonicalFieldKeys(entity)) v.report(`${base}.fields.${field}`, nonCanonicalMessage(entity, field));
273
- }
274
- function validateReadOnly(v, base, entity) {
275
- for (const field of entity.readOnly ?? []) if (!(field in entity.fields)) v.report(`${base}.readOnly`, `"${field}" is not one of this entity's fields`);
276
- }
277
- function validateFilter(v, base, entity) {
278
- if (!entity.filter || "$raw" in entity.filter) return;
279
- for (const column of Object.keys(entity.filter)) v.requireColumn(`${base}.filter.${column}`, entity.source, column);
280
- }
281
- function validateFkVia(v, relBase, via, entity, related) {
282
- const { table, column } = parseColumnRef(via.column);
283
- const fkTable = table ?? (via.fkOn === "contact" ? entity.source : related?.source);
284
- const path = `${relBase}.via.column`;
285
- if (!fkTable) {
286
- v.report(path, `cannot resolve table for FK column "${via.column}"`);
287
- return;
288
- }
289
- if (!v.tableExists(fkTable)) {
290
- v.report(path, `table "${fkTable}" is not in the discovered schema`);
291
- return;
292
- }
293
- v.requireColumn(path, fkTable, column);
294
- }
295
- function validateThroughVia(v, relBase, via) {
296
- if (!v.tableExists(via.through)) {
297
- v.report(`${relBase}.via.through`, `join table "${via.through}" is not in the discovered schema`);
298
- return;
299
- }
300
- v.requireColumn(`${relBase}.via.localKey`, via.through, via.localKey);
301
- v.requireColumn(`${relBase}.via.foreignKey`, via.through, via.foreignKey);
302
- }
303
- function validateRelationship(v, base, config, entity, relName, rel) {
304
- const relBase = `${base}.relationships.${relName}`;
305
- const related = config.entities[rel.entity];
306
- if (!related) v.report(`${relBase}.entity`, `"${rel.entity}" is not a defined entity`);
307
- const { via } = rel;
308
- if ("fkOn" in via) validateFkVia(v, relBase, via, entity, related);
309
- else if ("through" in via) validateThroughVia(v, relBase, via);
310
- }
311
- function validateEntity(v, config, key, entity) {
312
- const base = `entities.${key}`;
313
- if (!v.tableExists(entity.source)) {
314
- v.report(`${base}.source`, `table "${entity.source}" is not in the discovered schema`);
315
- return;
316
- }
317
- validateFields(v, base, entity);
318
- validateReadOnly(v, base, entity);
319
- validateFilter(v, base, entity);
320
- for (const [relName, rel] of Object.entries(entity.relationships ?? {})) validateRelationship(v, base, config, entity, relName, rel);
321
- }
322
- /**
323
- * Validate a (structurally valid) config against the host's discovered schema:
324
- * source tables exist, mapped columns exist, `readOnly` ⊆ `fields`, filter
325
- * columns exist, and relationships point at real entities/tables/columns.
326
- *
327
- * `$raw` filters and `$raw` relationship joins are opaque and skipped. Returns
328
- * every error found (not just the first) so a mapping UI or CLI can show them
329
- * all at once.
330
- */
331
- function validateEntitiesConfig(config, schema) {
332
- const validator = new SchemaValidator(schema);
333
- for (const [key, entity] of Object.entries(config.entities)) validateEntity(validator, config, key, entity);
334
- return {
335
- valid: validator.errors.length === 0,
336
- errors: validator.errors
337
- };
338
- }
339
- //#endregion
340
- exports.CANONICAL_FIELDS = CANONICAL_FIELDS;
341
- exports.CRM_CONCEPTS = CRM_CONCEPTS;
342
- exports.CUSTOM_CONCEPT = CUSTOM_CONCEPT;
343
- exports.ClivlyConfigError = ClivlyConfigError;
344
- exports.MATERIALIZING_CONCEPTS = MATERIALIZING_CONCEPTS;
345
- exports.defineClivlyConfig = defineClivlyConfig;
346
- exports.entitiesConfigSchema = entitiesConfigSchema;
347
- exports.filterSchema = filterSchema;
348
- exports.relationshipSchema = relationshipSchema;
349
- exports.relationshipsSchema = relationshipsSchema;
350
- exports.validateEntitiesConfig = validateEntitiesConfig;
2
+ const require_entity_config = require("./entity-config-PoOGzvpN.cjs");
3
+ exports.CANONICAL_FIELDS = require_entity_config.CANONICAL_FIELDS;
4
+ exports.CRM_CONCEPTS = require_entity_config.CRM_CONCEPTS;
5
+ exports.CUSTOM_CONCEPT = require_entity_config.CUSTOM_CONCEPT;
6
+ exports.ClivlyConfigError = require_entity_config.ClivlyConfigError;
7
+ exports.MATERIALIZING_CONCEPTS = require_entity_config.MATERIALIZING_CONCEPTS;
8
+ exports.defineClivlyConfig = require_entity_config.defineClivlyConfig;
9
+ exports.entitiesConfigSchema = require_entity_config.entitiesConfigSchema;
10
+ exports.filterSchema = require_entity_config.filterSchema;
11
+ exports.relationshipSchema = require_entity_config.relationshipSchema;
12
+ exports.relationshipsSchema = require_entity_config.relationshipsSchema;
13
+ exports.validateEntitiesConfig = require_entity_config.validateEntitiesConfig;
@@ -191,6 +191,53 @@ declare const entitySchema: z.ZodObject<{
191
191
  }, z.core.$strict>]>;
192
192
  }, z.core.$strict>>>;
193
193
  targetObjectTypeId: z.ZodOptional<z.ZodString>;
194
+ projection: z.ZodOptional<z.ZodObject<{
195
+ version: z.ZodLiteral<1>;
196
+ kind: z.ZodLiteral<"projection">;
197
+ entityKey: z.ZodString;
198
+ baseTable: z.ZodString;
199
+ joins: z.ZodArray<z.ZodObject<{
200
+ key: z.ZodString;
201
+ table: z.ZodString;
202
+ type: z.ZodEnum<{
203
+ inner: "inner";
204
+ left: "left";
205
+ }>;
206
+ on: z.ZodArray<z.ZodObject<{
207
+ left: z.ZodObject<{
208
+ table: z.ZodString;
209
+ column: z.ZodString;
210
+ }, z.core.$strict>;
211
+ op: z.ZodLiteral<"eq">;
212
+ right: z.ZodObject<{
213
+ table: z.ZodString;
214
+ column: z.ZodString;
215
+ }, z.core.$strict>;
216
+ }, z.core.$strict>>;
217
+ }, z.core.$strict>>;
218
+ fieldBindings: z.ZodRecord<z.ZodString, z.ZodObject<{
219
+ from: z.ZodObject<{
220
+ table: z.ZodString;
221
+ column: z.ZodString;
222
+ }, z.core.$strict>;
223
+ }, z.core.$strict>>;
224
+ identity: z.ZodUnion<readonly [z.ZodObject<{
225
+ kind: z.ZodLiteral<"base_pk">;
226
+ column: z.ZodOptional<z.ZodString>;
227
+ }, z.core.$strict>, z.ZodObject<{
228
+ kind: z.ZodLiteral<"column">;
229
+ from: z.ZodObject<{
230
+ table: z.ZodString;
231
+ column: z.ZodString;
232
+ }, z.core.$strict>;
233
+ }, z.core.$strict>, z.ZodObject<{
234
+ kind: z.ZodLiteral<"composite">;
235
+ parts: z.ZodArray<z.ZodObject<{
236
+ table: z.ZodString;
237
+ column: z.ZodString;
238
+ }, z.core.$strict>>;
239
+ }, z.core.$strict>]>;
240
+ }, z.core.$strict>>;
194
241
  }, z.core.$strict>;
195
242
  type ClivlyEntityConfig = z.infer<typeof entitySchema>;
196
243
  declare const entitiesConfigSchema: z.ZodObject<{
@@ -239,6 +286,53 @@ declare const entitiesConfigSchema: z.ZodObject<{
239
286
  }, z.core.$strict>]>;
240
287
  }, z.core.$strict>>>;
241
288
  targetObjectTypeId: z.ZodOptional<z.ZodString>;
289
+ projection: z.ZodOptional<z.ZodObject<{
290
+ version: z.ZodLiteral<1>;
291
+ kind: z.ZodLiteral<"projection">;
292
+ entityKey: z.ZodString;
293
+ baseTable: z.ZodString;
294
+ joins: z.ZodArray<z.ZodObject<{
295
+ key: z.ZodString;
296
+ table: z.ZodString;
297
+ type: z.ZodEnum<{
298
+ inner: "inner";
299
+ left: "left";
300
+ }>;
301
+ on: z.ZodArray<z.ZodObject<{
302
+ left: z.ZodObject<{
303
+ table: z.ZodString;
304
+ column: z.ZodString;
305
+ }, z.core.$strict>;
306
+ op: z.ZodLiteral<"eq">;
307
+ right: z.ZodObject<{
308
+ table: z.ZodString;
309
+ column: z.ZodString;
310
+ }, z.core.$strict>;
311
+ }, z.core.$strict>>;
312
+ }, z.core.$strict>>;
313
+ fieldBindings: z.ZodRecord<z.ZodString, z.ZodObject<{
314
+ from: z.ZodObject<{
315
+ table: z.ZodString;
316
+ column: z.ZodString;
317
+ }, z.core.$strict>;
318
+ }, z.core.$strict>>;
319
+ identity: z.ZodUnion<readonly [z.ZodObject<{
320
+ kind: z.ZodLiteral<"base_pk">;
321
+ column: z.ZodOptional<z.ZodString>;
322
+ }, z.core.$strict>, z.ZodObject<{
323
+ kind: z.ZodLiteral<"column">;
324
+ from: z.ZodObject<{
325
+ table: z.ZodString;
326
+ column: z.ZodString;
327
+ }, z.core.$strict>;
328
+ }, z.core.$strict>, z.ZodObject<{
329
+ kind: z.ZodLiteral<"composite">;
330
+ parts: z.ZodArray<z.ZodObject<{
331
+ table: z.ZodString;
332
+ column: z.ZodString;
333
+ }, z.core.$strict>>;
334
+ }, z.core.$strict>]>;
335
+ }, z.core.$strict>>;
242
336
  }, z.core.$strict>>;
243
337
  }, z.core.$strict>;
244
338
  type ClivlyEntitiesConfig = z.infer<typeof entitiesConfigSchema>;
@@ -263,14 +357,39 @@ declare function defineClivlyConfig(config: ClivlyEntitiesConfig, options?: {
263
357
  strict?: boolean;
264
358
  onWarn?: (message: string) => void;
265
359
  }): ClivlyEntitiesConfig;
360
+ /**
361
+ * Per-column metadata a host reports for a discovered table. Structurally
362
+ * identical to the SDK's `DiscoveredColumn` and to `entity-heuristics.ts`'s
363
+ * (currently separate) `DiscoveredColumnMeta` — a later task converges that
364
+ * module onto this one. Every field is optional: a v1 host reports column
365
+ * names only.
366
+ */
367
+ interface DiscoveredColumnMeta {
368
+ isForeignKey?: boolean;
369
+ isPrimaryKey?: boolean;
370
+ name: string;
371
+ nullable?: boolean;
372
+ references?: {
373
+ table: string;
374
+ column: string;
375
+ };
376
+ type?: string;
377
+ }
266
378
  /**
267
379
  * A host table the SDK reported for discovery. Structurally identical to the
268
380
  * SDK's `DiscoveredTable`; duplicated here to keep `@clivly/core` free of an
269
381
  * SDK dependency (core is the base contract SDK builds on).
382
+ *
383
+ * `columns` is the stable v1 contract. `columnsMeta` and `uniqueConstraints`
384
+ * are the enrichment projection mappings require — absent on v1 hosts, which
385
+ * is why projection validation fails closed rather than assuming "no keys".
270
386
  */
271
387
  interface DiscoveredSchemaTable {
272
388
  columns: string[];
389
+ columnsMeta?: DiscoveredColumnMeta[];
273
390
  name: string;
391
+ /** Every unique key as DB column names; PKs included. Absent = unknown. */
392
+ uniqueConstraints?: string[][];
274
393
  }
275
394
  interface ConfigValidationError {
276
395
  message: string;
@@ -292,4 +411,4 @@ interface ConfigValidationResult {
292
411
  */
293
412
  declare function validateEntitiesConfig(config: ClivlyEntitiesConfig, schema: DiscoveredSchemaTable[]): ConfigValidationResult;
294
413
  //#endregion
295
- export { CANONICAL_FIELDS, CRM_CONCEPTS, CUSTOM_CONCEPT, ClivlyConfigError, ClivlyEntitiesConfig, ClivlyEntityConfig, ConfigValidationError, ConfigValidationResult, CrmConcept, DiscoveredSchemaTable, FilterExpr, FilterValue, MATERIALIZING_CONCEPTS, MaterializingConcept, RelationshipSpec, RelationshipVia, RelationshipsMap, defineClivlyConfig, entitiesConfigSchema, filterSchema, relationshipSchema, relationshipsSchema, validateEntitiesConfig };
414
+ export { CANONICAL_FIELDS, CRM_CONCEPTS, CUSTOM_CONCEPT, ClivlyConfigError, ClivlyEntitiesConfig, ClivlyEntityConfig, ConfigValidationError, ConfigValidationResult, CrmConcept, DiscoveredColumnMeta, DiscoveredSchemaTable, FilterExpr, FilterValue, MATERIALIZING_CONCEPTS, MaterializingConcept, RelationshipSpec, RelationshipVia, RelationshipsMap, defineClivlyConfig, entitiesConfigSchema, filterSchema, relationshipSchema, relationshipsSchema, validateEntitiesConfig };
@@ -191,6 +191,53 @@ declare const entitySchema: z.ZodObject<{
191
191
  }, z.core.$strict>]>;
192
192
  }, z.core.$strict>>>;
193
193
  targetObjectTypeId: z.ZodOptional<z.ZodString>;
194
+ projection: z.ZodOptional<z.ZodObject<{
195
+ version: z.ZodLiteral<1>;
196
+ kind: z.ZodLiteral<"projection">;
197
+ entityKey: z.ZodString;
198
+ baseTable: z.ZodString;
199
+ joins: z.ZodArray<z.ZodObject<{
200
+ key: z.ZodString;
201
+ table: z.ZodString;
202
+ type: z.ZodEnum<{
203
+ inner: "inner";
204
+ left: "left";
205
+ }>;
206
+ on: z.ZodArray<z.ZodObject<{
207
+ left: z.ZodObject<{
208
+ table: z.ZodString;
209
+ column: z.ZodString;
210
+ }, z.core.$strict>;
211
+ op: z.ZodLiteral<"eq">;
212
+ right: z.ZodObject<{
213
+ table: z.ZodString;
214
+ column: z.ZodString;
215
+ }, z.core.$strict>;
216
+ }, z.core.$strict>>;
217
+ }, z.core.$strict>>;
218
+ fieldBindings: z.ZodRecord<z.ZodString, z.ZodObject<{
219
+ from: z.ZodObject<{
220
+ table: z.ZodString;
221
+ column: z.ZodString;
222
+ }, z.core.$strict>;
223
+ }, z.core.$strict>>;
224
+ identity: z.ZodUnion<readonly [z.ZodObject<{
225
+ kind: z.ZodLiteral<"base_pk">;
226
+ column: z.ZodOptional<z.ZodString>;
227
+ }, z.core.$strict>, z.ZodObject<{
228
+ kind: z.ZodLiteral<"column">;
229
+ from: z.ZodObject<{
230
+ table: z.ZodString;
231
+ column: z.ZodString;
232
+ }, z.core.$strict>;
233
+ }, z.core.$strict>, z.ZodObject<{
234
+ kind: z.ZodLiteral<"composite">;
235
+ parts: z.ZodArray<z.ZodObject<{
236
+ table: z.ZodString;
237
+ column: z.ZodString;
238
+ }, z.core.$strict>>;
239
+ }, z.core.$strict>]>;
240
+ }, z.core.$strict>>;
194
241
  }, z.core.$strict>;
195
242
  type ClivlyEntityConfig = z.infer<typeof entitySchema>;
196
243
  declare const entitiesConfigSchema: z.ZodObject<{
@@ -239,6 +286,53 @@ declare const entitiesConfigSchema: z.ZodObject<{
239
286
  }, z.core.$strict>]>;
240
287
  }, z.core.$strict>>>;
241
288
  targetObjectTypeId: z.ZodOptional<z.ZodString>;
289
+ projection: z.ZodOptional<z.ZodObject<{
290
+ version: z.ZodLiteral<1>;
291
+ kind: z.ZodLiteral<"projection">;
292
+ entityKey: z.ZodString;
293
+ baseTable: z.ZodString;
294
+ joins: z.ZodArray<z.ZodObject<{
295
+ key: z.ZodString;
296
+ table: z.ZodString;
297
+ type: z.ZodEnum<{
298
+ inner: "inner";
299
+ left: "left";
300
+ }>;
301
+ on: z.ZodArray<z.ZodObject<{
302
+ left: z.ZodObject<{
303
+ table: z.ZodString;
304
+ column: z.ZodString;
305
+ }, z.core.$strict>;
306
+ op: z.ZodLiteral<"eq">;
307
+ right: z.ZodObject<{
308
+ table: z.ZodString;
309
+ column: z.ZodString;
310
+ }, z.core.$strict>;
311
+ }, z.core.$strict>>;
312
+ }, z.core.$strict>>;
313
+ fieldBindings: z.ZodRecord<z.ZodString, z.ZodObject<{
314
+ from: z.ZodObject<{
315
+ table: z.ZodString;
316
+ column: z.ZodString;
317
+ }, z.core.$strict>;
318
+ }, z.core.$strict>>;
319
+ identity: z.ZodUnion<readonly [z.ZodObject<{
320
+ kind: z.ZodLiteral<"base_pk">;
321
+ column: z.ZodOptional<z.ZodString>;
322
+ }, z.core.$strict>, z.ZodObject<{
323
+ kind: z.ZodLiteral<"column">;
324
+ from: z.ZodObject<{
325
+ table: z.ZodString;
326
+ column: z.ZodString;
327
+ }, z.core.$strict>;
328
+ }, z.core.$strict>, z.ZodObject<{
329
+ kind: z.ZodLiteral<"composite">;
330
+ parts: z.ZodArray<z.ZodObject<{
331
+ table: z.ZodString;
332
+ column: z.ZodString;
333
+ }, z.core.$strict>>;
334
+ }, z.core.$strict>]>;
335
+ }, z.core.$strict>>;
242
336
  }, z.core.$strict>>;
243
337
  }, z.core.$strict>;
244
338
  type ClivlyEntitiesConfig = z.infer<typeof entitiesConfigSchema>;
@@ -263,14 +357,39 @@ declare function defineClivlyConfig(config: ClivlyEntitiesConfig, options?: {
263
357
  strict?: boolean;
264
358
  onWarn?: (message: string) => void;
265
359
  }): ClivlyEntitiesConfig;
360
+ /**
361
+ * Per-column metadata a host reports for a discovered table. Structurally
362
+ * identical to the SDK's `DiscoveredColumn` and to `entity-heuristics.ts`'s
363
+ * (currently separate) `DiscoveredColumnMeta` — a later task converges that
364
+ * module onto this one. Every field is optional: a v1 host reports column
365
+ * names only.
366
+ */
367
+ interface DiscoveredColumnMeta {
368
+ isForeignKey?: boolean;
369
+ isPrimaryKey?: boolean;
370
+ name: string;
371
+ nullable?: boolean;
372
+ references?: {
373
+ table: string;
374
+ column: string;
375
+ };
376
+ type?: string;
377
+ }
266
378
  /**
267
379
  * A host table the SDK reported for discovery. Structurally identical to the
268
380
  * SDK's `DiscoveredTable`; duplicated here to keep `@clivly/core` free of an
269
381
  * SDK dependency (core is the base contract SDK builds on).
382
+ *
383
+ * `columns` is the stable v1 contract. `columnsMeta` and `uniqueConstraints`
384
+ * are the enrichment projection mappings require — absent on v1 hosts, which
385
+ * is why projection validation fails closed rather than assuming "no keys".
270
386
  */
271
387
  interface DiscoveredSchemaTable {
272
388
  columns: string[];
389
+ columnsMeta?: DiscoveredColumnMeta[];
273
390
  name: string;
391
+ /** Every unique key as DB column names; PKs included. Absent = unknown. */
392
+ uniqueConstraints?: string[][];
274
393
  }
275
394
  interface ConfigValidationError {
276
395
  message: string;
@@ -292,4 +411,4 @@ interface ConfigValidationResult {
292
411
  */
293
412
  declare function validateEntitiesConfig(config: ClivlyEntitiesConfig, schema: DiscoveredSchemaTable[]): ConfigValidationResult;
294
413
  //#endregion
295
- export { CANONICAL_FIELDS, CRM_CONCEPTS, CUSTOM_CONCEPT, ClivlyConfigError, ClivlyEntitiesConfig, ClivlyEntityConfig, ConfigValidationError, ConfigValidationResult, CrmConcept, DiscoveredSchemaTable, FilterExpr, FilterValue, MATERIALIZING_CONCEPTS, MaterializingConcept, RelationshipSpec, RelationshipVia, RelationshipsMap, defineClivlyConfig, entitiesConfigSchema, filterSchema, relationshipSchema, relationshipsSchema, validateEntitiesConfig };
414
+ export { CANONICAL_FIELDS, CRM_CONCEPTS, CUSTOM_CONCEPT, ClivlyConfigError, ClivlyEntitiesConfig, ClivlyEntityConfig, ConfigValidationError, ConfigValidationResult, CrmConcept, DiscoveredColumnMeta, DiscoveredSchemaTable, FilterExpr, FilterValue, MATERIALIZING_CONCEPTS, MaterializingConcept, RelationshipSpec, RelationshipVia, RelationshipsMap, defineClivlyConfig, entitiesConfigSchema, filterSchema, relationshipSchema, relationshipsSchema, validateEntitiesConfig };