clivly 0.8.0 → 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.
Files changed (114) hide show
  1. package/README.md +330 -134
  2. package/core-drizzle.cjs +3 -1
  3. package/core-drizzle.d.cts +3 -2
  4. package/core-drizzle.d.mts +3 -2
  5. package/core-drizzle.mjs +2 -2
  6. package/core-entity-config.cjs +1 -1
  7. package/core-entity-config.d.cts +2 -2
  8. package/core-entity-config.d.mts +2 -2
  9. package/core-entity-config.mjs +1 -1
  10. package/core-entity-heuristics.d.cts +3 -2
  11. package/core-entity-heuristics.d.mts +3 -2
  12. package/core-mapping-form.d.cts +1 -1
  13. package/core-mapping-form.d.mts +1 -1
  14. package/core-mappings-config.cjs +1 -1
  15. package/core-mappings-config.d.cts +1 -1
  16. package/core-mappings-config.d.mts +1 -1
  17. package/core-mappings-config.mjs +1 -1
  18. package/core-sync-engine.cjs +1 -1
  19. package/core-sync-engine.d.cts +1 -1
  20. package/core-sync-engine.d.mts +1 -1
  21. package/core-sync-engine.mjs +1 -1
  22. package/core-view-compiler.cjs +1 -1
  23. package/core-view-compiler.d.cts +1 -1
  24. package/core-view-compiler.d.mts +1 -1
  25. package/core-view-compiler.mjs +1 -1
  26. package/core.cjs +21 -5
  27. package/core.d.cts +7 -6
  28. package/core.d.mts +7 -6
  29. package/core.mjs +7 -6
  30. package/drizzle-3kvhkwO7.mjs +110 -0
  31. package/drizzle-BhN5M9zt.d.cts +41 -0
  32. package/drizzle-bfjVzYR6.cjs +127 -0
  33. package/drizzle-kBULBAOa.d.mts +41 -0
  34. package/drizzle.cjs +12 -40
  35. package/drizzle.d.cts +3 -11
  36. package/drizzle.d.mts +3 -11
  37. package/drizzle.mjs +12 -41
  38. package/{entity-config-DoUCXtq5.d.cts → entity-config-C4Yf_fPj.d.cts} +120 -1
  39. package/{entity-config-DoUCXtq5.d.mts → entity-config-C4Yf_fPj.d.mts} +120 -1
  40. package/entity-config-CsLl-ftC.cjs +727 -0
  41. package/entity-config-D3LmzgLb.mjs +612 -0
  42. package/index.cjs +764 -120
  43. package/index.d.cts +12 -0
  44. package/index.d.mts +12 -0
  45. package/index.mjs +765 -121
  46. package/internal-B8ydhsKd.d.cts +73 -0
  47. package/internal-BARjyN4n.cjs +97 -0
  48. package/internal-DMySVbM0.mjs +68 -0
  49. package/internal-MBkNo3bw.d.mts +73 -0
  50. package/internal-drizzle.cjs +3 -1
  51. package/internal-drizzle.d.cts +2 -2
  52. package/internal-drizzle.d.mts +2 -2
  53. package/internal-drizzle.mjs +2 -2
  54. package/internal-entity-config.cjs +1 -1
  55. package/internal-entity-config.d.cts +2 -2
  56. package/internal-entity-config.d.mts +2 -2
  57. package/internal-entity-config.mjs +1 -1
  58. package/internal-entity-heuristics.cjs +28 -8
  59. package/internal-entity-heuristics.d.cts +2 -12
  60. package/internal-entity-heuristics.d.mts +2 -12
  61. package/internal-entity-heuristics.mjs +28 -8
  62. package/internal-mapping-form.d.cts +1 -1
  63. package/internal-mapping-form.d.mts +1 -1
  64. package/internal-mappings-config.cjs +1 -1
  65. package/internal-mappings-config.d.cts +1 -1
  66. package/internal-mappings-config.d.mts +1 -1
  67. package/internal-mappings-config.mjs +1 -1
  68. package/internal-sync-engine.cjs +1 -1
  69. package/internal-sync-engine.d.cts +1 -1
  70. package/internal-sync-engine.d.mts +1 -1
  71. package/internal-sync-engine.mjs +1 -1
  72. package/internal-view-compiler.cjs +1 -1
  73. package/internal-view-compiler.d.cts +1 -1
  74. package/internal-view-compiler.d.mts +1 -1
  75. package/internal-view-compiler.mjs +1 -1
  76. package/internal.cjs +21 -4
  77. package/internal.d.cts +7 -6
  78. package/internal.d.mts +7 -6
  79. package/internal.mjs +7 -5
  80. package/{mapping-form-D93lhHEc.d.mts → mapping-form-43bwOTat.d.cts} +1 -1
  81. package/{mapping-form-lfyEE7EJ.d.cts → mapping-form-DUvWUYCo.d.mts} +1 -1
  82. package/{mappings-config-BsRADNRv.cjs → mappings-config-BSSwdHiq.cjs} +32 -6
  83. package/mappings-config-BpcR8bBo.d.mts +167 -0
  84. package/{mappings-config-Dbgxx3Nv.mjs → mappings-config-bJQT_gSa.mjs} +32 -6
  85. package/mappings-config-xsLMn3I-.d.cts +167 -0
  86. package/namespace-probe-BSH_p9TK.d.cts +381 -0
  87. package/namespace-probe-Dsp5uNkS.d.mts +381 -0
  88. package/package.json +1 -1
  89. package/{entity-config-va2bkH5o.mjs → projection-C5244vGa.cjs} +130 -309
  90. package/{entity-config-D87rV-tE.cjs → projection-D7xmQP4S.mjs} +60 -381
  91. package/{sdk-iqOXeWIp.cjs → sdk-Bj-ZDf4u.cjs} +126 -28
  92. package/{sdk-C6WANW8M.mjs → sdk-BvRD2jAp.mjs} +126 -28
  93. package/sdk.cjs +2 -2
  94. package/sdk.d.cts +36 -1
  95. package/sdk.d.mts +36 -1
  96. package/sdk.mjs +2 -2
  97. package/{sync-engine-Do5ru3WF.mjs → sync-engine-Bg6-SznL.mjs} +9 -2
  98. package/{sync-engine-CzEbpQGv.cjs → sync-engine-DEJvKw6E.cjs} +9 -2
  99. package/{sync-engine-DGk_AeHJ.d.mts → sync-engine-GNqRB8eS.d.cts} +2 -2
  100. package/{sync-engine-B7QIbXro.d.cts → sync-engine-ZsusW-q_.d.mts} +2 -2
  101. package/{view-compiler-BWz0sGdm.d.cts → view-compiler-Cin0neDO.d.mts} +1 -1
  102. package/{view-compiler-ChWr_Lml.mjs → view-compiler-DlTpOfZi.mjs} +81 -12
  103. package/{view-compiler-BIwvm-o2.cjs → view-compiler-DplczSjF.cjs} +81 -12
  104. package/{view-compiler-Bn_EJJMO.d.mts → view-compiler-Ll9cbIJz.d.cts} +1 -1
  105. package/vite.d.cts +1 -1
  106. package/vite.d.mts +1 -1
  107. package/drizzle-B89tLo4T.mjs +0 -31
  108. package/drizzle-CFOr9dU9.d.mts +0 -20
  109. package/drizzle-DiReZf6d.cjs +0 -36
  110. package/drizzle-DmeKSqeF.d.cts +0 -20
  111. package/mappings-config-DsUa7-1w.d.mts +0 -41
  112. package/mappings-config-XmzB4dag.d.cts +0 -41
  113. package/types-CsL0rFYh.d.cts +0 -155
  114. package/types-CsL0rFYh.d.mts +0 -155
