@supalive/core 1.12.0 → 1.13.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/db-ClCicssQ.js.map +1 -1
- package/dist/helper-zdJT5FUc.js +58 -0
- package/dist/helper-zdJT5FUc.js.map +1 -0
- package/dist/index-BIIcgvu8.d.ts +2300 -0
- package/dist/index-BIIcgvu8.d.ts.map +1 -0
- package/dist/index-BedlCLem.d.ts +2292 -0
- package/dist/index-BedlCLem.d.ts.map +1 -0
- package/dist/index-BnV8LnOf.d.ts +2286 -0
- package/dist/index-BnV8LnOf.d.ts.map +1 -0
- package/dist/index-BziK63TZ.d.ts +2271 -0
- package/dist/index-BziK63TZ.d.ts.map +1 -0
- package/dist/index-D31hMBDE.d.ts +2288 -0
- package/dist/index-D31hMBDE.d.ts.map +1 -0
- package/dist/index-DZhReQWB.d.ts +2288 -0
- package/dist/index-DZhReQWB.d.ts.map +1 -0
- package/dist/index-DlG8HDCO.d.ts +2215 -0
- package/dist/index-DlG8HDCO.d.ts.map +1 -0
- package/dist/index-Wgi_dGzj.d.ts +2215 -0
- package/dist/index-Wgi_dGzj.d.ts.map +1 -0
- package/dist/index-j3YTMNlF.d.ts +2223 -0
- package/dist/index-j3YTMNlF.d.ts.map +1 -0
- package/dist/mysql-BBbilD4I.js +614 -0
- package/dist/mysql-BBbilD4I.js.map +1 -0
- package/dist/mysql-BHJ2mrwZ.d.ts +113 -0
- package/dist/mysql-BHJ2mrwZ.d.ts.map +1 -0
- package/dist/mysql-BQbHJ_rK.js +617 -0
- package/dist/mysql-BQbHJ_rK.js.map +1 -0
- package/dist/mysql-BXoAON7c.d.ts +113 -0
- package/dist/mysql-BXoAON7c.d.ts.map +1 -0
- package/dist/mysql-BgfCUYGt.d.ts +113 -0
- package/dist/mysql-BgfCUYGt.d.ts.map +1 -0
- package/dist/mysql-Br7MqUXg.js +620 -0
- package/dist/mysql-Br7MqUXg.js.map +1 -0
- package/dist/mysql-BrueWtO9.d.ts +113 -0
- package/dist/mysql-BrueWtO9.d.ts.map +1 -0
- package/dist/mysql-CCo--z6n.d.ts +113 -0
- package/dist/mysql-CCo--z6n.d.ts.map +1 -0
- package/dist/mysql-CQcbcZB8.d.ts +113 -0
- package/dist/mysql-CQcbcZB8.d.ts.map +1 -0
- package/dist/mysql-CbgCY89y.d.ts +113 -0
- package/dist/mysql-CbgCY89y.d.ts.map +1 -0
- package/dist/mysql-CqD1ZnNY.js +619 -0
- package/dist/mysql-CqD1ZnNY.js.map +1 -0
- package/dist/mysql-D3Db2q3_.js +619 -0
- package/dist/mysql-D3Db2q3_.js.map +1 -0
- package/dist/mysql-DBrhTmYm.js +619 -0
- package/dist/mysql-DBrhTmYm.js.map +1 -0
- package/dist/mysql-DX7YA7IS.d.ts +113 -0
- package/dist/mysql-DX7YA7IS.d.ts.map +1 -0
- package/dist/mysql-DZ0_Rsjc.js +614 -0
- package/dist/mysql-DZ0_Rsjc.js.map +1 -0
- package/dist/mysql-Dko1MJqQ.js +619 -0
- package/dist/mysql-Dko1MJqQ.js.map +1 -0
- package/dist/mysql-F831ycnw.d.ts +113 -0
- package/dist/mysql-F831ycnw.d.ts.map +1 -0
- package/dist/mysql-TLPG62tv.js +618 -0
- package/dist/mysql-TLPG62tv.js.map +1 -0
- package/dist/mysql-e-ii74yG.js +613 -0
- package/dist/mysql-e-ii74yG.js.map +1 -0
- package/dist/overlap-checker-BBkqRdcs.js +259 -0
- package/dist/overlap-checker-BBkqRdcs.js.map +1 -0
- package/dist/overlap-checker-DHUJe-Yy.js +259 -0
- package/dist/overlap-checker-DHUJe-Yy.js.map +1 -0
- package/dist/overlap-checker-DWeQLpSD.js +259 -0
- package/dist/overlap-checker-DWeQLpSD.js.map +1 -0
- package/dist/overlap-checker-Kz7B_jsp.js +259 -0
- package/dist/overlap-checker-Kz7B_jsp.js.map +1 -0
- package/dist/postgres-7hJuQX4Y.d.ts +117 -0
- package/dist/postgres-7hJuQX4Y.d.ts.map +1 -0
- package/dist/postgres-B4H78IkW.js +864 -0
- package/dist/postgres-B4H78IkW.js.map +1 -0
- package/dist/postgres-B4h8D5U2.d.ts +117 -0
- package/dist/postgres-B4h8D5U2.d.ts.map +1 -0
- package/dist/postgres-BXFPamkP.js +864 -0
- package/dist/postgres-BXFPamkP.js.map +1 -0
- package/dist/postgres-CoR3TUMd.js +865 -0
- package/dist/postgres-CoR3TUMd.js.map +1 -0
- package/dist/postgres-D-RsG12a.d.ts +117 -0
- package/dist/postgres-D-RsG12a.d.ts.map +1 -0
- package/dist/postgres-D5Z1S3J9.d.ts +117 -0
- package/dist/postgres-D5Z1S3J9.d.ts.map +1 -0
- package/dist/postgres-DHs89iWT.js +864 -0
- package/dist/postgres-DHs89iWT.js.map +1 -0
- package/dist/postgres-DvBIRnYA.d.ts +117 -0
- package/dist/postgres-DvBIRnYA.d.ts.map +1 -0
- package/dist/postgres-Eo-lpUiX.d.ts +117 -0
- package/dist/postgres-Eo-lpUiX.d.ts.map +1 -0
- package/dist/postgres-VLAGIFLR.d.ts +117 -0
- package/dist/postgres-VLAGIFLR.d.ts.map +1 -0
- package/dist/postgres-hrQmDPUp.d.ts +117 -0
- package/dist/postgres-hrQmDPUp.d.ts.map +1 -0
- package/dist/postgres-pXrP9Fx0.d.ts +117 -0
- package/dist/postgres-pXrP9Fx0.d.ts.map +1 -0
- package/dist/query-BuOs9w-U.js +730 -0
- package/dist/query-BuOs9w-U.js.map +1 -0
- package/dist/query-Xw2hi6TB.js +730 -0
- package/dist/query-Xw2hi6TB.js.map +1 -0
- package/dist/read-routing-BUcQAwnd.js +62 -0
- package/dist/read-routing-BUcQAwnd.js.map +1 -0
- package/dist/read-routing-BXflWzQI.js +61 -0
- package/dist/read-routing-BXflWzQI.js.map +1 -0
- package/dist/read-routing-CcsLp0ow.js +61 -0
- package/dist/read-routing-CcsLp0ow.js.map +1 -0
- package/dist/realtime_db-B70vqtRZ.js +272 -0
- package/dist/realtime_db-B70vqtRZ.js.map +1 -0
- package/dist/realtime_db-BMysrWym.js +312 -0
- package/dist/realtime_db-BMysrWym.js.map +1 -0
- package/dist/realtime_db-Bi7S938D.js +312 -0
- package/dist/realtime_db-Bi7S938D.js.map +1 -0
- package/dist/realtime_db-CVze3gM0.js +312 -0
- package/dist/realtime_db-CVze3gM0.js.map +1 -0
- package/dist/realtime_db-DMipkUmD.js +312 -0
- package/dist/realtime_db-DMipkUmD.js.map +1 -0
- package/dist/realtime_db-XWzuq2JS.js +272 -0
- package/dist/realtime_db-XWzuq2JS.js.map +1 -0
- package/dist/router-Kgrlci8X.js +337 -0
- package/dist/router-Kgrlci8X.js.map +1 -0
- package/dist/router-g8qSQb8m.js +314 -0
- package/dist/router-g8qSQb8m.js.map +1 -0
- package/dist/schema-DLue3K2s.js +134 -0
- package/dist/schema-DLue3K2s.js.map +1 -0
- package/dist/schema-DfowI0ip.js +134 -0
- package/dist/schema-DfowI0ip.js.map +1 -0
- package/dist/schema-fKldWzwc.js +134 -0
- package/dist/schema-fKldWzwc.js.map +1 -0
- package/dist/src/client/index.d.ts +1 -1
- package/dist/src/client/index.js +69 -29
- package/dist/src/client/index.js.map +1 -1
- package/dist/src/exports/mysql.d.ts +1 -1
- package/dist/src/exports/mysql.js +1 -1
- package/dist/src/exports/postgres.d.ts +1 -1
- package/dist/src/exports/postgres.js +1 -1
- package/dist/src/exports/procedure.d.ts +1 -1
- package/dist/src/exports/procedure.js +5 -6
- package/dist/src/exports/procedure.js.map +1 -1
- package/dist/src/exports/schema-sql.d.ts +18 -1
- package/dist/src/exports/schema-sql.d.ts.map +1 -1
- package/dist/src/exports/schema-sql.js +91 -15
- package/dist/src/exports/schema-sql.js.map +1 -1
- package/dist/src/exports/server.d.ts +11 -4
- package/dist/src/exports/server.d.ts.map +1 -1
- package/dist/src/exports/server.js +136 -59
- package/dist/src/exports/server.js.map +1 -1
- package/dist/src/exports/sub-manager-worker-entry.js +1 -1
- package/dist/src/exports/types.d.ts +2 -2
- package/dist/src/exports/types.js +4 -4
- package/dist/sub-worker-dispatch-CO9nVdSG.js +813 -0
- package/dist/sub-worker-dispatch-CO9nVdSG.js.map +1 -0
- package/dist/sub-worker-dispatch-CQBknEL7.js +813 -0
- package/dist/sub-worker-dispatch-CQBknEL7.js.map +1 -0
- package/dist/sub-worker-dispatch-CQHeit6D.js +813 -0
- package/dist/sub-worker-dispatch-CQHeit6D.js.map +1 -0
- package/dist/sub-worker-dispatch-CmEW10it.js +813 -0
- package/dist/sub-worker-dispatch-CmEW10it.js.map +1 -0
- package/dist/sub-worker-dispatch-CunxuOPT.js +823 -0
- package/dist/sub-worker-dispatch-CunxuOPT.js.map +1 -0
- package/dist/sub-worker-dispatch-CvvFm40r.js +817 -0
- package/dist/sub-worker-dispatch-CvvFm40r.js.map +1 -0
- package/dist/sub-worker-dispatch-D15UHo1g.js +813 -0
- package/dist/sub-worker-dispatch-D15UHo1g.js.map +1 -0
- package/dist/sub-worker-dispatch-D8DY56ry.js +817 -0
- package/dist/sub-worker-dispatch-D8DY56ry.js.map +1 -0
- package/dist/sub-worker-dispatch-DJgWxtSQ.js +823 -0
- package/dist/sub-worker-dispatch-DJgWxtSQ.js.map +1 -0
- package/dist/sub-worker-dispatch-Dplqeubd.js +813 -0
- package/dist/sub-worker-dispatch-Dplqeubd.js.map +1 -0
- package/dist/sub-worker-dispatch-dXjUmSMN.js +813 -0
- package/dist/sub-worker-dispatch-dXjUmSMN.js.map +1 -0
- package/dist/table-id-BJdmctEQ.js +91 -0
- package/dist/table-id-BJdmctEQ.js.map +1 -0
- package/dist/table-id-DyrJBKAQ.js +105 -0
- package/dist/table-id-DyrJBKAQ.js.map +1 -0
- package/dist/types_client_rpc-BGMwPJst.js +123 -0
- package/dist/types_client_rpc-BGMwPJst.js.map +1 -0
- package/dist/types_client_rpc-ByGwoRCL.js +132 -0
- package/dist/types_client_rpc-ByGwoRCL.js.map +1 -0
- package/dist/types_db-BsR02Sph.js +171 -0
- package/dist/types_db-BsR02Sph.js.map +1 -0
- package/dist/types_db-CgGlOMmr.js +187 -0
- package/dist/types_db-CgGlOMmr.js.map +1 -0
- package/dist/types_db-OUou3o2Z.js +172 -0
- package/dist/types_db-OUou3o2Z.js.map +1 -0
- package/dist/types_server-B3z8hi-Z.d.ts +523 -0
- package/dist/types_server-B3z8hi-Z.d.ts.map +1 -0
- package/dist/types_server-BXBvz4T8.d.ts +523 -0
- package/dist/types_server-BXBvz4T8.d.ts.map +1 -0
- package/dist/types_server-BhC19R2a.js +27 -0
- package/dist/types_server-BhC19R2a.js.map +1 -0
- package/dist/types_server-CClUPZn1.d.ts +521 -0
- package/dist/types_server-CClUPZn1.d.ts.map +1 -0
- package/dist/types_server-CnYTSPhM.d.ts +523 -0
- package/dist/types_server-CnYTSPhM.d.ts.map +1 -0
- package/dist/types_server-DHpGUHB_.d.ts +521 -0
- package/dist/types_server-DHpGUHB_.d.ts.map +1 -0
- package/dist/types_server-Ded1u3bL.d.ts +521 -0
- package/dist/types_server-Ded1u3bL.d.ts.map +1 -0
- package/dist/types_server-fons7nmo.d.ts +523 -0
- package/dist/types_server-fons7nmo.d.ts.map +1 -0
- package/dist/types_server-okvBG4v_.d.ts +521 -0
- package/dist/types_server-okvBG4v_.d.ts.map +1 -0
- package/dist/types_server-pD8DiI7s.d.ts +521 -0
- package/dist/types_server-pD8DiI7s.d.ts.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import { a as unwrapZodType, i as normalizeCodec } from "./table-id-DyrJBKAQ.js";
|
|
2
|
+
import { t as QueryBuilder } from "./query-Xw2hi6TB.js";
|
|
3
|
+
import { r as LazyCommitTsParam } from "./db-ClCicssQ.js";
|
|
4
|
+
import { l as OccConflictError } from "./types_db-BsR02Sph.js";
|
|
5
|
+
import { z as z$1 } from "zod";
|
|
6
|
+
import { ExponentialBackoff, handleWhen, retry } from "cockatiel";
|
|
7
|
+
//#region src/db/context.ts
|
|
8
|
+
/**
|
|
9
|
+
* Materialize a schema's runtime {@link Model}: column-name mapping, per-column
|
|
10
|
+
* coarse type tags, and the codec used to (de)serialize each column.
|
|
11
|
+
*
|
|
12
|
+
* Codecs are resolved via {@link normalizeCodec}, which supplies a sensible
|
|
13
|
+
* default per Zod type (e.g. a {@link dateCodec} for `z.date()`, so `Date`
|
|
14
|
+
* columns round-trip automatically) while letting a user-supplied codec in
|
|
15
|
+
* `schema.columnsOptions.codecs` override it — partially or fully.
|
|
16
|
+
*/
|
|
17
|
+
function buildModel(schema) {
|
|
18
|
+
const shape = schema.schema.shape;
|
|
19
|
+
const customColumns = schema.columns;
|
|
20
|
+
const columns = {};
|
|
21
|
+
const types = {};
|
|
22
|
+
const codecs = {};
|
|
23
|
+
const schemaCodecs = schema.columnsOptions.codecs ?? {};
|
|
24
|
+
for (const key of Object.keys(shape)) {
|
|
25
|
+
columns[key] = customColumns?.[key] ?? key;
|
|
26
|
+
const field = shape[key];
|
|
27
|
+
const baseField = unwrapZodType(field);
|
|
28
|
+
if (baseField instanceof z$1.ZodNumber) types[key] = "integer";
|
|
29
|
+
else if (baseField instanceof z$1.ZodString) types[key] = "string";
|
|
30
|
+
else if (baseField instanceof z$1.ZodBoolean) types[key] = "boolean";
|
|
31
|
+
else if (baseField instanceof z$1.ZodDate) types[key] = "date";
|
|
32
|
+
else if (baseField instanceof z$1.ZodArray) types[key] = "array";
|
|
33
|
+
else types[key] = "json";
|
|
34
|
+
codecs[key] = normalizeCodec(schemaCodecs[key], baseField);
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
table: schema.table,
|
|
38
|
+
tableId: schema.tableId,
|
|
39
|
+
columns,
|
|
40
|
+
reverseColumns: Object.fromEntries(Object.entries(columns).map(([k, v]) => [v, k])),
|
|
41
|
+
types,
|
|
42
|
+
codecs
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
var TxContext = class {
|
|
46
|
+
beginTs;
|
|
47
|
+
db;
|
|
48
|
+
readSet = [];
|
|
49
|
+
writeSet = [];
|
|
50
|
+
models = /* @__PURE__ */ new Map();
|
|
51
|
+
getOrCreateModel(schema) {
|
|
52
|
+
const existing = this.models.get(schema.table);
|
|
53
|
+
if (existing) return existing;
|
|
54
|
+
const model = buildModel(schema);
|
|
55
|
+
this.models.set(schema.table, model);
|
|
56
|
+
return model;
|
|
57
|
+
}
|
|
58
|
+
constructor(db, beginTs) {
|
|
59
|
+
this.db = db;
|
|
60
|
+
this.beginTs = beginTs;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Rebind the read connection (and its snapshot ts). Used by the replica
|
|
64
|
+
* router when a query handler calls `ctx.usePrimaryConn()`/`useReplicaConn()`.
|
|
65
|
+
* Must be called BEFORE any read — the readSet/beginTs would otherwise mix
|
|
66
|
+
* snapshots — so this throws if a read or write has already happened.
|
|
67
|
+
*/
|
|
68
|
+
useConnection(db, beginTs) {
|
|
69
|
+
if (this.readSet.length > 0 || this.writeSet.length > 0) throw new Error("useConnection()/usePrimaryConn()/useReplicaConn() must be called before any read or write in the handler");
|
|
70
|
+
this.db = db;
|
|
71
|
+
this.beginTs = beginTs;
|
|
72
|
+
}
|
|
73
|
+
internalGetReadSet() {
|
|
74
|
+
return this.readSet;
|
|
75
|
+
}
|
|
76
|
+
internalGetWriteSet() {
|
|
77
|
+
return this.writeSet;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Merge a parent procedure's reads into this context's readSet. Used when a
|
|
81
|
+
* `runMutation` sub-transaction inherits the reads of the procedure that
|
|
82
|
+
* invoked it, so the sub-tx's OCC validation also fences on those rows.
|
|
83
|
+
* Entries are shallow-cloned so the two readSets don't alias.
|
|
84
|
+
*/
|
|
85
|
+
appendInternalReadSet(parentEntries) {
|
|
86
|
+
this.readSet.push(...parentEntries.map((e) => ({ ...e })));
|
|
87
|
+
}
|
|
88
|
+
findPendingWrite(table, id) {
|
|
89
|
+
return this.writeSet.find((w) => w.table === table && w.id === id);
|
|
90
|
+
}
|
|
91
|
+
findPendingInsert(table, id) {
|
|
92
|
+
return this.writeSet.find((w) => w.table === table && w.id === id && w.op === "insert");
|
|
93
|
+
}
|
|
94
|
+
encodeWithCodec(model, property, value) {
|
|
95
|
+
const codec = model.codecs[property];
|
|
96
|
+
if (!codec?.encode || value === void 0 || value === null) return value;
|
|
97
|
+
return codec.encode(value);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
var DbReader = class extends TxContext {
|
|
101
|
+
query(schema) {
|
|
102
|
+
const model = this.getOrCreateModel(schema);
|
|
103
|
+
return new QueryBuilder(this, model);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
var DbWriter = class extends DbReader {
|
|
107
|
+
async insert(schema, id, data) {
|
|
108
|
+
const model = this.getOrCreateModel(schema);
|
|
109
|
+
const mappedData = { id };
|
|
110
|
+
const shape = schema.schema.shape;
|
|
111
|
+
for (const key of Object.keys(shape)) {
|
|
112
|
+
let value = data[key];
|
|
113
|
+
if (value === void 0) {
|
|
114
|
+
const field = shape[key];
|
|
115
|
+
if (field instanceof z$1.ZodDefault) {
|
|
116
|
+
const defaultVal = field.def.defaultValue;
|
|
117
|
+
value = typeof defaultVal === "function" ? defaultVal() : defaultVal;
|
|
118
|
+
if (value !== void 0) {
|
|
119
|
+
const columnName = model.columns[key] ?? key;
|
|
120
|
+
mappedData[columnName] = this.encodeWithCodec(model, key, value);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
const columnName = model.columns[key] ?? key;
|
|
125
|
+
mappedData[columnName] = this.encodeWithCodec(model, key, value);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
mappedData["id"] = id;
|
|
129
|
+
const existing = this.findPendingWrite(schema.table, id);
|
|
130
|
+
if (existing) if (existing.op === "delete") {
|
|
131
|
+
existing.op = "update";
|
|
132
|
+
existing.data = mappedData;
|
|
133
|
+
} else existing.data = {
|
|
134
|
+
...existing.data,
|
|
135
|
+
...mappedData
|
|
136
|
+
};
|
|
137
|
+
else this.writeSet.push({
|
|
138
|
+
table: schema.table,
|
|
139
|
+
tableId: schema.tableId,
|
|
140
|
+
id,
|
|
141
|
+
op: "insert",
|
|
142
|
+
data: mappedData
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
async update(schema, id, data) {
|
|
146
|
+
const model = this.getOrCreateModel(schema);
|
|
147
|
+
const mappedData = {};
|
|
148
|
+
for (const [key, value] of Object.entries(data)) {
|
|
149
|
+
const columnName = model.columns[key] ?? key;
|
|
150
|
+
mappedData[columnName] = this.encodeWithCodec(model, key, value);
|
|
151
|
+
}
|
|
152
|
+
const existing = this.findPendingWrite(schema.table, id);
|
|
153
|
+
if (existing) {
|
|
154
|
+
existing.op = existing.op === "insert" ? "insert" : "update";
|
|
155
|
+
existing.data = {
|
|
156
|
+
...existing.data,
|
|
157
|
+
...mappedData
|
|
158
|
+
};
|
|
159
|
+
} else this.writeSet.push({
|
|
160
|
+
table: schema.table,
|
|
161
|
+
tableId: schema.tableId,
|
|
162
|
+
id,
|
|
163
|
+
op: "update",
|
|
164
|
+
data: mappedData
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
async delete(schema, id) {
|
|
168
|
+
const existingInsert = this.findPendingInsert(schema.table, id);
|
|
169
|
+
if (existingInsert) {
|
|
170
|
+
this.writeSet = this.writeSet.filter((w) => w !== existingInsert);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
this.writeSet.push({
|
|
174
|
+
table: schema.table,
|
|
175
|
+
tableId: schema.tableId,
|
|
176
|
+
id,
|
|
177
|
+
op: "delete"
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
//#endregion
|
|
182
|
+
//#region src/db/realtime_db.ts
|
|
183
|
+
const occRetryPolicy = retry(handleWhen((err) => OccConflictError.isOccConflictError(err)), {
|
|
184
|
+
maxAttempts: 15,
|
|
185
|
+
backoff: new ExponentialBackoff({
|
|
186
|
+
initialDelay: 10,
|
|
187
|
+
maxDelay: 7e3,
|
|
188
|
+
exponent: 2
|
|
189
|
+
})
|
|
190
|
+
});
|
|
191
|
+
const getDbclientRetryPolicy = retry(handleWhen((err) => err.message.includes("timeout exceeded")), {
|
|
192
|
+
maxAttempts: 3,
|
|
193
|
+
backoff: new ExponentialBackoff({
|
|
194
|
+
initialDelay: 500,
|
|
195
|
+
maxDelay: 2e3,
|
|
196
|
+
exponent: 2
|
|
197
|
+
})
|
|
198
|
+
});
|
|
199
|
+
var SupaliveDb = class SupaliveDb {
|
|
200
|
+
impl;
|
|
201
|
+
replica;
|
|
202
|
+
/**
|
|
203
|
+
* @param impl the PRIMARY (writer) connection — all mutations, the OCC
|
|
204
|
+
* commit-ts source, and cache-freshness commit-log scans run here.
|
|
205
|
+
* @param replica optional read replica for query/action reads (see the
|
|
206
|
+
* routing in one-shot-query.ts). Undefined → reads use primary.
|
|
207
|
+
*/
|
|
208
|
+
constructor(impl, replica) {
|
|
209
|
+
this.impl = impl;
|
|
210
|
+
this.replica = replica;
|
|
211
|
+
}
|
|
212
|
+
static create(config) {
|
|
213
|
+
return new SupaliveDb(config.db, config.replica ? new SupaliveDb(config.replica) : void 0);
|
|
214
|
+
}
|
|
215
|
+
/** True when a distinct read replica is configured. */
|
|
216
|
+
get hasReplica() {
|
|
217
|
+
return this.replica !== void 0;
|
|
218
|
+
}
|
|
219
|
+
onAfterCommit;
|
|
220
|
+
/**
|
|
221
|
+
* Register a hook fired once after every successful `mutation()` commit
|
|
222
|
+
* (never on an OCC conflict/rollback), with the committed writeSet and its
|
|
223
|
+
* commitTs. The server uses this to drive subscription invalidation for ALL
|
|
224
|
+
* mutation sources — client WS calls, actions, jobs, and the internal caller —
|
|
225
|
+
* so a server-side write pushes live updates just like a client write does.
|
|
226
|
+
* Non-server consumers (migration tooling, tests) leave it unset (no-op).
|
|
227
|
+
*/
|
|
228
|
+
setOnAfterCommit(cb) {
|
|
229
|
+
this.onAfterCommit = cb;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* One-shot read against an EXPLICIT connection + snapshot ts (rather than this
|
|
233
|
+
* db's own latest). Used by the replica router: the caller pre-resolves the
|
|
234
|
+
* default read connection (replica for queries) and hands the handler a
|
|
235
|
+
* `DbReader` it can rebind via `useConnection` (ctx.usePrimaryConn/…). The
|
|
236
|
+
* returned `ts` is the FINAL bound connection's snapshot, so the cache entry is
|
|
237
|
+
* stamped with the ts actually read at.
|
|
238
|
+
*/
|
|
239
|
+
async queryInternalOn(read, fn) {
|
|
240
|
+
const ctx = new DbReader(read.impl, read.beginTs);
|
|
241
|
+
const data = await fn(ctx);
|
|
242
|
+
return {
|
|
243
|
+
ts: ctx.beginTs,
|
|
244
|
+
data,
|
|
245
|
+
readSet: ctx.internalGetReadSet()
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
async query(fn) {
|
|
249
|
+
const beginTs = await this.impl.getLatestSnapshotTimestamp();
|
|
250
|
+
return await fn(new DbReader(this.impl, beginTs));
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Compute a query once at the latest snapshot, returning the result together
|
|
254
|
+
* with the snapshot `ts` and the tracked `readSet`. This is a plain one-shot
|
|
255
|
+
* evaluation — it registers NO live subscription; the `readSet`/`ts` are
|
|
256
|
+
* returned purely so the caller (e.g. the one-shot query cache) can store
|
|
257
|
+
* freshness metadata. Use this for cached one-shot query calls (WS `call`,
|
|
258
|
+
* HTTP RPC). `liveQuery` is the same computation under the subscription path's
|
|
259
|
+
* name and simply delegates here.
|
|
260
|
+
*/
|
|
261
|
+
async queryInternal(fn) {
|
|
262
|
+
const beginTs = await this.impl.getLatestSnapshotTimestamp();
|
|
263
|
+
const ctx = new DbReader(this.impl, beginTs);
|
|
264
|
+
return {
|
|
265
|
+
ts: beginTs,
|
|
266
|
+
data: await fn(ctx),
|
|
267
|
+
readSet: ctx.internalGetReadSet()
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
async liveQuery(fn) {
|
|
271
|
+
return this.queryInternal(fn);
|
|
272
|
+
}
|
|
273
|
+
async mutation(fn, retryPolicy = occRetryPolicy) {
|
|
274
|
+
return await retryPolicy.execute(async (p) => {
|
|
275
|
+
let db;
|
|
276
|
+
try {
|
|
277
|
+
const beginTs = await this.impl.getLatestSnapshotTimestamp() + 1n;
|
|
278
|
+
const ctx = new DbWriter(this.impl, beginTs);
|
|
279
|
+
const result = await fn(ctx);
|
|
280
|
+
const readSet = ctx.internalGetReadSet();
|
|
281
|
+
const writeSet = ctx.internalGetWriteSet();
|
|
282
|
+
const commitTsLazy = new LazyCommitTsParam(void 0);
|
|
283
|
+
await this.impl.attachPrevDataToWriteSet(writeSet, readSet);
|
|
284
|
+
const queries = await this.impl.prepareCommitWrites(readSet, writeSet, commitTsLazy);
|
|
285
|
+
db = await getDbclientRetryPolicy.execute(() => this.impl.getTransactionClient());
|
|
286
|
+
await db.begin();
|
|
287
|
+
const commitTs = await db.getNextTimestamp();
|
|
288
|
+
commitTsLazy.value = commitTs;
|
|
289
|
+
const commitResult = await db.commitWrites(beginTs, queries, commitTs);
|
|
290
|
+
if (!commitResult.success) throw new OccConflictError("Commit failed", { commitResult });
|
|
291
|
+
await db.updateLatestSnapshotTimestamp(commitTs);
|
|
292
|
+
await db.commit();
|
|
293
|
+
this.onAfterCommit?.(writeSet, commitTs);
|
|
294
|
+
return {
|
|
295
|
+
result,
|
|
296
|
+
writeSet,
|
|
297
|
+
commitTs
|
|
298
|
+
};
|
|
299
|
+
} catch (err) {
|
|
300
|
+
await db?.rollback();
|
|
301
|
+
throw err;
|
|
302
|
+
} finally {
|
|
303
|
+
db?.release();
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
const sleep = (ms) => new Promise((res) => setTimeout(res, ms));
|
|
309
|
+
//#endregion
|
|
310
|
+
export { TxContext as a, DbWriter as i, sleep as n, DbReader as r, SupaliveDb as t };
|
|
311
|
+
|
|
312
|
+
//# sourceMappingURL=realtime_db-Bi7S938D.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"realtime_db-Bi7S938D.js","names":["z"],"sources":["../src/db/context.ts","../src/db/realtime_db.ts"],"sourcesContent":["import { z, ZodRawShape } from \"zod\";\nimport { Database } from \"./db\";\nimport { ReadEntry, WriteEntry, } from \"./types_db\";\n\nimport { ColumnCodec, InferSchema, InsertData, Model, SchemaDefinition } from \"./schema\";\nimport { normalizeCodec, unwrapZodType } from \"./codec\";\nimport { QueryBuilder } from \"./query\";\n\n/**\n * Materialize a schema's runtime {@link Model}: column-name mapping, per-column\n * coarse type tags, and the codec used to (de)serialize each column.\n *\n * Codecs are resolved via {@link normalizeCodec}, which supplies a sensible\n * default per Zod type (e.g. a {@link dateCodec} for `z.date()`, so `Date`\n * columns round-trip automatically) while letting a user-supplied codec in\n * `schema.columnsOptions.codecs` override it — partially or fully.\n */\nexport function buildModel(schema: SchemaDefinition<string, ZodRawShape>): Model {\n const shape = schema.schema.shape;\n const customColumns = (schema as any).columns;\n const columns: Record<string, string> = {};\n const types: Record<string, string> = {};\n const codecs: Record<string, ColumnCodec<any, any>> = {};\n const schemaCodecs = schema.columnsOptions.codecs ?? {};\n\n for (const key of Object.keys(shape)) {\n columns[key] = customColumns?.[key] ?? key;\n const field = shape[key];\n const baseField = unwrapZodType(field);\n if (baseField instanceof z.ZodNumber) {\n types[key] = \"integer\";\n } else if (baseField instanceof z.ZodString) {\n types[key] = \"string\";\n } else if (baseField instanceof z.ZodBoolean) {\n types[key] = \"boolean\";\n } else if (baseField instanceof z.ZodDate) {\n types[key] = \"date\";\n } else if (baseField instanceof z.ZodArray) {\n types[key] = \"array\";\n } else {\n types[key] = \"json\";\n }\n codecs[key] = normalizeCodec(schemaCodecs[key], baseField);\n }\n\n return {\n table: schema.table,\n tableId: schema.tableId,\n columns,\n reverseColumns: Object.fromEntries(Object.entries(columns).map(([k, v]) => [v, k])),\n types,\n codecs,\n };\n}\n\nexport class TxContext {\n // Not `readonly`: a query's read connection can be rebound (primary↔replica)\n // via {@link useConnection} before the first read — see the replica routing in\n // realtime_db.ts / one-shot-query.ts. Mutations never rebind.\n beginTs: bigint;\n db: Database;\n protected readSet: ReadEntry[] = [];\n protected writeSet: WriteEntry[] = [];\n private models: Map<string, Model> = new Map();\n\n protected getOrCreateModel(schema: SchemaDefinition<string, ZodRawShape>): Model {\n const existing = this.models.get(schema.table);\n if (existing) return existing;\n\n const model = buildModel(schema);\n this.models.set(schema.table, model);\n return model;\n }\n\n constructor(db: Database, beginTs: bigint) {\n this.db = db;\n this.beginTs = beginTs;\n }\n\n /**\n * Rebind the read connection (and its snapshot ts). Used by the replica\n * router when a query handler calls `ctx.usePrimaryConn()`/`useReplicaConn()`.\n * Must be called BEFORE any read — the readSet/beginTs would otherwise mix\n * snapshots — so this throws if a read or write has already happened.\n */\n useConnection(db: Database, beginTs: bigint): void {\n if (this.readSet.length > 0 || this.writeSet.length > 0) {\n throw new Error(\n \"useConnection()/usePrimaryConn()/useReplicaConn() must be called before any read or write in the handler\",\n );\n }\n this.db = db;\n this.beginTs = beginTs;\n }\n\n internalGetReadSet(): ReadEntry[] { return this.readSet; }\n internalGetWriteSet(): WriteEntry[] { return this.writeSet; }\n\n /**\n * Merge a parent procedure's reads into this context's readSet. Used when a\n * `runMutation` sub-transaction inherits the reads of the procedure that\n * invoked it, so the sub-tx's OCC validation also fences on those rows.\n * Entries are shallow-cloned so the two readSets don't alias.\n */\n appendInternalReadSet(parentEntries: ReadEntry[]) {\n this.readSet.push(...parentEntries.map((e) => ({...e})));\n }\n\n protected findPendingWrite(table: string, id: string): WriteEntry | undefined {\n return this.writeSet.find(w => w.table === table && w.id === id);\n }\n\n protected findPendingInsert(table: string, id: string): WriteEntry | undefined {\n return this.writeSet.find(w => w.table === table && w.id === id && w.op === \"insert\");\n }\n\n protected encodeWithCodec(model: Model, property: string, value: unknown): unknown {\n const codec = model.codecs[property];\n if (!codec?.encode || value === undefined || value === null) return value;\n return codec.encode(value);\n }\n}\n\nexport class DbReader extends TxContext {\n query<S extends SchemaDefinition<string, ZodRawShape>>(schema: S): QueryBuilder<S> {\n const model = this.getOrCreateModel(schema);\n return new QueryBuilder<S>(this, model);\n }\n}\n\nexport class DbWriter extends DbReader {\n async insert<S extends SchemaDefinition<string, ZodRawShape>, ID extends string>(\n schema: S,\n id: ID,\n data: InsertData<S>\n ): Promise<void> {\n const model = this.getOrCreateModel(schema);\n const mappedData: Record<string, unknown> = { id };\n const shape = schema.schema.shape;\n\n for (const key of Object.keys(shape)) {\n let value = data[key];\n\n if (value === undefined) {\n const field = shape[key];\n if (field instanceof z.ZodDefault) {\n const defaultVal = field.def.defaultValue;\n value = (typeof defaultVal === \"function\" ? defaultVal() : defaultVal)\n\n if (value !== undefined) {\n const columnName = model.columns[key] ?? key;\n mappedData[columnName] = this.encodeWithCodec(model, key, value);\n }\n }\n } else {\n const columnName = model.columns[key] ?? key;\n mappedData[columnName] = this.encodeWithCodec(model, key, value);\n }\n }\n // ensure id same\n mappedData[\"id\"] = id;\n\n // Check for existing pending write for the same ID\n const existing = this.findPendingWrite(schema.table, id);\n if (existing) {\n if (existing.op === \"delete\") {\n // If there's a pending delete, change it to an update with the new data\n existing.op = \"update\";\n existing.data = mappedData;\n } else {\n // If there's a pending update or same insert with the same ID , merge the new data\n existing.data = { ...existing.data, ...mappedData };\n }\n } else {\n this.writeSet.push({ table: schema.table, tableId: schema.tableId, id: id, op: \"insert\", data: mappedData });\n }\n }\n\n async update<S extends SchemaDefinition<string, ZodRawShape>, ID extends string>(\n schema: S,\n id: ID,\n data: Partial<Omit<InferSchema<S>, \"id\" | \"commitTs\">>,\n ): Promise<void> {\n const model = this.getOrCreateModel(schema);\n const mappedData: Record<string, unknown> = {};\n\n for (const [key, value] of Object.entries(data)) {\n const columnName = model.columns[key] ?? key;\n mappedData[columnName] = this.encodeWithCodec(model, key, value);\n }\n\n const existing = this.findPendingWrite(schema.table, id);\n if (existing) {\n existing.op = existing.op === \"insert\" ? \"insert\" : \"update\";\n existing.data = { ...existing.data, ...mappedData };\n } else {\n // expectedTs will be filled by the committer via batch fetch if not known yet\n this.writeSet.push({ table: schema.table, tableId: schema.tableId, id, op: \"update\", data: mappedData });\n }\n }\n\n async delete<S extends SchemaDefinition<string, ZodRawShape>, ID extends string>(\n schema: S,\n id: ID,\n ): Promise<void> {\n // If there's a pending insert for the same ID, remove it instead of adding a delete\n const existingInsert = this.findPendingInsert(schema.table, id);\n if (existingInsert) {\n this.writeSet = this.writeSet.filter(w => w !== existingInsert);\n return;\n }\n\n // expectedTs will be filled by the committer via batch fetch if not known yet\n this.writeSet.push({ table: schema.table, tableId: schema.tableId, id, op: \"delete\" });\n }\n}\n","import { Database, LazyCommitTsParam, TxDatabase } from \"./db\";\nimport {\n OccConflictError, WriteEntry, CommitTs, LiveResult, MutationResult\n} from \"./types_db\";\nimport { DbReader, DbWriter } from \"./context\";\nimport { ExponentialBackoff, handleWhen, retry, RetryPolicy } from \"cockatiel\";\n\nexport const occRetryPolicy = retry(\n handleWhen((err) => OccConflictError.isOccConflictError(err)),\n {\n maxAttempts: 15,\n backoff: new ExponentialBackoff({\n initialDelay: 10,\n maxDelay: 7000,\n exponent: 2,\n }),\n },\n);\n\nexport const getDbclientRetryPolicy = retry(\n handleWhen((err) => err.message.includes('timeout exceeded')),\n {\n maxAttempts: 3,\n backoff: new ExponentialBackoff({\n initialDelay: 500,\n maxDelay: 2000,\n exponent: 2,\n }),\n },\n);\n\nexport class SupaliveDb {\n /**\n * @param impl the PRIMARY (writer) connection — all mutations, the OCC\n * commit-ts source, and cache-freshness commit-log scans run here.\n * @param replica optional read replica for query/action reads (see the\n * routing in one-shot-query.ts). Undefined → reads use primary.\n */\n constructor(readonly impl: Database, readonly replica?: SupaliveDb) { }\n\n static create(config: { db: Database; replica?: Database }): SupaliveDb {\n return new SupaliveDb(config.db, config.replica ? new SupaliveDb(config.replica) : undefined);\n }\n\n /** True when a distinct read replica is configured. */\n get hasReplica(): boolean {\n return this.replica !== undefined;\n }\n\n protected onAfterCommit?: (writeSet: WriteEntry[], commitTs: CommitTs) => void;\n\n /**\n * Register a hook fired once after every successful `mutation()` commit\n * (never on an OCC conflict/rollback), with the committed writeSet and its\n * commitTs. The server uses this to drive subscription invalidation for ALL\n * mutation sources — client WS calls, actions, jobs, and the internal caller —\n * so a server-side write pushes live updates just like a client write does.\n * Non-server consumers (migration tooling, tests) leave it unset (no-op).\n */\n setOnAfterCommit(cb: (writeSet: WriteEntry[], commitTs: CommitTs) => void): void {\n this.onAfterCommit = cb;\n }\n\n // ── Read-only query ──────────────────────────────────────────────\n\n /**\n * One-shot read against an EXPLICIT connection + snapshot ts (rather than this\n * db's own latest). Used by the replica router: the caller pre-resolves the\n * default read connection (replica for queries) and hands the handler a\n * `DbReader` it can rebind via `useConnection` (ctx.usePrimaryConn/…). The\n * returned `ts` is the FINAL bound connection's snapshot, so the cache entry is\n * stamped with the ts actually read at.\n */\n async queryInternalOn<T>(\n read: { impl: Database; beginTs: bigint },\n fn: (ctx: DbReader) => Promise<T>,\n ): Promise<LiveResult<T>> {\n const ctx = new DbReader(read.impl, read.beginTs);\n const data = await fn(ctx);\n return { ts: ctx.beginTs, data, readSet: ctx.internalGetReadSet() };\n }\n\n async query<T>(fn: (ctx: DbReader) => Promise<T>): Promise<T> {\n const beginTs = await this.impl.getLatestSnapshotTimestamp();\n const ctx = new DbReader(this.impl, beginTs);\n return await fn(ctx);\n }\n\n /**\n * Compute a query once at the latest snapshot, returning the result together\n * with the snapshot `ts` and the tracked `readSet`. This is a plain one-shot\n * evaluation — it registers NO live subscription; the `readSet`/`ts` are\n * returned purely so the caller (e.g. the one-shot query cache) can store\n * freshness metadata. Use this for cached one-shot query calls (WS `call`,\n * HTTP RPC). `liveQuery` is the same computation under the subscription path's\n * name and simply delegates here.\n */\n async queryInternal<T>(fn: (ctx: DbReader) => Promise<T>): Promise<LiveResult<T>> {\n const beginTs = await this.impl.getLatestSnapshotTimestamp();\n const ctx = new DbReader(this.impl, beginTs);\n const data = await fn(ctx);\n\n return {\n ts: beginTs,\n data,\n readSet: ctx.internalGetReadSet(),\n };\n }\n\n async liveQuery<T>(fn: (ctx: DbReader) => Promise<T>): Promise<LiveResult<T>> {\n return this.queryInternal(fn);\n }\n\n // ── Mutation ───────────────────────────────────────────────────────\n\n async mutation<T>(\n fn: (ctx: DbWriter) => Promise<T>,\n retryPolicy: RetryPolicy = occRetryPolicy,\n ): Promise<MutationResult<T>> {\n return await retryPolicy.execute(async (p) => {\n let db: TxDatabase | undefined;\n try {\n // Step1: Get latest snapshot timestamp as beginTs\n const beginTs = await this.impl.getLatestSnapshotTimestamp() + 1n;\n\n const ctx = new DbWriter(this.impl, beginTs);\n const result = await fn(ctx);\n\n const readSet = ctx.internalGetReadSet();\n const writeSet = ctx.internalGetWriteSet();\n const commitTsLazy = new LazyCommitTsParam(undefined);\n\n // Step2: Capture prevData + expectedCommitTs per write. prevData\n // is consumed by the invalidator (range subs need it to detect\n // result-set transitions). expectedCommitTs becomes the CAS fence\n // for the apply step — preferring prior find()-captured ts so\n // read-then-update patterns get beginTs-level isolation.\n await this.impl.attachPrevDataToWriteSet(writeSet, readSet);\n\n // Step3: Prepare quieries before transaction\n const queries = await this.impl.prepareCommitWrites(\n readSet,\n writeSet,\n commitTsLazy,\n );\n\n // Step4: Starts new transaction\n db = await getDbclientRetryPolicy.execute(() => this.impl.getTransactionClient());\n await db.begin();\n\n // Step5: Get commitTs from DB sequence (bigger than all previous)\n const commitTs = await db.getNextTimestamp();\n commitTsLazy.value = commitTs;\n\n // Step6: Apply + validate. CAS UPDATE/DELETE catches concurrent\n // peer touches on the write set; the readSet is validated either\n // per-row (fast path, no range reads) or via commit-log scan\n // (slow path, when range reads are present so phantoms still\n // need detection).\n const commitResult = await db.commitWrites(\n beginTs,\n queries,\n commitTs,\n );\n\n if (!commitResult.success) {\n throw new OccConflictError(\"Commit failed\", { commitResult });\n }\n\n await db.updateLatestSnapshotTimestamp(commitTs);\n await db.commit();\n\n // Notify after successful commit\n this.onAfterCommit?.(writeSet, commitTs);\n\n return { result, writeSet, commitTs };\n } catch (err) {\n await db?.rollback();\n throw err;\n } finally {\n db?.release();\n }\n });\n }\n}\n\nexport const sleep = (ms: number) => new Promise<void>((res) => setTimeout(res, ms));\n"],"mappings":";;;;;;;;;;;;;;;;AAiBA,SAAgB,WAAW,QAAsD;CAC/E,MAAM,QAAQ,OAAO,OAAO;CAC5B,MAAM,gBAAiB,OAAe;CACtC,MAAM,UAAkC,CAAC;CACzC,MAAM,QAAgC,CAAC;CACvC,MAAM,SAAgD,CAAC;CACvD,MAAM,eAAe,OAAO,eAAe,UAAU,CAAC;CAEtD,KAAK,MAAM,OAAO,OAAO,KAAK,KAAK,GAAG;EACpC,QAAQ,OAAO,gBAAgB,QAAQ;EACvC,MAAM,QAAQ,MAAM;EACpB,MAAM,YAAY,cAAc,KAAK;EACrC,IAAI,qBAAqBA,IAAE,WACzB,MAAM,OAAO;OACR,IAAI,qBAAqBA,IAAE,WAChC,MAAM,OAAO;OACR,IAAI,qBAAqBA,IAAE,YAChC,MAAM,OAAO;OACR,IAAI,qBAAqBA,IAAE,SAChC,MAAM,OAAO;OACR,IAAI,qBAAqBA,IAAE,UAChC,MAAM,OAAO;OAEb,MAAM,OAAO;EAEf,OAAO,OAAO,eAAe,aAAa,MAAM,SAAS;CAC3D;CAEA,OAAO;EACL,OAAO,OAAO;EACd,SAAS,OAAO;EAChB;EACA,gBAAgB,OAAO,YAAY,OAAO,QAAQ,OAAO,CAAC,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC;EAClF;EACA;CACF;AACF;AAEA,IAAa,YAAb,MAAuB;CAIrB;CACA;CACA,UAAiC,CAAC;CAClC,WAAmC,CAAC;CACpC,yBAAqC,IAAI,IAAI;CAE7C,iBAA2B,QAAsD;EAC/E,MAAM,WAAW,KAAK,OAAO,IAAI,OAAO,KAAK;EAC7C,IAAI,UAAU,OAAO;EAErB,MAAM,QAAQ,WAAW,MAAM;EAC/B,KAAK,OAAO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K,gBAAgB,UAAU,QAAQ;IAEvC,OAAO;KAAE;KAAQ;KAAU;IAAS;GACtC,SAAS,KAAK;IACZ,MAAM,IAAI,SAAS;IACnB,MAAM;GACR,UAAU;IACR,IAAI,QAAQ;GACd;EACF,CAAC;CACH;AACF;AAEA,MAAa,SAAS,OAAe,IAAI,SAAe,QAAQ,WAAW,KAAK,EAAE,CAAC"}
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import { a as unwrapZodType, i as normalizeCodec } from "./table-id-DyrJBKAQ.js";
|
|
2
|
+
import { t as QueryBuilder } from "./query-Xw2hi6TB.js";
|
|
3
|
+
import { r as LazyCommitTsParam } from "./db-ClCicssQ.js";
|
|
4
|
+
import { l as OccConflictError } from "./types_db-OUou3o2Z.js";
|
|
5
|
+
import { z as z$1 } from "zod";
|
|
6
|
+
import { ExponentialBackoff, handleWhen, retry } from "cockatiel";
|
|
7
|
+
//#region src/db/context.ts
|
|
8
|
+
/**
|
|
9
|
+
* Materialize a schema's runtime {@link Model}: column-name mapping, per-column
|
|
10
|
+
* coarse type tags, and the codec used to (de)serialize each column.
|
|
11
|
+
*
|
|
12
|
+
* Codecs are resolved via {@link normalizeCodec}, which supplies a sensible
|
|
13
|
+
* default per Zod type (e.g. a {@link dateCodec} for `z.date()`, so `Date`
|
|
14
|
+
* columns round-trip automatically) while letting a user-supplied codec in
|
|
15
|
+
* `schema.columnsOptions.codecs` override it — partially or fully.
|
|
16
|
+
*/
|
|
17
|
+
function buildModel(schema) {
|
|
18
|
+
const shape = schema.schema.shape;
|
|
19
|
+
const customColumns = schema.columns;
|
|
20
|
+
const columns = {};
|
|
21
|
+
const types = {};
|
|
22
|
+
const codecs = {};
|
|
23
|
+
const schemaCodecs = schema.columnsOptions.codecs ?? {};
|
|
24
|
+
for (const key of Object.keys(shape)) {
|
|
25
|
+
columns[key] = customColumns?.[key] ?? key;
|
|
26
|
+
const field = shape[key];
|
|
27
|
+
const baseField = unwrapZodType(field);
|
|
28
|
+
if (baseField instanceof z$1.ZodNumber) types[key] = "integer";
|
|
29
|
+
else if (baseField instanceof z$1.ZodString) types[key] = "string";
|
|
30
|
+
else if (baseField instanceof z$1.ZodBoolean) types[key] = "boolean";
|
|
31
|
+
else if (baseField instanceof z$1.ZodDate) types[key] = "date";
|
|
32
|
+
else if (baseField instanceof z$1.ZodArray) types[key] = "array";
|
|
33
|
+
else types[key] = "json";
|
|
34
|
+
codecs[key] = normalizeCodec(schemaCodecs[key], baseField);
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
table: schema.table,
|
|
38
|
+
tableId: schema.tableId,
|
|
39
|
+
columns,
|
|
40
|
+
reverseColumns: Object.fromEntries(Object.entries(columns).map(([k, v]) => [v, k])),
|
|
41
|
+
types,
|
|
42
|
+
codecs
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
var TxContext = class {
|
|
46
|
+
beginTs;
|
|
47
|
+
db;
|
|
48
|
+
readSet = [];
|
|
49
|
+
writeSet = [];
|
|
50
|
+
models = /* @__PURE__ */ new Map();
|
|
51
|
+
getOrCreateModel(schema) {
|
|
52
|
+
const existing = this.models.get(schema.table);
|
|
53
|
+
if (existing) return existing;
|
|
54
|
+
const model = buildModel(schema);
|
|
55
|
+
this.models.set(schema.table, model);
|
|
56
|
+
return model;
|
|
57
|
+
}
|
|
58
|
+
constructor(db, beginTs) {
|
|
59
|
+
this.db = db;
|
|
60
|
+
this.beginTs = beginTs;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Rebind the read connection (and its snapshot ts). Used by the replica
|
|
64
|
+
* router when a query handler calls `ctx.usePrimaryConn()`/`useReplicaConn()`.
|
|
65
|
+
* Must be called BEFORE any read — the readSet/beginTs would otherwise mix
|
|
66
|
+
* snapshots — so this throws if a read or write has already happened.
|
|
67
|
+
*/
|
|
68
|
+
useConnection(db, beginTs) {
|
|
69
|
+
if (this.readSet.length > 0 || this.writeSet.length > 0) throw new Error("useConnection()/usePrimaryConn()/useReplicaConn() must be called before any read or write in the handler");
|
|
70
|
+
this.db = db;
|
|
71
|
+
this.beginTs = beginTs;
|
|
72
|
+
}
|
|
73
|
+
internalGetReadSet() {
|
|
74
|
+
return this.readSet;
|
|
75
|
+
}
|
|
76
|
+
internalGetWriteSet() {
|
|
77
|
+
return this.writeSet;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Merge a parent procedure's reads into this context's readSet. Used when a
|
|
81
|
+
* `runMutation` sub-transaction inherits the reads of the procedure that
|
|
82
|
+
* invoked it, so the sub-tx's OCC validation also fences on those rows.
|
|
83
|
+
* Entries are shallow-cloned so the two readSets don't alias.
|
|
84
|
+
*/
|
|
85
|
+
appendInternalReadSet(parentEntries) {
|
|
86
|
+
this.readSet.push(...parentEntries.map((e) => ({ ...e })));
|
|
87
|
+
}
|
|
88
|
+
findPendingWrite(table, id) {
|
|
89
|
+
return this.writeSet.find((w) => w.table === table && w.id === id);
|
|
90
|
+
}
|
|
91
|
+
findPendingInsert(table, id) {
|
|
92
|
+
return this.writeSet.find((w) => w.table === table && w.id === id && w.op === "insert");
|
|
93
|
+
}
|
|
94
|
+
encodeWithCodec(model, property, value) {
|
|
95
|
+
const codec = model.codecs[property];
|
|
96
|
+
if (!codec?.encode || value === void 0 || value === null) return value;
|
|
97
|
+
return codec.encode(value);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
var DbReader = class extends TxContext {
|
|
101
|
+
query(schema) {
|
|
102
|
+
const model = this.getOrCreateModel(schema);
|
|
103
|
+
return new QueryBuilder(this, model);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
var DbWriter = class extends DbReader {
|
|
107
|
+
async insert(schema, id, data) {
|
|
108
|
+
const model = this.getOrCreateModel(schema);
|
|
109
|
+
const mappedData = { id };
|
|
110
|
+
const shape = schema.schema.shape;
|
|
111
|
+
for (const key of Object.keys(shape)) {
|
|
112
|
+
let value = data[key];
|
|
113
|
+
if (value === void 0) {
|
|
114
|
+
const field = shape[key];
|
|
115
|
+
if (field instanceof z$1.ZodDefault) {
|
|
116
|
+
const defaultVal = field.def.defaultValue;
|
|
117
|
+
value = typeof defaultVal === "function" ? defaultVal() : defaultVal;
|
|
118
|
+
if (value !== void 0) {
|
|
119
|
+
const columnName = model.columns[key] ?? key;
|
|
120
|
+
mappedData[columnName] = this.encodeWithCodec(model, key, value);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
const columnName = model.columns[key] ?? key;
|
|
125
|
+
mappedData[columnName] = this.encodeWithCodec(model, key, value);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
mappedData["id"] = id;
|
|
129
|
+
const existing = this.findPendingWrite(schema.table, id);
|
|
130
|
+
if (existing) if (existing.op === "delete") {
|
|
131
|
+
existing.op = "update";
|
|
132
|
+
existing.data = mappedData;
|
|
133
|
+
} else existing.data = {
|
|
134
|
+
...existing.data,
|
|
135
|
+
...mappedData
|
|
136
|
+
};
|
|
137
|
+
else this.writeSet.push({
|
|
138
|
+
table: schema.table,
|
|
139
|
+
tableId: schema.tableId,
|
|
140
|
+
id,
|
|
141
|
+
op: "insert",
|
|
142
|
+
data: mappedData
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
async update(schema, id, data) {
|
|
146
|
+
const model = this.getOrCreateModel(schema);
|
|
147
|
+
const mappedData = {};
|
|
148
|
+
for (const [key, value] of Object.entries(data)) {
|
|
149
|
+
const columnName = model.columns[key] ?? key;
|
|
150
|
+
mappedData[columnName] = this.encodeWithCodec(model, key, value);
|
|
151
|
+
}
|
|
152
|
+
const existing = this.findPendingWrite(schema.table, id);
|
|
153
|
+
if (existing) {
|
|
154
|
+
existing.op = existing.op === "insert" ? "insert" : "update";
|
|
155
|
+
existing.data = {
|
|
156
|
+
...existing.data,
|
|
157
|
+
...mappedData
|
|
158
|
+
};
|
|
159
|
+
} else this.writeSet.push({
|
|
160
|
+
table: schema.table,
|
|
161
|
+
tableId: schema.tableId,
|
|
162
|
+
id,
|
|
163
|
+
op: "update",
|
|
164
|
+
data: mappedData
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
async delete(schema, id) {
|
|
168
|
+
const existingInsert = this.findPendingInsert(schema.table, id);
|
|
169
|
+
if (existingInsert) {
|
|
170
|
+
this.writeSet = this.writeSet.filter((w) => w !== existingInsert);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
this.writeSet.push({
|
|
174
|
+
table: schema.table,
|
|
175
|
+
tableId: schema.tableId,
|
|
176
|
+
id,
|
|
177
|
+
op: "delete"
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
//#endregion
|
|
182
|
+
//#region src/db/realtime_db.ts
|
|
183
|
+
const occRetryPolicy = retry(handleWhen((err) => OccConflictError.isOccConflictError(err)), {
|
|
184
|
+
maxAttempts: 15,
|
|
185
|
+
backoff: new ExponentialBackoff({
|
|
186
|
+
initialDelay: 10,
|
|
187
|
+
maxDelay: 7e3,
|
|
188
|
+
exponent: 2
|
|
189
|
+
})
|
|
190
|
+
});
|
|
191
|
+
const getDbclientRetryPolicy = retry(handleWhen((err) => err.message.includes("timeout exceeded")), {
|
|
192
|
+
maxAttempts: 3,
|
|
193
|
+
backoff: new ExponentialBackoff({
|
|
194
|
+
initialDelay: 500,
|
|
195
|
+
maxDelay: 2e3,
|
|
196
|
+
exponent: 2
|
|
197
|
+
})
|
|
198
|
+
});
|
|
199
|
+
var SupaliveDb = class SupaliveDb {
|
|
200
|
+
impl;
|
|
201
|
+
replica;
|
|
202
|
+
/**
|
|
203
|
+
* @param impl the PRIMARY (writer) connection — all mutations, the OCC
|
|
204
|
+
* commit-ts source, and cache-freshness commit-log scans run here.
|
|
205
|
+
* @param replica optional read replica for query/action reads (see the
|
|
206
|
+
* routing in one-shot-query.ts). Undefined → reads use primary.
|
|
207
|
+
*/
|
|
208
|
+
constructor(impl, replica) {
|
|
209
|
+
this.impl = impl;
|
|
210
|
+
this.replica = replica;
|
|
211
|
+
}
|
|
212
|
+
static create(config) {
|
|
213
|
+
return new SupaliveDb(config.db, config.replica ? new SupaliveDb(config.replica) : void 0);
|
|
214
|
+
}
|
|
215
|
+
/** True when a distinct read replica is configured. */
|
|
216
|
+
get hasReplica() {
|
|
217
|
+
return this.replica !== void 0;
|
|
218
|
+
}
|
|
219
|
+
onAfterCommit;
|
|
220
|
+
/**
|
|
221
|
+
* Register a hook fired once after every successful `mutation()` commit
|
|
222
|
+
* (never on an OCC conflict/rollback), with the committed writeSet and its
|
|
223
|
+
* commitTs. The server uses this to drive subscription invalidation for ALL
|
|
224
|
+
* mutation sources — client WS calls, actions, jobs, and the internal caller —
|
|
225
|
+
* so a server-side write pushes live updates just like a client write does.
|
|
226
|
+
* Non-server consumers (migration tooling, tests) leave it unset (no-op).
|
|
227
|
+
*/
|
|
228
|
+
setOnAfterCommit(cb) {
|
|
229
|
+
this.onAfterCommit = cb;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* One-shot read against an EXPLICIT connection + snapshot ts (rather than this
|
|
233
|
+
* db's own latest). Used by the replica router: the caller pre-resolves the
|
|
234
|
+
* default read connection (replica for queries) and hands the handler a
|
|
235
|
+
* `DbReader` it can rebind via `useConnection` (ctx.usePrimaryConn/…). The
|
|
236
|
+
* returned `ts` is the FINAL bound connection's snapshot, so the cache entry is
|
|
237
|
+
* stamped with the ts actually read at.
|
|
238
|
+
*/
|
|
239
|
+
async queryInternalOn(read, fn) {
|
|
240
|
+
const ctx = new DbReader(read.impl, read.beginTs);
|
|
241
|
+
const data = await fn(ctx);
|
|
242
|
+
return {
|
|
243
|
+
ts: ctx.beginTs,
|
|
244
|
+
data,
|
|
245
|
+
readSet: ctx.internalGetReadSet()
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
async query(fn) {
|
|
249
|
+
const beginTs = await this.impl.getLatestSnapshotTimestamp();
|
|
250
|
+
return await fn(new DbReader(this.impl, beginTs));
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Compute a query once at the latest snapshot, returning the result together
|
|
254
|
+
* with the snapshot `ts` and the tracked `readSet`. This is a plain one-shot
|
|
255
|
+
* evaluation — it registers NO live subscription; the `readSet`/`ts` are
|
|
256
|
+
* returned purely so the caller (e.g. the one-shot query cache) can store
|
|
257
|
+
* freshness metadata. Use this for cached one-shot query calls (WS `call`,
|
|
258
|
+
* HTTP RPC). `liveQuery` is the same computation under the subscription path's
|
|
259
|
+
* name and simply delegates here.
|
|
260
|
+
*/
|
|
261
|
+
async queryInternal(fn) {
|
|
262
|
+
const beginTs = await this.impl.getLatestSnapshotTimestamp();
|
|
263
|
+
const ctx = new DbReader(this.impl, beginTs);
|
|
264
|
+
return {
|
|
265
|
+
ts: beginTs,
|
|
266
|
+
data: await fn(ctx),
|
|
267
|
+
readSet: ctx.internalGetReadSet()
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
async liveQuery(fn) {
|
|
271
|
+
return this.queryInternal(fn);
|
|
272
|
+
}
|
|
273
|
+
async mutation(fn, retryPolicy = occRetryPolicy) {
|
|
274
|
+
return await retryPolicy.execute(async (p) => {
|
|
275
|
+
let db;
|
|
276
|
+
try {
|
|
277
|
+
const beginTs = await this.impl.getLatestSnapshotTimestamp() + 1n;
|
|
278
|
+
const ctx = new DbWriter(this.impl, beginTs);
|
|
279
|
+
const result = await fn(ctx);
|
|
280
|
+
const readSet = ctx.internalGetReadSet();
|
|
281
|
+
const writeSet = ctx.internalGetWriteSet();
|
|
282
|
+
const commitTsLazy = new LazyCommitTsParam(void 0);
|
|
283
|
+
await this.impl.attachPrevDataToWriteSet(writeSet, readSet);
|
|
284
|
+
const queries = await this.impl.prepareCommitWrites(readSet, writeSet, commitTsLazy);
|
|
285
|
+
db = await getDbclientRetryPolicy.execute(() => this.impl.getTransactionClient());
|
|
286
|
+
await db.begin();
|
|
287
|
+
const commitTs = await db.getNextTimestamp();
|
|
288
|
+
commitTsLazy.value = commitTs;
|
|
289
|
+
const commitResult = await db.commitWrites(beginTs, queries, commitTs);
|
|
290
|
+
if (!commitResult.success) throw new OccConflictError("Commit failed", { commitResult });
|
|
291
|
+
await db.updateLatestSnapshotTimestamp(commitTs);
|
|
292
|
+
await db.commit();
|
|
293
|
+
this.onAfterCommit?.(writeSet, commitTs);
|
|
294
|
+
return {
|
|
295
|
+
result,
|
|
296
|
+
writeSet,
|
|
297
|
+
commitTs
|
|
298
|
+
};
|
|
299
|
+
} catch (err) {
|
|
300
|
+
await db?.rollback();
|
|
301
|
+
throw err;
|
|
302
|
+
} finally {
|
|
303
|
+
db?.release();
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
const sleep = (ms) => new Promise((res) => setTimeout(res, ms));
|
|
309
|
+
//#endregion
|
|
310
|
+
export { TxContext as a, DbWriter as i, sleep as n, DbReader as r, SupaliveDb as t };
|
|
311
|
+
|
|
312
|
+
//# sourceMappingURL=realtime_db-CVze3gM0.js.map
|