clivly 0.18.0 → 0.19.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/drizzle.cjs +51 -2
- package/drizzle.d.cts +34 -2
- package/drizzle.d.mts +34 -2
- package/drizzle.mjs +50 -3
- package/index.cjs +11 -3
- package/index.mjs +11 -3
- package/{namespace-probe-qgjiudCo.d.cts → namespace-probe-C1tm5rGh.d.cts} +74 -1
- package/{namespace-probe-BAXprXQh.d.mts → namespace-probe-ah2awcHO.d.mts} +74 -1
- package/package.json +1 -1
- package/{sdk-D53fH4uu.cjs → sdk-BEHVvB18.cjs} +251 -23
- package/{sdk-D5J7kRQm.mjs → sdk-ChyrSl32.mjs} +251 -23
- package/sdk.cjs +1 -1
- package/sdk.d.cts +2 -2
- package/sdk.d.mts +2 -2
- package/sdk.mjs +1 -1
- package/vite.d.cts +1 -1
- package/vite.d.mts +1 -1
|
@@ -1358,7 +1358,7 @@ function selfCheck(input) {
|
|
|
1358
1358
|
return issues;
|
|
1359
1359
|
}
|
|
1360
1360
|
// ../sdk/src/sync.ts
|
|
1361
|
-
const DEFAULT_ID_FIELD = "id";
|
|
1361
|
+
const DEFAULT_ID_FIELD$1 = "id";
|
|
1362
1362
|
function isAfter(candidate, current) {
|
|
1363
1363
|
const ct = candidate.time.getTime();
|
|
1364
1364
|
const bt = current.time.getTime();
|
|
@@ -1381,9 +1381,64 @@ function nextCursor(rows, cursorField, idField, current) {
|
|
|
1381
1381
|
}
|
|
1382
1382
|
return max;
|
|
1383
1383
|
}
|
|
1384
|
+
const CREATED_AT_CANDIDATES = [
|
|
1385
|
+
"created_at",
|
|
1386
|
+
"createdAt",
|
|
1387
|
+
"inserted_at",
|
|
1388
|
+
"insertedAt",
|
|
1389
|
+
"signed_up_at",
|
|
1390
|
+
"signedUpAt",
|
|
1391
|
+
"joined_at",
|
|
1392
|
+
"joinedAt"
|
|
1393
|
+
];
|
|
1394
|
+
/**
|
|
1395
|
+
* Which columns of these rows hold their times: the cursor column (it moves
|
|
1396
|
+
* when a row changes) and the creation column, configured or detected from
|
|
1397
|
+
* the rows' own keys. A creation column the rows do not carry is left out.
|
|
1398
|
+
*/
|
|
1399
|
+
function sourceTimeFields(source, rows) {
|
|
1400
|
+
const first = rows[0] ?? {};
|
|
1401
|
+
const createdAt = source.createdAtField ?? CREATED_AT_CANDIDATES.find((column) => column in first);
|
|
1402
|
+
return createdAt === void 0 ? { updatedAt: source.cursorField } : {
|
|
1403
|
+
updatedAt: source.cursorField,
|
|
1404
|
+
createdAt
|
|
1405
|
+
};
|
|
1406
|
+
}
|
|
1407
|
+
function toWireCursor(cursor) {
|
|
1408
|
+
return {
|
|
1409
|
+
id: cursor.id,
|
|
1410
|
+
time: cursor.time.toISOString()
|
|
1411
|
+
};
|
|
1412
|
+
}
|
|
1413
|
+
/**
|
|
1414
|
+
* Read the positions Clivly sent with a triggered sync. Anything malformed is
|
|
1415
|
+
* dropped, so that source falls back to the app's own cursor store.
|
|
1416
|
+
*/
|
|
1417
|
+
function parseWireCursors(value) {
|
|
1418
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return;
|
|
1419
|
+
const out = {};
|
|
1420
|
+
for (const [entity, raw] of Object.entries(value)) {
|
|
1421
|
+
if (raw === null || typeof raw !== "object") continue;
|
|
1422
|
+
const { id, time } = raw;
|
|
1423
|
+
if (typeof id !== "string" || typeof time !== "string") continue;
|
|
1424
|
+
const parsed = new Date(time);
|
|
1425
|
+
if (!Number.isNaN(parsed.getTime())) out[entity] = {
|
|
1426
|
+
id,
|
|
1427
|
+
time: parsed
|
|
1428
|
+
};
|
|
1429
|
+
}
|
|
1430
|
+
return out;
|
|
1431
|
+
}
|
|
1432
|
+
function cursorReport(source, cursor, report) {
|
|
1433
|
+
return report && cursor ? {
|
|
1434
|
+
entity: source.entity,
|
|
1435
|
+
nextCursor: toWireCursor(cursor)
|
|
1436
|
+
} : {};
|
|
1437
|
+
}
|
|
1384
1438
|
async function syncSource(args) {
|
|
1385
1439
|
const { source, path, since, batchSize, push, objectType, maxRows } = args;
|
|
1386
|
-
const
|
|
1440
|
+
const reportCursor = args.reportCursor ?? true;
|
|
1441
|
+
const idField = source.idField ?? DEFAULT_ID_FIELD$1;
|
|
1387
1442
|
let cursor = since;
|
|
1388
1443
|
let pushed = 0;
|
|
1389
1444
|
const resumedFrom = since === null ? null : since.time.toISOString();
|
|
@@ -1394,12 +1449,15 @@ async function syncSource(args) {
|
|
|
1394
1449
|
});
|
|
1395
1450
|
if (rows.length === 0) break;
|
|
1396
1451
|
const capped = maxRows === void 0 ? rows : rows.slice(0, maxRows);
|
|
1452
|
+
const next = nextCursor(capped, source.cursorField, idField, cursor);
|
|
1397
1453
|
if (!await push(path, {
|
|
1398
1454
|
source: pushSourceOf(source),
|
|
1399
1455
|
rows: capped,
|
|
1400
1456
|
mode: "delta",
|
|
1401
1457
|
fetched: rows.length,
|
|
1402
1458
|
cursor: resumedFrom,
|
|
1459
|
+
sourceFields: sourceTimeFields(source, capped),
|
|
1460
|
+
...cursorReport(source, next, reportCursor),
|
|
1403
1461
|
...objectType ? { objectType } : {}
|
|
1404
1462
|
})) return {
|
|
1405
1463
|
pushed,
|
|
@@ -1407,7 +1465,7 @@ async function syncSource(args) {
|
|
|
1407
1465
|
ok: false
|
|
1408
1466
|
};
|
|
1409
1467
|
pushed += capped.length;
|
|
1410
|
-
cursor =
|
|
1468
|
+
cursor = next;
|
|
1411
1469
|
if (maxRows !== void 0 || rows.length < batchSize) break;
|
|
1412
1470
|
}
|
|
1413
1471
|
return {
|
|
@@ -1418,7 +1476,8 @@ async function syncSource(args) {
|
|
|
1418
1476
|
}
|
|
1419
1477
|
async function fullSyncSource(args) {
|
|
1420
1478
|
const { source, path, batchSize, push, objectType } = args;
|
|
1421
|
-
const
|
|
1479
|
+
const reportCursor = args.reportCursor ?? true;
|
|
1480
|
+
const idField = source.idField ?? DEFAULT_ID_FIELD$1;
|
|
1422
1481
|
const all = [];
|
|
1423
1482
|
let cursor = null;
|
|
1424
1483
|
for (;;) {
|
|
@@ -1444,6 +1503,8 @@ async function fullSyncSource(args) {
|
|
|
1444
1503
|
mode: "full",
|
|
1445
1504
|
fetched: all.length,
|
|
1446
1505
|
cursor: null,
|
|
1506
|
+
sourceFields: sourceTimeFields(source, all),
|
|
1507
|
+
...cursorReport(source, cursor, reportCursor),
|
|
1447
1508
|
...objectType ? { objectType } : {}
|
|
1448
1509
|
});
|
|
1449
1510
|
return {
|
|
@@ -1452,6 +1513,90 @@ async function fullSyncSource(args) {
|
|
|
1452
1513
|
ok
|
|
1453
1514
|
};
|
|
1454
1515
|
}
|
|
1516
|
+
// ../sdk/src/source-ids.ts
|
|
1517
|
+
/**
|
|
1518
|
+
* The daily delete check (continuous sync D20). A delta pull never sees a row
|
|
1519
|
+
* that was deleted, so once a day Clivly asks the app for the IDs of a source,
|
|
1520
|
+
* page by page, and archives CRM records whose source row is gone.
|
|
1521
|
+
*
|
|
1522
|
+
* Only IDs leave the app — never row bodies — and only on a signed request
|
|
1523
|
+
* (the trigger route refuses an unsigned one with a body).
|
|
1524
|
+
*/
|
|
1525
|
+
/** Most IDs one page may return; matches the server's cap. */
|
|
1526
|
+
const IDS_PAGE_MAX = 5e3;
|
|
1527
|
+
const DEFAULT_ID_FIELD = "id";
|
|
1528
|
+
const SOURCE_KEY = "id";
|
|
1529
|
+
/** The configured source whose cursor entity is `entity`. */
|
|
1530
|
+
function findSourceByEntity(config, entity) {
|
|
1531
|
+
const src = config.source;
|
|
1532
|
+
if (!src) return null;
|
|
1533
|
+
const fixed = [
|
|
1534
|
+
["company", src.companies],
|
|
1535
|
+
["contact", src.contacts],
|
|
1536
|
+
["deal", src.deals]
|
|
1537
|
+
];
|
|
1538
|
+
for (const [concept, source] of fixed) if (source?.entity === entity) return {
|
|
1539
|
+
concept,
|
|
1540
|
+
source
|
|
1541
|
+
};
|
|
1542
|
+
const custom = (src.custom ?? []).find((source) => source.entity === entity);
|
|
1543
|
+
return custom ? {
|
|
1544
|
+
concept: "custom",
|
|
1545
|
+
source: custom,
|
|
1546
|
+
objectType: custom.objectType
|
|
1547
|
+
} : null;
|
|
1548
|
+
}
|
|
1549
|
+
function readLimit(value) {
|
|
1550
|
+
return typeof value === "number" && Number.isInteger(value) && value > 0 ? Math.min(value, IDS_PAGE_MAX) : IDS_PAGE_MAX;
|
|
1551
|
+
}
|
|
1552
|
+
/**
|
|
1553
|
+
* One page of a source's IDs, in the same order a delta pull reads it, and
|
|
1554
|
+
* where the next page starts (null on the last page).
|
|
1555
|
+
*/
|
|
1556
|
+
async function listSourceIds(config, request) {
|
|
1557
|
+
if (typeof request.entity !== "string") return {
|
|
1558
|
+
status: 400,
|
|
1559
|
+
body: {
|
|
1560
|
+
ok: false,
|
|
1561
|
+
error: "invalid_ids_request"
|
|
1562
|
+
}
|
|
1563
|
+
};
|
|
1564
|
+
const found = findSourceByEntity(config, request.entity);
|
|
1565
|
+
if (!found) return {
|
|
1566
|
+
status: 404,
|
|
1567
|
+
body: {
|
|
1568
|
+
ok: false,
|
|
1569
|
+
error: "unknown_entity"
|
|
1570
|
+
}
|
|
1571
|
+
};
|
|
1572
|
+
const { source } = found;
|
|
1573
|
+
const limit = readLimit(request.limit);
|
|
1574
|
+
const after = parseWireCursors({ after: request.after })?.after ?? null;
|
|
1575
|
+
const rows = await source.fetchPage({
|
|
1576
|
+
since: after,
|
|
1577
|
+
limit
|
|
1578
|
+
});
|
|
1579
|
+
const ids = [];
|
|
1580
|
+
for (const row of rows) {
|
|
1581
|
+
const id = row[SOURCE_KEY];
|
|
1582
|
+
if (id !== null && id !== void 0 && id !== "") ids.push(String(id));
|
|
1583
|
+
}
|
|
1584
|
+
const cursor = nextCursor(rows, source.cursorField, source.idField ?? DEFAULT_ID_FIELD, after);
|
|
1585
|
+
const stalled = cursor !== null && after !== null && cursor.time.getTime() === after.time.getTime() && cursor.id === after.id;
|
|
1586
|
+
const next = rows.length < limit || cursor === null || stalled ? null : toWireCursor(cursor);
|
|
1587
|
+
return {
|
|
1588
|
+
status: 200,
|
|
1589
|
+
body: {
|
|
1590
|
+
ok: true,
|
|
1591
|
+
entity: request.entity,
|
|
1592
|
+
source: pushSourceOf(source),
|
|
1593
|
+
concept: found.concept,
|
|
1594
|
+
...found.objectType ? { objectType: found.objectType } : {},
|
|
1595
|
+
ids,
|
|
1596
|
+
next
|
|
1597
|
+
}
|
|
1598
|
+
};
|
|
1599
|
+
}
|
|
1455
1600
|
// ../sdk/src/trigger-method.ts
|
|
1456
1601
|
/** Host-side override, mirroring `CLIVLY_INSTANT_PUSH_DISABLED`. */
|
|
1457
1602
|
const REQUIRE_POST_ENV_VAR = "CLIVLY_TRIGGER_REQUIRE_POST";
|
|
@@ -1767,6 +1912,7 @@ const SYNC_CONTACTS_PATH = "/clivly/sdk/sync/contacts";
|
|
|
1767
1912
|
const SYNC_COMPANIES_PATH = "/clivly/sdk/sync/companies";
|
|
1768
1913
|
const SYNC_DEALS_PATH = "/clivly/sdk/sync/deals";
|
|
1769
1914
|
const SYNC_CUSTOM_PATH = "/clivly/sdk/sync/custom";
|
|
1915
|
+
const CONSENT_PATH = "/clivly/sdk/consent";
|
|
1770
1916
|
const TRAILING_SLASH = /\/$/;
|
|
1771
1917
|
const BEARER_PREFIX = /^Bearer\s+/i;
|
|
1772
1918
|
function entitiesToTables(entities, byName) {
|
|
@@ -1908,6 +2054,14 @@ function timingSafeEqual(a, b) {
|
|
|
1908
2054
|
const RUN_ID_HEADER = "x-clivly-run-id";
|
|
1909
2055
|
const SIGNATURE_HEADER = "x-clivly-signature";
|
|
1910
2056
|
/**
|
|
2057
|
+
* The positions a trigger carried, if it may move where a sync resumes. Only a
|
|
2058
|
+
* signed request from Clivly may: an unsigned one carrying positions could make
|
|
2059
|
+
* the app skip rows.
|
|
2060
|
+
*/
|
|
2061
|
+
function trustedCursors(auth, payload) {
|
|
2062
|
+
return auth === "signed" ? parseWireCursors(payload.cursors) : void 0;
|
|
2063
|
+
}
|
|
2064
|
+
/**
|
|
1911
2065
|
* An error the mounted trigger route should answer with a specific HTTP
|
|
1912
2066
|
* status, rather than the generic 500 it uses for a genuinely unhandled
|
|
1913
2067
|
* error. A fail-safe rejection (e.g. an unresolvable sample `entityKey`) is a
|
|
@@ -2098,6 +2252,11 @@ async function handleProjectionPreviewRequest(config, request) {
|
|
|
2098
2252
|
sampledCount: rows.length
|
|
2099
2253
|
}, 200);
|
|
2100
2254
|
}
|
|
2255
|
+
function failureMessage(body, status) {
|
|
2256
|
+
if (typeof body.message === "string") return body.message;
|
|
2257
|
+
if (typeof body.error === "string") return body.error;
|
|
2258
|
+
return `HTTP ${status}`;
|
|
2259
|
+
}
|
|
2101
2260
|
/**
|
|
2102
2261
|
* Create the Clivly SDK. The implemented surface — heartbeat/discovery reporting
|
|
2103
2262
|
* and the auth-verify handler — connects a developer's app to a running Clivly
|
|
@@ -2122,7 +2281,7 @@ function createClivlySDK(config) {
|
|
|
2122
2281
|
let presenceClient = null;
|
|
2123
2282
|
let httpHeartbeatActive = true;
|
|
2124
2283
|
const batchSize = config.sync?.batchSize ?? DEFAULT_BATCH_SIZE;
|
|
2125
|
-
if (serverless && !config.cursorStore) console.warn("Clivly: no `cursorStore` is configured and this looks like a serverless/edge runtime, where memory does not survive between invocations.
|
|
2284
|
+
if (serverless && !config.cursorStore) console.warn("Clivly: no `cursorStore` is configured and this looks like a serverless/edge runtime, where memory does not survive between invocations. Syncs Clivly triggers resume from the position Clivly keeps for you, but a `delta` sync this app starts itself (`sdk.sync()`, your own cron) will start from scratch and re-read your entire source tables — a full sync, on every run. If you run your own syncs, pass a persistent store: `cursorStore: kvCursorStore(env.MY_KV)` (exported from `@clivly/sdk`). See https://clivly.com/docs/concepts/sync#cursor-stores");
|
|
2126
2285
|
const cursorStore = config.cursorStore ?? memoryCursorStore();
|
|
2127
2286
|
const fetcher = createFetcher(config.retry);
|
|
2128
2287
|
let warnedUnsignedTrigger = false;
|
|
@@ -2191,8 +2350,8 @@ function createClivlySDK(config) {
|
|
|
2191
2350
|
const status = lastPushFailure?.status ?? null;
|
|
2192
2351
|
return status !== null && status >= CLIENT_ERROR_FLOOR && status < RETRYABLE_STATUS_FLOOR ? new TriggerRequestError(status, message) : new Error(message);
|
|
2193
2352
|
};
|
|
2194
|
-
const syncOne = async (source, path, mode, objectType, runId, batchSizeOverride, maxRowsOverride, persistCursor = true) => {
|
|
2195
|
-
const since = mode === "full" ? null : await cursorStore.get(source.entity) ?? null;
|
|
2353
|
+
const syncOne = async (source, path, mode, objectType, runId, batchSizeOverride, maxRowsOverride, persistCursor = true, clivlyCursors) => {
|
|
2354
|
+
const since = mode === "full" ? null : clivlyCursors?.[source.entity] ?? await cursorStore.get(source.entity) ?? null;
|
|
2196
2355
|
const push = createPush(runId);
|
|
2197
2356
|
const effectiveBatchSize = batchSizeOverride ?? batchSize;
|
|
2198
2357
|
const result = mode === "full" ? await fullSyncSource({
|
|
@@ -2200,7 +2359,8 @@ function createClivlySDK(config) {
|
|
|
2200
2359
|
path,
|
|
2201
2360
|
batchSize: effectiveBatchSize,
|
|
2202
2361
|
push,
|
|
2203
|
-
objectType
|
|
2362
|
+
objectType,
|
|
2363
|
+
reportCursor: persistCursor
|
|
2204
2364
|
}) : await syncSource({
|
|
2205
2365
|
source,
|
|
2206
2366
|
path,
|
|
@@ -2208,16 +2368,17 @@ function createClivlySDK(config) {
|
|
|
2208
2368
|
batchSize: effectiveBatchSize,
|
|
2209
2369
|
push,
|
|
2210
2370
|
objectType,
|
|
2211
|
-
maxRows: maxRowsOverride
|
|
2371
|
+
maxRows: maxRowsOverride,
|
|
2372
|
+
reportCursor: persistCursor
|
|
2212
2373
|
});
|
|
2213
2374
|
if (persistCursor) await cursorStore.set(source.entity, result.cursor);
|
|
2214
2375
|
return result.ok;
|
|
2215
2376
|
};
|
|
2216
|
-
const syncCustomSources = async (customSources, mode, runId) => {
|
|
2377
|
+
const syncCustomSources = async (customSources, mode, runId, clivlyCursors) => {
|
|
2217
2378
|
const failed = [];
|
|
2218
2379
|
for (const customSource of customSources ?? []) {
|
|
2219
2380
|
customSource.pushSource = customPushSource(config, customSource);
|
|
2220
|
-
if (!await syncOne(customSource, SYNC_CUSTOM_PATH, mode, customSource.objectType, runId)) failed.push(customSource.entity);
|
|
2381
|
+
if (!await syncOne(customSource, SYNC_CUSTOM_PATH, mode, customSource.objectType, runId, void 0, void 0, true, clivlyCursors)) failed.push(customSource.entity);
|
|
2221
2382
|
}
|
|
2222
2383
|
return failed;
|
|
2223
2384
|
};
|
|
@@ -2264,13 +2425,14 @@ function createClivlySDK(config) {
|
|
|
2264
2425
|
lastPushFailure = null;
|
|
2265
2426
|
const mode = options?.mode ?? "delta";
|
|
2266
2427
|
const runId = options?.runId;
|
|
2428
|
+
const cursors = options?.cursors;
|
|
2267
2429
|
const failed = [];
|
|
2268
2430
|
if (config.source?.companies) {
|
|
2269
|
-
if (!await syncOne(config.source.companies, SYNC_COMPANIES_PATH, mode, void 0, runId)) failed.push(config.source.companies.entity);
|
|
2431
|
+
if (!await syncOne(config.source.companies, SYNC_COMPANIES_PATH, mode, void 0, runId, void 0, void 0, true, cursors)) failed.push(config.source.companies.entity);
|
|
2270
2432
|
}
|
|
2271
|
-
if (config.source?.contacts && !await syncOne(config.source.contacts, SYNC_CONTACTS_PATH, mode, void 0, runId)) failed.push(config.source.contacts.entity);
|
|
2272
|
-
if (config.source?.deals && !await syncOne(config.source.deals, SYNC_DEALS_PATH, mode, void 0, runId)) failed.push(config.source.deals.entity);
|
|
2273
|
-
failed.push(...await syncCustomSources(config.source?.custom, mode, runId));
|
|
2433
|
+
if (config.source?.contacts && !await syncOne(config.source.contacts, SYNC_CONTACTS_PATH, mode, void 0, runId, void 0, void 0, true, cursors)) failed.push(config.source.contacts.entity);
|
|
2434
|
+
if (config.source?.deals && !await syncOne(config.source.deals, SYNC_DEALS_PATH, mode, void 0, runId, void 0, void 0, true, cursors)) failed.push(config.source.deals.entity);
|
|
2435
|
+
failed.push(...await syncCustomSources(config.source?.custom, mode, runId, cursors));
|
|
2274
2436
|
const remote = await syncRemoteSources(remoteSources, runId);
|
|
2275
2437
|
failed.push(...remote.failed);
|
|
2276
2438
|
await reportRemoteOutcomes(lastRemoteOutcomes, remote.pushed, runId);
|
|
@@ -2290,28 +2452,87 @@ function createClivlySDK(config) {
|
|
|
2290
2452
|
lastPushFailure = null;
|
|
2291
2453
|
if (!await syncOne(resolved.source, resolved.path, "delta", resolved.objectType, runId, SAMPLE_BATCH_SIZE, SAMPLE_BATCH_SIZE, false)) throw pushFailureError(describePushFailure([resolved.source.entity]));
|
|
2292
2454
|
};
|
|
2293
|
-
const
|
|
2294
|
-
|
|
2295
|
-
|
|
2455
|
+
const recordConsent = async (input) => {
|
|
2456
|
+
const occurredAt = input.occurredAt instanceof Date ? input.occurredAt.toISOString() : input.occurredAt;
|
|
2457
|
+
const response = await fetcher(`${apiUrl}${CONSENT_PATH}`, {
|
|
2458
|
+
method: "POST",
|
|
2459
|
+
headers: {
|
|
2460
|
+
"content-type": "application/json",
|
|
2461
|
+
authorization: `Bearer ${config.apiKey}`
|
|
2462
|
+
},
|
|
2463
|
+
body: JSON.stringify({
|
|
2464
|
+
...input,
|
|
2465
|
+
occurredAt
|
|
2466
|
+
})
|
|
2467
|
+
});
|
|
2468
|
+
if (!response) return {
|
|
2469
|
+
error: "Could not reach Clivly.",
|
|
2470
|
+
ok: false,
|
|
2471
|
+
status: null
|
|
2472
|
+
};
|
|
2473
|
+
let body = {};
|
|
2474
|
+
try {
|
|
2475
|
+
body = await response.json();
|
|
2476
|
+
} catch {}
|
|
2477
|
+
if (!response.ok) return {
|
|
2478
|
+
error: failureMessage(body, response.status),
|
|
2479
|
+
ok: false,
|
|
2480
|
+
status: response.status
|
|
2481
|
+
};
|
|
2482
|
+
const { ok: _ok, ...rest } = body;
|
|
2483
|
+
return {
|
|
2484
|
+
ok: true,
|
|
2485
|
+
...rest
|
|
2486
|
+
};
|
|
2487
|
+
};
|
|
2488
|
+
const instantTarget = (entityKey) => {
|
|
2489
|
+
if (config.sync?.instant !== true) return null;
|
|
2490
|
+
if (isInstantPushDisabled(config.env ?? process.env)) return null;
|
|
2296
2491
|
const resolved = resolveEntitySource(config, entityKey);
|
|
2297
2492
|
if (!resolved) {
|
|
2298
2493
|
console.warn(`Clivly instant push: no source is configured for entity "${entityKey}" — this row was not pushed. It will still sync on the next delta run once \`entities\` declares it.`);
|
|
2299
|
-
return;
|
|
2494
|
+
return null;
|
|
2300
2495
|
}
|
|
2301
2496
|
const path = CONCEPT_PATHS[resolved.expectedKey];
|
|
2302
|
-
|
|
2497
|
+
return path ? {
|
|
2498
|
+
path,
|
|
2499
|
+
resolved
|
|
2500
|
+
} : null;
|
|
2501
|
+
};
|
|
2502
|
+
const objectTypeOf = (resolved) => resolved.expectedKey === "custom" ? { objectType: resolved.source.objectType } : {};
|
|
2503
|
+
const pushOne = async (entityKey, row) => {
|
|
2504
|
+
const target = instantTarget(entityKey);
|
|
2505
|
+
if (!target) return;
|
|
2506
|
+
const { path, resolved } = target;
|
|
2303
2507
|
const push = createPush();
|
|
2304
2508
|
try {
|
|
2305
2509
|
if (!await push(path, {
|
|
2306
2510
|
source: pushSourceOf(resolved.source),
|
|
2307
2511
|
rows: [row],
|
|
2308
2512
|
mode: "delta",
|
|
2309
|
-
|
|
2513
|
+
sourceFields: sourceTimeFields(resolved.source, [row]),
|
|
2514
|
+
...objectTypeOf(resolved)
|
|
2310
2515
|
})) console.warn(`Clivly instant push failed for "${entityKey}". This row will still sync on the next delta run.`);
|
|
2311
2516
|
} catch (error) {
|
|
2312
2517
|
console.warn(`Clivly instant push failed for "${entityKey}": ${error instanceof Error ? error.message : String(error)}. This row will still sync on the next delta run.`);
|
|
2313
2518
|
}
|
|
2314
2519
|
};
|
|
2520
|
+
const pushDelete = async (entityKey, ids) => {
|
|
2521
|
+
const target = instantTarget(entityKey);
|
|
2522
|
+
if (!target || ids.length === 0) return;
|
|
2523
|
+
const { path, resolved } = target;
|
|
2524
|
+
try {
|
|
2525
|
+
if (!await createPush()(path, {
|
|
2526
|
+
source: pushSourceOf(resolved.source),
|
|
2527
|
+
rows: [],
|
|
2528
|
+
mode: "delta",
|
|
2529
|
+
deleted: ids.map(String),
|
|
2530
|
+
...objectTypeOf(resolved)
|
|
2531
|
+
})) console.warn(`Clivly instant delete failed for "${entityKey}". Clivly's daily delete check will still archive these records.`);
|
|
2532
|
+
} catch (error) {
|
|
2533
|
+
console.warn(`Clivly instant delete failed for "${entityKey}": ${error instanceof Error ? error.message : String(error)}. Clivly's daily delete check will still archive these records.`);
|
|
2534
|
+
}
|
|
2535
|
+
};
|
|
2315
2536
|
const describeSyncTrigger = () => ({
|
|
2316
2537
|
allowUnsigned: config.syncTrigger?.allowUnsigned === true,
|
|
2317
2538
|
configured: config.syncTrigger?.url,
|
|
@@ -2601,6 +2822,10 @@ function createClivlySDK(config) {
|
|
|
2601
2822
|
const handleIfPreview = async (payload) => {
|
|
2602
2823
|
if (payload.preview) return await handlePreviewRequest(config, payload.preview);
|
|
2603
2824
|
if (payload.previewProjection) return await handleProjectionPreviewRequest(config, payload.previewProjection);
|
|
2825
|
+
if (payload.ids) {
|
|
2826
|
+
const { body, status } = await listSourceIds(config, payload.ids);
|
|
2827
|
+
return jsonResponse(body, status);
|
|
2828
|
+
}
|
|
2604
2829
|
return null;
|
|
2605
2830
|
};
|
|
2606
2831
|
const refuseUnauthorized = (auth) => {
|
|
@@ -2632,7 +2857,8 @@ function createClivlySDK(config) {
|
|
|
2632
2857
|
if (payload.sample) await runSampleSync(payload.sample.entityKey, payload.runId);
|
|
2633
2858
|
else await runScheduled({
|
|
2634
2859
|
mode: payload.mode,
|
|
2635
|
-
runId: payload.runId
|
|
2860
|
+
runId: payload.runId,
|
|
2861
|
+
cursors: trustedCursors(auth, payload)
|
|
2636
2862
|
});
|
|
2637
2863
|
return jsonResponse({
|
|
2638
2864
|
ok: true,
|
|
@@ -2730,7 +2956,9 @@ function createClivlySDK(config) {
|
|
|
2730
2956
|
createClivlyHandler,
|
|
2731
2957
|
sync: runSync,
|
|
2732
2958
|
createAuthVerifyHandler,
|
|
2733
|
-
pushOne
|
|
2959
|
+
pushOne,
|
|
2960
|
+
pushDelete,
|
|
2961
|
+
recordConsent
|
|
2734
2962
|
};
|
|
2735
2963
|
}
|
|
2736
2964
|
Object.defineProperty(exports, "CURSOR_KEY_PREFIX", {
|