@@ -4203,398 +4203,77 @@ function superRefine(fn, params) {
4203
4203
  return /* @__PURE__ */ _superRefine(fn, params);
4204
4204
  }
4205
4205
  //#endregion
4206
- //#region ../core/src/entity-config.ts
4206
+ //#region ../core/src/projection.ts
4207
4207
  /**
4208
- * Entity Config v2 — the declarative contract that turns Clivly's "schema-aware"
4209
- * promise into a real capability.
4208
+ * Projection-backed custom mappings — one custom object derived from a base
4209
+ * table plus explicit joins.
4210
4210
  *
4211
- * A CRM entity is no longer a flat table name. It is a *derived entity*: a
4212
- * filtered slice of a host table (`source` + `filter`), a column allowlist
4213
- * (`fields` + `readOnly`), and declared `relationships` to other entities. The
4214
- * same host table can back several entities (e.g. `users` → owners + members)
4215
- * distinguished by `role`.
4211
+ * A projection is deliberately narrow: equality joins only, direct column
4212
+ * bindings only, no expressions and no aggregation. Everything a user can
4213
+ * express here compiles to a single read-only view whose `id` column is
4214
+ * provably unique per row, which is what lets the sync engine consume it
4215
+ * unchanged.
4216
4216
  *
4217
- * This module is the Phase 1 slice: types, schema, `defineClivlyConfig` (a
4218
- * structural helper) and `validateEntitiesConfig` (semantic validation against
4219
- * the host's discovered schema). It performs NO DB access and generates no SQL —
4220
- * the view compiler (Phase 2) and sync engine (Phase 3) consume this contract.
4217
+ * Refs address tables symbolically: `"base"` for the base table, or a join's
4218
+ * `key`. Raw table names are never accepted in a ref, so a projection can
4219
+ * never reach a table it did not declare.
4221
4220
  */
