@pgxsinkit/client 0.1.16 → 0.1.18
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/index.js +24 -265
- package/dist/index.js.map +5 -5
- package/package.json +3 -3
- package/src/index.ts +10 -5
- package/src/mutation.ts +26 -390
- package/src/schema.ts +13 -5
package/dist/index.js
CHANGED
|
@@ -8,6 +8,7 @@ import { getSyncRegistrySchema as getSyncRegistrySchema3 } from "@pgxsinkit/cont
|
|
|
8
8
|
import { createElectricExtension, startConfiguredSync } from "@pgxsinkit/sync-engine";
|
|
9
9
|
|
|
10
10
|
// packages/client/src/mutation.ts
|
|
11
|
+
import { getViewConfig } from "drizzle-orm/pg-core";
|
|
11
12
|
import { ZodObject } from "zod";
|
|
12
13
|
import { getProjectedColumns as getProjectedTableColumns, getSyncRegistrySchema } from "@pgxsinkit/contracts";
|
|
13
14
|
var DEFAULT_FLUSH_BATCH_SIZE = 100;
|
|
@@ -28,7 +29,7 @@ function createMutationRuntime(options) {
|
|
|
28
29
|
}
|
|
29
30
|
return;
|
|
30
31
|
};
|
|
31
|
-
const tableContexts = buildTableContexts(options.registry
|
|
32
|
+
const tableContexts = buildTableContexts(options.registry);
|
|
32
33
|
let flushQueue = Promise.resolve();
|
|
33
34
|
const getTableContext = (table) => {
|
|
34
35
|
const context = tableContexts[table];
|
|
@@ -248,29 +249,17 @@ function createMutationRuntime(options) {
|
|
|
248
249
|
}
|
|
249
250
|
};
|
|
250
251
|
const runFlush = async (table) => {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
for (const context of batchResult.affectedContexts) {
|
|
259
|
-
affectedContexts.set(context.key, context);
|
|
260
|
-
}
|
|
261
|
-
} while (processedCount > 0);
|
|
262
|
-
for (const context of affectedContexts.values()) {
|
|
263
|
-
await reconcileTable(options.db, context);
|
|
252
|
+
const affectedContexts = new Map;
|
|
253
|
+
let processedCount = 0;
|
|
254
|
+
do {
|
|
255
|
+
const batchResult = await flushBatch(options.db, tableContexts, options.writeUrl, table, resolveAuthToken);
|
|
256
|
+
processedCount = batchResult.processedCount;
|
|
257
|
+
for (const context of batchResult.affectedContexts) {
|
|
258
|
+
affectedContexts.set(context.key, context);
|
|
264
259
|
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
processedCount = 0;
|
|
269
|
-
const contexts = filterContexts(tableContexts, table);
|
|
270
|
-
for (const context of contexts) {
|
|
271
|
-
processedCount += await flushTable(options.db, context, options.writeUrl, resolveAuthToken);
|
|
272
|
-
}
|
|
273
|
-
} while (processedCount > 0);
|
|
260
|
+
} while (processedCount > 0);
|
|
261
|
+
for (const context of affectedContexts.values()) {
|
|
262
|
+
await reconcileTable(options.db, context);
|
|
274
263
|
}
|
|
275
264
|
};
|
|
276
265
|
return {
|
|
@@ -314,7 +303,6 @@ function createMutationRuntime(options) {
|
|
|
314
303
|
});
|
|
315
304
|
},
|
|
316
305
|
flush: async (table) => {
|
|
317
|
-
console.log(`Initiating flush for table: ${table ?? "all tables"}`);
|
|
318
306
|
const nextFlush = flushQueue.then(() => runFlush(table));
|
|
319
307
|
flushQueue = nextFlush.catch(() => {
|
|
320
308
|
return;
|
|
@@ -433,17 +421,14 @@ function createMutationRuntime(options) {
|
|
|
433
421
|
}
|
|
434
422
|
};
|
|
435
423
|
}
|
|
436
|
-
function buildTableContexts(registry
|
|
424
|
+
function buildTableContexts(registry) {
|
|
437
425
|
const localSchema = getSyncRegistrySchema(registry);
|
|
438
|
-
return Object.fromEntries(Object.entries(registry).filter(([, entry]) => entry.mode !== "readonly").map(([key, entry]) => [key, buildTableContext(key, entry, localSchema
|
|
426
|
+
return Object.fromEntries(Object.entries(registry).filter(([, entry]) => entry.mode !== "readonly").map(([key, entry]) => [key, buildTableContext(key, entry, localSchema)]));
|
|
439
427
|
}
|
|
440
|
-
function buildTableContext(key, entry, localSchema
|
|
428
|
+
function buildTableContext(key, entry, localSchema) {
|
|
441
429
|
if (!entry.clientProjection?.overlayTable || !entry.clientProjection.journalTable) {
|
|
442
430
|
throw new Error(`overlay and journal tables are required for writable table ${key}`);
|
|
443
431
|
}
|
|
444
|
-
if (requireRouteBasePath && !entry.routes?.basePath) {
|
|
445
|
-
throw new Error(`routes.basePath is required for writable table ${key}`);
|
|
446
|
-
}
|
|
447
432
|
const columns = getProjectedTableColumns(entry).map(({ propertyKey, column }) => ({
|
|
448
433
|
propertyKey,
|
|
449
434
|
column
|
|
@@ -458,12 +443,11 @@ function buildTableContext(key, entry, localSchema, requireRouteBasePath) {
|
|
|
458
443
|
return {
|
|
459
444
|
key,
|
|
460
445
|
entry,
|
|
461
|
-
readModel: qualifyLocalIdentifier(localSchema, entry.clientProjection.
|
|
446
|
+
readModel: qualifyLocalIdentifier(localSchema, entry.view != null ? getViewConfig(entry.view).name : entry.clientProjection.syncedTable),
|
|
462
447
|
syncedTable: qualifyLocalIdentifier(localSchema, entry.clientProjection.syncedTable),
|
|
463
448
|
overlayTable: qualifyLocalIdentifier(localSchema, entry.clientProjection.overlayTable),
|
|
464
449
|
journalTable: qualifyLocalIdentifier(localSchema, entry.clientProjection.journalTable),
|
|
465
450
|
journalSequence: qualifyLocalIdentifier(localSchema, buildJournalSequenceName(entry.clientProjection.journalTable)),
|
|
466
|
-
...entry.routes?.basePath ? { routeBasePath: entry.routes.basePath } : {},
|
|
467
451
|
pkColumnNames: [...entry.primaryKey.columns],
|
|
468
452
|
pkPropertyKeys,
|
|
469
453
|
recordIncludesOverlayState: recordSchemaIncludesOverlayState(entry.schemas?.recordSchema),
|
|
@@ -1006,120 +990,6 @@ function toSqlColumnPayload(context, payload) {
|
|
|
1006
990
|
}
|
|
1007
991
|
return normalized;
|
|
1008
992
|
}
|
|
1009
|
-
function filterProjectedPayload(context, payload) {
|
|
1010
|
-
const normalized = {};
|
|
1011
|
-
for (const { propertyKey, column } of context.columns) {
|
|
1012
|
-
if (Object.prototype.hasOwnProperty.call(payload, propertyKey)) {
|
|
1013
|
-
normalized[propertyKey] = payload[propertyKey];
|
|
1014
|
-
continue;
|
|
1015
|
-
}
|
|
1016
|
-
if (Object.prototype.hasOwnProperty.call(payload, column.name)) {
|
|
1017
|
-
normalized[column.name] = payload[column.name];
|
|
1018
|
-
}
|
|
1019
|
-
}
|
|
1020
|
-
return normalized;
|
|
1021
|
-
}
|
|
1022
|
-
async function flushTable(db, context, writeUrl, getAuthToken) {
|
|
1023
|
-
const result = await db.query(`
|
|
1024
|
-
SELECT
|
|
1025
|
-
mutation_id AS "mutationId",
|
|
1026
|
-
entity_key_json AS "entityKeyJson",
|
|
1027
|
-
mutation_seq AS "mutationSeq",
|
|
1028
|
-
mutation_kind AS "mutationKind",
|
|
1029
|
-
status,
|
|
1030
|
-
payload_json AS "payloadJson",
|
|
1031
|
-
attempt_count AS "attemptCount",
|
|
1032
|
-
last_http_status AS "lastHttpStatus",
|
|
1033
|
-
last_error AS "lastError",
|
|
1034
|
-
conflict_reason AS "conflictReason",
|
|
1035
|
-
next_retry_at_us::text AS "nextRetryAtUs",
|
|
1036
|
-
server_updated_at_us::text AS "serverUpdatedAtUs"
|
|
1037
|
-
FROM ${context.journalTable}
|
|
1038
|
-
WHERE status IN ('pending', 'failed')
|
|
1039
|
-
AND COALESCE(next_retry_at_us, 0) <= $1::bigint
|
|
1040
|
-
AND NOT EXISTS (
|
|
1041
|
-
SELECT 1
|
|
1042
|
-
FROM ${context.journalTable} AS earlier
|
|
1043
|
-
WHERE earlier.entity_key_json = ${context.journalTable}.entity_key_json
|
|
1044
|
-
AND earlier.mutation_seq < ${context.journalTable}.mutation_seq
|
|
1045
|
-
AND earlier.status IN ('pending', 'failed', 'sending')
|
|
1046
|
-
)
|
|
1047
|
-
ORDER BY enqueued_at_us ASC, mutation_seq ASC
|
|
1048
|
-
LIMIT ${DEFAULT_FLUSH_BATCH_SIZE}
|
|
1049
|
-
`, [nowMicroseconds()]);
|
|
1050
|
-
if (result.rows.length === 0) {
|
|
1051
|
-
return 0;
|
|
1052
|
-
}
|
|
1053
|
-
for (const mutation of result.rows) {
|
|
1054
|
-
const sentAtUs = nowMicroseconds();
|
|
1055
|
-
await setMutationStatus(db, context, mutation.mutationId, "sending", {
|
|
1056
|
-
attemptCount: mutation.attemptCount + 1,
|
|
1057
|
-
sentAtUs,
|
|
1058
|
-
updatedAtUs: sentAtUs,
|
|
1059
|
-
lastError: null,
|
|
1060
|
-
nextRetryAtUs: null,
|
|
1061
|
-
lastHttpStatus: null,
|
|
1062
|
-
conflictReason: null
|
|
1063
|
-
});
|
|
1064
|
-
try {
|
|
1065
|
-
const entityKey = JSON.parse(mutation.entityKeyJson);
|
|
1066
|
-
const payload = JSON.parse(mutation.payloadJson);
|
|
1067
|
-
let response = await sendMutationRequest(writeUrl, context, entityKey, payload, await getAuthToken?.());
|
|
1068
|
-
if ([401, 403].includes(response.status) && getAuthToken) {
|
|
1069
|
-
response = await sendMutationRequest(writeUrl, context, entityKey, payload, await getAuthToken());
|
|
1070
|
-
}
|
|
1071
|
-
const responseText = await response.text();
|
|
1072
|
-
const responseJson = responseText.length > 0 ? JSON.parse(responseText) : null;
|
|
1073
|
-
if (!response.ok) {
|
|
1074
|
-
const errorMessage = responseJson && typeof responseJson.message === "string" ? responseJson.message : `Write API responded with ${response.status}`;
|
|
1075
|
-
if (mutation.mutationKind === "delete" && response.status === 404) {
|
|
1076
|
-
await markDeleteAcknowledged(db, context, mutation.mutationId);
|
|
1077
|
-
continue;
|
|
1078
|
-
}
|
|
1079
|
-
throw new MutationRequestError(errorMessage, response.status);
|
|
1080
|
-
}
|
|
1081
|
-
const ackedAtUs = nowMicroseconds();
|
|
1082
|
-
if (mutation.mutationKind === "delete") {
|
|
1083
|
-
await markDeleteAcknowledged(db, context, mutation.mutationId);
|
|
1084
|
-
} else {
|
|
1085
|
-
const created = parseRecord(context, responseJson);
|
|
1086
|
-
const createdRecord = ensureRecord(created);
|
|
1087
|
-
const serverUpdatedAtUs = toOptionalString(createdRecord.updatedAtUs);
|
|
1088
|
-
await db.query(`
|
|
1089
|
-
UPDATE ${context.journalTable}
|
|
1090
|
-
SET
|
|
1091
|
-
status = 'acked',
|
|
1092
|
-
acked_at_us = $2::bigint,
|
|
1093
|
-
updated_at_us = $2::bigint,
|
|
1094
|
-
server_updated_at_us = $3::bigint,
|
|
1095
|
-
last_error = NULL,
|
|
1096
|
-
last_http_status = 200,
|
|
1097
|
-
conflict_reason = NULL,
|
|
1098
|
-
next_retry_at_us = NULL
|
|
1099
|
-
WHERE mutation_id = $1
|
|
1100
|
-
`, [mutation.mutationId, ackedAtUs, serverUpdatedAtUs]);
|
|
1101
|
-
if (!await hasLaterMutations(db, context, mutation.entityKeyJson, mutation.mutationSeq)) {
|
|
1102
|
-
await updateOverlayTimestamps(db, context, entityKey, createdRecord, ackedAtUs);
|
|
1103
|
-
}
|
|
1104
|
-
}
|
|
1105
|
-
} catch (error) {
|
|
1106
|
-
const failedAtUs = nowMicroseconds();
|
|
1107
|
-
const attemptCount = mutation.attemptCount + 1;
|
|
1108
|
-
const nextRetryAtUs = computeNextRetryAtUs(failedAtUs, attemptCount);
|
|
1109
|
-
const lastHttpStatus = error instanceof MutationRequestError ? error.status : null;
|
|
1110
|
-
const conflictReason = lastHttpStatus === 404 || lastHttpStatus === 409 ? error instanceof Error ? error.message : "Server conflict" : null;
|
|
1111
|
-
await setMutationStatus(db, context, mutation.mutationId, "failed", {
|
|
1112
|
-
attemptCount,
|
|
1113
|
-
updatedAtUs: failedAtUs,
|
|
1114
|
-
lastError: error instanceof Error ? error.message : "Unknown write failure",
|
|
1115
|
-
nextRetryAtUs,
|
|
1116
|
-
lastHttpStatus,
|
|
1117
|
-
conflictReason
|
|
1118
|
-
});
|
|
1119
|
-
}
|
|
1120
|
-
}
|
|
1121
|
-
return result.rows.length;
|
|
1122
|
-
}
|
|
1123
993
|
async function reconcileTable(db, context) {
|
|
1124
994
|
await db.exec("BEGIN");
|
|
1125
995
|
try {
|
|
@@ -1131,32 +1001,6 @@ async function reconcileTable(db, context) {
|
|
|
1131
1001
|
throw error;
|
|
1132
1002
|
}
|
|
1133
1003
|
}
|
|
1134
|
-
async function sendMutationRequest(writeUrl, context, entityKey, payload, bearerToken) {
|
|
1135
|
-
if (!context.routeBasePath) {
|
|
1136
|
-
throw new Error(`routes.basePath is required for direct mutation requests on table ${context.key}`);
|
|
1137
|
-
}
|
|
1138
|
-
switch (payload.kind) {
|
|
1139
|
-
case "create":
|
|
1140
|
-
return fetch(`${writeUrl}${context.routeBasePath}`, {
|
|
1141
|
-
method: "POST",
|
|
1142
|
-
headers: buildRequestHeaders(bearerToken),
|
|
1143
|
-
body: JSON.stringify(filterProjectedPayload(context, stripManagedFields(context, ensureRecord(payload.value), "create")))
|
|
1144
|
-
});
|
|
1145
|
-
case "update":
|
|
1146
|
-
return fetch(`${writeUrl}${context.routeBasePath}/${encodeURIComponent(getSingleEntityKeyValue(context, entityKey))}`, {
|
|
1147
|
-
method: "PATCH",
|
|
1148
|
-
headers: buildRequestHeaders(bearerToken),
|
|
1149
|
-
body: JSON.stringify(filterProjectedPayload(context, stripManagedFields(context, ensureRecord(payload.patch), "update")))
|
|
1150
|
-
});
|
|
1151
|
-
case "delete":
|
|
1152
|
-
return fetch(`${writeUrl}${context.routeBasePath}/${encodeURIComponent(getSingleEntityKeyValue(context, entityKey))}`, {
|
|
1153
|
-
method: "DELETE",
|
|
1154
|
-
headers: buildRequestHeaders(bearerToken)
|
|
1155
|
-
});
|
|
1156
|
-
default:
|
|
1157
|
-
throw new Error(`Unknown mutation kind for table ${context.key}`);
|
|
1158
|
-
}
|
|
1159
|
-
}
|
|
1160
1004
|
function buildRequestHeaders(bearerToken) {
|
|
1161
1005
|
if (!bearerToken) {
|
|
1162
1006
|
return {
|
|
@@ -1168,37 +1012,6 @@ function buildRequestHeaders(bearerToken) {
|
|
|
1168
1012
|
Authorization: `Bearer ${bearerToken}`
|
|
1169
1013
|
};
|
|
1170
1014
|
}
|
|
1171
|
-
function getSingleEntityKeyValue(context, entityKey) {
|
|
1172
|
-
if (context.pkPropertyKeys.length !== 1) {
|
|
1173
|
-
throw new Error(`Only single-column primary keys are supported for routes on table ${context.key}`);
|
|
1174
|
-
}
|
|
1175
|
-
return entityKey[context.pkPropertyKeys[0]];
|
|
1176
|
-
}
|
|
1177
|
-
async function setMutationStatus(db, context, mutationId, status, options) {
|
|
1178
|
-
await db.query(`
|
|
1179
|
-
UPDATE ${context.journalTable}
|
|
1180
|
-
SET
|
|
1181
|
-
status = $2,
|
|
1182
|
-
attempt_count = $3,
|
|
1183
|
-
sent_at_us = COALESCE($4::bigint, sent_at_us),
|
|
1184
|
-
updated_at_us = $5::bigint,
|
|
1185
|
-
last_error = $6,
|
|
1186
|
-
next_retry_at_us = $7::bigint,
|
|
1187
|
-
last_http_status = $8,
|
|
1188
|
-
conflict_reason = $9
|
|
1189
|
-
WHERE mutation_id = $1
|
|
1190
|
-
`, [
|
|
1191
|
-
mutationId,
|
|
1192
|
-
status,
|
|
1193
|
-
options.attemptCount,
|
|
1194
|
-
options.sentAtUs ?? null,
|
|
1195
|
-
options.updatedAtUs,
|
|
1196
|
-
options.lastError ?? null,
|
|
1197
|
-
options.nextRetryAtUs ?? null,
|
|
1198
|
-
options.lastHttpStatus ?? null,
|
|
1199
|
-
options.conflictReason ?? null
|
|
1200
|
-
]);
|
|
1201
|
-
}
|
|
1202
1015
|
async function applyMutationStatusUpdates(db, context, updates) {
|
|
1203
1016
|
if (updates.length === 0) {
|
|
1204
1017
|
return;
|
|
@@ -1281,46 +1094,6 @@ async function applyMutationStatusUpdates(db, context, updates) {
|
|
|
1281
1094
|
WHERE journal.mutation_id = updates.mutation_id::uuid
|
|
1282
1095
|
`, params);
|
|
1283
1096
|
}
|
|
1284
|
-
async function markDeleteAcknowledged(db, context, mutationId) {
|
|
1285
|
-
const ackedAtUs = nowMicroseconds();
|
|
1286
|
-
await db.query(`
|
|
1287
|
-
UPDATE ${context.journalTable}
|
|
1288
|
-
SET
|
|
1289
|
-
status = 'acked',
|
|
1290
|
-
acked_at_us = $2::bigint,
|
|
1291
|
-
updated_at_us = $2::bigint,
|
|
1292
|
-
last_error = NULL,
|
|
1293
|
-
last_http_status = 204,
|
|
1294
|
-
conflict_reason = NULL,
|
|
1295
|
-
next_retry_at_us = NULL
|
|
1296
|
-
WHERE mutation_id = $1
|
|
1297
|
-
`, [mutationId, ackedAtUs]);
|
|
1298
|
-
}
|
|
1299
|
-
async function hasLaterMutations(db, context, entityKeyJson, mutationSeq) {
|
|
1300
|
-
const result = await db.query(`
|
|
1301
|
-
SELECT COUNT(*)::int AS count
|
|
1302
|
-
FROM ${context.journalTable}
|
|
1303
|
-
WHERE entity_key_json = $1
|
|
1304
|
-
AND mutation_seq > $2
|
|
1305
|
-
`, [entityKeyJson, mutationSeq]);
|
|
1306
|
-
return (result.rows[0]?.count ?? 0) > 0;
|
|
1307
|
-
}
|
|
1308
|
-
async function updateOverlayTimestamps(db, context, entityKey, record, localUpdatedAtUs) {
|
|
1309
|
-
if (!hasProperty(context, "createdAtUs") || !hasProperty(context, "updatedAtUs")) {
|
|
1310
|
-
return;
|
|
1311
|
-
}
|
|
1312
|
-
await db.query(`
|
|
1313
|
-
UPDATE ${context.overlayTable}
|
|
1314
|
-
SET
|
|
1315
|
-
created_at_us = $${context.pkPropertyKeys.length + 1}::bigint,
|
|
1316
|
-
updated_at_us = $${context.pkPropertyKeys.length + 2}::bigint,
|
|
1317
|
-
local_updated_at_us = $${context.pkPropertyKeys.length + 3}::bigint
|
|
1318
|
-
WHERE ${buildWhereClause(context)}
|
|
1319
|
-
`, [...buildWhereParams(context, entityKey), String(record.createdAtUs), String(record.updatedAtUs), localUpdatedAtUs]);
|
|
1320
|
-
}
|
|
1321
|
-
function buildWhereClause(context) {
|
|
1322
|
-
return context.pkColumnNames.map((columnName, index) => `${columnName} = $${index + 1}`).join(" AND ");
|
|
1323
|
-
}
|
|
1324
1097
|
function buildColumnEquality(columnNames, leftAlias, rightAlias) {
|
|
1325
1098
|
return columnNames.map((columnName) => `${leftAlias}.${columnName} = ${rightAlias}.${columnName}`).join(" AND ");
|
|
1326
1099
|
}
|
|
@@ -1339,9 +1112,6 @@ function buildJournalSequenceName(journalTable) {
|
|
|
1339
1112
|
function escapeSqlString(value) {
|
|
1340
1113
|
return value.replace(/'/g, "''");
|
|
1341
1114
|
}
|
|
1342
|
-
function buildWhereParams(context, entityKey) {
|
|
1343
|
-
return context.pkPropertyKeys.map((propertyKey) => entityKey[propertyKey]);
|
|
1344
|
-
}
|
|
1345
1115
|
function formatSqlValuePlaceholder(position, columnName) {
|
|
1346
1116
|
return /_at_us$/.test(columnName) ? `$${position}::bigint` : `$${position}`;
|
|
1347
1117
|
}
|
|
@@ -1360,21 +1130,6 @@ function extractRecordFromState(context, state) {
|
|
|
1360
1130
|
function isRecord(value) {
|
|
1361
1131
|
return typeof value === "object" && value !== null;
|
|
1362
1132
|
}
|
|
1363
|
-
function toOptionalString(value) {
|
|
1364
|
-
if (value === undefined || value === null) {
|
|
1365
|
-
return null;
|
|
1366
|
-
}
|
|
1367
|
-
switch (typeof value) {
|
|
1368
|
-
case "string":
|
|
1369
|
-
return value;
|
|
1370
|
-
case "number":
|
|
1371
|
-
case "boolean":
|
|
1372
|
-
case "bigint":
|
|
1373
|
-
return String(value);
|
|
1374
|
-
default:
|
|
1375
|
-
return JSON.stringify(value);
|
|
1376
|
-
}
|
|
1377
|
-
}
|
|
1378
1133
|
|
|
1379
1134
|
class MutationRequestError extends Error {
|
|
1380
1135
|
status;
|
|
@@ -1386,6 +1141,7 @@ class MutationRequestError extends Error {
|
|
|
1386
1141
|
}
|
|
1387
1142
|
|
|
1388
1143
|
// packages/client/src/schema.ts
|
|
1144
|
+
import { getViewConfig as getViewConfig2 } from "drizzle-orm/pg-core";
|
|
1389
1145
|
import {
|
|
1390
1146
|
getLocalSyncPrimaryKeyColumns,
|
|
1391
1147
|
getSyncRegistrySchema as getSyncRegistrySchema2,
|
|
@@ -1686,11 +1442,12 @@ function buildPrimaryKeyMatch(primaryKeyColumns) {
|
|
|
1686
1442
|
}
|
|
1687
1443
|
function getClientProjection(entry, tableKey, localSchema) {
|
|
1688
1444
|
const projection = baseProjection(entry, tableKey);
|
|
1445
|
+
const readModelName = entry.view != null ? getViewConfig2(entry.view).name : projection.syncedTable;
|
|
1689
1446
|
return {
|
|
1690
1447
|
syncedTable: qualifyIdentifier(localSchema, projection.syncedTable),
|
|
1691
1448
|
...projection.overlayTable ? { overlayTable: qualifyIdentifier(localSchema, projection.overlayTable) } : {},
|
|
1692
1449
|
...projection.journalTable ? { journalTable: qualifyIdentifier(localSchema, projection.journalTable) } : {},
|
|
1693
|
-
readModel: qualifyIdentifier(localSchema,
|
|
1450
|
+
readModel: qualifyIdentifier(localSchema, readModelName)
|
|
1694
1451
|
};
|
|
1695
1452
|
}
|
|
1696
1453
|
function baseProjection(entry, tableKey) {
|
|
@@ -1772,7 +1529,6 @@ async function createSyncClient(options) {
|
|
|
1772
1529
|
db: pglite,
|
|
1773
1530
|
registry: options.registry,
|
|
1774
1531
|
writeUrl: options.writeUrl,
|
|
1775
|
-
batchWriteUrl: options.batchWriteUrl ?? options.writeUrl,
|
|
1776
1532
|
...options.getAuthToken ? { getAuthToken: options.getAuthToken } : {}
|
|
1777
1533
|
});
|
|
1778
1534
|
await mutationRuntime.recoverSending();
|
|
@@ -1785,6 +1541,7 @@ async function createSyncClient(options) {
|
|
|
1785
1541
|
const client = {
|
|
1786
1542
|
drizzle: drizzleDb,
|
|
1787
1543
|
pglite,
|
|
1544
|
+
views: buildViews(options.registry),
|
|
1788
1545
|
tables: Object.fromEntries(Object.keys(options.registry).map((tableKey) => [
|
|
1789
1546
|
tableKey,
|
|
1790
1547
|
{
|
|
@@ -1843,6 +1600,9 @@ function createDrizzleDatabase(client, schema) {
|
|
|
1843
1600
|
function buildSchema(registry) {
|
|
1844
1601
|
return Object.fromEntries(Object.entries(registry).map(([key, entry]) => [key, entry.table]));
|
|
1845
1602
|
}
|
|
1603
|
+
function buildViews(registry) {
|
|
1604
|
+
return Object.fromEntries(Object.entries(registry).flatMap(([key, entry]) => entry.view != null ? [[key, entry.view]] : []));
|
|
1605
|
+
}
|
|
1846
1606
|
function buildSyncConfigFromRegistry(registry, electricUrl) {
|
|
1847
1607
|
return {
|
|
1848
1608
|
electricUrl,
|
|
@@ -1857,7 +1617,6 @@ function buildSyncTableInput(entry, tableKey) {
|
|
|
1857
1617
|
mode: entry.mode,
|
|
1858
1618
|
primaryKey: entry.primaryKey,
|
|
1859
1619
|
shape: entry.shape,
|
|
1860
|
-
routes: entry.routes,
|
|
1861
1620
|
clientProjection
|
|
1862
1621
|
};
|
|
1863
1622
|
}
|
|
@@ -1872,4 +1631,4 @@ export {
|
|
|
1872
1631
|
createSyncClient
|
|
1873
1632
|
};
|
|
1874
1633
|
|
|
1875
|
-
//# debugId=
|
|
1634
|
+
//# debugId=630ADC166CA4025464756E2164756E21
|