@lunora/do 1.0.0-alpha.13 → 1.0.0-alpha.14
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.d.mts +240 -55
- package/dist/index.d.ts +240 -55
- package/dist/index.mjs +8 -5
- package/dist/packem_shared/{ADMIN_FUNCTIONS-D_UiYJFk.mjs → ADMIN_FUNCTIONS-fMl1vyt1.mjs} +40 -1
- package/dist/packem_shared/{NotUniqueError-DZQtH02h.mjs → NotUniqueError-BXn2j5vu.mjs} +1 -1
- package/dist/packem_shared/{ROOT_DO_SIZE_WARN_BYTES-XEs4csIH.mjs → ROOT_DO_SIZE_WARN_BYTES-DXVOGLBS.mjs} +923 -80
- package/dist/packem_shared/{ctx-db-shapes-DVoeZpo-.mjs → ctx-db-shapes-BQapRFjB.mjs} +1 -1
- package/dist/packem_shared/diffExternalSource-Br2GYVZB.mjs +44 -0
- package/dist/packem_shared/isSourceDue-D_wTv871.mjs +40 -0
- package/dist/packem_shared/materializeExternalRows-CcsfmiDe.mjs +23 -0
- package/dist/packem_shared/{serveRelationFanout-C6lDaesn.mjs → serveRelationFanout-EZLD9-O3.mjs} +1 -1
- package/dist/packem_shared/{subscriptionListDeltas-ce84gpwL.mjs → subscriptionListDeltas-DoB136JT.mjs} +14 -1
- package/package.json +1 -1
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { stableStringify } from './stableStringify-CyHKJXre.mjs';
|
|
2
|
+
|
|
3
|
+
const projectExternalSourceRow = (row, columns) => {
|
|
4
|
+
const projected = { _id: row._id };
|
|
5
|
+
if (columns) {
|
|
6
|
+
for (const key of columns) {
|
|
7
|
+
if (Object.hasOwn(row, key)) {
|
|
8
|
+
projected[key] = row[key];
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
return projected;
|
|
12
|
+
}
|
|
13
|
+
for (const [key, value] of Object.entries(row)) {
|
|
14
|
+
if (key !== "_id" && key !== "_creationTime") {
|
|
15
|
+
projected[key] = value;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return projected;
|
|
19
|
+
};
|
|
20
|
+
const diffExternalSource = (pulled, baseline, options) => {
|
|
21
|
+
const { columns, table } = options;
|
|
22
|
+
const changes = [];
|
|
23
|
+
const nextBaseline = /* @__PURE__ */ new Map();
|
|
24
|
+
for (const source of pulled) {
|
|
25
|
+
const value = projectExternalSourceRow(source, columns);
|
|
26
|
+
const id = String(value._id);
|
|
27
|
+
const json = stableStringify(value);
|
|
28
|
+
nextBaseline.set(id, json);
|
|
29
|
+
const before = baseline.get(id);
|
|
30
|
+
if (before === void 0) {
|
|
31
|
+
changes.push({ doc: value, id, op: "insert", seq: 0, table, ts: 0 });
|
|
32
|
+
} else if (before !== json) {
|
|
33
|
+
changes.push({ doc: value, id, op: "update", seq: 0, table, ts: 0 });
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
for (const id of baseline.keys()) {
|
|
37
|
+
if (!nextBaseline.has(id)) {
|
|
38
|
+
changes.push({ id, op: "delete", seq: 0, table, ts: 0 });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return { changes, nextBaseline };
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export { diffExternalSource, projectExternalSourceRow };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { runExternalSourceTick } from './materializeExternalRows-CcsfmiDe.mjs';
|
|
2
|
+
|
|
3
|
+
const liftSourceId = (row, options = {}) => {
|
|
4
|
+
const { idColumn = "id", map } = options;
|
|
5
|
+
const idValue = row[idColumn];
|
|
6
|
+
if (idValue === void 0 || idValue === null) {
|
|
7
|
+
throw new Error(`external-source: row is missing id column "${idColumn}"`);
|
|
8
|
+
}
|
|
9
|
+
if (typeof idValue !== "string" && typeof idValue !== "number" && typeof idValue !== "bigint") {
|
|
10
|
+
throw new TypeError(`external-source: id column "${idColumn}" must be a string or number`);
|
|
11
|
+
}
|
|
12
|
+
const id = String(idValue);
|
|
13
|
+
if (map) {
|
|
14
|
+
return { ...map(row), _id: id };
|
|
15
|
+
}
|
|
16
|
+
const body = {};
|
|
17
|
+
for (const [key, value] of Object.entries(row)) {
|
|
18
|
+
if (key !== idColumn) {
|
|
19
|
+
body[key] = value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return { ...body, _id: id };
|
|
23
|
+
};
|
|
24
|
+
const isSourceDue = (refresh, lastPolledMs, nowMs) => {
|
|
25
|
+
if (refresh === "manual") {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
if (refresh === void 0 || lastPolledMs === void 0) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
return nowMs - lastPolledMs >= refresh.everyMs;
|
|
32
|
+
};
|
|
33
|
+
const pullExternalSourceTick = async (sql, writer, client, table, source, shardKey) => {
|
|
34
|
+
const parameters = source.tenantBy ? source.tenantBy(shardKey) : [];
|
|
35
|
+
const rows = await client.query(source.query, parameters);
|
|
36
|
+
const documents = rows.map((row) => liftSourceId(row, { idColumn: source.idColumn, map: source.map }));
|
|
37
|
+
return runExternalSourceTick(sql, writer, documents, { columns: source.columns, table });
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export { isSourceDue, liftSourceId, pullExternalSourceTick };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { applyCdcChanges } from './CDC_LOG_TABLE-DSycmnDf.mjs';
|
|
2
|
+
import { s as selectShapeRows } from './ctx-db-shapes-BQapRFjB.mjs';
|
|
3
|
+
import { diffExternalSource, projectExternalSourceRow } from './diffExternalSource-Br2GYVZB.mjs';
|
|
4
|
+
import { stableStringify } from './stableStringify-CyHKJXre.mjs';
|
|
5
|
+
|
|
6
|
+
const materializeExternalRows = async (writer, pulled, baseline, options) => {
|
|
7
|
+
const { changes, nextBaseline } = diffExternalSource(pulled, baseline, options);
|
|
8
|
+
await applyCdcChanges(writer, changes);
|
|
9
|
+
return { applied: changes.length, nextBaseline };
|
|
10
|
+
};
|
|
11
|
+
const readExternalSourceBaseline = (sql, table, columns) => {
|
|
12
|
+
const baseline = /* @__PURE__ */ new Map();
|
|
13
|
+
for (const { doc, id } of selectShapeRows(sql, table, void 0)) {
|
|
14
|
+
baseline.set(id, stableStringify(projectExternalSourceRow({ ...doc, _id: id }, columns)));
|
|
15
|
+
}
|
|
16
|
+
return baseline;
|
|
17
|
+
};
|
|
18
|
+
const runExternalSourceTick = async (sql, writer, pulled, options) => {
|
|
19
|
+
const baseline = readExternalSourceBaseline(sql, options.table, options.columns);
|
|
20
|
+
return materializeExternalRows(writer, pulled, baseline, options);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export { materializeExternalRows, readExternalSourceBaseline, runExternalSourceTick };
|
package/dist/packem_shared/{serveRelationFanout-C6lDaesn.mjs → serveRelationFanout-EZLD9-O3.mjs}
RENAMED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { RELATION_FUNCTION_PREFIX } from './ADMIN_FUNCTIONS-
|
|
1
|
+
import { RELATION_FUNCTION_PREFIX } from './ADMIN_FUNCTIONS-fMl1vyt1.mjs';
|
|
2
2
|
|
|
3
3
|
const serveRelationFanout = async (schema, database, functionPath, args) => {
|
|
4
4
|
const table = typeof args["table"] === "string" ? args["table"] : "";
|
|
@@ -107,5 +107,18 @@ const sendDeltaFrames = (ws, subId, deltaFrames, cursorSuffix) => {
|
|
|
107
107
|
}
|
|
108
108
|
return delivered;
|
|
109
109
|
};
|
|
110
|
+
const awaitWsDrain = async (ws) => {
|
|
111
|
+
let attempts = 0;
|
|
112
|
+
while (attempts < 100) {
|
|
113
|
+
attempts += 1;
|
|
114
|
+
const buffered = ws.bufferedAmount;
|
|
115
|
+
if (typeof buffered !== "number" || buffered < 1048576) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
await new Promise((resolve) => {
|
|
119
|
+
setTimeout(resolve, 20);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
};
|
|
110
123
|
|
|
111
|
-
export { sendDeltaFrames, subscriptionListDeltas, trySendFrame };
|
|
124
|
+
export { awaitWsDrain, sendDeltaFrames, subscriptionListDeltas, trySendFrame };
|