4222
- /** Canonical CRM concepts a host entity can map onto. */
4223
- const CRM_CONCEPTS = [
4224
- "contact",
4225
- "company",
4226
- "deal",
4227
- "activity",
4228
- "conversation",
4229
- "note"
4230
- ];
4231
- /**
4232
- * The canonical `fields` keys the mirror actually persists, per concept — the
4233
- * crm_* columns the sync store writes. An entity's `fields` map keys must be one
4234
- * of these: a non-canonical key (e.g. `fullName` instead of `name`) validates
4235
- * structurally but has no destination column, so the store silently writes an
4236
- * empty value. `validateEntitiesConfig` flags those.
4237
- *
4238
- * This is the single source of truth — the Drizzle store imports it, so the
4239
- * two can't drift (the drift is what made the silent-data-loss trap possible).
4240
- * Concepts absent here aren't materialized yet, so their field keys are
4241
- * unconstrained.
4242
- */
4243
- const CANONICAL_FIELDS = {
4244
- contact: [
4245
- "name",
4246
- "email",
4247
- "title",
4248
- "status"
4249
- ],
4250
- company: [
4251
- "name",
4252
- "domain",
4253
- "industry",
4254
- "size"
4255
- ],
4256
- deal: [
4257
- "name",
4258
- "value",
4259
- "stage"
4260
- ]
4261
- };
4262
- /**
4263
- * The concepts the mapping→sync pipeline can actually materialize into `crm_*`
4264
- * tables. These are exactly the CANONICAL_FIELDS keys — the Drizzle sync store's
4265
- * `persist()` throws for any other concept, so offering one (in the mapping
4266
- * editor, in suggestions, or via the upsert API) is a broken affordance.
4267
- *
4268
- * The broader CRM_CONCEPTS set still validates structurally, so previously
4269
- * stored rows for not-yet-materialized concepts keep rendering. When a concept
4270
- * gains a sync-store path, add its canonical fields above and it joins this set
4271
- * automatically (guarded by the MATERIALIZING_CONCEPTS drift test).
4272
- */
4273
- const MATERIALIZING_CONCEPTS = [
4274
- "contact",
4275
- "company",
4276
- "deal"
4277
- ];
4278
- /**
4279
- * The org-defined custom-object target. Deliberately NOT a member of
4280
- * CRM_CONCEPTS / CANONICAL_FIELDS / MATERIALIZING_CONCEPTS — a custom object has
4281
- * no fixed canonical columns; its mapping's fieldMap keys ARE its properties.
4282
- * Handled as an explicit branch wherever concepts are switched on, so the
4283
- * canonical-concept invariants (and their drift test) stay intact.
4284
- */
4285
- const CUSTOM_CONCEPT = "custom";
4286
- /**
4287
- * Operator forms for a single column predicate. All predicates on an entity are
4288
- * AND-ed together. `$raw` (see FilterExpr) is the escape hatch for anything the
4289
- * operators can't express.
4290
- */
4291
- const filterOperatorsSchema = union([
4292
- strictObject({ eq: union([
4293
- string(),
4294
- number(),
4295
- boolean()
4296
- ]) }),
4297
- strictObject({ ne: union([
4298
- string(),
4299
- number(),
4300
- boolean()
4301
- ]) }),
4302
- strictObject({ in: array(union([
4303
- string(),
4304
- number(),
4305
- boolean()
4306
- ])).min(1) }),
4307
- strictObject({ notIn: array(union([
4308
- string(),
4309
- number(),
4310
- boolean()
4311
- ])).min(1) }),
4312
- strictObject({ isNull: literal(true) }),
4313
- strictObject({ isNotNull: literal(true) })
4314
- ]);
4315
- const filterValueSchema = union([
4316
- string(),
4317
- number(),
4318
- boolean(),
4319
- filterOperatorsSchema
4320
- ]);
4321
- /** `$raw` is a reserved key; a column literally named `$raw` is not addressable. */
4322
- const filterSchema = union([strictObject({ $raw: string().min(1) }), record(string(), filterValueSchema)]);
4323
- /**
4324
- * How the link between two entities is expressed in the host schema:
4325
- * - `fkOn: "company"` — FK lives on the company table (e.g. organizations.owner_id)
4326
- * - `fkOn: "contact"` — FK lives on the contact table (e.g. users.organization_id)
4327
- * - `through` — a join table sits between them (many-to-many, role often here)
4328
- * - `$raw` — opaque SQL join fragment (escape hatch; not schema-validated)
4329
- *
4330
- * `column` and the join-table keys are `table.column` or bare `column` strings.
4331
- */
4332
- const relationshipViaSchema = union([
4221
+ /** The reserved ref name for the projection's base table. */
4222
+ const BASE_REF = "base";
4223
+ /** Entity keys become view names (`crm_src_<entityKey>`), so keep them slugs. */
4224
+ const PROJECTION_KEY_PATTERN = /^[a-z][a-z0-9_]*$/;
4225
+ const projectionRefSchema = strictObject({
4226
+ table: string().min(1),
4227
+ column: string().min(1)
4228
+ });
4229
+ const projectionJoinConditionSchema = strictObject({
4230
+ left: projectionRefSchema,
4231
+ op: literal("eq"),
4232
+ right: projectionRefSchema
4233
+ });
4234
+ const projectionJoinSchema = strictObject({
4235
+ /** SQL alias and ref target. Unique within the projection. */
4236
+ key: string().regex(PROJECTION_KEY_PATTERN),
4237
+ table: string().min(1),
4238
+ type: _enum(["inner", "left"]),
4239
+ on: array(projectionJoinConditionSchema).min(1)
4240
+ });
4241
+ const projectionFieldBindingSchema = strictObject({ from: projectionRefSchema });
4242
+ const projectionIdentitySchema = union([
4333
4243
  strictObject({
4334
- fkOn: _enum(["company", "contact"]),
4335
- column: string().min(1)
4244
+ kind: literal("base_pk"),
4245
+ /** Defaults to the discovered single-column PK of the base table. */
4246
+ column: string().min(1).optional()
4336
4247
  }),
4337
4248
  strictObject({
4338
- through: string().min(1),
4339
- localKey: string().min(1),
4340
- foreignKey: string().min(1)
4249
+ kind: literal("column"),
4250
+ from: projectionRefSchema
4341
4251
  }),
4342
- strictObject({ $raw: string().min(1) })
4252
+ strictObject({
4253
+ kind: literal("composite"),
4254
+ parts: array(projectionRefSchema).min(2)
4255
+ })
4343
4256
  ]);
4344
- const relationshipSchema = strictObject({
4345
- /** Key of the related entity in the `entities` map. */
4346
- entity: string().min(1),
4347
- via: relationshipViaSchema
4257
+ const projectionSchema = strictObject({
4258
+ version: literal(1),
4259
+ kind: literal("projection"),
4260
+ entityKey: string().regex(PROJECTION_KEY_PATTERN),
4261
+ baseTable: string().min(1),
4262
+ joins: array(projectionJoinSchema),
4263
+ fieldBindings: record(string(), projectionFieldBindingSchema).refine((b) => Object.keys(b).length > 0, { message: "at least one field must be bound" }),
4264
+ identity: projectionIdentitySchema
4348
4265
  });
4349
- /** A named map of relationships, as stored on an entity / in the mappings row. */
4350
- const relationshipsSchema = record(string(), relationshipSchema);
4351
- const entitySchema = strictObject({
4352
- /** Which CRM concept this entity maps onto — a canonical concept or "custom". */
4353
- concept: union([_enum(CRM_CONCEPTS), literal(CUSTOM_CONCEPT)]),
4354
- /** Host table (or view) this entity is derived from. */
4355
- source: string().min(1),
4356
- /**
4357
- * Distinguishes multiple entities backed by the same table (e.g. "primary"
4358
- * for owners, "secondary" for members). Free text; defaults to "primary".
4359
- */
4360
- role: string().min(1).optional(),
4361
- /** Predicate that narrows `source` into this entity's rows. Omit = whole table. */
4362
- filter: filterSchema.optional(),
4363
- /** clivlyField → sourceColumn. At least one field is required. */
4364
- fields: record(string(), string()).refine((f) => Object.keys(f).length > 0, { message: "at least one field must be mapped" }),
4365
- /**
4366
- * Host-owned identity fields. Sync overwrites them; the CRM UI cannot edit
4367
- * them. Must be a subset of `fields` keys.
4368
- */
4369
- readOnly: array(string()).optional(),
4370
- /** Named relationships to other entities in this config. */
4371
- relationships: record(string(), relationshipSchema).optional(),
4372
- /**
4373
- * For a "custom" entity, the crm_object_types row this materializes into.
4374
- * Required for custom (enforced at the API layer), unused for standard concepts.
4375
- */
4376
- targetObjectTypeId: string().min(1).optional()
4377
- });
4378
- const entitiesConfigSchema = strictObject({ entities: record(string(), entitySchema).refine((e) => Object.keys(e).length > 0, { message: "at least one entity must be defined" }) });
4379
- /** Thrown by `defineClivlyConfig({ strict: true })` on non-canonical field keys. */
4380
- var ClivlyConfigError = class extends Error {
4381
- issues;
4382
- constructor(issues) {
4383
- 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")}`);
4384
- this.name = "ClivlyConfigError";
4385
- this.issues = issues;
4386
- }
4387
- };
4388
- function nonCanonicalFieldKeys(entity) {
4389
- const canonical = CANONICAL_FIELDS[entity.concept];
4390
- if (!canonical) return [];
4391
- return Object.keys(entity.fields).filter((key) => !canonical.includes(key));
4392
- }
4393
- function nonCanonicalMessage(entity, field) {
4394
- const canonical = CANONICAL_FIELDS[entity.concept] ?? [];
4395
- return `"${field}" is not a canonical ${entity.concept} field (expected one of: ${canonical.join(", ")})`;
4396
- }
4397
- function canonicalFieldIssues(config) {
4398
- const issues = [];
4399
- for (const [key, entity] of Object.entries(config.entities)) for (const field of nonCanonicalFieldKeys(entity)) issues.push({
4400
- path: `entities.${key}.fields.${field}`,
4401
- message: nonCanonicalMessage(entity, field)
4402
- });
4403
- return issues;
4404
- }
4405
- function defaultWarn(message) {
4406
- console.warn(message);
4407
- }
4408
- /**
4409
- * Structural helper for `clivly.config.ts`. Gives full type inference on the
4410
- * literal you pass and eagerly parses it, so shape errors surface at config
4411
- * load rather than at sync time.
4412
- *
4413
- * It also catches the silent-data-loss footgun: a non-canonical field key for a
4414
- * materialised concept (e.g. `fullName` instead of `name` on a `contact`) has no
4415
- * destination column, so the store would write an empty value. By default these
4416
- * are reported via `onWarn` (a `console.warn`); pass `{ strict: true }` to throw
4417
- * a `ClivlyConfigError` instead. Does NOT validate against the host schema — that
4418
- * needs the discovered schema; see `validateEntitiesConfig`.
4419
- */
4420
- function defineClivlyConfig(config, options) {
4421
- const parsed = entitiesConfigSchema.parse(config);
4422
- const issues = canonicalFieldIssues(parsed);
4423
- if (issues.length > 0) {
4424
- if (options?.strict) throw new ClivlyConfigError(issues);
4425
- const warn = options?.onWarn ?? defaultWarn;
4426
- 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.`);
4427
- }
4428
- return parsed;
4429
- }
4430
- /** Split a `table.column` / bare `column` ref into its parts. */
4431
- function parseColumnRef(ref) {
4432
- const dot = ref.indexOf(".");
4433
- if (dot === -1) return { column: ref };
4434
- return {
4435
- table: ref.slice(0, dot),
4436
- column: ref.slice(dot + 1)
4437
- };
4438
- }
4439
- /** Shared lookup + error-collection state threaded through the validators. */
4440
- var SchemaValidator = class {
4441
- errors = [];
4442
- columnsByTable = /* @__PURE__ */ new Map();
4443
- constructor(schema) {
4444
- for (const table of schema) this.columnsByTable.set(table.name, new Set(table.columns));
4445
- }
4446
- tableExists(name) {
4447
- return this.columnsByTable.has(name);
4448
- }
4449
- columnExists(table, column) {
4450
- return this.columnsByTable.get(table)?.has(column) ?? false;
4451
- }
4452
- report(path, message) {
4453
- this.errors.push({
4454
- path,
4455
- message
4456
- });
4457
- }
4458
- /** Report if `column` is absent from `table`. Returns whether it existed. */
4459
- requireColumn(path, table, column) {
4460
- if (this.columnExists(table, column)) return true;
4461
- this.report(path, `column "${column}" does not exist on "${table}"`);
4462
- return false;
4463
- }
4464
- };
4465
- function validateFields(v, base, entity) {
4466
- for (const [field, column] of Object.entries(entity.fields)) v.requireColumn(`${base}.fields.${field}`, entity.source, column);
4467
- for (const field of nonCanonicalFieldKeys(entity)) v.report(`${base}.fields.${field}`, nonCanonicalMessage(entity, field));
4468
- }
4469
- function validateReadOnly(v, base, entity) {
4470
- for (const field of entity.readOnly ?? []) if (!(field in entity.fields)) v.report(`${base}.readOnly`, `"${field}" is not one of this entity's fields`);
4471
- }
4472
- function validateFilter(v, base, entity) {
4473
- if (!entity.filter || "$raw" in entity.filter) return;
4474
- for (const column of Object.keys(entity.filter)) v.requireColumn(`${base}.filter.${column}`, entity.source, column);
4475
- }
4476
- function validateFkVia(v, relBase, via, entity, related) {
4477
- const { table, column } = parseColumnRef(via.column);
4478
- const fkTable = table ?? (via.fkOn === "contact" ? entity.source : related?.source);
4479
- const path = `${relBase}.via.column`;
4480
- if (!fkTable) {
4481
- v.report(path, `cannot resolve table for FK column "${via.column}"`);
4482
- return;
4483
- }
4484
- if (!v.tableExists(fkTable)) {
4485
- v.report(path, `table "${fkTable}" is not in the discovered schema`);
4486
- return;
4487
- }
4488
- v.requireColumn(path, fkTable, column);
4489
- }
4490
- function validateThroughVia(v, relBase, via) {
4491
- if (!v.tableExists(via.through)) {
4492
- v.report(`${relBase}.via.through`, `join table "${via.through}" is not in the discovered schema`);
4493
- return;
4494
- }
4495
- v.requireColumn(`${relBase}.via.localKey`, via.through, via.localKey);
4496
- v.requireColumn(`${relBase}.via.foreignKey`, via.through, via.foreignKey);
4497
- }
4498
- function validateRelationship(v, base, config, entity, relName, rel) {
4499
- const relBase = `${base}.relationships.${relName}`;
4500
- const related = config.entities[rel.entity];
4501
- if (!related) v.report(`${relBase}.entity`, `"${rel.entity}" is not a defined entity`);
4502
- const { via } = rel;
4503
- if ("fkOn" in via) validateFkVia(v, relBase, via, entity, related);
4504
- else if ("through" in via) validateThroughVia(v, relBase, via);
4505
- }
4506
- function validateEntity(v, config, key, entity) {
4507
- const base = `entities.${key}`;
4508
- if (!v.tableExists(entity.source)) {
4509
- v.report(`${base}.source`, `table "${entity.source}" is not in the discovered schema`);
4510
- return;
4511
- }
4512
- validateFields(v, base, entity);
4513
- validateReadOnly(v, base, entity);
4514
- validateFilter(v, base, entity);
4515
- for (const [relName, rel] of Object.entries(entity.relationships ?? {})) validateRelationship(v, base, config, entity, relName, rel);
4516
- }
4517
4266
  /**
4518
- * Validate a (structurally valid) config against the host's discovered schema:
4519
- * source tables exist, mapped columns exist, `readOnly` ⊆ `fields`, filter
4520
- * columns exist, and relationships point at real entities/tables/columns.
4521
- *
4522
- * `$raw` filters and `$raw` relationship joins are opaque and skipped. Returns
4523
- * every error found (not just the first) so a mapping UI or CLI can show them
4524
- * all at once.
4267
+ * Every column ref an identity depends on. `basePkColumn` is the base table's
4268
+ * discovered PK, used only when a `base_pk` identity omits an explicit column.
4525
4269
  */
4526
- function validateEntitiesConfig(config, schema) {
4527
- const validator = new SchemaValidator(schema);
4528
- for (const [key, entity] of Object.entries(config.entities)) validateEntity(validator, config, key, entity);
4529
- return {
4530
- valid: validator.errors.length === 0,
4531
- errors: validator.errors
4532
- };
4270
+ function identityRefs(identity, basePkColumn) {
4271
+ if (identity.kind === "base_pk") return [{
4272
+ table: BASE_REF,
4273
+ column: identity.column ?? basePkColumn
4274
+ }];
4275
+ if (identity.kind === "column") return [identity.from];
4276
+ return identity.parts;
4533
4277
  }
4534
4278
  //#endregion
4535
- Object.defineProperty(exports, "CANONICAL_FIELDS", {
4536
- enumerable: true,
4537
- get: function() {
4538
- return CANONICAL_FIELDS;
4539
- }
4540
- });
4541
- Object.defineProperty(exports, "CRM_CONCEPTS", {
4542
- enumerable: true,
4543
- get: function() {
4544
- return CRM_CONCEPTS;
4545
- }
4546
- });
4547
- Object.defineProperty(exports, "CUSTOM_CONCEPT", {
4548
- enumerable: true,
4549
- get: function() {
4550
- return CUSTOM_CONCEPT;
4551
- }
4552
- });
4553
- Object.defineProperty(exports, "ClivlyConfigError", {
4554
- enumerable: true,
4555
- get: function() {
4556
- return ClivlyConfigError;
4557
- }
4558
- });
4559
- Object.defineProperty(exports, "MATERIALIZING_CONCEPTS", {
4560
- enumerable: true,
4561
- get: function() {
4562
- return MATERIALIZING_CONCEPTS;
4563
- }
4564
- });
4565
- Object.defineProperty(exports, "defineClivlyConfig", {
4566
- enumerable: true,
4567
- get: function() {
4568
- return defineClivlyConfig;
4569
- }
4570
- });
4571
- Object.defineProperty(exports, "entitiesConfigSchema", {
4572
- enumerable: true,
4573
- get: function() {
4574
- return entitiesConfigSchema;
4575
- }
4576
- });
4577
- Object.defineProperty(exports, "filterSchema", {
4578
- enumerable: true,
4579
- get: function() {
4580
- return filterSchema;
4581
- }
4582
- });
4583
- Object.defineProperty(exports, "relationshipSchema", {
4584
- enumerable: true,
4585
- get: function() {
4586
- return relationshipSchema;
4587
- }
4588
- });
4589
- Object.defineProperty(exports, "relationshipsSchema", {
4590
- enumerable: true,
4591
- get: function() {
4592
- return relationshipsSchema;
4593
- }
4594
- });
4595
- Object.defineProperty(exports, "validateEntitiesConfig", {
4596
- enumerable: true,
4597
- get: function() {
4598
- return validateEntitiesConfig;
4599
- }
4600
- });
4279
+ export { _enum as a, literal as c, strictObject as d, string as f, projectionSchema as i, number as l, PROJECTION_KEY_PATTERN as n, array as o, union as p, identityRefs as r, boolean as s, BASE_REF as t, record as u };
@@ -276,6 +276,46 @@ function errorMessage(error) {
276
276
  }
277
277
  //#endregion
278
278
  //#region ../sdk/src/doctor.ts
279
+ const TEMPORAL_CURSOR_TYPES = [
280
+ "timestamp",
281
+ "timestamptz",
282
+ "datetime",
283
+ "date",
284
+ "time"
285
+ ];
286
+ /**
287
+ * Warn when the sync cursor cannot observe an update.
288
+ *
289
+ * `clivly init` falls back to the primary key when a table has no
290
+ * `updated_at`-like column, and emits a TODO saying "confirm it is monotonic".
291
+ * A serial `id` *is* monotonic, so the note reads as satisfied — but monotonic
292
+ * is only half the requirement. An `id` never changes, so every insert mirrors
293
+ * and **no edit ever does**: a contact who changes their email keeps the old one
294
+ * in the CRM forever, with nothing anywhere reporting a problem.
295
+ *
296
+ * A code comment could not enforce this; a real integrator shipped exactly this
297
+ * configuration to production. This is the check that says so out loud.
298
+ */
299
+ function cursorMonotonicityCheck(source) {
300
+ const meta = source[require_drizzle_meta.DRIZZLE_META];
301
+ if (!meta?.columnsMeta) return null;
302
+ const index = meta.columnKeys.indexOf(meta.cursorField);
303
+ const type = meta.columnsMeta[index]?.type?.toLowerCase();
304
+ if (!type) return null;
305
+ const temporal = TEMPORAL_CURSOR_TYPES.some((t) => type.startsWith(t));
306
+ const name = `Cursor tracks updates (${source.entity})`;
307
+ if (temporal) return {
308
+ name,
309
+ ok: true,
310
+ detail: `"${source.entity}" pages on ${meta.cursorField} (${type}) — edits re-sync.`
311
+ };
312
+ return {
313
+ name,
314
+ ok: true,
315
+ warn: true,
316
+ detail: `"${source.entity}" pages on ${meta.cursorField} (${type}), which does not change when a row is edited — inserts will sync, edits will not. Add an updated_at column to "${meta.tableName ?? source.entity}" and set it as the cursor, or accept insert-only mirroring.`
317
+ };
318
+ }
279
319
  function configuredSources(config) {
280
320
  const sources = [];
281
321
  if (config.source?.contacts) sources.push(config.source.contacts);
@@ -304,32 +344,12 @@ async function mappingDryRunChecks(config) {
304
344
  return checks;
305
345
  }
306
346
  /**
307
- * Run the read-only setup checks and return a structured report. Every check
308
- * captures its own error into `detail` rather than throwing, so one failure never
309
- * hides the rest. `ok` is the AND of all non-skipped checks.
347
+ * Confirm each drizzle source actually has the cursor and id columns the sync
348
+ * pages on. Hand-written sources have no column metadata, so they are skipped
349
+ * rather than failed.
310
350
  */
311
- async function runDoctor(args) {
312
- const { config, connect } = args;
351
+ function cursorColumnChecks(sources) {
313
352
  const checks = [];
314
- const sources = configuredSources(config);
315
- const hasKey = Boolean(config.apiKey);
316
- checks.push({
317
- name: "API key present",
318
- ok: hasKey,
319
- detail: hasKey ? "CLIVLY_API_KEY is set." : "apiKey is empty — set CLIVLY_API_KEY."
320
- });
321
- const entityCount = config.entities ? Object.keys(config.entities.entities).length : 0;
322
- checks.push({
323
- name: "Entities configured",
324
- ok: entityCount > 0,
325
- detail: entityCount > 0 ? `${entityCount} entit${entityCount === 1 ? "y" : "ies"} configured.` : "No `entities` config — pass defineClivlyConfig(...) as `entities`."
326
- });
327
- checks.push({
328
- name: "Sources configured",
329
- ok: sources.length > 0,
330
- detail: sources.length > 0 ? `${sources.length} source(s) to mirror.` : "No `source.contacts` — nothing to sync."
331
- });
332
- checks.push(checkConceptCoverage(config));
333
353
  for (const source of sources) {
334
354
  const meta = source[require_drizzle_meta.DRIZZLE_META];
335
355
  if (!meta) {
@@ -341,7 +361,7 @@ async function runDoctor(args) {
341
361
  });
342
362
  continue;
343
363
  }
344
- const idField = source.idField ?? "id";
364
+ const idField = meta.idField ?? source.idField ?? "id";
345
365
  const missing = [meta.cursorField, idField].filter((field) => !meta.columnKeys.includes(field));
346
366
  checks.push({
347
367
  name: `Cursor/id columns exist (${source.entity})`,
@@ -349,6 +369,14 @@ async function runDoctor(args) {
349
369
  detail: missing.length === 0 ? `"${source.entity}": ${meta.cursorField} + ${idField} present.` : `"${source.entity}" missing column(s): ${missing.join(", ")}.`
350
370
  });
351
371
  }
372
+ return checks;
373
+ }
374
+ /**
375
+ * Read one row from each source to prove the query works and that rows actually
376
+ * carry the cursor key. Fetch errors are captured as failures, never thrown.
377
+ */
378
+ async function sampleFetchChecks(sources) {
379
+ const checks = [];
352
380
  for (const source of sources) try {
353
381
  const rows = await source.fetchPage({
354
382
  since: null,
@@ -368,10 +396,45 @@ async function runDoctor(args) {
368
396
  detail: describeFetchError(source.entity, error)
369
397
  });
370
398
  }
399
+ return checks;
400
+ }
401
+ /**
402
+ * Run the read-only setup checks and return a structured report. Every check
403
+ * captures its own error into `detail` rather than throwing, so one failure never
404
+ * hides the rest. `ok` is the AND of all non-skipped checks.
405
+ */
406
+ async function runDoctor(args) {
407
+ const { config, probe } = args;
408
+ const checks = [];
409
+ const sources = configuredSources(config);
410
+ const hasKey = Boolean(config.apiKey);
411
+ checks.push({
412
+ name: "API key present",
413
+ ok: hasKey,
414
+ detail: hasKey ? "CLIVLY_API_KEY is set." : "apiKey is empty — set CLIVLY_API_KEY."
415
+ });
416
+ const entityCount = config.entities ? Object.keys(config.entities.entities).length : 0;
417
+ checks.push({
418
+ name: "Entities configured",
419
+ ok: entityCount > 0,
420
+ detail: entityCount > 0 ? `${entityCount} entit${entityCount === 1 ? "y" : "ies"} configured.` : "No `entities` config — pass defineClivlyConfig(...) as `entities`."
421
+ });
422
+ checks.push({
423
+ name: "Sources configured",
424
+ ok: sources.length > 0,
425
+ detail: sources.length > 0 ? `${sources.length} source(s) to mirror.` : "No `source.contacts` — nothing to sync."
426
+ });
427
+ checks.push(checkConceptCoverage(config));
428
+ checks.push(...cursorColumnChecks(sources));
429
+ for (const source of sources) {
430
+ const check = cursorMonotonicityCheck(source);
431
+ if (check) checks.push(check);
432
+ }
433
+ checks.push(...await sampleFetchChecks(sources));
371
434
  checks.push(...await mappingDryRunChecks(config));
372
- const result = await connect();
435
+ const result = await probe();
373
436
  checks.push({
374
- name: "Heartbeat",
437
+ name: "Clivly reachable",
375
438
  ok: result.ok,
376
439
  detail: result.ok ? `Reached Clivly — org "${result.org?.name ?? "(unnamed)"}".` : `Could not connect (${result.reason}).`
377
440
  });
@@ -810,6 +873,7 @@ const UNAUTHORIZED_STATUS = 401;
810
873
  const FORBIDDEN_STATUS = 403;
811
874
  const CLIENT_ERROR_FLOOR = 400;
812
875
  const SERVICE_UNAVAILABLE_STATUS = 503;
876
+ const NOT_FOUND_STATUS = 404;
813
877
  function connectReason(status) {
814
878
  if (status === UNAUTHORIZED_STATUS) return "invalid_api_key";
815
879
  if (status === FORBIDDEN_STATUS) return "forbidden";
@@ -818,6 +882,10 @@ function connectReason(status) {
818
882
  if (status >= CLIENT_ERROR_FLOOR) return "client_error";
819
883
  return "server_error";
820
884
  }
885
+ function pingReason(status) {
886
+ if (status === NOT_FOUND_STATUS) return "server_too_old";
887
+ return connectReason(status);
888
+ }
821
889
  async function resolveOrg(response) {
822
890
  try {
823
891
  return (await response.json())?.org;
@@ -1049,9 +1117,38 @@ function createClivlySDK(config) {
1049
1117
  retryable: isRetryableStatus(response.status)
1050
1118
  };
1051
1119
  };
1120
+ const ping = async () => {
1121
+ const response = await fetcher(`${apiUrl}/clivly/sdk/ping`, {
1122
+ method: "GET",
1123
+ headers: { authorization: `Bearer ${config.apiKey}` }
1124
+ });
1125
+ if (response === null) return {
1126
+ ok: false,
1127
+ status: null,
1128
+ reason: "network_error",
1129
+ retryable: true
1130
+ };
1131
+ if (response.ok) {
1132
+ const body = await response.json().catch(() => ({}));
1133
+ return {
1134
+ ok: true,
1135
+ status: response.status,
1136
+ reason: "ok",
1137
+ retryable: false,
1138
+ org: body?.org,
1139
+ ..."appLastSeenAt" in body ? { appLastSeenAt: body.appLastSeenAt ?? null } : {}
1140
+ };
1141
+ }
1142
+ return {
1143
+ ok: false,
1144
+ status: response.status,
1145
+ reason: pingReason(response.status),
1146
+ retryable: isRetryableStatus(response.status)
1147
+ };
1148
+ };
1052
1149
  const doctor = () => runDoctor({
1053
1150
  config,
1054
- connect
1151
+ probe: ping
1055
1152
  });
1056
1153
  const runScheduled = async (options) => {
1057
1154
  await heartbeat();
@@ -1144,6 +1241,7 @@ function createClivlySDK(config) {
1144
1241
  stop,
1145
1242
  heartbeat,
1146
1243
  connect,
1244
+ ping,
1147
1245
  doctor,
1148
1246
  runScheduled,
1149
1247
  createClivlyHandler,