@clivly/core 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.
- package/README.md +96 -15
- package/dist/drizzle.cjs +82 -1
- package/dist/drizzle.d.cts +23 -2
- package/dist/drizzle.d.mts +23 -2
- package/dist/drizzle.mjs +81 -2
- package/dist/entity-config-PoOGzvpN.cjs +728 -0
- package/dist/entity-config-Tb5bbKLj.mjs +621 -0
- package/dist/entity-config.cjs +12 -349
- package/dist/entity-config.d.cts +120 -1
- package/dist/entity-config.d.mts +120 -1
- package/dist/entity-config.mjs +1 -338
- package/dist/entity-heuristics.cjs +28 -8
- package/dist/entity-heuristics.d.cts +2 -12
- package/dist/entity-heuristics.d.mts +2 -12
- package/dist/entity-heuristics.mjs +28 -8
- package/dist/env-file.cjs +59 -0
- package/dist/env-file.d.cts +23 -0
- package/dist/env-file.d.mts +23 -0
- package/dist/env-file.mjs +55 -0
- package/dist/index.cjs +83 -2
- package/dist/index.d.cts +73 -3
- package/dist/index.d.mts +73 -3
- package/dist/index.mjs +69 -3
- package/dist/integration-state.cjs +242 -0
- package/dist/integration-state.d.cts +88 -0
- package/dist/integration-state.d.mts +88 -0
- package/dist/integration-state.mjs +232 -0
- package/dist/mappings-config-B0W5bVZj.d.mts +168 -0
- package/dist/mappings-config-BWo4cRDF.d.cts +168 -0
- package/dist/mappings-config.cjs +32 -6
- package/dist/mappings-config.d.cts +1 -40
- package/dist/mappings-config.d.mts +1 -40
- package/dist/mappings-config.mjs +32 -6
- package/dist/projection-BXBkh7qp.cjs +98 -0
- package/dist/projection-C_EMRmZ8.mjs +75 -0
- package/dist/sync-engine.cjs +9 -2
- package/dist/sync-engine.mjs +9 -2
- package/dist/view-compiler-BKrBA_II.mjs +254 -0
- package/dist/view-compiler-BwZ9UgLp.cjs +277 -0
- package/dist/view-compiler.cjs +5 -188
- package/dist/view-compiler.mjs +1 -184
- package/package.json +21 -1
package/dist/entity-config.mjs
CHANGED
|
@@ -1,339 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
//#region src/entity-config.ts
|
|
3
|
-
/**
|
|
4
|
-
* Entity Config v2 — the declarative contract that turns Clivly's "schema-aware"
|
|
5
|
-
* promise into a real capability.
|
|
6
|
-
*
|
|
7
|
-
* A CRM entity is no longer a flat table name. It is a *derived entity*: a
|
|
8
|
-
* filtered slice of a host table (`source` + `filter`), a column allowlist
|
|
9
|
-
* (`fields` + `readOnly`), and declared `relationships` to other entities. The
|
|
10
|
-
* same host table can back several entities (e.g. `users` → owners + members)
|
|
11
|
-
* distinguished by `role`.
|
|
12
|
-
*
|
|
13
|
-
* This module is the Phase 1 slice: types, schema, `defineClivlyConfig` (a
|
|
14
|
-
* structural helper) and `validateEntitiesConfig` (semantic validation against
|
|
15
|
-
* the host's discovered schema). It performs NO DB access and generates no SQL —
|
|
16
|
-
* the view compiler (Phase 2) and sync engine (Phase 3) consume this contract.
|
|
17
|
-
*/
|
|
18
|
-
/** Canonical CRM concepts a host entity can map onto. */
|
|
19
|
-
const CRM_CONCEPTS = [
|
|
20
|
-
"contact",
|
|
21
|
-
"company",
|
|
22
|
-
"deal",
|
|
23
|
-
"activity",
|
|
24
|
-
"conversation",
|
|
25
|
-
"note"
|
|
26
|
-
];
|
|
27
|
-
/**
|
|
28
|
-
* The canonical `fields` keys the mirror actually persists, per concept — the
|
|
29
|
-
* crm_* columns the sync store writes. An entity's `fields` map keys must be one
|
|
30
|
-
* of these: a non-canonical key (e.g. `fullName` instead of `name`) validates
|
|
31
|
-
* structurally but has no destination column, so the store silently writes an
|
|
32
|
-
* empty value. `validateEntitiesConfig` flags those.
|
|
33
|
-
*
|
|
34
|
-
* This is the single source of truth — the Drizzle store imports it, so the
|
|
35
|
-
* two can't drift (the drift is what made the silent-data-loss trap possible).
|
|
36
|
-
* Concepts absent here aren't materialized yet, so their field keys are
|
|
37
|
-
* unconstrained.
|
|
38
|
-
*/
|
|
39
|
-
const CANONICAL_FIELDS = {
|
|
40
|
-
contact: [
|
|
41
|
-
"name",
|
|
42
|
-
"email",
|
|
43
|
-
"title",
|
|
44
|
-
"status"
|
|
45
|
-
],
|
|
46
|
-
company: [
|
|
47
|
-
"name",
|
|
48
|
-
"domain",
|
|
49
|
-
"industry",
|
|
50
|
-
"size"
|
|
51
|
-
],
|
|
52
|
-
deal: [
|
|
53
|
-
"name",
|
|
54
|
-
"value",
|
|
55
|
-
"stage"
|
|
56
|
-
]
|
|
57
|
-
};
|
|
58
|
-
/**
|
|
59
|
-
* The concepts the mapping→sync pipeline can actually materialize into `crm_*`
|
|
60
|
-
* tables. These are exactly the CANONICAL_FIELDS keys — the Drizzle sync store's
|
|
61
|
-
* `persist()` throws for any other concept, so offering one (in the mapping
|
|
62
|
-
* editor, in suggestions, or via the upsert API) is a broken affordance.
|
|
63
|
-
*
|
|
64
|
-
* The broader CRM_CONCEPTS set still validates structurally, so previously
|
|
65
|
-
* stored rows for not-yet-materialized concepts keep rendering. When a concept
|
|
66
|
-
* gains a sync-store path, add its canonical fields above and it joins this set
|
|
67
|
-
* automatically (guarded by the MATERIALIZING_CONCEPTS drift test).
|
|
68
|
-
*/
|
|
69
|
-
const MATERIALIZING_CONCEPTS = [
|
|
70
|
-
"contact",
|
|
71
|
-
"company",
|
|
72
|
-
"deal"
|
|
73
|
-
];
|
|
74
|
-
/**
|
|
75
|
-
* The org-defined custom-object target. Deliberately NOT a member of
|
|
76
|
-
* CRM_CONCEPTS / CANONICAL_FIELDS / MATERIALIZING_CONCEPTS — a custom object has
|
|
77
|
-
* no fixed canonical columns; its mapping's fieldMap keys ARE its properties.
|
|
78
|
-
* Handled as an explicit branch wherever concepts are switched on, so the
|
|
79
|
-
* canonical-concept invariants (and their drift test) stay intact.
|
|
80
|
-
*/
|
|
81
|
-
const CUSTOM_CONCEPT = "custom";
|
|
82
|
-
/**
|
|
83
|
-
* Operator forms for a single column predicate. All predicates on an entity are
|
|
84
|
-
* AND-ed together. `$raw` (see FilterExpr) is the escape hatch for anything the
|
|
85
|
-
* operators can't express.
|
|
86
|
-
*/
|
|
87
|
-
const filterOperatorsSchema = z.union([
|
|
88
|
-
z.strictObject({ eq: z.union([
|
|
89
|
-
z.string(),
|
|
90
|
-
z.number(),
|
|
91
|
-
z.boolean()
|
|
92
|
-
]) }),
|
|
93
|
-
z.strictObject({ ne: z.union([
|
|
94
|
-
z.string(),
|
|
95
|
-
z.number(),
|
|
96
|
-
z.boolean()
|
|
97
|
-
]) }),
|
|
98
|
-
z.strictObject({ in: z.array(z.union([
|
|
99
|
-
z.string(),
|
|
100
|
-
z.number(),
|
|
101
|
-
z.boolean()
|
|
102
|
-
])).min(1) }),
|
|
103
|
-
z.strictObject({ notIn: z.array(z.union([
|
|
104
|
-
z.string(),
|
|
105
|
-
z.number(),
|
|
106
|
-
z.boolean()
|
|
107
|
-
])).min(1) }),
|
|
108
|
-
z.strictObject({ isNull: z.literal(true) }),
|
|
109
|
-
z.strictObject({ isNotNull: z.literal(true) })
|
|
110
|
-
]);
|
|
111
|
-
const filterValueSchema = z.union([
|
|
112
|
-
z.string(),
|
|
113
|
-
z.number(),
|
|
114
|
-
z.boolean(),
|
|
115
|
-
filterOperatorsSchema
|
|
116
|
-
]);
|
|
117
|
-
/**
|
|
118
|
-
* A raw SQL predicate fragment. Kept as a *string* (not a query-builder callback)
|
|
119
|
-
* so the whole config stays JSON-serializable — it can be persisted into
|
|
120
|
-
* `crm_entity_mappings` and shipped to the cloud unchanged. Embedded-only in
|
|
121
|
-
* practice; not validated against the schema.
|
|
122
|
-
*/
|
|
123
|
-
const rawFilterSchema = z.strictObject({ $raw: z.string().min(1) });
|
|
124
|
-
const columnFilterSchema = z.record(z.string(), filterValueSchema);
|
|
125
|
-
/** `$raw` is a reserved key; a column literally named `$raw` is not addressable. */
|
|
126
|
-
const filterSchema = z.union([rawFilterSchema, columnFilterSchema]);
|
|
127
|
-
/**
|
|
128
|
-
* How the link between two entities is expressed in the host schema:
|
|
129
|
-
* - `fkOn: "company"` — FK lives on the company table (e.g. organizations.owner_id)
|
|
130
|
-
* - `fkOn: "contact"` — FK lives on the contact table (e.g. users.organization_id)
|
|
131
|
-
* - `through` — a join table sits between them (many-to-many, role often here)
|
|
132
|
-
* - `$raw` — opaque SQL join fragment (escape hatch; not schema-validated)
|
|
133
|
-
*
|
|
134
|
-
* `column` and the join-table keys are `table.column` or bare `column` strings.
|
|
135
|
-
*/
|
|
136
|
-
const relationshipViaSchema = z.union([
|
|
137
|
-
z.strictObject({
|
|
138
|
-
fkOn: z.enum(["company", "contact"]),
|
|
139
|
-
column: z.string().min(1)
|
|
140
|
-
}),
|
|
141
|
-
z.strictObject({
|
|
142
|
-
through: z.string().min(1),
|
|
143
|
-
localKey: z.string().min(1),
|
|
144
|
-
foreignKey: z.string().min(1)
|
|
145
|
-
}),
|
|
146
|
-
z.strictObject({ $raw: z.string().min(1) })
|
|
147
|
-
]);
|
|
148
|
-
const relationshipSchema = z.strictObject({
|
|
149
|
-
/** Key of the related entity in the `entities` map. */
|
|
150
|
-
entity: z.string().min(1),
|
|
151
|
-
via: relationshipViaSchema
|
|
152
|
-
});
|
|
153
|
-
/** A named map of relationships, as stored on an entity / in the mappings row. */
|
|
154
|
-
const relationshipsSchema = z.record(z.string(), relationshipSchema);
|
|
155
|
-
const entitySchema = z.strictObject({
|
|
156
|
-
/** Which CRM concept this entity maps onto — a canonical concept or "custom". */
|
|
157
|
-
concept: z.union([z.enum(CRM_CONCEPTS), z.literal(CUSTOM_CONCEPT)]),
|
|
158
|
-
/** Host table (or view) this entity is derived from. */
|
|
159
|
-
source: z.string().min(1),
|
|
160
|
-
/**
|
|
161
|
-
* Distinguishes multiple entities backed by the same table (e.g. "primary"
|
|
162
|
-
* for owners, "secondary" for members). Free text; defaults to "primary".
|
|
163
|
-
*/
|
|
164
|
-
role: z.string().min(1).optional(),
|
|
165
|
-
/** Predicate that narrows `source` into this entity's rows. Omit = whole table. */
|
|
166
|
-
filter: filterSchema.optional(),
|
|
167
|
-
/** clivlyField → sourceColumn. At least one field is required. */
|
|
168
|
-
fields: z.record(z.string(), z.string()).refine((f) => Object.keys(f).length > 0, { message: "at least one field must be mapped" }),
|
|
169
|
-
/**
|
|
170
|
-
* Host-owned identity fields. Sync overwrites them; the CRM UI cannot edit
|
|
171
|
-
* them. Must be a subset of `fields` keys.
|
|
172
|
-
*/
|
|
173
|
-
readOnly: z.array(z.string()).optional(),
|
|
174
|
-
/** Named relationships to other entities in this config. */
|
|
175
|
-
relationships: z.record(z.string(), relationshipSchema).optional(),
|
|
176
|
-
/**
|
|
177
|
-
* For a "custom" entity, the crm_object_types row this materializes into.
|
|
178
|
-
* Required for custom (enforced at the API layer), unused for standard concepts.
|
|
179
|
-
*/
|
|
180
|
-
targetObjectTypeId: z.string().min(1).optional()
|
|
181
|
-
});
|
|
182
|
-
const entitiesConfigSchema = z.strictObject({ entities: z.record(z.string(), entitySchema).refine((e) => Object.keys(e).length > 0, { message: "at least one entity must be defined" }) });
|
|
183
|
-
/** Thrown by `defineClivlyConfig({ strict: true })` on non-canonical field keys. */
|
|
184
|
-
var ClivlyConfigError = class extends Error {
|
|
185
|
-
issues;
|
|
186
|
-
constructor(issues) {
|
|
187
|
-
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")}`);
|
|
188
|
-
this.name = "ClivlyConfigError";
|
|
189
|
-
this.issues = issues;
|
|
190
|
-
}
|
|
191
|
-
};
|
|
192
|
-
function nonCanonicalFieldKeys(entity) {
|
|
193
|
-
const canonical = CANONICAL_FIELDS[entity.concept];
|
|
194
|
-
if (!canonical) return [];
|
|
195
|
-
return Object.keys(entity.fields).filter((key) => !canonical.includes(key));
|
|
196
|
-
}
|
|
197
|
-
function nonCanonicalMessage(entity, field) {
|
|
198
|
-
const canonical = CANONICAL_FIELDS[entity.concept] ?? [];
|
|
199
|
-
return `"${field}" is not a canonical ${entity.concept} field (expected one of: ${canonical.join(", ")})`;
|
|
200
|
-
}
|
|
201
|
-
function canonicalFieldIssues(config) {
|
|
202
|
-
const issues = [];
|
|
203
|
-
for (const [key, entity] of Object.entries(config.entities)) for (const field of nonCanonicalFieldKeys(entity)) issues.push({
|
|
204
|
-
path: `entities.${key}.fields.${field}`,
|
|
205
|
-
message: nonCanonicalMessage(entity, field)
|
|
206
|
-
});
|
|
207
|
-
return issues;
|
|
208
|
-
}
|
|
209
|
-
function defaultWarn(message) {
|
|
210
|
-
console.warn(message);
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* Structural helper for `clivly.config.ts`. Gives full type inference on the
|
|
214
|
-
* literal you pass and eagerly parses it, so shape errors surface at config
|
|
215
|
-
* load rather than at sync time.
|
|
216
|
-
*
|
|
217
|
-
* It also catches the silent-data-loss footgun: a non-canonical field key for a
|
|
218
|
-
* materialised concept (e.g. `fullName` instead of `name` on a `contact`) has no
|
|
219
|
-
* destination column, so the store would write an empty value. By default these
|
|
220
|
-
* are reported via `onWarn` (a `console.warn`); pass `{ strict: true }` to throw
|
|
221
|
-
* a `ClivlyConfigError` instead. Does NOT validate against the host schema — that
|
|
222
|
-
* needs the discovered schema; see `validateEntitiesConfig`.
|
|
223
|
-
*/
|
|
224
|
-
function defineClivlyConfig(config, options) {
|
|
225
|
-
const parsed = entitiesConfigSchema.parse(config);
|
|
226
|
-
const issues = canonicalFieldIssues(parsed);
|
|
227
|
-
if (issues.length > 0) {
|
|
228
|
-
if (options?.strict) throw new ClivlyConfigError(issues);
|
|
229
|
-
const warn = options?.onWarn ?? defaultWarn;
|
|
230
|
-
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.`);
|
|
231
|
-
}
|
|
232
|
-
return parsed;
|
|
233
|
-
}
|
|
234
|
-
/** Split a `table.column` / bare `column` ref into its parts. */
|
|
235
|
-
function parseColumnRef(ref) {
|
|
236
|
-
const dot = ref.indexOf(".");
|
|
237
|
-
if (dot === -1) return { column: ref };
|
|
238
|
-
return {
|
|
239
|
-
table: ref.slice(0, dot),
|
|
240
|
-
column: ref.slice(dot + 1)
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
/** Shared lookup + error-collection state threaded through the validators. */
|
|
244
|
-
var SchemaValidator = class {
|
|
245
|
-
errors = [];
|
|
246
|
-
columnsByTable = /* @__PURE__ */ new Map();
|
|
247
|
-
constructor(schema) {
|
|
248
|
-
for (const table of schema) this.columnsByTable.set(table.name, new Set(table.columns));
|
|
249
|
-
}
|
|
250
|
-
tableExists(name) {
|
|
251
|
-
return this.columnsByTable.has(name);
|
|
252
|
-
}
|
|
253
|
-
columnExists(table, column) {
|
|
254
|
-
return this.columnsByTable.get(table)?.has(column) ?? false;
|
|
255
|
-
}
|
|
256
|
-
report(path, message) {
|
|
257
|
-
this.errors.push({
|
|
258
|
-
path,
|
|
259
|
-
message
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
/** Report if `column` is absent from `table`. Returns whether it existed. */
|
|
263
|
-
requireColumn(path, table, column) {
|
|
264
|
-
if (this.columnExists(table, column)) return true;
|
|
265
|
-
this.report(path, `column "${column}" does not exist on "${table}"`);
|
|
266
|
-
return false;
|
|
267
|
-
}
|
|
268
|
-
};
|
|
269
|
-
function validateFields(v, base, entity) {
|
|
270
|
-
for (const [field, column] of Object.entries(entity.fields)) v.requireColumn(`${base}.fields.${field}`, entity.source, column);
|
|
271
|
-
for (const field of nonCanonicalFieldKeys(entity)) v.report(`${base}.fields.${field}`, nonCanonicalMessage(entity, field));
|
|
272
|
-
}
|
|
273
|
-
function validateReadOnly(v, base, entity) {
|
|
274
|
-
for (const field of entity.readOnly ?? []) if (!(field in entity.fields)) v.report(`${base}.readOnly`, `"${field}" is not one of this entity's fields`);
|
|
275
|
-
}
|
|
276
|
-
function validateFilter(v, base, entity) {
|
|
277
|
-
if (!entity.filter || "$raw" in entity.filter) return;
|
|
278
|
-
for (const column of Object.keys(entity.filter)) v.requireColumn(`${base}.filter.${column}`, entity.source, column);
|
|
279
|
-
}
|
|
280
|
-
function validateFkVia(v, relBase, via, entity, related) {
|
|
281
|
-
const { table, column } = parseColumnRef(via.column);
|
|
282
|
-
const fkTable = table ?? (via.fkOn === "contact" ? entity.source : related?.source);
|
|
283
|
-
const path = `${relBase}.via.column`;
|
|
284
|
-
if (!fkTable) {
|
|
285
|
-
v.report(path, `cannot resolve table for FK column "${via.column}"`);
|
|
286
|
-
return;
|
|
287
|
-
}
|
|
288
|
-
if (!v.tableExists(fkTable)) {
|
|
289
|
-
v.report(path, `table "${fkTable}" is not in the discovered schema`);
|
|
290
|
-
return;
|
|
291
|
-
}
|
|
292
|
-
v.requireColumn(path, fkTable, column);
|
|
293
|
-
}
|
|
294
|
-
function validateThroughVia(v, relBase, via) {
|
|
295
|
-
if (!v.tableExists(via.through)) {
|
|
296
|
-
v.report(`${relBase}.via.through`, `join table "${via.through}" is not in the discovered schema`);
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
299
|
-
v.requireColumn(`${relBase}.via.localKey`, via.through, via.localKey);
|
|
300
|
-
v.requireColumn(`${relBase}.via.foreignKey`, via.through, via.foreignKey);
|
|
301
|
-
}
|
|
302
|
-
function validateRelationship(v, base, config, entity, relName, rel) {
|
|
303
|
-
const relBase = `${base}.relationships.${relName}`;
|
|
304
|
-
const related = config.entities[rel.entity];
|
|
305
|
-
if (!related) v.report(`${relBase}.entity`, `"${rel.entity}" is not a defined entity`);
|
|
306
|
-
const { via } = rel;
|
|
307
|
-
if ("fkOn" in via) validateFkVia(v, relBase, via, entity, related);
|
|
308
|
-
else if ("through" in via) validateThroughVia(v, relBase, via);
|
|
309
|
-
}
|
|
310
|
-
function validateEntity(v, config, key, entity) {
|
|
311
|
-
const base = `entities.${key}`;
|
|
312
|
-
if (!v.tableExists(entity.source)) {
|
|
313
|
-
v.report(`${base}.source`, `table "${entity.source}" is not in the discovered schema`);
|
|
314
|
-
return;
|
|
315
|
-
}
|
|
316
|
-
validateFields(v, base, entity);
|
|
317
|
-
validateReadOnly(v, base, entity);
|
|
318
|
-
validateFilter(v, base, entity);
|
|
319
|
-
for (const [relName, rel] of Object.entries(entity.relationships ?? {})) validateRelationship(v, base, config, entity, relName, rel);
|
|
320
|
-
}
|
|
321
|
-
/**
|
|
322
|
-
* Validate a (structurally valid) config against the host's discovered schema:
|
|
323
|
-
* source tables exist, mapped columns exist, `readOnly` ⊆ `fields`, filter
|
|
324
|
-
* columns exist, and relationships point at real entities/tables/columns.
|
|
325
|
-
*
|
|
326
|
-
* `$raw` filters and `$raw` relationship joins are opaque and skipped. Returns
|
|
327
|
-
* every error found (not just the first) so a mapping UI or CLI can show them
|
|
328
|
-
* all at once.
|
|
329
|
-
*/
|
|
330
|
-
function validateEntitiesConfig(config, schema) {
|
|
331
|
-
const validator = new SchemaValidator(schema);
|
|
332
|
-
for (const [key, entity] of Object.entries(config.entities)) validateEntity(validator, config, key, entity);
|
|
333
|
-
return {
|
|
334
|
-
valid: validator.errors.length === 0,
|
|
335
|
-
errors: validator.errors
|
|
336
|
-
};
|
|
337
|
-
}
|
|
338
|
-
//#endregion
|
|
1
|
+
import { a as MATERIALIZING_CONCEPTS, c as filterSchema, d as validateEntitiesConfig, i as ClivlyConfigError, l as relationshipSchema, n as CRM_CONCEPTS, o as defineClivlyConfig, r as CUSTOM_CONCEPT, s as entitiesConfigSchema, t as CANONICAL_FIELDS, u as relationshipsSchema } from "./entity-config-Tb5bbKLj.mjs";
|
|
339
2
|
export { CANONICAL_FIELDS, CRM_CONCEPTS, CUSTOM_CONCEPT, ClivlyConfigError, MATERIALIZING_CONCEPTS, defineClivlyConfig, entitiesConfigSchema, filterSchema, relationshipSchema, relationshipsSchema, validateEntitiesConfig };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_entity_config = require("./entity-config.cjs");
|
|
2
|
+
const require_entity_config = require("./entity-config-PoOGzvpN.cjs");
|
|
3
3
|
const require_mapping_score = require("./mapping-score.cjs");
|
|
4
4
|
//#region src/entity-heuristics.ts
|
|
5
5
|
const TARGET_CONCEPTS = [
|
|
@@ -101,7 +101,10 @@ const CONCEPT_FIELDS = {
|
|
|
101
101
|
"name",
|
|
102
102
|
"full_name",
|
|
103
103
|
"fullname",
|
|
104
|
-
"display_name"
|
|
104
|
+
"display_name",
|
|
105
|
+
"first_name",
|
|
106
|
+
"firstname",
|
|
107
|
+
"given_name"
|
|
105
108
|
],
|
|
106
109
|
required: false
|
|
107
110
|
}, {
|
|
@@ -224,12 +227,28 @@ function suggestCursorField(columnKeys) {
|
|
|
224
227
|
return null;
|
|
225
228
|
}
|
|
226
229
|
const MATERIALIZING = new Set(require_entity_config.MATERIALIZING_CONCEPTS);
|
|
230
|
+
const ENDS_IN_CONSONANT_Y = /[^aeiou]y$/;
|
|
231
|
+
const NEEDS_ES = /(s|x|z|ch|sh)$/;
|
|
232
|
+
const IRREGULAR_PLURALS = {
|
|
233
|
+
person: "people",
|
|
234
|
+
child: "children",
|
|
235
|
+
man: "men",
|
|
236
|
+
woman: "women"
|
|
237
|
+
};
|
|
238
|
+
function pluralize(name) {
|
|
239
|
+
const irregular = IRREGULAR_PLURALS[name];
|
|
240
|
+
if (irregular) return irregular;
|
|
241
|
+
if (ENDS_IN_CONSONANT_Y.test(name)) return `${name.slice(0, -1)}ies`;
|
|
242
|
+
if (NEEDS_ES.test(name)) return `${name}es`;
|
|
243
|
+
return `${name}s`;
|
|
244
|
+
}
|
|
227
245
|
function matchesConcept(tableName, concept) {
|
|
228
246
|
const normalized = tableName.toLowerCase();
|
|
229
247
|
const entry = TABLE_PATTERNS.find((p) => p.concept === concept);
|
|
230
248
|
if (!entry) return null;
|
|
231
|
-
|
|
232
|
-
if (
|
|
249
|
+
const candidates = [normalized, pluralize(normalized)];
|
|
250
|
+
if (candidates.some((candidate) => entry.names.includes(candidate))) return { exact: true };
|
|
251
|
+
if (entry.names.some((name) => candidates.some((candidate) => candidate.includes(name)))) return { exact: false };
|
|
233
252
|
return null;
|
|
234
253
|
}
|
|
235
254
|
function metaFor(table, column) {
|
|
@@ -245,17 +264,18 @@ function scoreConcepts(table) {
|
|
|
245
264
|
for (const concept of TARGET_CONCEPTS) {
|
|
246
265
|
if (!MATERIALIZING.has(concept)) continue;
|
|
247
266
|
const nameMatch = matchesConcept(table.name, concept);
|
|
248
|
-
|
|
267
|
+
const definingColumns = hasDefiningColumn(concept, table.columns);
|
|
268
|
+
if (!(nameMatch || definingColumns)) continue;
|
|
249
269
|
let score = 0;
|
|
250
270
|
const reasons = [];
|
|
251
|
-
if (nameMatch
|
|
271
|
+
if (nameMatch?.exact) {
|
|
252
272
|
score += require_mapping_score.W.nameExact;
|
|
253
273
|
reasons.push(`name matches "${table.name}"`);
|
|
254
|
-
} else {
|
|
274
|
+
} else if (nameMatch) {
|
|
255
275
|
score += require_mapping_score.W.nameSubstring;
|
|
256
276
|
reasons.push(`name looks like ${concept}`);
|
|
257
277
|
}
|
|
258
|
-
if (
|
|
278
|
+
if (definingColumns) {
|
|
259
279
|
score += require_mapping_score.W.definingColumn;
|
|
260
280
|
reasons.push(`has the columns a ${concept} needs`);
|
|
261
281
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DiscoveredColumnMeta } from "./entity-config.cjs";
|
|
1
2
|
import { Scored } from "./mapping-score.cjs";
|
|
2
3
|
//#region src/entity-heuristics.d.ts
|
|
3
4
|
declare const TARGET_CONCEPTS: readonly ["contact", "company", "deal", "activity", "conversation", "note"];
|
|
@@ -6,17 +7,6 @@ declare function suggestCursorField(columnKeys: string[]): string | null;
|
|
|
6
7
|
declare function matchesConcept(tableName: string, concept: TargetConcept): {
|
|
7
8
|
exact: boolean;
|
|
8
9
|
} | null;
|
|
9
|
-
interface DiscoveredColumnMeta {
|
|
10
|
-
isForeignKey?: boolean;
|
|
11
|
-
isPrimaryKey?: boolean;
|
|
12
|
-
name: string;
|
|
13
|
-
nullable?: boolean;
|
|
14
|
-
references?: {
|
|
15
|
-
table: string;
|
|
16
|
-
column: string;
|
|
17
|
-
};
|
|
18
|
-
type?: string;
|
|
19
|
-
}
|
|
20
10
|
interface ScoredTable {
|
|
21
11
|
columns: string[];
|
|
22
12
|
columnsMeta?: DiscoveredColumnMeta[];
|
|
@@ -64,4 +54,4 @@ interface FieldSuggestion {
|
|
|
64
54
|
declare function scoreFieldMap(concept: TargetConcept, table: ScoredTable): Scored<FieldSuggestion>[];
|
|
65
55
|
declare function toFieldMap(scored: Scored<FieldSuggestion>[]): Record<string, string>;
|
|
66
56
|
//#endregion
|
|
67
|
-
export { DiscoveredColumnMeta, FieldSuggestion, RelationshipSuggestion, ScoredTable, SuggestedVia, TARGET_CONCEPTS, TargetConcept, matchesConcept, requiredFieldsFor, scoreConcepts, scoreFieldMap, scoreRelationships, suggestCursorField, suggestDiscriminatorColumns, toFieldMap, topConcept, validateMapping };
|
|
57
|
+
export { type DiscoveredColumnMeta, FieldSuggestion, RelationshipSuggestion, ScoredTable, SuggestedVia, TARGET_CONCEPTS, TargetConcept, matchesConcept, requiredFieldsFor, scoreConcepts, scoreFieldMap, scoreRelationships, suggestCursorField, suggestDiscriminatorColumns, toFieldMap, topConcept, validateMapping };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DiscoveredColumnMeta } from "./entity-config.mjs";
|
|
1
2
|
import { Scored } from "./mapping-score.mjs";
|
|
2
3
|
//#region src/entity-heuristics.d.ts
|
|
3
4
|
declare const TARGET_CONCEPTS: readonly ["contact", "company", "deal", "activity", "conversation", "note"];
|
|
@@ -6,17 +7,6 @@ declare function suggestCursorField(columnKeys: string[]): string | null;
|
|
|
6
7
|
declare function matchesConcept(tableName: string, concept: TargetConcept): {
|
|
7
8
|
exact: boolean;
|
|
8
9
|
} | null;
|
|
9
|
-
interface DiscoveredColumnMeta {
|
|
10
|
-
isForeignKey?: boolean;
|
|
11
|
-
isPrimaryKey?: boolean;
|
|
12
|
-
name: string;
|
|
13
|
-
nullable?: boolean;
|
|
14
|
-
references?: {
|
|
15
|
-
table: string;
|
|
16
|
-
column: string;
|
|
17
|
-
};
|
|
18
|
-
type?: string;
|
|
19
|
-
}
|
|
20
10
|
interface ScoredTable {
|
|
21
11
|
columns: string[];
|
|
22
12
|
columnsMeta?: DiscoveredColumnMeta[];
|
|
@@ -64,4 +54,4 @@ interface FieldSuggestion {
|
|
|
64
54
|
declare function scoreFieldMap(concept: TargetConcept, table: ScoredTable): Scored<FieldSuggestion>[];
|
|
65
55
|
declare function toFieldMap(scored: Scored<FieldSuggestion>[]): Record<string, string>;
|
|
66
56
|
//#endregion
|
|
67
|
-
export { DiscoveredColumnMeta, FieldSuggestion, RelationshipSuggestion, ScoredTable, SuggestedVia, TARGET_CONCEPTS, TargetConcept, matchesConcept, requiredFieldsFor, scoreConcepts, scoreFieldMap, scoreRelationships, suggestCursorField, suggestDiscriminatorColumns, toFieldMap, topConcept, validateMapping };
|
|
57
|
+
export { type DiscoveredColumnMeta, FieldSuggestion, RelationshipSuggestion, ScoredTable, SuggestedVia, TARGET_CONCEPTS, TargetConcept, matchesConcept, requiredFieldsFor, scoreConcepts, scoreFieldMap, scoreRelationships, suggestCursorField, suggestDiscriminatorColumns, toFieldMap, topConcept, validateMapping };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MATERIALIZING_CONCEPTS } from "./entity-config.mjs";
|
|
1
|
+
import { a as MATERIALIZING_CONCEPTS } from "./entity-config-Tb5bbKLj.mjs";
|
|
2
2
|
import { CONCEPT_TIERS, FIELD_TIERS, RELATIONSHIP_TIERS, W, isAmbiguous, isNumericLike, isTextLike, isTimestampLike, tierFor } from "./mapping-score.mjs";
|
|
3
3
|
//#region src/entity-heuristics.ts
|
|
4
4
|
const TARGET_CONCEPTS = [
|
|
@@ -100,7 +100,10 @@ const CONCEPT_FIELDS = {
|
|
|
100
100
|
"name",
|
|
101
101
|
"full_name",
|
|
102
102
|
"fullname",
|
|
103
|
-
"display_name"
|
|
103
|
+
"display_name",
|
|
104
|
+
"first_name",
|
|
105
|
+
"firstname",
|
|
106
|
+
"given_name"
|
|
104
107
|
],
|
|
105
108
|
required: false
|
|
106
109
|
}, {
|
|
@@ -223,12 +226,28 @@ function suggestCursorField(columnKeys) {
|
|
|
223
226
|
return null;
|
|
224
227
|
}
|
|
225
228
|
const MATERIALIZING = new Set(MATERIALIZING_CONCEPTS);
|
|
229
|
+
const ENDS_IN_CONSONANT_Y = /[^aeiou]y$/;
|
|
230
|
+
const NEEDS_ES = /(s|x|z|ch|sh)$/;
|
|
231
|
+
const IRREGULAR_PLURALS = {
|
|
232
|
+
person: "people",
|
|
233
|
+
child: "children",
|
|
234
|
+
man: "men",
|
|
235
|
+
woman: "women"
|
|
236
|
+
};
|
|
237
|
+
function pluralize(name) {
|
|
238
|
+
const irregular = IRREGULAR_PLURALS[name];
|
|
239
|
+
if (irregular) return irregular;
|
|
240
|
+
if (ENDS_IN_CONSONANT_Y.test(name)) return `${name.slice(0, -1)}ies`;
|
|
241
|
+
if (NEEDS_ES.test(name)) return `${name}es`;
|
|
242
|
+
return `${name}s`;
|
|
243
|
+
}
|
|
226
244
|
function matchesConcept(tableName, concept) {
|
|
227
245
|
const normalized = tableName.toLowerCase();
|
|
228
246
|
const entry = TABLE_PATTERNS.find((p) => p.concept === concept);
|
|
229
247
|
if (!entry) return null;
|
|
230
|
-
|
|
231
|
-
if (
|
|
248
|
+
const candidates = [normalized, pluralize(normalized)];
|
|
249
|
+
if (candidates.some((candidate) => entry.names.includes(candidate))) return { exact: true };
|
|
250
|
+
if (entry.names.some((name) => candidates.some((candidate) => candidate.includes(name)))) return { exact: false };
|
|
232
251
|
return null;
|
|
233
252
|
}
|
|
234
253
|
function metaFor(table, column) {
|
|
@@ -244,17 +263,18 @@ function scoreConcepts(table) {
|
|
|
244
263
|
for (const concept of TARGET_CONCEPTS) {
|
|
245
264
|
if (!MATERIALIZING.has(concept)) continue;
|
|
246
265
|
const nameMatch = matchesConcept(table.name, concept);
|
|
247
|
-
|
|
266
|
+
const definingColumns = hasDefiningColumn(concept, table.columns);
|
|
267
|
+
if (!(nameMatch || definingColumns)) continue;
|
|
248
268
|
let score = 0;
|
|
249
269
|
const reasons = [];
|
|
250
|
-
if (nameMatch
|
|
270
|
+
if (nameMatch?.exact) {
|
|
251
271
|
score += W.nameExact;
|
|
252
272
|
reasons.push(`name matches "${table.name}"`);
|
|
253
|
-
} else {
|
|
273
|
+
} else if (nameMatch) {
|
|
254
274
|
score += W.nameSubstring;
|
|
255
275
|
reasons.push(`name looks like ${concept}`);
|
|
256
276
|
}
|
|
257
|
-
if (
|
|
277
|
+
if (definingColumns) {
|
|
258
278
|
score += W.definingColumn;
|
|
259
279
|
reasons.push(`has the columns a ${concept} needs`);
|
|
260
280
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/env-file.ts
|
|
3
|
+
const ENV_FRAMEWORKS = [
|
|
4
|
+
"next",
|
|
5
|
+
"tanstack-start",
|
|
6
|
+
"sveltekit",
|
|
7
|
+
"remix",
|
|
8
|
+
"nuxt",
|
|
9
|
+
"hono-workers"
|
|
10
|
+
];
|
|
11
|
+
const ENV_FILES = {
|
|
12
|
+
next: ".env.local",
|
|
13
|
+
"tanstack-start": ".env",
|
|
14
|
+
sveltekit: ".env",
|
|
15
|
+
remix: ".env",
|
|
16
|
+
nuxt: ".env",
|
|
17
|
+
"hono-workers": ".dev.vars"
|
|
18
|
+
};
|
|
19
|
+
/** The conventional env file for a framework. `.env` when we do not know. */
|
|
20
|
+
function envFileFor(framework) {
|
|
21
|
+
return ENV_FILES[normalizeFramework(framework)] ?? ".env";
|
|
22
|
+
}
|
|
23
|
+
const ALIASES = {
|
|
24
|
+
next: "next",
|
|
25
|
+
nextjs: "next",
|
|
26
|
+
"next.js": "next",
|
|
27
|
+
"tanstack-start": "tanstack-start",
|
|
28
|
+
tanstack: "tanstack-start",
|
|
29
|
+
sveltekit: "sveltekit",
|
|
30
|
+
remix: "remix",
|
|
31
|
+
nuxt: "nuxt",
|
|
32
|
+
"hono-workers": "hono-workers",
|
|
33
|
+
hono: "hono-workers",
|
|
34
|
+
workers: "hono-workers"
|
|
35
|
+
};
|
|
36
|
+
/** The canonical id for a framework name, or null when unrecognised. */
|
|
37
|
+
function normalizeFramework(framework) {
|
|
38
|
+
if (!framework) return null;
|
|
39
|
+
return ALIASES[framework.toLowerCase()] ?? null;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Which env file to actually write to.
|
|
43
|
+
*
|
|
44
|
+
* Prefers a file that already holds the key over the conventional one: a
|
|
45
|
+
* project that has been running with credentials in `.env` must not suddenly
|
|
46
|
+
* get a second copy in `.env.local`, leaving two files disagreeing and the
|
|
47
|
+
* stale one still loading. Convention applies to new setups, not to a working
|
|
48
|
+
* one someone already has.
|
|
49
|
+
*/
|
|
50
|
+
function resolveEnvFile(input) {
|
|
51
|
+
const conventional = envFileFor(input.framework);
|
|
52
|
+
if (input.existing.includes(conventional)) return conventional;
|
|
53
|
+
return input.existing[0] ?? conventional;
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
exports.ENV_FRAMEWORKS = ENV_FRAMEWORKS;
|
|
57
|
+
exports.envFileFor = envFileFor;
|
|
58
|
+
exports.normalizeFramework = normalizeFramework;
|
|
59
|
+
exports.resolveEnvFile = resolveEnvFile;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/env-file.d.ts
|
|
2
|
+
declare const ENV_FRAMEWORKS: readonly ["next", "tanstack-start", "sveltekit", "remix", "nuxt", "hono-workers"];
|
|
3
|
+
type EnvFramework = (typeof ENV_FRAMEWORKS)[number];
|
|
4
|
+
/** The conventional env file for a framework. `.env` when we do not know. */
|
|
5
|
+
declare function envFileFor(framework: string | null | undefined): string;
|
|
6
|
+
/** The canonical id for a framework name, or null when unrecognised. */
|
|
7
|
+
declare function normalizeFramework(framework: string | null | undefined): EnvFramework | null;
|
|
8
|
+
/**
|
|
9
|
+
* Which env file to actually write to.
|
|
10
|
+
*
|
|
11
|
+
* Prefers a file that already holds the key over the conventional one: a
|
|
12
|
+
* project that has been running with credentials in `.env` must not suddenly
|
|
13
|
+
* get a second copy in `.env.local`, leaving two files disagreeing and the
|
|
14
|
+
* stale one still loading. Convention applies to new setups, not to a working
|
|
15
|
+
* one someone already has.
|
|
16
|
+
*/
|
|
17
|
+
declare function resolveEnvFile(input: {
|
|
18
|
+
/** Candidate filenames that already contain the credential, if any. */
|
|
19
|
+
existing: string[];
|
|
20
|
+
framework: string | null | undefined;
|
|
21
|
+
}): string;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { ENV_FRAMEWORKS, EnvFramework, envFileFor, normalizeFramework, resolveEnvFile };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/env-file.d.ts
|
|
2
|
+
declare const ENV_FRAMEWORKS: readonly ["next", "tanstack-start", "sveltekit", "remix", "nuxt", "hono-workers"];
|
|
3
|
+
type EnvFramework = (typeof ENV_FRAMEWORKS)[number];
|
|
4
|
+
/** The conventional env file for a framework. `.env` when we do not know. */
|
|
5
|
+
declare function envFileFor(framework: string | null | undefined): string;
|
|
6
|
+
/** The canonical id for a framework name, or null when unrecognised. */
|
|
7
|
+
declare function normalizeFramework(framework: string | null | undefined): EnvFramework | null;
|
|
8
|
+
/**
|
|
9
|
+
* Which env file to actually write to.
|
|
10
|
+
*
|
|
11
|
+
* Prefers a file that already holds the key over the conventional one: a
|
|
12
|
+
* project that has been running with credentials in `.env` must not suddenly
|
|
13
|
+
* get a second copy in `.env.local`, leaving two files disagreeing and the
|
|
14
|
+
* stale one still loading. Convention applies to new setups, not to a working
|
|
15
|
+
* one someone already has.
|
|
16
|
+
*/
|
|
17
|
+
declare function resolveEnvFile(input: {
|
|
18
|
+
/** Candidate filenames that already contain the credential, if any. */
|
|
19
|
+
existing: string[];
|
|
20
|
+
framework: string | null | undefined;
|
|
21
|
+
}): string;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { ENV_FRAMEWORKS, EnvFramework, envFileFor, normalizeFramework, resolveEnvFile };
|