@clivly/core 0.7.4 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +1 -1
- package/dist/entity-heuristics.d.cts +2 -12
- package/dist/entity-heuristics.d.mts +2 -12
- package/dist/entity-heuristics.mjs +1 -1
- 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/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 +1 -1
package/dist/mappings-config.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CANONICAL_FIELDS } from "./entity-config.mjs";
|
|
1
|
+
import { t as CANONICAL_FIELDS } from "./entity-config-Tb5bbKLj.mjs";
|
|
2
2
|
//#region src/mappings-config.ts
|
|
3
3
|
const RESERVED_FIELD = "id";
|
|
4
4
|
/**
|
|
@@ -21,7 +21,9 @@ function canonicalFields(concept, fieldMap) {
|
|
|
21
21
|
const DEFAULT_ROLE = "primary";
|
|
22
22
|
const ACTIVE_STATUS = "active";
|
|
23
23
|
function isEligible(row) {
|
|
24
|
-
|
|
24
|
+
if ((row.status ?? ACTIVE_STATUS) !== ACTIVE_STATUS) return false;
|
|
25
|
+
if (row.projection) return Object.keys(row.projection.fieldBindings ?? {}).length > 0;
|
|
26
|
+
return Object.keys(row.fieldMap ?? {}).length > 0;
|
|
25
27
|
}
|
|
26
28
|
/**
|
|
27
29
|
* Preferred key for a row: the bare source table when it backs a single entity,
|
|
@@ -45,13 +47,28 @@ function uniquify(base, used) {
|
|
|
45
47
|
used.add(key);
|
|
46
48
|
return key;
|
|
47
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* A projection's `fields` map, derived mechanically from its bindings.
|
|
52
|
+
*
|
|
53
|
+
* `ClivlyEntityConfig.fields` is required and read by existing consumers, so a
|
|
54
|
+
* projection entity must populate it. The values are ref *tokens*
|
|
55
|
+
* (`"base.full_name"`), never host column names — the compiler reads
|
|
56
|
+
* `projection.fieldBindings`, and `validateEntitiesConfig` skips the legacy
|
|
57
|
+
* column check for projection entities precisely because of this.
|
|
58
|
+
*/
|
|
59
|
+
function projectionFields(projection) {
|
|
60
|
+
const fields = {};
|
|
61
|
+
for (const [field, binding] of Object.entries(projection.fieldBindings)) fields[field] = `${binding.from.table}.${binding.from.column}`;
|
|
62
|
+
return fields;
|
|
63
|
+
}
|
|
48
64
|
function buildEntity(row, refIndex) {
|
|
49
65
|
const entity = {
|
|
50
66
|
concept: row.targetConcept,
|
|
51
|
-
source: row.sourceTable,
|
|
67
|
+
source: row.projection?.baseTable ?? row.sourceTable,
|
|
52
68
|
role: row.role ?? DEFAULT_ROLE,
|
|
53
|
-
fields: canonicalFields(row.targetConcept, row.fieldMap)
|
|
69
|
+
fields: row.projection ? projectionFields(row.projection) : canonicalFields(row.targetConcept, row.fieldMap)
|
|
54
70
|
};
|
|
71
|
+
if (row.projection) entity.projection = row.projection;
|
|
55
72
|
if (row.targetConcept === "custom" && row.targetObjectTypeId) entity.targetObjectTypeId = row.targetObjectTypeId;
|
|
56
73
|
if (row.filter && Object.keys(row.filter).length > 0) entity.filter = row.filter;
|
|
57
74
|
if (row.relationships && Object.keys(row.relationships).length > 0) {
|
|
@@ -76,11 +93,16 @@ function buildEntity(row, refIndex) {
|
|
|
76
93
|
function mappingsToEntitiesConfig(rows) {
|
|
77
94
|
const eligible = rows.filter(isEligible);
|
|
78
95
|
const tableCounts = /* @__PURE__ */ new Map();
|
|
79
|
-
for (const row of eligible)
|
|
96
|
+
for (const row of eligible) {
|
|
97
|
+
if (row.projection) continue;
|
|
98
|
+
tableCounts.set(row.sourceTable, (tableCounts.get(row.sourceTable) ?? 0) + 1);
|
|
99
|
+
}
|
|
80
100
|
const used = /* @__PURE__ */ new Set();
|
|
81
101
|
const tableAnchor = /* @__PURE__ */ new Map();
|
|
82
102
|
const assigned = [];
|
|
83
|
-
|
|
103
|
+
const legacyRows = eligible.filter((row) => !row.projection);
|
|
104
|
+
const projectionRows = eligible.filter((row) => row.projection);
|
|
105
|
+
for (const row of legacyRows) {
|
|
84
106
|
const key = uniquify(preferredKey(row, tableCounts), used);
|
|
85
107
|
assigned.push({
|
|
86
108
|
row,
|
|
@@ -88,6 +110,10 @@ function mappingsToEntitiesConfig(rows) {
|
|
|
88
110
|
});
|
|
89
111
|
if ((row.role ?? DEFAULT_ROLE) === DEFAULT_ROLE || !tableAnchor.has(row.sourceTable)) tableAnchor.set(row.sourceTable, key);
|
|
90
112
|
}
|
|
113
|
+
for (const row of projectionRows) assigned.push({
|
|
114
|
+
row,
|
|
115
|
+
key: uniquify(row.projection?.entityKey ?? row.sourceTable, used)
|
|
116
|
+
});
|
|
91
117
|
const refIndex = /* @__PURE__ */ new Map();
|
|
92
118
|
for (const { key } of assigned) refIndex.set(key, key);
|
|
93
119
|
for (const [table, key] of tableAnchor) if (!refIndex.has(table)) refIndex.set(table, key);
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
let zod = require("zod");
|
|
2
|
+
//#region src/projection.ts
|
|
3
|
+
/**
|
|
4
|
+
* Projection-backed custom mappings — one custom object derived from a base
|
|
5
|
+
* table plus explicit joins.
|
|
6
|
+
*
|
|
7
|
+
* A projection is deliberately narrow: equality joins only, direct column
|
|
8
|
+
* bindings only, no expressions and no aggregation. Everything a user can
|
|
9
|
+
* express here compiles to a single read-only view whose `id` column is
|
|
10
|
+
* provably unique per row, which is what lets the sync engine consume it
|
|
11
|
+
* unchanged.
|
|
12
|
+
*
|
|
13
|
+
* Refs address tables symbolically: `"base"` for the base table, or a join's
|
|
14
|
+
* `key`. Raw table names are never accepted in a ref, so a projection can
|
|
15
|
+
* never reach a table it did not declare.
|
|
16
|
+
*/
|
|
17
|
+
/** The reserved ref name for the projection's base table. */
|
|
18
|
+
const BASE_REF = "base";
|
|
19
|
+
/** Entity keys become view names (`crm_src_<entityKey>`), so keep them slugs. */
|
|
20
|
+
const PROJECTION_KEY_PATTERN = /^[a-z][a-z0-9_]*$/;
|
|
21
|
+
const projectionRefSchema = zod.z.strictObject({
|
|
22
|
+
table: zod.z.string().min(1),
|
|
23
|
+
column: zod.z.string().min(1)
|
|
24
|
+
});
|
|
25
|
+
const projectionJoinConditionSchema = zod.z.strictObject({
|
|
26
|
+
left: projectionRefSchema,
|
|
27
|
+
op: zod.z.literal("eq"),
|
|
28
|
+
right: projectionRefSchema
|
|
29
|
+
});
|
|
30
|
+
const projectionJoinSchema = zod.z.strictObject({
|
|
31
|
+
/** SQL alias and ref target. Unique within the projection. */
|
|
32
|
+
key: zod.z.string().regex(PROJECTION_KEY_PATTERN),
|
|
33
|
+
table: zod.z.string().min(1),
|
|
34
|
+
type: zod.z.enum(["inner", "left"]),
|
|
35
|
+
on: zod.z.array(projectionJoinConditionSchema).min(1)
|
|
36
|
+
});
|
|
37
|
+
const projectionFieldBindingSchema = zod.z.strictObject({ from: projectionRefSchema });
|
|
38
|
+
const projectionIdentitySchema = zod.z.union([
|
|
39
|
+
zod.z.strictObject({
|
|
40
|
+
kind: zod.z.literal("base_pk"),
|
|
41
|
+
/** Defaults to the discovered single-column PK of the base table. */
|
|
42
|
+
column: zod.z.string().min(1).optional()
|
|
43
|
+
}),
|
|
44
|
+
zod.z.strictObject({
|
|
45
|
+
kind: zod.z.literal("column"),
|
|
46
|
+
from: projectionRefSchema
|
|
47
|
+
}),
|
|
48
|
+
zod.z.strictObject({
|
|
49
|
+
kind: zod.z.literal("composite"),
|
|
50
|
+
parts: zod.z.array(projectionRefSchema).min(2)
|
|
51
|
+
})
|
|
52
|
+
]);
|
|
53
|
+
const projectionSchema = zod.z.strictObject({
|
|
54
|
+
version: zod.z.literal(1),
|
|
55
|
+
kind: zod.z.literal("projection"),
|
|
56
|
+
entityKey: zod.z.string().regex(PROJECTION_KEY_PATTERN),
|
|
57
|
+
baseTable: zod.z.string().min(1),
|
|
58
|
+
joins: zod.z.array(projectionJoinSchema),
|
|
59
|
+
fieldBindings: zod.z.record(zod.z.string(), projectionFieldBindingSchema).refine((b) => Object.keys(b).length > 0, { message: "at least one field must be bound" }),
|
|
60
|
+
identity: projectionIdentitySchema
|
|
61
|
+
});
|
|
62
|
+
/**
|
|
63
|
+
* Every column ref an identity depends on. `basePkColumn` is the base table's
|
|
64
|
+
* discovered PK, used only when a `base_pk` identity omits an explicit column.
|
|
65
|
+
*/
|
|
66
|
+
function identityRefs(identity, basePkColumn) {
|
|
67
|
+
if (identity.kind === "base_pk") return [{
|
|
68
|
+
table: BASE_REF,
|
|
69
|
+
column: identity.column ?? basePkColumn
|
|
70
|
+
}];
|
|
71
|
+
if (identity.kind === "column") return [identity.from];
|
|
72
|
+
return identity.parts;
|
|
73
|
+
}
|
|
74
|
+
//#endregion
|
|
75
|
+
Object.defineProperty(exports, "BASE_REF", {
|
|
76
|
+
enumerable: true,
|
|
77
|
+
get: function() {
|
|
78
|
+
return BASE_REF;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
Object.defineProperty(exports, "PROJECTION_KEY_PATTERN", {
|
|
82
|
+
enumerable: true,
|
|
83
|
+
get: function() {
|
|
84
|
+
return PROJECTION_KEY_PATTERN;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
Object.defineProperty(exports, "identityRefs", {
|
|
88
|
+
enumerable: true,
|
|
89
|
+
get: function() {
|
|
90
|
+
return identityRefs;
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
Object.defineProperty(exports, "projectionSchema", {
|
|
94
|
+
enumerable: true,
|
|
95
|
+
get: function() {
|
|
96
|
+
return projectionSchema;
|
|
97
|
+
}
|
|
98
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
//#region src/projection.ts
|
|
3
|
+
/**
|
|
4
|
+
* Projection-backed custom mappings — one custom object derived from a base
|
|
5
|
+
* table plus explicit joins.
|
|
6
|
+
*
|
|
7
|
+
* A projection is deliberately narrow: equality joins only, direct column
|
|
8
|
+
* bindings only, no expressions and no aggregation. Everything a user can
|
|
9
|
+
* express here compiles to a single read-only view whose `id` column is
|
|
10
|
+
* provably unique per row, which is what lets the sync engine consume it
|
|
11
|
+
* unchanged.
|
|
12
|
+
*
|
|
13
|
+
* Refs address tables symbolically: `"base"` for the base table, or a join's
|
|
14
|
+
* `key`. Raw table names are never accepted in a ref, so a projection can
|
|
15
|
+
* never reach a table it did not declare.
|
|
16
|
+
*/
|
|
17
|
+
/** The reserved ref name for the projection's base table. */
|
|
18
|
+
const BASE_REF = "base";
|
|
19
|
+
/** Entity keys become view names (`crm_src_<entityKey>`), so keep them slugs. */
|
|
20
|
+
const PROJECTION_KEY_PATTERN = /^[a-z][a-z0-9_]*$/;
|
|
21
|
+
const projectionRefSchema = z.strictObject({
|
|
22
|
+
table: z.string().min(1),
|
|
23
|
+
column: z.string().min(1)
|
|
24
|
+
});
|
|
25
|
+
const projectionJoinConditionSchema = z.strictObject({
|
|
26
|
+
left: projectionRefSchema,
|
|
27
|
+
op: z.literal("eq"),
|
|
28
|
+
right: projectionRefSchema
|
|
29
|
+
});
|
|
30
|
+
const projectionJoinSchema = z.strictObject({
|
|
31
|
+
/** SQL alias and ref target. Unique within the projection. */
|
|
32
|
+
key: z.string().regex(PROJECTION_KEY_PATTERN),
|
|
33
|
+
table: z.string().min(1),
|
|
34
|
+
type: z.enum(["inner", "left"]),
|
|
35
|
+
on: z.array(projectionJoinConditionSchema).min(1)
|
|
36
|
+
});
|
|
37
|
+
const projectionFieldBindingSchema = z.strictObject({ from: projectionRefSchema });
|
|
38
|
+
const projectionIdentitySchema = z.union([
|
|
39
|
+
z.strictObject({
|
|
40
|
+
kind: z.literal("base_pk"),
|
|
41
|
+
/** Defaults to the discovered single-column PK of the base table. */
|
|
42
|
+
column: z.string().min(1).optional()
|
|
43
|
+
}),
|
|
44
|
+
z.strictObject({
|
|
45
|
+
kind: z.literal("column"),
|
|
46
|
+
from: projectionRefSchema
|
|
47
|
+
}),
|
|
48
|
+
z.strictObject({
|
|
49
|
+
kind: z.literal("composite"),
|
|
50
|
+
parts: z.array(projectionRefSchema).min(2)
|
|
51
|
+
})
|
|
52
|
+
]);
|
|
53
|
+
const projectionSchema = z.strictObject({
|
|
54
|
+
version: z.literal(1),
|
|
55
|
+
kind: z.literal("projection"),
|
|
56
|
+
entityKey: z.string().regex(PROJECTION_KEY_PATTERN),
|
|
57
|
+
baseTable: z.string().min(1),
|
|
58
|
+
joins: z.array(projectionJoinSchema),
|
|
59
|
+
fieldBindings: z.record(z.string(), projectionFieldBindingSchema).refine((b) => Object.keys(b).length > 0, { message: "at least one field must be bound" }),
|
|
60
|
+
identity: projectionIdentitySchema
|
|
61
|
+
});
|
|
62
|
+
/**
|
|
63
|
+
* Every column ref an identity depends on. `basePkColumn` is the base table's
|
|
64
|
+
* discovered PK, used only when a `base_pk` identity omits an explicit column.
|
|
65
|
+
*/
|
|
66
|
+
function identityRefs(identity, basePkColumn) {
|
|
67
|
+
if (identity.kind === "base_pk") return [{
|
|
68
|
+
table: BASE_REF,
|
|
69
|
+
column: identity.column ?? basePkColumn
|
|
70
|
+
}];
|
|
71
|
+
if (identity.kind === "column") return [identity.from];
|
|
72
|
+
return identity.parts;
|
|
73
|
+
}
|
|
74
|
+
//#endregion
|
|
75
|
+
export { projectionSchema as i, PROJECTION_KEY_PATTERN as n, identityRefs as r, BASE_REF as t };
|
package/dist/sync-engine.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_view_compiler = require("./view-compiler.cjs");
|
|
2
|
+
const require_view_compiler = require("./view-compiler-BwZ9UgLp.cjs");
|
|
3
3
|
//#region src/sync-engine.ts
|
|
4
4
|
/**
|
|
5
5
|
* Diff a view's rows against the current mirror population for one entity.
|
|
@@ -171,10 +171,17 @@ function isEmptyValue(value) {
|
|
|
171
171
|
*/
|
|
172
172
|
function computeSampleWarnings(rows, requiredFields, sourceTable) {
|
|
173
173
|
if (rows.length === 0) return [{ message: `no rows found in ${sourceTable}` }];
|
|
174
|
+
const seenRefs = /* @__PURE__ */ new Set();
|
|
175
|
+
const duplicateRefs = /* @__PURE__ */ new Set();
|
|
176
|
+
for (const row of rows) {
|
|
177
|
+
if (seenRefs.has(row.sourceRef)) duplicateRefs.add(row.sourceRef);
|
|
178
|
+
seenRefs.add(row.sourceRef);
|
|
179
|
+
}
|
|
180
|
+
const identityWarnings = duplicateRefs.size > 0 ? [{ message: `duplicate id in ${duplicateRefs.size} of ${rows.length} sampled rows — identity is not unique, so sync would collapse these into one record` }] : [];
|
|
174
181
|
const required = new Set(requiredFields);
|
|
175
182
|
const fields = new Set(requiredFields);
|
|
176
183
|
for (const row of rows) for (const key of Object.keys(row.fields)) fields.add(key);
|
|
177
|
-
const warnings = [];
|
|
184
|
+
const warnings = [...identityWarnings];
|
|
178
185
|
for (const field of [...fields].sort()) {
|
|
179
186
|
const emptyCount = rows.filter((row) => isEmptyValue(row.fields[field])).length;
|
|
180
187
|
if (required.has(field)) {
|
package/dist/sync-engine.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { compileEntityViews } from "./view-compiler.mjs";
|
|
1
|
+
import { r as compileEntityViews } from "./view-compiler-BKrBA_II.mjs";
|
|
2
2
|
//#region src/sync-engine.ts
|
|
3
3
|
/**
|
|
4
4
|
* Diff a view's rows against the current mirror population for one entity.
|
|
@@ -170,10 +170,17 @@ function isEmptyValue(value) {
|
|
|
170
170
|
*/
|
|
171
171
|
function computeSampleWarnings(rows, requiredFields, sourceTable) {
|
|
172
172
|
if (rows.length === 0) return [{ message: `no rows found in ${sourceTable}` }];
|
|
173
|
+
const seenRefs = /* @__PURE__ */ new Set();
|
|
174
|
+
const duplicateRefs = /* @__PURE__ */ new Set();
|
|
175
|
+
for (const row of rows) {
|
|
176
|
+
if (seenRefs.has(row.sourceRef)) duplicateRefs.add(row.sourceRef);
|
|
177
|
+
seenRefs.add(row.sourceRef);
|
|
178
|
+
}
|
|
179
|
+
const identityWarnings = duplicateRefs.size > 0 ? [{ message: `duplicate id in ${duplicateRefs.size} of ${rows.length} sampled rows — identity is not unique, so sync would collapse these into one record` }] : [];
|
|
173
180
|
const required = new Set(requiredFields);
|
|
174
181
|
const fields = new Set(requiredFields);
|
|
175
182
|
for (const row of rows) for (const key of Object.keys(row.fields)) fields.add(key);
|
|
176
|
-
const warnings = [];
|
|
183
|
+
const warnings = [...identityWarnings];
|
|
177
184
|
for (const field of [...fields].sort()) {
|
|
178
185
|
const emptyCount = rows.filter((row) => isEmptyValue(row.fields[field])).length;
|
|
179
186
|
if (required.has(field)) {
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import { r as identityRefs, t as BASE_REF } from "./projection-C_EMRmZ8.mjs";
|
|
2
|
+
//#region src/projection-sql.ts
|
|
3
|
+
/**
|
|
4
|
+
* SQL generation for projection-backed entities.
|
|
5
|
+
*
|
|
6
|
+
* Base and joins are aliased by their ref key (`base`, then each join's `key`),
|
|
7
|
+
* which is what allows the same table to be joined twice and keeps generated
|
|
8
|
+
* SQL independent of host table names.
|
|
9
|
+
*
|
|
10
|
+
* Pure string generation — no DB access. Identifiers are escaped so output is
|
|
11
|
+
* deterministic and injection-safe.
|
|
12
|
+
*/
|
|
13
|
+
/** Quote a single identifier part, escaping embedded double-quotes. */
|
|
14
|
+
function quoteIdent(name) {
|
|
15
|
+
return `"${name.replace(/"/g, "\"\"")}"`;
|
|
16
|
+
}
|
|
17
|
+
function refSql(ref) {
|
|
18
|
+
return `${quoteIdent(ref.table)}.${quoteIdent(ref.column)}`;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The identity expression, aliased as `id`. Composite identities concatenate
|
|
22
|
+
* their parts with `::`; validation guarantees every part is NOT NULL and
|
|
23
|
+
* non-text, which is what makes the concatenation collision-free.
|
|
24
|
+
*/
|
|
25
|
+
function identitySql(projection, basePkColumn) {
|
|
26
|
+
const refs = identityRefs(projection.identity, basePkColumn);
|
|
27
|
+
if (projection.identity.kind === "composite") return `CONCAT_WS('::', ${refs.map((ref) => `${refSql(ref)}::text`).join(", ")}) AS ${quoteIdent("id")}`;
|
|
28
|
+
const [ref] = refs;
|
|
29
|
+
if (!ref) throw new Error("projection identity resolved to no column");
|
|
30
|
+
return `${refSql(ref)} AS ${quoteIdent("id")}`;
|
|
31
|
+
}
|
|
32
|
+
function compileProjectionSelect(projection, basePkColumn) {
|
|
33
|
+
const selectItems = [identitySql(projection, basePkColumn)];
|
|
34
|
+
const columns = ["id"];
|
|
35
|
+
for (const [field, binding] of Object.entries(projection.fieldBindings)) {
|
|
36
|
+
selectItems.push(`${refSql(binding.from)} AS ${quoteIdent(field)}`);
|
|
37
|
+
columns.push(field);
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
columns,
|
|
41
|
+
selectItems
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function compileProjectionJoins(projection) {
|
|
45
|
+
return projection.joins.map((join) => {
|
|
46
|
+
const keyword = join.type === "inner" ? "INNER JOIN" : "LEFT JOIN";
|
|
47
|
+
const on = join.on.map((c) => `${refSql(c.left)} = ${refSql(c.right)}`).join(" AND ");
|
|
48
|
+
return `${keyword} ${quoteIdent(join.table)} AS ${quoteIdent(join.key)} ON ${on}`;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/** `FROM <baseTable> AS "base"` — the alias every ref resolves against. */
|
|
52
|
+
function compileProjectionFrom(projection) {
|
|
53
|
+
return `FROM ${quoteIdent(projection.baseTable)} AS ${quoteIdent(BASE_REF)}`;
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/view-compiler.ts
|
|
57
|
+
/**
|
|
58
|
+
* View compiler (Phase 2) — turns a validated entity config into read-only
|
|
59
|
+
* Postgres view DDL.
|
|
60
|
+
*
|
|
61
|
+
* Each entity compiles to `crm_src_<entity>`: a projection over the host table
|
|
62
|
+
* that (a) selects the source primary key as `id` (the sync engine's
|
|
63
|
+
* `source_ref`), (b) aliases mapped columns to their Clivly field names,
|
|
64
|
+
* (c) resolves each relationship to a `<relationship>_ref` column carrying the
|
|
65
|
+
* host-side key, and (d) applies the entity's filter as a WHERE clause.
|
|
66
|
+
*
|
|
67
|
+
* The view is the security boundary: it exposes only allow-listed columns, and
|
|
68
|
+
* the sync engine (Phase 3) reads exclusively from it — never the raw table.
|
|
69
|
+
* Relationship refs stay host-side keys here; Phase 3 maps `<rel>_ref` onto the
|
|
70
|
+
* corresponding `crm_companies.id` mirror row.
|
|
71
|
+
*
|
|
72
|
+
* Pure string generation — no DB access. Config is developer-authored, but all
|
|
73
|
+
* identifiers and literals are still escaped so output is deterministic and
|
|
74
|
+
* injection-safe.
|
|
75
|
+
*/
|
|
76
|
+
/** The source primary-key column. Assumed `id`; the sync engine reads it as `source_ref`. */
|
|
77
|
+
const SOURCE_KEY = "id";
|
|
78
|
+
const DEFAULT_VIEW_PREFIX = "crm_src_";
|
|
79
|
+
const DEFAULT_ROLE = "primary";
|
|
80
|
+
/** Emit a SQL literal for a scalar filter value. */
|
|
81
|
+
function quoteLiteral(value) {
|
|
82
|
+
if (typeof value === "number") return String(value);
|
|
83
|
+
if (typeof value === "boolean") return value ? "TRUE" : "FALSE";
|
|
84
|
+
return `'${value.replace(/'/g, "''")}'`;
|
|
85
|
+
}
|
|
86
|
+
function quoteList(values) {
|
|
87
|
+
return `(${values.map((v) => quoteLiteral(v)).join(", ")})`;
|
|
88
|
+
}
|
|
89
|
+
/** Compile one column predicate into a SQL boolean expression. */
|
|
90
|
+
function compilePredicate(table, column, value) {
|
|
91
|
+
const col = `${quoteIdent(table)}.${quoteIdent(column)}`;
|
|
92
|
+
if (value === null || typeof value !== "object") return `${col} = ${quoteLiteral(value)}`;
|
|
93
|
+
if ("eq" in value) return `${col} = ${quoteLiteral(value.eq)}`;
|
|
94
|
+
if ("ne" in value) return `${col} <> ${quoteLiteral(value.ne)}`;
|
|
95
|
+
if ("in" in value) return `${col} IN ${quoteList(value.in)}`;
|
|
96
|
+
if ("notIn" in value) return `${col} NOT IN ${quoteList(value.notIn)}`;
|
|
97
|
+
if ("isNull" in value) return `${col} IS NULL`;
|
|
98
|
+
if ("isNotNull" in value) return `${col} IS NOT NULL`;
|
|
99
|
+
throw new Error(`unsupported filter predicate on ${table}.${column}`);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Build the WHERE conditions for an entity. Returns an array of SQL fragments
|
|
103
|
+
* (ANDed by the caller). `$raw` filters pass through verbatim.
|
|
104
|
+
*/
|
|
105
|
+
function compileFilterConditions(table, filter) {
|
|
106
|
+
if (!filter) return [];
|
|
107
|
+
if ("$raw" in filter) return [`(${filter.$raw})`];
|
|
108
|
+
return Object.entries(filter).map(([column, value]) => compilePredicate(table, column, value));
|
|
109
|
+
}
|
|
110
|
+
/** Split a `table.column` ref, defaulting the table when unqualified. */
|
|
111
|
+
function refParts(ref, defaultTable) {
|
|
112
|
+
const dot = ref.indexOf(".");
|
|
113
|
+
if (dot === -1) return {
|
|
114
|
+
table: defaultTable,
|
|
115
|
+
column: ref
|
|
116
|
+
};
|
|
117
|
+
return {
|
|
118
|
+
table: ref.slice(0, dot),
|
|
119
|
+
column: ref.slice(dot + 1)
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function compileRelationship(relName, rel, source, related) {
|
|
123
|
+
const refCol = `${relName}_ref`;
|
|
124
|
+
const refAlias = quoteIdent(refCol);
|
|
125
|
+
const { via } = rel;
|
|
126
|
+
if ("fkOn" in via) {
|
|
127
|
+
const relatedTable = related?.source ?? "";
|
|
128
|
+
if (via.fkOn === "company") {
|
|
129
|
+
const { table, column } = refParts(via.column, relatedTable);
|
|
130
|
+
const relatedFilter = compileFilterConditions(table, related?.filter);
|
|
131
|
+
const on = [`${quoteIdent(table)}.${quoteIdent(column)} = ${quoteIdent(source.source)}.${quoteIdent(SOURCE_KEY)}`, ...relatedFilter].join(" AND ");
|
|
132
|
+
return {
|
|
133
|
+
clause: `LEFT JOIN ${quoteIdent(table)} ON ${on}`,
|
|
134
|
+
refSelect: `${quoteIdent(table)}.${quoteIdent(SOURCE_KEY)} AS ${refAlias}`,
|
|
135
|
+
refColumn: refCol
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
const { table, column } = refParts(via.column, source.source);
|
|
139
|
+
const on = [`${quoteIdent(relatedTable)}.${quoteIdent(SOURCE_KEY)} = ${quoteIdent(table)}.${quoteIdent(column)}`, ...compileFilterConditions(relatedTable, related?.filter)].join(" AND ");
|
|
140
|
+
return {
|
|
141
|
+
clause: `LEFT JOIN ${quoteIdent(relatedTable)} ON ${on}`,
|
|
142
|
+
refSelect: `${quoteIdent(relatedTable)}.${quoteIdent(SOURCE_KEY)} AS ${refAlias}`,
|
|
143
|
+
refColumn: refCol
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
if ("through" in via) {
|
|
147
|
+
const on = `${quoteIdent(via.through)}.${quoteIdent(via.localKey)} = ${quoteIdent(source.source)}.${quoteIdent(SOURCE_KEY)}`;
|
|
148
|
+
return {
|
|
149
|
+
clause: `LEFT JOIN ${quoteIdent(via.through)} ON ${on}`,
|
|
150
|
+
refSelect: `${quoteIdent(via.through)}.${quoteIdent(via.foreignKey)} AS ${refAlias}`,
|
|
151
|
+
refColumn: refCol
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
clause: via.$raw,
|
|
156
|
+
refSelect: null,
|
|
157
|
+
refColumn: refCol
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
/** SELECT items for a legacy single-table entity: source key, then fields. */
|
|
161
|
+
function legacySelect(entity, src) {
|
|
162
|
+
const selectItems = [`${quoteIdent(src)}.${quoteIdent(SOURCE_KEY)} AS ${quoteIdent("id")}`];
|
|
163
|
+
const columns = ["id"];
|
|
164
|
+
for (const [field, column] of Object.entries(entity.fields)) {
|
|
165
|
+
selectItems.push(`${quoteIdent(src)}.${quoteIdent(column)} AS ${quoteIdent(field)}`);
|
|
166
|
+
columns.push(field);
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
columns,
|
|
170
|
+
selectItems
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Relationship JOINs for a legacy entity. Appends each relationship's ref
|
|
175
|
+
* column to `columns`/`selectItems` in place, preserving the existing order.
|
|
176
|
+
*/
|
|
177
|
+
function legacyJoins(config, entity, columns, selectItems) {
|
|
178
|
+
const joins = [];
|
|
179
|
+
for (const [relName, rel] of Object.entries(entity.relationships ?? {})) {
|
|
180
|
+
const compiled = compileRelationship(relName, rel, entity, config.entities[rel.entity]);
|
|
181
|
+
joins.push(compiled.clause);
|
|
182
|
+
if (compiled.refSelect) {
|
|
183
|
+
selectItems.push(compiled.refSelect);
|
|
184
|
+
columns.push(compiled.refColumn);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return joins;
|
|
188
|
+
}
|
|
189
|
+
function compileEntityView(config, entityKey, options = {}) {
|
|
190
|
+
const entity = config.entities[entityKey];
|
|
191
|
+
if (!entity) throw new Error(`entity "${entityKey}" is not defined`);
|
|
192
|
+
const viewName = `${options.viewPrefix ?? "crm_src_"}${entityKey}`;
|
|
193
|
+
const src = entity.source;
|
|
194
|
+
const projection = entity.projection;
|
|
195
|
+
const filterTable = projection ? BASE_REF : src;
|
|
196
|
+
const { selectItems, columns } = projection ? compileProjectionSelect(projection, SOURCE_KEY) : legacySelect(entity, src);
|
|
197
|
+
const joins = projection ? compileProjectionJoins(projection) : legacyJoins(config, entity, columns, selectItems);
|
|
198
|
+
const conditions = compileFilterConditions(filterTable, entity.filter);
|
|
199
|
+
if (options.orgScope) conditions.push(`${quoteIdent(filterTable)}.${quoteIdent(options.orgScope.column)} = ${quoteLiteral(options.orgScope.value)}`);
|
|
200
|
+
const lines = [
|
|
201
|
+
options.securityInvoker ?? true ? `CREATE OR REPLACE VIEW ${quoteIdent(viewName)} WITH (security_invoker = true) AS` : `CREATE OR REPLACE VIEW ${quoteIdent(viewName)} AS`,
|
|
202
|
+
`SELECT ${selectItems.join(", ")}`,
|
|
203
|
+
projection ? compileProjectionFrom(projection) : `FROM ${quoteIdent(src)}`,
|
|
204
|
+
...joins
|
|
205
|
+
];
|
|
206
|
+
if (conditions.length > 0) lines.push(`WHERE ${conditions.join(" AND ")}`);
|
|
207
|
+
const sql = `${lines.join("\n")};`;
|
|
208
|
+
return {
|
|
209
|
+
entityKey,
|
|
210
|
+
viewName,
|
|
211
|
+
concept: entity.concept,
|
|
212
|
+
role: entity.role ?? DEFAULT_ROLE,
|
|
213
|
+
sourceTable: src,
|
|
214
|
+
columns,
|
|
215
|
+
sql,
|
|
216
|
+
dropSql: `DROP VIEW IF EXISTS ${quoteIdent(viewName)};`
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
/** Compile every entity in the config into its view, in declaration order. */
|
|
220
|
+
function compileEntityViews(config, options = {}) {
|
|
221
|
+
return Object.keys(config.entities).map((key) => compileEntityView(config, key, options));
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Dry-run each entity's compiled view against the host DB via a caller-provided
|
|
225
|
+
* `run` function, so `$raw` filter/join fragments — which skip schema validation
|
|
226
|
+
* (see `validateEntitiesConfig`) — fail loudly *before* the first sync instead of
|
|
227
|
+
* at sync time. Never throws: each view's outcome is collected into an
|
|
228
|
+
* `ExplainResult`, so a mapping UI or CLI can show every problem at once.
|
|
229
|
+
*
|
|
230
|
+
* `run` should execute the SQL somewhere side-effect-free — e.g. inside a
|
|
231
|
+
* transaction it rolls back, or by `EXPLAIN`-ing the statement — since the
|
|
232
|
+
* compiled SQL is a `CREATE OR REPLACE VIEW`.
|
|
233
|
+
*/
|
|
234
|
+
async function explainEntitiesConfig(config, run, options = {}) {
|
|
235
|
+
const results = [];
|
|
236
|
+
for (const view of compileEntityViews(config, options)) try {
|
|
237
|
+
await run(view.sql);
|
|
238
|
+
results.push({
|
|
239
|
+
entityKey: view.entityKey,
|
|
240
|
+
ok: true,
|
|
241
|
+
sql: view.sql
|
|
242
|
+
});
|
|
243
|
+
} catch (error) {
|
|
244
|
+
results.push({
|
|
245
|
+
entityKey: view.entityKey,
|
|
246
|
+
ok: false,
|
|
247
|
+
sql: view.sql,
|
|
248
|
+
error: error instanceof Error ? error.message : String(error)
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
return results;
|
|
252
|
+
}
|
|
253
|
+
//#endregion
|
|
254
|
+
export { explainEntitiesConfig as i, compileEntityView as n, compileEntityViews as r, DEFAULT_VIEW_PREFIX as t };
|