import { z } from "zod"; //#region src/projection.d.ts /** * Projection-backed custom mappings — one custom object derived from a base * table plus explicit joins. * * A projection is deliberately narrow: equality joins only, direct column * bindings only, no expressions and no aggregation. Everything a user can * express here compiles to a single read-only view whose `id` column is * provably unique per row, which is what lets the sync engine consume it * unchanged. * * Refs address tables symbolically: `"base"` for the base table, or a join's * `key`. Raw table names are never accepted in a ref, so a projection can * never reach a table it did not declare. */ /** The reserved ref name for the projection's base table. */ declare const BASE_REF = "base"; /** * Entity keys become view names (`crm_src_`), so keep them slugs. * * Accepts hyphens as well as underscores, matching `OBJECT_TYPE_SLUG_PATTERN` * on the server. Those two were widened apart: the server learned hyphens * because the SDK's own `slugify` collapses non-alphanumerics to them and * `clivly status` reports that form as canonical — but this pattern was missed, * so `clivly status` would tell you to key an entity `client-accounts` and then * `defineClivlyConfig` would reject the config it had just asked for. Same bug, * opposite half of the product. * * Widening is the safe direction: every key that was valid before still is, so * nothing existing is renamed. Safe at all because neither use is ever a bare * SQL identifier — the view name is quoted (`view-compiler.ts`, `quoteIdent`), * and so is a join's `key` when it is emitted as an alias. */ declare const PROJECTION_KEY_PATTERN: RegExp; declare const projectionRefSchema: z.ZodObject<{ table: z.ZodString; column: z.ZodString; }, z.core.$strict>; type ProjectionRef = z.infer; declare const projectionJoinSchema: z.ZodObject<{ key: z.ZodString; table: z.ZodString; type: z.ZodEnum<{ inner: "inner"; left: "left"; }>; on: z.ZodArray; op: z.ZodLiteral<"eq">; right: z.ZodObject<{ table: z.ZodString; column: z.ZodString; }, z.core.$strict>; }, z.core.$strict>>; }, z.core.$strict>; type ProjectionJoin = z.infer; declare const projectionFieldBindingSchema: z.ZodObject<{ from: z.ZodObject<{ table: z.ZodString; column: z.ZodString; }, z.core.$strict>; }, z.core.$strict>; type ProjectionFieldBinding = z.infer; declare const projectionIdentitySchema: z.ZodUnion; column: z.ZodOptional; }, z.core.$strict>, z.ZodObject<{ kind: z.ZodLiteral<"column">; from: z.ZodObject<{ table: z.ZodString; column: z.ZodString; }, z.core.$strict>; }, z.core.$strict>, z.ZodObject<{ kind: z.ZodLiteral<"composite">; parts: z.ZodArray>; }, z.core.$strict>]>; type ProjectionIdentity = z.infer; declare const projectionSchema: z.ZodObject<{ version: z.ZodLiteral<1>; kind: z.ZodLiteral<"projection">; entityKey: z.ZodString; baseTable: z.ZodString; joins: z.ZodArray; on: z.ZodArray; op: z.ZodLiteral<"eq">; right: z.ZodObject<{ table: z.ZodString; column: z.ZodString; }, z.core.$strict>; }, z.core.$strict>>; }, z.core.$strict>>; fieldBindings: z.ZodRecord; }, z.core.$strict>>; identity: z.ZodUnion; column: z.ZodOptional; }, z.core.$strict>, z.ZodObject<{ kind: z.ZodLiteral<"column">; from: z.ZodObject<{ table: z.ZodString; column: z.ZodString; }, z.core.$strict>; }, z.core.$strict>, z.ZodObject<{ kind: z.ZodLiteral<"composite">; parts: z.ZodArray>; }, z.core.$strict>]>; }, z.core.$strict>; type ProjectionMapping = z.infer; /** * Every column ref an identity depends on. `basePkColumn` is the base table's * discovered PK, used only when a `base_pk` identity omits an explicit column. */ declare function identityRefs(identity: ProjectionIdentity, basePkColumn: string): ProjectionRef[]; //#endregion //#region src/entity-config.d.ts /** * Entity Config v2 — the declarative contract that turns Clivly's "schema-aware" * promise into a real capability. * * A CRM entity is no longer a flat table name. It is a *derived entity*: a * filtered slice of a host table (`source` + `filter`), a column allowlist * (`fields` + `readOnly`), and declared `relationships` to other entities. The * same host table can back several entities (e.g. `users` → owners + members) * distinguished by `role`. * * This module is the Phase 1 slice: types, schema, `defineClivlyConfig` (a * structural helper) and `validateEntitiesConfig` (semantic validation against * the host's discovered schema). It performs NO DB access and generates no SQL — * the view compiler (Phase 2) and sync engine (Phase 3) consume this contract. */ /** Canonical CRM concepts a host entity can map onto. */ declare const CRM_CONCEPTS: readonly ["contact", "company", "deal", "activity", "conversation", "note"]; type CrmConcept = (typeof CRM_CONCEPTS)[number]; /** * The canonical `fields` keys the mirror actually persists, per concept — the * crm_* columns the sync store writes. An entity's `fields` map keys must be one * of these: a non-canonical key (e.g. `fullName` instead of `name`) validates * structurally but has no destination column, so the store silently writes an * empty value. `validateEntitiesConfig` flags those. * * This is the single source of truth — the Drizzle store imports it, so the * two can't drift (the drift is what made the silent-data-loss trap possible). * Concepts absent here aren't materialized yet, so their field keys are * unconstrained. */ declare const CANONICAL_FIELDS: { readonly contact: readonly ["name", "email", "phone", "title", "status"]; readonly company: readonly ["name", "domain", "industry", "size", "email", "phone"]; readonly deal: readonly ["name", "value", "stage"]; }; /** * The subset of `CANONICAL_FIELDS` a mapping cannot sync without. * * Kept beside CANONICAL_FIELDS, and exported, for the reason stated above: the * heuristics module holds the same knowledge as `required: true` flags, and the * dashboard held a third copy. That third copy is what let an invalid mapping * save cleanly and then be dropped by a sync that still reported "Succeeded" — * the editor had no way to ask what was mandatory, so it did not ask. * * Every field listed here must also appear in CANONICAL_FIELDS for the same * concept; `requiredFieldsAreCanonical` in the tests enforces that. * * A concept absent here has no mandatory field, so nothing is blocked by * accident. */ declare const REQUIRED_FIELDS: { readonly contact: readonly ["email"]; readonly company: readonly ["name"]; readonly deal: readonly ["name"]; }; /** The fields this concept must have mapped before a sync will keep it. */ declare function requiredFieldsFor(concept: string): readonly string[]; /** * Which required fields a draft mapping has not filled in. Empty means the * mapping is complete enough for a sync to accept it. */ declare function missingRequiredFields(concept: string, fieldMap: Record): string[]; /** * The concepts the mapping→sync pipeline can actually materialize into `crm_*` * tables. These are exactly the CANONICAL_FIELDS keys — the Drizzle sync store's * `persist()` throws for any other concept, so offering one (in the mapping * editor, in suggestions, or via the upsert API) is a broken affordance. * * The broader CRM_CONCEPTS set still validates structurally, so previously * stored rows for not-yet-materialized concepts keep rendering. When a concept * gains a sync-store path, add its canonical fields above and it joins this set * automatically (guarded by the MATERIALIZING_CONCEPTS drift test). */ declare const MATERIALIZING_CONCEPTS: readonly ["contact", "company", "deal"]; type MaterializingConcept = (typeof MATERIALIZING_CONCEPTS)[number]; /** * The org-defined custom-object target. Deliberately NOT a member of * CRM_CONCEPTS / CANONICAL_FIELDS / MATERIALIZING_CONCEPTS — a custom object has * no fixed canonical columns; its mapping's fieldMap keys ARE its properties. * Handled as an explicit branch wherever concepts are switched on, so the * canonical-concept invariants (and their drift test) stay intact. */ declare const CUSTOM_CONCEPT: "custom"; declare const filterValueSchema: z.ZodUnion; }, z.core.$strict>, z.ZodObject<{ ne: z.ZodUnion; }, z.core.$strict>, z.ZodObject<{ in: z.ZodArray>; }, z.core.$strict>, z.ZodObject<{ notIn: z.ZodArray>; }, z.core.$strict>, z.ZodObject<{ isNull: z.ZodLiteral; }, z.core.$strict>, z.ZodObject<{ isNotNull: z.ZodLiteral; }, z.core.$strict>]>]>; /** `$raw` is a reserved key; a column literally named `$raw` is not addressable. */ declare const filterSchema: z.ZodUnion, z.ZodRecord; }, z.core.$strict>, z.ZodObject<{ ne: z.ZodUnion; }, z.core.$strict>, z.ZodObject<{ in: z.ZodArray>; }, z.core.$strict>, z.ZodObject<{ notIn: z.ZodArray>; }, z.core.$strict>, z.ZodObject<{ isNull: z.ZodLiteral; }, z.core.$strict>, z.ZodObject<{ isNotNull: z.ZodLiteral; }, z.core.$strict>]>]>>]>; type FilterExpr = z.infer; /** A single column predicate value (scalar shorthand or operator object). */ type FilterValue = z.infer; /** * How the link between two entities is expressed in the host schema: * - `fkOn: "company"` — FK lives on the company table (e.g. organizations.owner_id) * - `fkOn: "contact"` — FK lives on the contact table (e.g. users.organization_id) * - `through` — a join table sits between them (many-to-many, role often here) * - `$raw` — opaque SQL join fragment (escape hatch; not schema-validated) * * `column` and the join-table keys are `table.column` or bare `column` strings. */ declare const relationshipViaSchema: z.ZodUnion; column: z.ZodString; }, z.core.$strict>, z.ZodObject<{ through: z.ZodString; localKey: z.ZodString; foreignKey: z.ZodString; }, z.core.$strict>, z.ZodObject<{ $raw: z.ZodString; }, z.core.$strict>]>; type RelationshipVia = z.infer; declare const relationshipSchema: z.ZodObject<{ entity: z.ZodString; via: z.ZodUnion; column: z.ZodString; }, z.core.$strict>, z.ZodObject<{ through: z.ZodString; localKey: z.ZodString; foreignKey: z.ZodString; }, z.core.$strict>, z.ZodObject<{ $raw: z.ZodString; }, z.core.$strict>]>; }, z.core.$strict>; type RelationshipSpec = z.infer; /** A named map of relationships, as stored on an entity / in the mappings row. */ declare const relationshipsSchema: z.ZodRecord; column: z.ZodString; }, z.core.$strict>, z.ZodObject<{ through: z.ZodString; localKey: z.ZodString; foreignKey: z.ZodString; }, z.core.$strict>, z.ZodObject<{ $raw: z.ZodString; }, z.core.$strict>]>; }, z.core.$strict>>; type RelationshipsMap = z.infer; declare const entitySchema: z.ZodObject<{ concept: z.ZodUnion, z.ZodLiteral<"custom">]>; source: z.ZodString; role: z.ZodOptional; filter: z.ZodOptional, z.ZodRecord; }, z.core.$strict>, z.ZodObject<{ ne: z.ZodUnion; }, z.core.$strict>, z.ZodObject<{ in: z.ZodArray>; }, z.core.$strict>, z.ZodObject<{ notIn: z.ZodArray>; }, z.core.$strict>, z.ZodObject<{ isNull: z.ZodLiteral; }, z.core.$strict>, z.ZodObject<{ isNotNull: z.ZodLiteral; }, z.core.$strict>]>]>>]>>; fields: z.ZodRecord; readOnly: z.ZodOptional>; relationships: z.ZodOptional; column: z.ZodString; }, z.core.$strict>, z.ZodObject<{ through: z.ZodString; localKey: z.ZodString; foreignKey: z.ZodString; }, z.core.$strict>, z.ZodObject<{ $raw: z.ZodString; }, z.core.$strict>]>; }, z.core.$strict>>>; isPrimaryContact: z.ZodOptional; targetObjectTypeId: z.ZodOptional; objectType: z.ZodOptional; iconKey: z.ZodOptional; }, z.core.$strict>>; projection: z.ZodOptional; kind: z.ZodLiteral<"projection">; entityKey: z.ZodString; baseTable: z.ZodString; joins: z.ZodArray; on: z.ZodArray; op: z.ZodLiteral<"eq">; right: z.ZodObject<{ table: z.ZodString; column: z.ZodString; }, z.core.$strict>; }, z.core.$strict>>; }, z.core.$strict>>; fieldBindings: z.ZodRecord; }, z.core.$strict>>; identity: z.ZodUnion; column: z.ZodOptional; }, z.core.$strict>, z.ZodObject<{ kind: z.ZodLiteral<"column">; from: z.ZodObject<{ table: z.ZodString; column: z.ZodString; }, z.core.$strict>; }, z.core.$strict>, z.ZodObject<{ kind: z.ZodLiteral<"composite">; parts: z.ZodArray>; }, z.core.$strict>]>; }, z.core.$strict>>; }, z.core.$strict>; type ClivlyEntityConfig = z.infer; declare const entitiesConfigSchema: z.ZodObject<{ entities: z.ZodRecord, z.ZodLiteral<"custom">]>; source: z.ZodString; role: z.ZodOptional; filter: z.ZodOptional, z.ZodRecord; }, z.core.$strict>, z.ZodObject<{ ne: z.ZodUnion; }, z.core.$strict>, z.ZodObject<{ in: z.ZodArray>; }, z.core.$strict>, z.ZodObject<{ notIn: z.ZodArray>; }, z.core.$strict>, z.ZodObject<{ isNull: z.ZodLiteral; }, z.core.$strict>, z.ZodObject<{ isNotNull: z.ZodLiteral; }, z.core.$strict>]>]>>]>>; fields: z.ZodRecord; readOnly: z.ZodOptional>; relationships: z.ZodOptional; column: z.ZodString; }, z.core.$strict>, z.ZodObject<{ through: z.ZodString; localKey: z.ZodString; foreignKey: z.ZodString; }, z.core.$strict>, z.ZodObject<{ $raw: z.ZodString; }, z.core.$strict>]>; }, z.core.$strict>>>; isPrimaryContact: z.ZodOptional; targetObjectTypeId: z.ZodOptional; objectType: z.ZodOptional; iconKey: z.ZodOptional; }, z.core.$strict>>; projection: z.ZodOptional; kind: z.ZodLiteral<"projection">; entityKey: z.ZodString; baseTable: z.ZodString; joins: z.ZodArray; on: z.ZodArray; op: z.ZodLiteral<"eq">; right: z.ZodObject<{ table: z.ZodString; column: z.ZodString; }, z.core.$strict>; }, z.core.$strict>>; }, z.core.$strict>>; fieldBindings: z.ZodRecord; }, z.core.$strict>>; identity: z.ZodUnion; column: z.ZodOptional; }, z.core.$strict>, z.ZodObject<{ kind: z.ZodLiteral<"column">; from: z.ZodObject<{ table: z.ZodString; column: z.ZodString; }, z.core.$strict>; }, z.core.$strict>, z.ZodObject<{ kind: z.ZodLiteral<"composite">; parts: z.ZodArray>; }, z.core.$strict>]>; }, z.core.$strict>>; }, z.core.$strict>>; }, z.core.$strict>; type ClivlyEntitiesConfig = z.infer; /** * What an author may hand to `defineClivlyConfig`. Identical to * `ClivlyEntityConfig` except that a projection entity may omit `fields` — * it is derived from `projection.fieldBindings`. Without a projection `fields` * stays required, so the looser input never weakens the ordinary case. */ type ClivlyEntityConfigInput = (ClivlyEntityConfig & { projection?: undefined; }) | (Omit & { fields?: Record; projection: ProjectionMapping; }); type ClivlyEntitiesConfigInput = Omit & { entities: Record; }; /** Thrown by `defineClivlyConfig({ strict: true })` on non-canonical field keys. */ declare class ClivlyConfigError extends Error { readonly issues: ConfigValidationError[]; constructor(issues: ConfigValidationError[]); } /** * Structural helper for `clivly.config.ts`. Gives full type inference on the * literal you pass and eagerly parses it, so shape errors surface at config * load rather than at sync time. * * It also catches the silent-data-loss footgun: a non-canonical field key for a * materialised concept (e.g. `fullName` instead of `name` on a `contact`) has no * destination column, so the store would write an empty value. By default these * are reported via `onWarn` (a `console.warn`); pass `{ strict: true }` to throw * a `ClivlyConfigError` instead. Does NOT validate against the host schema — that * needs the discovered schema; see `validateEntitiesConfig`. */ /** A custom object the host declares in config, for `push-schema` to ensure. */ interface CustomObjectDeclaration { iconKey?: string; name: string; pluralName?: string; /** Unique per org and immutable — the entity key. */ slug: string; } /** * The custom objects a config declares, keyed by entity slug. Entities that * already carry an explicit `targetObjectTypeId` are omitted: their object type * exists server-side and re-declaring it would be a no-op at best. */ declare function customObjectDeclarations(config: ClivlyEntitiesConfig): CustomObjectDeclaration[]; /** * The `fields` entry a projection binding implies, qualified as `ref.column`. * Qualified rather than bare: two bindings can pull the same column name from * different tables (`cl.full_name` and `adv.full_name`), which a bare column * name cannot tell apart. * * This is the single derivation of that map, and both consumers depend on it * emitting ref tokens. `mappingsToEntitiesConfig` populates a projection * entity's required `fields`; the cloud ingest path needs the same map because * a projection's persisted `crm_entity_mappings.field_map` is empty by design, * so `rowToSourceRow` derives the effective field map from the projection * instead. The tokens emitted here are exactly how `fromDrizzleProjection` keys * the rows it pushes (`drizzle-projection.ts` `refToken`), so the two line up. * Deriving this a second time anywhere would let the copies drift. */ declare function projectionFields(projection: ProjectionMapping): Record; declare function defineClivlyConfig(config: ClivlyEntitiesConfigInput, options?: { strict?: boolean; onWarn?: (message: string) => void; }): ClivlyEntitiesConfig; /** * Per-column metadata a host reports for a discovered table. Structurally * identical to the SDK's `DiscoveredColumn` and to `entity-heuristics.ts`'s * (currently separate) `DiscoveredColumnMeta` — a later task converges that * module onto this one. Every field is optional: a v1 host reports column * names only. */ interface DiscoveredColumnMeta { isForeignKey?: boolean; isPrimaryKey?: boolean; name: string; nullable?: boolean; references?: { table: string; column: string; }; type?: string; } /** * A host table the SDK reported for discovery. Structurally identical to the * SDK's `DiscoveredTable`; duplicated here to keep `@clivly/core` free of an * SDK dependency (core is the base contract SDK builds on). * * `columns` is the stable v1 contract. `columnsMeta` and `uniqueConstraints` * are the enrichment projection mappings require — absent on v1 hosts, which * is why projection validation fails closed rather than assuming "no keys". */ interface DiscoveredSchemaTable { columns: string[]; columnsMeta?: DiscoveredColumnMeta[]; name: string; /** Every unique key as DB column names; PKs included. Absent = unknown. */ uniqueConstraints?: string[][]; } interface ConfigValidationError { message: string; /** Dotted path into the config, e.g. `entities.owner.fields.email`. */ path: string; } interface ConfigValidationResult { errors: ConfigValidationError[]; valid: boolean; } /** * Validate a (structurally valid) config against the host's discovered schema: * source tables exist, mapped columns exist, `readOnly` ⊆ `fields`, filter * columns exist, and relationships point at real entities/tables/columns. * * `$raw` filters and `$raw` relationship joins are opaque and skipped. Returns * every error found (not just the first) so a mapping UI or CLI can show them * all at once. */ declare function validateEntitiesConfig(config: ClivlyEntitiesConfig, schema: DiscoveredSchemaTable[]): ConfigValidationResult; //#endregion export { relationshipsSchema as A, identityRefs as B, customObjectDeclarations as C, missingRequiredFields as D, filterSchema as E, ProjectionFieldBinding as F, ProjectionIdentity as I, ProjectionJoin as L, validateEntitiesConfig as M, BASE_REF as N, projectionFields as O, PROJECTION_KEY_PATTERN as P, ProjectionMapping as R, RelationshipsMap as S, entitiesConfigSchema as T, projectionSchema as V, MATERIALIZING_CONCEPTS as _, ClivlyEntitiesConfig as a, RelationshipSpec as b, ClivlyEntityConfigInput as c, CrmConcept as d, CustomObjectDeclaration as f, FilterValue as g, FilterExpr as h, ClivlyConfigError as i, requiredFieldsFor as j, relationshipSchema as k, ConfigValidationError as l, DiscoveredSchemaTable as m, CRM_CONCEPTS as n, ClivlyEntitiesConfigInput as o, DiscoveredColumnMeta as p, CUSTOM_CONCEPT as r, ClivlyEntityConfig as s, CANONICAL_FIELDS as t, ConfigValidationResult as u, MaterializingConcept as v, defineClivlyConfig as w, RelationshipVia as x, REQUIRED_FIELDS as y, ProjectionRef as z };