clivly 0.8.0 → 0.9.1
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.
- package/README.md +330 -134
- package/core-drizzle.cjs +3 -1
- package/core-drizzle.d.cts +3 -2
- package/core-drizzle.d.mts +3 -2
- package/core-drizzle.mjs +2 -2
- package/core-entity-config.cjs +2 -1
- package/core-entity-config.d.cts +2 -2
- package/core-entity-config.d.mts +2 -2
- package/core-entity-config.mjs +2 -2
- package/core-entity-heuristics.d.cts +3 -2
- package/core-entity-heuristics.d.mts +3 -2
- package/core-mapping-form.d.cts +1 -1
- package/core-mapping-form.d.mts +1 -1
- package/core-mappings-config.cjs +1 -1
- package/core-mappings-config.d.cts +1 -1
- package/core-mappings-config.d.mts +1 -1
- package/core-mappings-config.mjs +1 -1
- package/core-sync-engine.cjs +1 -1
- package/core-sync-engine.d.cts +1 -1
- package/core-sync-engine.d.mts +1 -1
- package/core-sync-engine.mjs +1 -1
- package/core-view-compiler.cjs +2 -1
- package/core-view-compiler.d.cts +2 -2
- package/core-view-compiler.d.mts +2 -2
- package/core-view-compiler.mjs +2 -2
- package/core.cjs +21 -5
- package/core.d.cts +7 -6
- package/core.d.mts +7 -6
- package/core.mjs +7 -6
- package/drizzle-3kvhkwO7.mjs +110 -0
- package/drizzle-DooDUGqO.d.mts +41 -0
- package/drizzle-bfjVzYR6.cjs +127 -0
- package/drizzle-jz0lrAgl.d.cts +41 -0
- package/drizzle.cjs +12 -40
- package/drizzle.d.cts +3 -11
- package/drizzle.d.mts +3 -11
- package/drizzle.mjs +12 -41
- package/entity-config-D08zAeFY.mjs +657 -0
- package/{entity-config-DoUCXtq5.d.cts → entity-config-fKQhimtD.d.cts} +263 -2
- package/{entity-config-DoUCXtq5.d.mts → entity-config-fKQhimtD.d.mts} +263 -2
- package/entity-config-nuXge6rY.cjs +778 -0
- package/index.cjs +768 -122
- package/index.d.cts +12 -0
- package/index.d.mts +12 -0
- package/index.mjs +769 -123
- package/internal-BGVUG312.mjs +68 -0
- package/internal-BLCK81kB.d.mts +72 -0
- package/internal-C-YBW2DB.d.cts +72 -0
- package/internal-D4HDi2GC.cjs +97 -0
- package/internal-drizzle.cjs +3 -1
- package/internal-drizzle.d.cts +2 -2
- package/internal-drizzle.d.mts +2 -2
- package/internal-drizzle.mjs +2 -2
- package/internal-entity-config.cjs +2 -1
- package/internal-entity-config.d.cts +2 -2
- package/internal-entity-config.d.mts +2 -2
- package/internal-entity-config.mjs +2 -2
- package/internal-entity-heuristics.cjs +28 -8
- package/internal-entity-heuristics.d.cts +2 -12
- package/internal-entity-heuristics.d.mts +2 -12
- package/internal-entity-heuristics.mjs +28 -8
- package/internal-mapping-form.d.cts +1 -1
- package/internal-mapping-form.d.mts +1 -1
- package/internal-mappings-config.cjs +1 -1
- package/internal-mappings-config.d.cts +1 -1
- package/internal-mappings-config.d.mts +1 -1
- package/internal-mappings-config.mjs +1 -1
- package/internal-sync-engine.cjs +1 -1
- package/internal-sync-engine.d.cts +1 -1
- package/internal-sync-engine.d.mts +1 -1
- package/internal-sync-engine.mjs +1 -1
- package/internal-view-compiler.cjs +2 -1
- package/internal-view-compiler.d.cts +2 -2
- package/internal-view-compiler.d.mts +2 -2
- package/internal-view-compiler.mjs +2 -2
- package/internal.cjs +21 -4
- package/internal.d.cts +7 -6
- package/internal.d.mts +7 -6
- package/internal.mjs +7 -5
- package/{mapping-form-D93lhHEc.d.mts → mapping-form-DhY0iFmN.d.cts} +1 -1
- package/{mapping-form-lfyEE7EJ.d.cts → mapping-form-Du36VZr1.d.mts} +1 -1
- package/{mappings-config-DsUa7-1w.d.mts → mappings-config-B1qNM8VL.d.cts} +7 -1
- package/{mappings-config-XmzB4dag.d.cts → mappings-config-BChZsany.d.mts} +7 -1
- package/{mappings-config-BsRADNRv.cjs → mappings-config-C8ynKmDK.cjs} +32 -6
- package/{mappings-config-Dbgxx3Nv.mjs → mappings-config-DNaYGyKN.mjs} +32 -6
- package/namespace-probe-BkUs8raG.d.mts +381 -0
- package/namespace-probe-aRFmpU85.d.cts +381 -0
- package/package.json +1 -1
- package/{entity-config-va2bkH5o.mjs → projection-C5244vGa.cjs} +130 -309
- package/{entity-config-D87rV-tE.cjs → projection-D7xmQP4S.mjs} +60 -381
- package/{sdk-iqOXeWIp.cjs → sdk-Bj-ZDf4u.cjs} +126 -28
- package/{sdk-C6WANW8M.mjs → sdk-BvRD2jAp.mjs} +126 -28
- package/sdk.cjs +2 -2
- package/sdk.d.cts +36 -1
- package/sdk.d.mts +36 -1
- package/sdk.mjs +2 -2
- package/{sync-engine-DGk_AeHJ.d.mts → sync-engine-DHQ7rXZe.d.mts} +2 -2
- package/{sync-engine-B7QIbXro.d.cts → sync-engine-DiPjH53O.d.cts} +2 -2
- package/{sync-engine-Do5ru3WF.mjs → sync-engine-SuxSzbZ4.mjs} +9 -2
- package/{sync-engine-CzEbpQGv.cjs → sync-engine-abE94g6X.cjs} +9 -2
- package/{view-compiler-BIwvm-o2.cjs → view-compiler-Blu4OXJ7.cjs} +103 -12
- package/{view-compiler-ChWr_Lml.mjs → view-compiler-CQx8TRrW.mjs} +98 -13
- package/{view-compiler-BWz0sGdm.d.cts → view-compiler-CcUwzZut.d.mts} +12 -2
- package/{view-compiler-Bn_EJJMO.d.mts → view-compiler-DqAdVxLq.d.cts} +12 -2
- package/vite.d.cts +1 -1
- package/vite.d.mts +1 -1
- package/drizzle-B89tLo4T.mjs +0 -31
- package/drizzle-CFOr9dU9.d.mts +0 -20
- package/drizzle-DiReZf6d.cjs +0 -36
- package/drizzle-DmeKSqeF.d.cts +0 -20
- package/types-CsL0rFYh.d.cts +0 -155
- 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/
|
|
4206
|
+
//#region ../core/src/projection.ts
|
|
4207
4207
|
/**
|
|
4208
|
-
*
|
|
4209
|
-
*
|
|
4208
|
+
* Projection-backed custom mappings — one custom object derived from a base
|
|
4209
|
+
* table plus explicit joins.
|
|
4210
4210
|
*
|
|
4211
|
-
* A
|
|
4212
|
-
*
|
|
4213
|
-
*
|
|
4214
|
-
*
|
|
4215
|
-
*
|
|
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
|
-
*
|
|
4218
|
-
*
|
|
4219
|
-
*
|
|
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
|
-
/**
|
|
4223
|
-
const
|
|
4224
|
-
|
|
4225
|
-
|
|
4226
|
-
|
|
4227
|
-
|
|
4228
|
-
|
|
4229
|
-
|
|
4230
|
-
|
|
4231
|
-
|
|
4232
|
-
|
|
4233
|
-
|
|
4234
|
-
|
|
4235
|
-
|
|
4236
|
-
|
|
4237
|
-
|
|
4238
|
-
|
|
4239
|
-
|
|
4240
|
-
|
|
4241
|
-
|
|
4242
|
-
|
|
4243
|
-
const
|
|
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
|
-
|
|
4335
|
-
column
|
|
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
|
-
|
|
4339
|
-
|
|
4340
|
-
foreignKey: string().min(1)
|
|
4249
|
+
kind: literal("column"),
|
|
4250
|
+
from: projectionRefSchema
|
|
4341
4251
|
}),
|
|
4342
|
-
strictObject({
|
|
4252
|
+
strictObject({
|
|
4253
|
+
kind: literal("composite"),
|
|
4254
|
+
parts: array(projectionRefSchema).min(2)
|
|
4255
|
+
})
|
|
4343
4256
|
]);
|
|
4344
|
-
const
|
|
4345
|
-
|
|
4346
|
-
|
|
4347
|
-
|
|
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
|
-
*
|
|
4519
|
-
*
|
|
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
|
|
4527
|
-
|
|
4528
|
-
|
|
4529
|
-
|
|
4530
|
-
|
|
4531
|
-
|
|
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
|
-
|
|
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
|
-
*
|
|
308
|
-
*
|
|
309
|
-
*
|
|
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
|
-
|
|
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
|
|
435
|
+
const result = await probe();
|
|
373
436
|
checks.push({
|
|
374
|
-
name: "
|
|
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
|
-
|
|
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,
|