clivly 0.23.1 → 0.24.0-next.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 +28 -0
- package/{adapter-B4v4dwgD.d.mts → adapter--YV7du3i.d.cts} +1 -1
- package/{adapter-BK3gsby5.d.cts → adapter-DD3K3dCW.d.mts} +1 -1
- package/{auth-adapter-BIa-ZyVV.d.mts → auth-adapter-C17dzlzF.d.cts} +1 -1
- package/{auth-adapter-DwtMJb7D.d.cts → auth-adapter-CizFv7oU.d.mts} +1 -1
- package/auth-better-auth.d.cts +1 -1
- package/auth-better-auth.d.mts +1 -1
- package/auth-clerk.d.cts +1 -1
- package/auth-clerk.d.mts +1 -1
- package/auth-supabase.cjs +121 -0
- package/auth-supabase.d.cts +23 -0
- package/auth-supabase.d.mts +23 -0
- package/auth-supabase.mjs +119 -0
- package/auth-workos.d.cts +1 -1
- package/auth-workos.d.mts +1 -1
- package/core-adapter.d.cts +1 -1
- package/core-adapter.d.mts +1 -1
- package/core-auth-adapter.d.cts +1 -1
- package/core-auth-adapter.d.mts +1 -1
- package/core-mapping-score.d.cts +1 -0
- package/core-sync-engine.cjs +1 -1
- package/core-sync-engine.mjs +1 -1
- package/core-types.cjs +5 -1
- package/core-types.d.cts +2 -2
- package/core-types.d.mts +2 -2
- package/core-types.mjs +1 -1
- package/core-view-compiler.cjs +1 -1
- package/core-view-compiler.mjs +1 -1
- package/core.cjs +9 -5
- package/core.d.cts +6 -6
- package/core.d.mts +6 -6
- package/core.mjs +6 -6
- package/discover-BZIPXl23.cjs +200 -0
- package/discover-CFIi43Qn.mjs +195 -0
- package/drizzle.cjs +1 -1
- package/drizzle.d.cts +2 -2
- package/drizzle.d.mts +2 -2
- package/drizzle.mjs +1 -1
- package/executor-CJEWurjZ.mjs +23 -0
- package/executor-CU-jRgpC.cjs +40 -0
- package/index-CZkp8TV4.d.cts +108 -0
- package/index-Cnrz4ywf.d.mts +108 -0
- package/{index-Bh7o2J7K.d.mts → index-DOoX2GgI.d.mts} +3 -3
- package/{index-DRRTE5IJ.d.cts → index-uFDiQEjj.d.cts} +231 -5
- package/index.cjs +903 -30
- package/index.d.cts +27 -0
- package/index.d.mts +26 -0
- package/index.mjs +901 -28
- package/{internal-CdfVkh4O.d.mts → internal-D_2q2BOB.d.mts} +1 -1
- package/internal-adapter.d.cts +1 -1
- package/internal-adapter.d.mts +1 -1
- package/internal-auth-adapter.d.cts +1 -1
- package/internal-auth-adapter.d.mts +1 -1
- package/internal-sync-engine.cjs +1 -1
- package/internal-sync-engine.mjs +1 -1
- package/internal-types.cjs +5 -1
- package/internal-types.d.cts +2 -2
- package/internal-types.d.mts +2 -2
- package/internal-types.mjs +1 -1
- package/internal-view-compiler.cjs +1 -1
- package/internal-view-compiler.mjs +1 -1
- package/internal.cjs +9 -5
- package/internal.d.cts +5 -6
- package/internal.d.mts +6 -6
- package/internal.mjs +6 -6
- package/introspector-BwYgHGcS.cjs +363 -0
- package/introspector-Bz52xOUr.mjs +334 -0
- package/{namespace-probe-JuHriruA.d.mts → namespace-probe--GvxJ06J.d.mts} +8 -1
- package/package.json +31 -1
- package/projection-guard-7AMY0aCk.mjs +389 -0
- package/projection-guard-D8VUhCz7.cjs +508 -0
- package/{projection-sql-DQQdjw9I.mjs → projection-sql-2joBkLz2.mjs} +13 -3
- package/{projection-sql-BkrHR2ea.cjs → projection-sql-DS5sVTY-.cjs} +18 -2
- package/sdk-Bzkr24NW.cjs +207 -0
- package/sdk-D2WwGBzk.mjs +178 -0
- package/sdk.cjs +8 -6
- package/sdk.d.cts +7 -1
- package/sdk.d.mts +7 -1
- package/sdk.mjs +3 -2
- package/sql.cjs +13 -0
- package/sql.d.cts +2 -0
- package/sql.d.mts +2 -0
- package/sql.mjs +4 -0
- package/{sdk-BkMDOfCk.mjs → src-Dq2nY1Sj.mjs} +36 -495
- package/{sdk-BOarpe5I.cjs → src-X0-yIA3O.cjs} +63 -546
- package/supabase.cjs +255 -0
- package/supabase.d.cts +19 -0
- package/supabase.d.mts +19 -0
- package/supabase.mjs +254 -0
- package/{sync-engine-yFLEyId3.mjs → sync-engine-D0mJUeqP.mjs} +1 -1
- package/{sync-engine-DNSoZeo0.cjs → sync-engine-hU041kXO.cjs} +1 -1
- package/types-KMH5D5Lg.mjs +32 -0
- package/{namespace-probe-CcL6Wj8E.d.cts → types-UR-tT4c1.d.cts} +52 -45
- package/{types-BBfwsDxe.d.cts → types-f-Vcv-pl.d.cts} +18 -2
- package/{types-BBfwsDxe.d.mts → types-f-Vcv-pl.d.mts} +18 -2
- package/types-kR1kUlmz.cjs +67 -0
- package/{view-compiler-CSHSqZl7.mjs → view-compiler-BhKiezRH.mjs} +1 -1
- package/{view-compiler-NqiortOA.cjs → view-compiler-DgZadq1F.cjs} +1 -1
- package/vite.d.cts +1 -1
- package/vite.d.mts +1 -1
- package/internal-D7qVRIFb.d.cts +0 -228
- package/types-8KSw2AWg.mjs +0 -8
- package/types-B8m39L1s.cjs +0 -19
package/supabase.cjs
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_src = require("./src-X0-yIA3O.cjs");
|
|
3
|
+
const require_discover = require("./discover-BZIPXl23.cjs");
|
|
4
|
+
const require_introspector = require("./introspector-BwYgHGcS.cjs");
|
|
5
|
+
// ../sdk/src/supabase/constants.ts
|
|
6
|
+
/**
|
|
7
|
+
* The cadence this function reports as its own heartbeat interval.
|
|
8
|
+
*
|
|
9
|
+
* An Edge Function is invoked, not running, so it heartbeats only when Clivly
|
|
10
|
+
* pulls it. This is the cloud's idle auto-sync slot: `IDLE_SLOT_MS`
|
|
11
|
+
* (15 * 60_000) in `packages/api/src/auto-sync-schedule.ts`, the longest gap
|
|
12
|
+
* between pulls for a healthy app. `livenessWindows()` in
|
|
13
|
+
* `packages/core/src/integration-state.ts` treats an app as live for 2.5x its
|
|
14
|
+
* reported cadence, so the function stays live between pulls. The sdk must not
|
|
15
|
+
* import packages/api, so the value is repeated here; a pin test (Task 12)
|
|
16
|
+
* keeps the two in step.
|
|
17
|
+
*/
|
|
18
|
+
const SUPABASE_EDGE_HEARTBEAT_MS = 9e5;
|
|
19
|
+
// ../sdk/src/supabase/index.ts
|
|
20
|
+
const AUTH_USERS = "auth.users";
|
|
21
|
+
const PROFILES = "profiles";
|
|
22
|
+
const ENTITY_KEY = "users";
|
|
23
|
+
const OBJECT_TYPE = "users";
|
|
24
|
+
const ROUTE_PREFIX = "/functions/v1";
|
|
25
|
+
const TRAILING_SLASH = /\/+$/;
|
|
26
|
+
const OK = 200;
|
|
27
|
+
const BAD_GATEWAY = 502;
|
|
28
|
+
const SERVER_ERROR = 500;
|
|
29
|
+
const UNAVAILABLE = 503;
|
|
30
|
+
const NOT_FOUND = 404;
|
|
31
|
+
const json = (body, status) => new Response(JSON.stringify(body), {
|
|
32
|
+
status,
|
|
33
|
+
headers: { "content-type": "application/json" }
|
|
34
|
+
});
|
|
35
|
+
const routeOf = (req) => {
|
|
36
|
+
let path = new URL(req.url).pathname;
|
|
37
|
+
if (path === ROUTE_PREFIX || path.startsWith(`${ROUTE_PREFIX}/`)) path = path.slice(13);
|
|
38
|
+
return path.replace(TRAILING_SLASH, "") || "/";
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* `public.profiles` joins only when its `id` is a foreign key to auth.users.
|
|
42
|
+
* Its columns are exposed under their bare names; one that collides with an
|
|
43
|
+
* `auth.users` column (`id`, `email`, `created_at`, `updated_at`...) is
|
|
44
|
+
* dropped, not prefixed: both rows are the same person, so the auth value
|
|
45
|
+
* stays the single source of truth for that name.
|
|
46
|
+
*/
|
|
47
|
+
function profileJoin(tables, authColumns) {
|
|
48
|
+
const profiles = tables.find((t) => t.name === PROFILES);
|
|
49
|
+
const id = profiles?.columnsMeta?.find((c) => c.name === "id");
|
|
50
|
+
if (!(profiles && id?.isForeignKey && id.references?.table === AUTH_USERS)) return null;
|
|
51
|
+
const columns = (profiles.columnsMeta ?? []).filter((c) => !authColumns.includes(c.name)).map((c) => ({
|
|
52
|
+
...c,
|
|
53
|
+
isPrimaryKey: false,
|
|
54
|
+
isForeignKey: false
|
|
55
|
+
}));
|
|
56
|
+
return columns.length === 0 ? null : { columns };
|
|
57
|
+
}
|
|
58
|
+
const flatKey = (key) => key.startsWith("base.") || key.startsWith("p.") ? key.slice(key.indexOf(".") + 1) : key;
|
|
59
|
+
function profilesProjection(authColumns, join) {
|
|
60
|
+
const bindings = {};
|
|
61
|
+
for (const column of authColumns) if (column !== "id") bindings[column] = { from: {
|
|
62
|
+
table: "base",
|
|
63
|
+
column
|
|
64
|
+
} };
|
|
65
|
+
for (const { name } of join.columns) bindings[name] = { from: {
|
|
66
|
+
table: "p",
|
|
67
|
+
column: name
|
|
68
|
+
} };
|
|
69
|
+
return {
|
|
70
|
+
version: 1,
|
|
71
|
+
kind: "projection",
|
|
72
|
+
entityKey: ENTITY_KEY,
|
|
73
|
+
baseTable: AUTH_USERS,
|
|
74
|
+
joins: [{
|
|
75
|
+
key: "p",
|
|
76
|
+
table: PROFILES,
|
|
77
|
+
type: "left",
|
|
78
|
+
on: [{
|
|
79
|
+
left: {
|
|
80
|
+
table: "base",
|
|
81
|
+
column: "id"
|
|
82
|
+
},
|
|
83
|
+
op: "eq",
|
|
84
|
+
right: {
|
|
85
|
+
table: "p",
|
|
86
|
+
column: "id"
|
|
87
|
+
}
|
|
88
|
+
}]
|
|
89
|
+
}],
|
|
90
|
+
fieldBindings: bindings,
|
|
91
|
+
identity: {
|
|
92
|
+
kind: "base_pk",
|
|
93
|
+
column: "id"
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The projection writes each column under its ref token (`p.full_name`). The
|
|
99
|
+
* cloud reads `row[sourceColumn]` through a mapping built from the heartbeat
|
|
100
|
+
* schema, so contact rows go out flat, under bare column names.
|
|
101
|
+
*/
|
|
102
|
+
const flatten = (source) => ({
|
|
103
|
+
...source,
|
|
104
|
+
async fetchPage(args) {
|
|
105
|
+
return (await source.fetchPage(args)).map((row) => Object.fromEntries(Object.entries(row).map(([key, value]) => [flatKey(key), value])));
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
var ContactTableMissing = class extends Error {};
|
|
109
|
+
/** A cold-start failure, reduced to the one code that is safe to return. */
|
|
110
|
+
var SetupFailure = class extends Error {
|
|
111
|
+
reason;
|
|
112
|
+
constructor(reason) {
|
|
113
|
+
super(reason);
|
|
114
|
+
this.reason = reason;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
function planContact(executor, tables, contact) {
|
|
118
|
+
const table = contact?.table ?? AUTH_USERS;
|
|
119
|
+
const meta = tables.find((t) => t.name === table);
|
|
120
|
+
if (!meta) throw new ContactTableMissing();
|
|
121
|
+
const join = table === AUTH_USERS && contact?.joinProfiles !== false ? profileJoin(tables, meta.columns) : null;
|
|
122
|
+
const base = require_introspector.fromSql(executor, {
|
|
123
|
+
entity: ENTITY_KEY,
|
|
124
|
+
table,
|
|
125
|
+
meta,
|
|
126
|
+
pushSource: table
|
|
127
|
+
});
|
|
128
|
+
if (!join) return {
|
|
129
|
+
schema: tables,
|
|
130
|
+
source: base,
|
|
131
|
+
table
|
|
132
|
+
};
|
|
133
|
+
const advertised = {
|
|
134
|
+
...meta,
|
|
135
|
+
columns: [...meta.columns, ...join.columns.map((c) => c.name)],
|
|
136
|
+
columnsMeta: [...meta.columnsMeta ?? [], ...join.columns]
|
|
137
|
+
};
|
|
138
|
+
const projected = require_introspector.fromSqlProjection(executor, profilesProjection(meta.columns, join), {
|
|
139
|
+
entity: ENTITY_KEY,
|
|
140
|
+
objectType: OBJECT_TYPE
|
|
141
|
+
});
|
|
142
|
+
return {
|
|
143
|
+
schema: tables.map((t) => t === meta ? advertised : t),
|
|
144
|
+
source: flatten({
|
|
145
|
+
...projected,
|
|
146
|
+
pushSource: table
|
|
147
|
+
}),
|
|
148
|
+
table
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* The Edge Function handler: `POST /clivly/tick` runs a sync Clivly asked for,
|
|
153
|
+
* `GET /clivly/connect` checks the connection, and every other request is a 404
|
|
154
|
+
* that still heartbeats through `waitUntil`.
|
|
155
|
+
*/
|
|
156
|
+
function createSupabaseClivly(opts) {
|
|
157
|
+
const { env, executor } = opts;
|
|
158
|
+
const secretKey = env.CLIVLY_SECRET_KEY;
|
|
159
|
+
let runtime = null;
|
|
160
|
+
const getRuntime = () => {
|
|
161
|
+
runtime ??= (async () => {
|
|
162
|
+
let tables;
|
|
163
|
+
try {
|
|
164
|
+
tables = await require_discover.discoverFromSql(executor);
|
|
165
|
+
} catch {
|
|
166
|
+
throw new SetupFailure("discovery_failed");
|
|
167
|
+
}
|
|
168
|
+
let plan;
|
|
169
|
+
try {
|
|
170
|
+
plan = planContact(executor, tables, opts.contact);
|
|
171
|
+
} catch (error) {
|
|
172
|
+
throw new SetupFailure(error instanceof ContactTableMissing ? "contact_table_missing" : "sdk_init_failed");
|
|
173
|
+
}
|
|
174
|
+
try {
|
|
175
|
+
return buildRuntime(plan);
|
|
176
|
+
} catch {
|
|
177
|
+
throw new SetupFailure("sdk_init_failed");
|
|
178
|
+
}
|
|
179
|
+
})().catch((error) => {
|
|
180
|
+
runtime = null;
|
|
181
|
+
throw error;
|
|
182
|
+
});
|
|
183
|
+
return runtime;
|
|
184
|
+
};
|
|
185
|
+
const buildRuntime = (plan) => {
|
|
186
|
+
const sdk = require_src.createClivlySDK({
|
|
187
|
+
apiKey: secretKey ?? "",
|
|
188
|
+
apiUrl: env.CLIVLY_API_URL,
|
|
189
|
+
env,
|
|
190
|
+
schema: plan.schema,
|
|
191
|
+
source: { contacts: plan.source },
|
|
192
|
+
contactEntity: plan.table,
|
|
193
|
+
introspector: require_introspector.sqlIntrospector(executor),
|
|
194
|
+
appIdStore: require_introspector.sqlAppIdStore(executor),
|
|
195
|
+
cursorStore: require_src.memoryCursorStore(),
|
|
196
|
+
syncTrigger: {
|
|
197
|
+
url: `${env.SUPABASE_URL}/functions/v1/clivly/tick`,
|
|
198
|
+
secret: env.CLIVLY_SYNC_TRIGGER_SECRET
|
|
199
|
+
},
|
|
200
|
+
framework: "supabase-edge",
|
|
201
|
+
heartbeatIntervalMs: SUPABASE_EDGE_HEARTBEAT_MS,
|
|
202
|
+
sync: { onBoot: false }
|
|
203
|
+
});
|
|
204
|
+
return {
|
|
205
|
+
sdk,
|
|
206
|
+
table: plan.table,
|
|
207
|
+
tick: sdk.createClivlyHandler()
|
|
208
|
+
};
|
|
209
|
+
};
|
|
210
|
+
const heartbeatInBackground = () => {
|
|
211
|
+
if (!(opts.waitUntil && secretKey)) return;
|
|
212
|
+
try {
|
|
213
|
+
opts.waitUntil(getRuntime().then((r) => r.sdk.connect()).catch(() => void 0));
|
|
214
|
+
} catch {}
|
|
215
|
+
};
|
|
216
|
+
const missingKey = () => json({
|
|
217
|
+
ok: false,
|
|
218
|
+
reason: "missing_secret_key"
|
|
219
|
+
}, UNAVAILABLE);
|
|
220
|
+
const failure = (error) => json({
|
|
221
|
+
ok: false,
|
|
222
|
+
reason: error instanceof SetupFailure ? error.reason : "connect_failed"
|
|
223
|
+
}, SERVER_ERROR);
|
|
224
|
+
const connect = async () => {
|
|
225
|
+
if (!secretKey) return missingKey();
|
|
226
|
+
try {
|
|
227
|
+
const { sdk, table } = await getRuntime();
|
|
228
|
+
const result = await sdk.connect();
|
|
229
|
+
if (!result.ok) return json(result, BAD_GATEWAY);
|
|
230
|
+
const hasContacts = await require_introspector.existsFromSql(executor, { table });
|
|
231
|
+
return json({
|
|
232
|
+
...result,
|
|
233
|
+
hasContacts
|
|
234
|
+
}, OK);
|
|
235
|
+
} catch (error) {
|
|
236
|
+
return failure(error);
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
const tick = async (req) => {
|
|
240
|
+
if (!secretKey) return missingKey();
|
|
241
|
+
try {
|
|
242
|
+
return await (await getRuntime()).tick(req);
|
|
243
|
+
} catch (error) {
|
|
244
|
+
return failure(error);
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
return (req) => {
|
|
248
|
+
const path = routeOf(req);
|
|
249
|
+
if (path === "/clivly/connect" && req.method === "GET") return connect();
|
|
250
|
+
heartbeatInBackground();
|
|
251
|
+
if (path === "/clivly/tick") return tick(req);
|
|
252
|
+
return Promise.resolve(json({ error: "not_found" }, NOT_FOUND));
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
exports.createSupabaseClivly = createSupabaseClivly;
|
package/supabase.d.cts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { s as SqlExecutor } from "./index-CZkp8TV4.cjs";
|
|
2
|
+
export interface SupabaseClivlyOptions {
|
|
3
|
+
/** Defaults to `auth.users`, joined to `public.profiles` when it qualifies. */
|
|
4
|
+
contact?: {
|
|
5
|
+
joinProfiles?: boolean;
|
|
6
|
+
table: string;
|
|
7
|
+
};
|
|
8
|
+
/** The function's environment, e.g. `Deno.env.toObject()`. */
|
|
9
|
+
env: Record<string, string | undefined>;
|
|
10
|
+
executor: SqlExecutor;
|
|
11
|
+
/** `EdgeRuntime.waitUntil`: lets every request double as a heartbeat. */
|
|
12
|
+
waitUntil?: (promise: Promise<unknown>) => void;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The Edge Function handler: `POST /clivly/tick` runs a sync Clivly asked for,
|
|
16
|
+
* `GET /clivly/connect` checks the connection, and every other request is a 404
|
|
17
|
+
* that still heartbeats through `waitUntil`.
|
|
18
|
+
*/
|
|
19
|
+
export declare function createSupabaseClivly(opts: SupabaseClivlyOptions): (req: Request) => Promise<Response>;
|
package/supabase.d.mts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { s as SqlExecutor } from "./index-Cnrz4ywf.mjs";
|
|
2
|
+
export interface SupabaseClivlyOptions {
|
|
3
|
+
/** Defaults to `auth.users`, joined to `public.profiles` when it qualifies. */
|
|
4
|
+
contact?: {
|
|
5
|
+
joinProfiles?: boolean;
|
|
6
|
+
table: string;
|
|
7
|
+
};
|
|
8
|
+
/** The function's environment, e.g. `Deno.env.toObject()`. */
|
|
9
|
+
env: Record<string, string | undefined>;
|
|
10
|
+
executor: SqlExecutor;
|
|
11
|
+
/** `EdgeRuntime.waitUntil`: lets every request double as a heartbeat. */
|
|
12
|
+
waitUntil?: (promise: Promise<unknown>) => void;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The Edge Function handler: `POST /clivly/tick` runs a sync Clivly asked for,
|
|
16
|
+
* `GET /clivly/connect` checks the connection, and every other request is a 404
|
|
17
|
+
* that still heartbeats through `waitUntil`.
|
|
18
|
+
*/
|
|
19
|
+
export declare function createSupabaseClivly(opts: SupabaseClivlyOptions): (req: Request) => Promise<Response>;
|
package/supabase.mjs
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import { i as memoryCursorStore, n as createClivlySDK } from "./src-Dq2nY1Sj.mjs";
|
|
2
|
+
import { t as discoverFromSql } from "./discover-CFIi43Qn.mjs";
|
|
3
|
+
import { a as sqlAppIdStore, i as fromSql, n as fromSqlProjection, r as existsFromSql, t as sqlIntrospector } from "./introspector-Bz52xOUr.mjs";
|
|
4
|
+
// ../sdk/src/supabase/constants.ts
|
|
5
|
+
/**
|
|
6
|
+
* The cadence this function reports as its own heartbeat interval.
|
|
7
|
+
*
|
|
8
|
+
* An Edge Function is invoked, not running, so it heartbeats only when Clivly
|
|
9
|
+
* pulls it. This is the cloud's idle auto-sync slot: `IDLE_SLOT_MS`
|
|
10
|
+
* (15 * 60_000) in `packages/api/src/auto-sync-schedule.ts`, the longest gap
|
|
11
|
+
* between pulls for a healthy app. `livenessWindows()` in
|
|
12
|
+
* `packages/core/src/integration-state.ts` treats an app as live for 2.5x its
|
|
13
|
+
* reported cadence, so the function stays live between pulls. The sdk must not
|
|
14
|
+
* import packages/api, so the value is repeated here; a pin test (Task 12)
|
|
15
|
+
* keeps the two in step.
|
|
16
|
+
*/
|
|
17
|
+
const SUPABASE_EDGE_HEARTBEAT_MS = 9e5;
|
|
18
|
+
// ../sdk/src/supabase/index.ts
|
|
19
|
+
const AUTH_USERS = "auth.users";
|
|
20
|
+
const PROFILES = "profiles";
|
|
21
|
+
const ENTITY_KEY = "users";
|
|
22
|
+
const OBJECT_TYPE = "users";
|
|
23
|
+
const ROUTE_PREFIX = "/functions/v1";
|
|
24
|
+
const TRAILING_SLASH = /\/+$/;
|
|
25
|
+
const OK = 200;
|
|
26
|
+
const BAD_GATEWAY = 502;
|
|
27
|
+
const SERVER_ERROR = 500;
|
|
28
|
+
const UNAVAILABLE = 503;
|
|
29
|
+
const NOT_FOUND = 404;
|
|
30
|
+
const json = (body, status) => new Response(JSON.stringify(body), {
|
|
31
|
+
status,
|
|
32
|
+
headers: { "content-type": "application/json" }
|
|
33
|
+
});
|
|
34
|
+
const routeOf = (req) => {
|
|
35
|
+
let path = new URL(req.url).pathname;
|
|
36
|
+
if (path === ROUTE_PREFIX || path.startsWith(`${ROUTE_PREFIX}/`)) path = path.slice(13);
|
|
37
|
+
return path.replace(TRAILING_SLASH, "") || "/";
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* `public.profiles` joins only when its `id` is a foreign key to auth.users.
|
|
41
|
+
* Its columns are exposed under their bare names; one that collides with an
|
|
42
|
+
* `auth.users` column (`id`, `email`, `created_at`, `updated_at`...) is
|
|
43
|
+
* dropped, not prefixed: both rows are the same person, so the auth value
|
|
44
|
+
* stays the single source of truth for that name.
|
|
45
|
+
*/
|
|
46
|
+
function profileJoin(tables, authColumns) {
|
|
47
|
+
const profiles = tables.find((t) => t.name === PROFILES);
|
|
48
|
+
const id = profiles?.columnsMeta?.find((c) => c.name === "id");
|
|
49
|
+
if (!(profiles && id?.isForeignKey && id.references?.table === AUTH_USERS)) return null;
|
|
50
|
+
const columns = (profiles.columnsMeta ?? []).filter((c) => !authColumns.includes(c.name)).map((c) => ({
|
|
51
|
+
...c,
|
|
52
|
+
isPrimaryKey: false,
|
|
53
|
+
isForeignKey: false
|
|
54
|
+
}));
|
|
55
|
+
return columns.length === 0 ? null : { columns };
|
|
56
|
+
}
|
|
57
|
+
const flatKey = (key) => key.startsWith("base.") || key.startsWith("p.") ? key.slice(key.indexOf(".") + 1) : key;
|
|
58
|
+
function profilesProjection(authColumns, join) {
|
|
59
|
+
const bindings = {};
|
|
60
|
+
for (const column of authColumns) if (column !== "id") bindings[column] = { from: {
|
|
61
|
+
table: "base",
|
|
62
|
+
column
|
|
63
|
+
} };
|
|
64
|
+
for (const { name } of join.columns) bindings[name] = { from: {
|
|
65
|
+
table: "p",
|
|
66
|
+
column: name
|
|
67
|
+
} };
|
|
68
|
+
return {
|
|
69
|
+
version: 1,
|
|
70
|
+
kind: "projection",
|
|
71
|
+
entityKey: ENTITY_KEY,
|
|
72
|
+
baseTable: AUTH_USERS,
|
|
73
|
+
joins: [{
|
|
74
|
+
key: "p",
|
|
75
|
+
table: PROFILES,
|
|
76
|
+
type: "left",
|
|
77
|
+
on: [{
|
|
78
|
+
left: {
|
|
79
|
+
table: "base",
|
|
80
|
+
column: "id"
|
|
81
|
+
},
|
|
82
|
+
op: "eq",
|
|
83
|
+
right: {
|
|
84
|
+
table: "p",
|
|
85
|
+
column: "id"
|
|
86
|
+
}
|
|
87
|
+
}]
|
|
88
|
+
}],
|
|
89
|
+
fieldBindings: bindings,
|
|
90
|
+
identity: {
|
|
91
|
+
kind: "base_pk",
|
|
92
|
+
column: "id"
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* The projection writes each column under its ref token (`p.full_name`). The
|
|
98
|
+
* cloud reads `row[sourceColumn]` through a mapping built from the heartbeat
|
|
99
|
+
* schema, so contact rows go out flat, under bare column names.
|
|
100
|
+
*/
|
|
101
|
+
const flatten = (source) => ({
|
|
102
|
+
...source,
|
|
103
|
+
async fetchPage(args) {
|
|
104
|
+
return (await source.fetchPage(args)).map((row) => Object.fromEntries(Object.entries(row).map(([key, value]) => [flatKey(key), value])));
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
var ContactTableMissing = class extends Error {};
|
|
108
|
+
/** A cold-start failure, reduced to the one code that is safe to return. */
|
|
109
|
+
var SetupFailure = class extends Error {
|
|
110
|
+
reason;
|
|
111
|
+
constructor(reason) {
|
|
112
|
+
super(reason);
|
|
113
|
+
this.reason = reason;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
function planContact(executor, tables, contact) {
|
|
117
|
+
const table = contact?.table ?? AUTH_USERS;
|
|
118
|
+
const meta = tables.find((t) => t.name === table);
|
|
119
|
+
if (!meta) throw new ContactTableMissing();
|
|
120
|
+
const join = table === AUTH_USERS && contact?.joinProfiles !== false ? profileJoin(tables, meta.columns) : null;
|
|
121
|
+
const base = fromSql(executor, {
|
|
122
|
+
entity: ENTITY_KEY,
|
|
123
|
+
table,
|
|
124
|
+
meta,
|
|
125
|
+
pushSource: table
|
|
126
|
+
});
|
|
127
|
+
if (!join) return {
|
|
128
|
+
schema: tables,
|
|
129
|
+
source: base,
|
|
130
|
+
table
|
|
131
|
+
};
|
|
132
|
+
const advertised = {
|
|
133
|
+
...meta,
|
|
134
|
+
columns: [...meta.columns, ...join.columns.map((c) => c.name)],
|
|
135
|
+
columnsMeta: [...meta.columnsMeta ?? [], ...join.columns]
|
|
136
|
+
};
|
|
137
|
+
const projected = fromSqlProjection(executor, profilesProjection(meta.columns, join), {
|
|
138
|
+
entity: ENTITY_KEY,
|
|
139
|
+
objectType: OBJECT_TYPE
|
|
140
|
+
});
|
|
141
|
+
return {
|
|
142
|
+
schema: tables.map((t) => t === meta ? advertised : t),
|
|
143
|
+
source: flatten({
|
|
144
|
+
...projected,
|
|
145
|
+
pushSource: table
|
|
146
|
+
}),
|
|
147
|
+
table
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* The Edge Function handler: `POST /clivly/tick` runs a sync Clivly asked for,
|
|
152
|
+
* `GET /clivly/connect` checks the connection, and every other request is a 404
|
|
153
|
+
* that still heartbeats through `waitUntil`.
|
|
154
|
+
*/
|
|
155
|
+
function createSupabaseClivly(opts) {
|
|
156
|
+
const { env, executor } = opts;
|
|
157
|
+
const secretKey = env.CLIVLY_SECRET_KEY;
|
|
158
|
+
let runtime = null;
|
|
159
|
+
const getRuntime = () => {
|
|
160
|
+
runtime ??= (async () => {
|
|
161
|
+
let tables;
|
|
162
|
+
try {
|
|
163
|
+
tables = await discoverFromSql(executor);
|
|
164
|
+
} catch {
|
|
165
|
+
throw new SetupFailure("discovery_failed");
|
|
166
|
+
}
|
|
167
|
+
let plan;
|
|
168
|
+
try {
|
|
169
|
+
plan = planContact(executor, tables, opts.contact);
|
|
170
|
+
} catch (error) {
|
|
171
|
+
throw new SetupFailure(error instanceof ContactTableMissing ? "contact_table_missing" : "sdk_init_failed");
|
|
172
|
+
}
|
|
173
|
+
try {
|
|
174
|
+
return buildRuntime(plan);
|
|
175
|
+
} catch {
|
|
176
|
+
throw new SetupFailure("sdk_init_failed");
|
|
177
|
+
}
|
|
178
|
+
})().catch((error) => {
|
|
179
|
+
runtime = null;
|
|
180
|
+
throw error;
|
|
181
|
+
});
|
|
182
|
+
return runtime;
|
|
183
|
+
};
|
|
184
|
+
const buildRuntime = (plan) => {
|
|
185
|
+
const sdk = createClivlySDK({
|
|
186
|
+
apiKey: secretKey ?? "",
|
|
187
|
+
apiUrl: env.CLIVLY_API_URL,
|
|
188
|
+
env,
|
|
189
|
+
schema: plan.schema,
|
|
190
|
+
source: { contacts: plan.source },
|
|
191
|
+
contactEntity: plan.table,
|
|
192
|
+
introspector: sqlIntrospector(executor),
|
|
193
|
+
appIdStore: sqlAppIdStore(executor),
|
|
194
|
+
cursorStore: memoryCursorStore(),
|
|
195
|
+
syncTrigger: {
|
|
196
|
+
url: `${env.SUPABASE_URL}/functions/v1/clivly/tick`,
|
|
197
|
+
secret: env.CLIVLY_SYNC_TRIGGER_SECRET
|
|
198
|
+
},
|
|
199
|
+
framework: "supabase-edge",
|
|
200
|
+
heartbeatIntervalMs: SUPABASE_EDGE_HEARTBEAT_MS,
|
|
201
|
+
sync: { onBoot: false }
|
|
202
|
+
});
|
|
203
|
+
return {
|
|
204
|
+
sdk,
|
|
205
|
+
table: plan.table,
|
|
206
|
+
tick: sdk.createClivlyHandler()
|
|
207
|
+
};
|
|
208
|
+
};
|
|
209
|
+
const heartbeatInBackground = () => {
|
|
210
|
+
if (!(opts.waitUntil && secretKey)) return;
|
|
211
|
+
try {
|
|
212
|
+
opts.waitUntil(getRuntime().then((r) => r.sdk.connect()).catch(() => void 0));
|
|
213
|
+
} catch {}
|
|
214
|
+
};
|
|
215
|
+
const missingKey = () => json({
|
|
216
|
+
ok: false,
|
|
217
|
+
reason: "missing_secret_key"
|
|
218
|
+
}, UNAVAILABLE);
|
|
219
|
+
const failure = (error) => json({
|
|
220
|
+
ok: false,
|
|
221
|
+
reason: error instanceof SetupFailure ? error.reason : "connect_failed"
|
|
222
|
+
}, SERVER_ERROR);
|
|
223
|
+
const connect = async () => {
|
|
224
|
+
if (!secretKey) return missingKey();
|
|
225
|
+
try {
|
|
226
|
+
const { sdk, table } = await getRuntime();
|
|
227
|
+
const result = await sdk.connect();
|
|
228
|
+
if (!result.ok) return json(result, BAD_GATEWAY);
|
|
229
|
+
const hasContacts = await existsFromSql(executor, { table });
|
|
230
|
+
return json({
|
|
231
|
+
...result,
|
|
232
|
+
hasContacts
|
|
233
|
+
}, OK);
|
|
234
|
+
} catch (error) {
|
|
235
|
+
return failure(error);
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
const tick = async (req) => {
|
|
239
|
+
if (!secretKey) return missingKey();
|
|
240
|
+
try {
|
|
241
|
+
return await (await getRuntime()).tick(req);
|
|
242
|
+
} catch (error) {
|
|
243
|
+
return failure(error);
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
return (req) => {
|
|
247
|
+
const path = routeOf(req);
|
|
248
|
+
if (path === "/clivly/connect" && req.method === "GET") return connect();
|
|
249
|
+
heartbeatInBackground();
|
|
250
|
+
if (path === "/clivly/tick") return tick(req);
|
|
251
|
+
return Promise.resolve(json({ error: "not_found" }, NOT_FOUND));
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
export { createSupabaseClivly };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { r as compileEntityViews } from "./view-compiler-
|
|
1
|
+
import { r as compileEntityViews } from "./view-compiler-BhKiezRH.mjs";
|
|
2
2
|
// ../core/src/sync-engine.ts
|
|
3
3
|
/** Actions that remove something — only safe when the source set is complete. */
|
|
4
4
|
function isRemovalAction(action) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_view_compiler = require("./view-compiler-
|
|
1
|
+
const require_view_compiler = require("./view-compiler-DgZadq1F.cjs");
|
|
2
2
|
// ../core/src/sync-engine.ts
|
|
3
3
|
/** Actions that remove something — only safe when the source set is complete. */
|
|
4
4
|
function isRemovalAction(action) {
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// ../core/src/types.ts
|
|
2
|
+
/**
|
|
3
|
+
* Every contact status, in display order. The first four are the lifecycle
|
|
4
|
+
* (lead → active → customer → churned). `vendor` sits outside it: it says
|
|
5
|
+
* *who* someone is (a business selling to you), not how far along they are.
|
|
6
|
+
* Only a person sets it, so it is never derived or synced.
|
|
7
|
+
*/
|
|
8
|
+
const CONTACT_STATUSES = [
|
|
9
|
+
"lead",
|
|
10
|
+
"active",
|
|
11
|
+
"customer",
|
|
12
|
+
"churned",
|
|
13
|
+
"vendor"
|
|
14
|
+
];
|
|
15
|
+
/** The statuses the lifecycle moves through; reports count only these. */
|
|
16
|
+
const LIFECYCLE_STATUSES = [
|
|
17
|
+
"lead",
|
|
18
|
+
"active",
|
|
19
|
+
"customer",
|
|
20
|
+
"churned"
|
|
21
|
+
];
|
|
22
|
+
const VENDOR_STATUS = "vendor";
|
|
23
|
+
function isContactStatus(value) {
|
|
24
|
+
return CONTACT_STATUSES.includes(value);
|
|
25
|
+
}
|
|
26
|
+
/** Named once so SQL predicates and UI fallbacks cannot drift apart. */
|
|
27
|
+
const OPEN_STAGE_TYPE = "open";
|
|
28
|
+
/** A closed deal is a historical fact: automation never reopens one. */
|
|
29
|
+
function isClosedStageType(type) {
|
|
30
|
+
return type === "won" || type === "lost";
|
|
31
|
+
}
|
|
32
|
+
export { isClosedStageType as a, VENDOR_STATUS as i, LIFECYCLE_STATUSES as n, isContactStatus as o, OPEN_STAGE_TYPE as r, CONTACT_STATUSES as t };
|