@supalive/core 1.18.0 → 1.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.
Files changed (81) hide show
  1. package/dist/index-BuWa503L.d.ts +2445 -0
  2. package/dist/index-BuWa503L.d.ts.map +1 -0
  3. package/dist/index-CBIjmPUS.d.ts +2444 -0
  4. package/dist/index-CBIjmPUS.d.ts.map +1 -0
  5. package/dist/index-CKo6HvJc.d.ts +2455 -0
  6. package/dist/index-CKo6HvJc.d.ts.map +1 -0
  7. package/dist/index-DShKjpzF.d.ts +2453 -0
  8. package/dist/index-DShKjpzF.d.ts.map +1 -0
  9. package/dist/index-Dcxf_Gm-.d.ts +2454 -0
  10. package/dist/index-Dcxf_Gm-.d.ts.map +1 -0
  11. package/dist/logger-DrXccWFZ.js +346 -0
  12. package/dist/logger-DrXccWFZ.js.map +1 -0
  13. package/dist/mysql-B2u0Ye4x.d.ts +114 -0
  14. package/dist/mysql-B2u0Ye4x.d.ts.map +1 -0
  15. package/dist/mysql-BHW-tAqt.d.ts +114 -0
  16. package/dist/mysql-BHW-tAqt.d.ts.map +1 -0
  17. package/dist/mysql-C_TTCZ8j.d.ts +114 -0
  18. package/dist/mysql-C_TTCZ8j.d.ts.map +1 -0
  19. package/dist/mysql-CyzJqrvO.d.ts +114 -0
  20. package/dist/mysql-CyzJqrvO.d.ts.map +1 -0
  21. package/dist/mysql-ygH2EMGn.js +622 -0
  22. package/dist/mysql-ygH2EMGn.js.map +1 -0
  23. package/dist/mysql-z9Jefhd6.d.ts +114 -0
  24. package/dist/mysql-z9Jefhd6.d.ts.map +1 -0
  25. package/dist/one-shot-query-9mCboWaB.js +565 -0
  26. package/dist/one-shot-query-9mCboWaB.js.map +1 -0
  27. package/dist/one-shot-query-B9SjfcCD.js +550 -0
  28. package/dist/one-shot-query-B9SjfcCD.js.map +1 -0
  29. package/dist/one-shot-query-BBiv9LGP.js +565 -0
  30. package/dist/one-shot-query-BBiv9LGP.js.map +1 -0
  31. package/dist/one-shot-query-Bxz_N-NE.js +563 -0
  32. package/dist/one-shot-query-Bxz_N-NE.js.map +1 -0
  33. package/dist/one-shot-query-DE9oQU40.js +550 -0
  34. package/dist/one-shot-query-DE9oQU40.js.map +1 -0
  35. package/dist/one-shot-query-adoWqGLs.js +565 -0
  36. package/dist/one-shot-query-adoWqGLs.js.map +1 -0
  37. package/dist/overlap-checker-CCgq_Tpa.js +284 -0
  38. package/dist/overlap-checker-CCgq_Tpa.js.map +1 -0
  39. package/dist/postgres-B2FLN1Kw.d.ts +118 -0
  40. package/dist/postgres-B2FLN1Kw.d.ts.map +1 -0
  41. package/dist/postgres-C7xHWbij.d.ts +118 -0
  42. package/dist/postgres-C7xHWbij.d.ts.map +1 -0
  43. package/dist/postgres-DBMAv_dq.d.ts +118 -0
  44. package/dist/postgres-DBMAv_dq.d.ts.map +1 -0
  45. package/dist/postgres-DBuP9ON_.d.ts +118 -0
  46. package/dist/postgres-DBuP9ON_.d.ts.map +1 -0
  47. package/dist/postgres-DwiUT_A6.js +867 -0
  48. package/dist/postgres-DwiUT_A6.js.map +1 -0
  49. package/dist/postgres-I4sgdBnG.d.ts +118 -0
  50. package/dist/postgres-I4sgdBnG.d.ts.map +1 -0
  51. package/dist/router-DP2ThAwh.js.map +1 -1
  52. package/dist/src/client/index.d.ts +1 -1
  53. package/dist/src/client/index.js +45 -50
  54. package/dist/src/client/index.js.map +1 -1
  55. package/dist/src/exports/mysql.d.ts +1 -1
  56. package/dist/src/exports/mysql.js +1 -1
  57. package/dist/src/exports/postgres.d.ts +1 -1
  58. package/dist/src/exports/postgres.js +1 -1
  59. package/dist/src/exports/procedure.d.ts +1 -1
  60. package/dist/src/exports/schema-sql.d.ts +1 -1
  61. package/dist/src/exports/server.d.ts +11 -4
  62. package/dist/src/exports/server.d.ts.map +1 -1
  63. package/dist/src/exports/server.js +29 -410
  64. package/dist/src/exports/server.js.map +1 -1
  65. package/dist/src/exports/sub-manager-worker-entry.js +1 -1
  66. package/dist/src/exports/types.d.ts +2 -2
  67. package/dist/sub-worker-dispatch-BLujn13f.js +792 -0
  68. package/dist/sub-worker-dispatch-BLujn13f.js.map +1 -0
  69. package/dist/types_server-BCst4TfZ.d.ts +703 -0
  70. package/dist/types_server-BCst4TfZ.d.ts.map +1 -0
  71. package/dist/types_server-BYwLkwbC.d.ts +691 -0
  72. package/dist/types_server-BYwLkwbC.d.ts.map +1 -0
  73. package/dist/types_server-Bvrohyc8.d.ts +691 -0
  74. package/dist/types_server-Bvrohyc8.d.ts.map +1 -0
  75. package/dist/types_server-C6oCm9oC.d.ts +691 -0
  76. package/dist/types_server-C6oCm9oC.d.ts.map +1 -0
  77. package/dist/types_server-CHJLfdxb.d.ts +691 -0
  78. package/dist/types_server-CHJLfdxb.d.ts.map +1 -0
  79. package/dist/types_server-DnO_fp-F.d.ts +691 -0
  80. package/dist/types_server-DnO_fp-F.d.ts.map +1 -0
  81. package/package.json +3 -1
@@ -1,11 +1,11 @@
1
- import { c as parentConn, i as createActionBuilder, l as primaryOnlyConn, n as getContextRegistry, o as createMutationBuilder, r as router, s as createQueryBuilder } from "../../router-DP2ThAwh.js";
1
+ import { i as createActionBuilder, l as primaryOnlyConn, n as getContextRegistry, o as createMutationBuilder, r as router, s as createQueryBuilder } from "../../router-DP2ThAwh.js";
2
2
  import { r as supaliveStringify } from "../../helper-zdJT5FUc.js";
3
3
  import { o as matchesPredicate } from "../../query-LJranz0c.js";
4
4
  import { n as defineSchema, t as defineComputedField } from "../../schema-DRtz5h5l.js";
5
5
  import { C as normalizeIdToBytes, S as bytesFromJson, _ as RawRangeReadSchema, a as DEFAULT_RETRY, b as WriteEntrySchema, c as OccAbortError, d as PointReadSchema, f as PredicateSchema, g as RawPointReadSchema, h as RangeReadSchema, i as CompareOperatorSchema, l as OccConflictError, m as QueryCacheMetadataSchema, n as BigIntSchema, o as LeafPredicateSchema, p as QueryCacheEntrySchema, r as CachedPgMetadataSchema, s as NO_RETRY, t as AndPredicateSchema, u as OrPredicateSchema, v as RawReadEntrySchema, w as normalizeToBytes, x as WriteOpSchema, y as ReadEntrySchema } from "../../types_db-OUou3o2Z.js";
6
6
  import { l as ServerMessageSchema } from "../../types_client_rpc-B8jh6MKl.js";
7
- import { a as TxContext, i as DbWriter, n as sleep, r as DbReader, t as SupaliveDb } from "../../realtime_db-B0Tin87E.js";
8
- import { n as resolveReadRouting, t as makeConnRouting } from "../../read-routing-BUcQAwnd.js";
7
+ import { c as DbReader, l as DbWriter, o as SupaliveDb, s as sleep, u as TxContext } from "../../logger-DrXccWFZ.js";
8
+ import { n as runCachedQueryInParent, t as runCachedQuery } from "../../one-shot-query-BBiv9LGP.js";
9
9
  import { sha1 } from "hash-wasm";
10
10
  //#region src/db/query_definition.ts
11
11
  const _globalDefs = /* @__PURE__ */ new Map();
@@ -1157,56 +1157,45 @@ var LiveQueryHandleImpl = class {
1157
1157
  //#endregion
1158
1158
  //#region src/client/caller_client.ts
1159
1159
  /**
1160
- * Run a standalone (non-transaction-joining) server-side query with replica
1161
- * routing that DEFAULTS to the primary.
1162
- *
1163
- * Unlike a client one-shot query, the internal caller has no client `minTs`
1164
- * high-water to gate replica staleness — a caller that just performed an
1165
- * internal write and then reads back would silently read a lagging replica. So
1166
- * the safe default here is the PRIMARY; a handler can still opt a heavy
1167
- * reporting read onto the replica with `ctx.useReplicaConn()` (and force back
1168
- * with `ctx.usePrimaryConn()` / `{ readOwnWrite: true }`). The connection knobs
1169
- * use the SAME shared routing rule as the one-shot path ({@link resolveReadRouting}
1170
- * / {@link makeConnRouting}) — the only difference is the DEFAULT connection
1171
- * (primary here, replica-if-caught-up there). With no minTs floor, an explicit
1172
- * `useReplicaConn()` resolves to the replica whenever one is configured. With no
1173
- * replica configured every branch resolves to the primary.
1174
- */
1175
- async function runRoutedCallerQuery(db, serverCtx, fn, log) {
1176
- const routing = await resolveReadRouting(db, await db.impl.getLatestSnapshotTimestamp(), 0n);
1177
- return (await db.queryInternalOn({
1178
- impl: routing.primaryImpl,
1179
- beginTs: routing.primaryTs
1180
- }, async (dbCtx) => {
1181
- return fn({
1182
- db: dbCtx,
1183
- serverCtx,
1184
- log,
1185
- ...makeConnRouting(dbCtx, routing)
1186
- });
1187
- })).data;
1188
- }
1189
- /**
1190
1160
  * Create a server-side caller that bypasses HTTP/WebSocket.
1191
1161
  *
1192
1162
  * For in-memory calls, input validation is skipped (data is already a JS object).
1193
1163
  * Query procedures also get a liveQuery() method for server-side use.
1164
+ *
1165
+ * `init` requires the shared Redis-backed query cache, so every read path —
1166
+ * `query`, `runQuery`, and `liveQuery` — behaves like a normal client procedure
1167
+ * call: it checks cache freshness first and only falls back to executing the
1168
+ * query on a miss/stale entry (see runCachedQuery and runCachedQueryInParent).
1169
+ * `runQuery` keeps joining the parent snapshot/readSet (or opening a fresh
1170
+ * routed read for action/job parents), so nested reads stay consistent with the
1171
+ * parent and mutations keep reads-your-writes.
1194
1172
  */
1195
1173
  function createCaller(options) {
1196
1174
  const { isInternal = true, inContext } = options;
1197
1175
  const registry = getContextRegistry(inContext ?? "default");
1198
1176
  const cache = /* @__PURE__ */ new Map();
1177
+ let callerConfig;
1199
1178
  let db;
1200
1179
  let storage;
1201
1180
  let scheduler;
1202
1181
  let obs;
1182
+ let cacheLayer;
1183
+ let callerDepsInstance;
1203
1184
  return new Proxy({}, { get(_target, procedureName) {
1204
1185
  if (typeof procedureName !== "string") return;
1205
1186
  if (procedureName == "init") return async (config) => {
1187
+ callerConfig = config;
1206
1188
  db = config.db;
1207
1189
  storage = config.storage;
1208
1190
  scheduler = config.scheduler;
1209
1191
  obs = config.obs;
1192
+ cacheLayer = config.cache;
1193
+ callerDepsInstance = {
1194
+ db: config.db,
1195
+ cache: cacheLayer,
1196
+ getMinRetainedTs: config.getMinRetainedTs,
1197
+ getUserId: config.getUserId
1198
+ };
1210
1199
  };
1211
1200
  const callerLog = () => obs.lazyChildLogger({
1212
1201
  caller: "internal",
@@ -1225,21 +1214,32 @@ function createCaller(options) {
1225
1214
  query: async (input, serverCtx) => {
1226
1215
  if (procedure.type !== "query") throw new Error(`Procedure "${procedureName}" is not a query`);
1227
1216
  const validatedInput = validateInput(input);
1228
- return runRoutedCallerQuery(db, serverCtx, (ctx) => procedure.fn(ctx, validatedInput), callerLog());
1217
+ const lastSnapshotTs = await db.impl.getLatestSnapshotTimestamp();
1218
+ const { data } = await runCachedQuery(callerDepsInstance, { log: callerLog() }, procedure, procedureName, validatedInput, serverCtx, lastSnapshotTs);
1219
+ return data;
1229
1220
  },
1230
1221
  runQuery: async (input, parentCtx) => {
1231
1222
  if (procedure.type !== "query") throw new Error(`Procedure "${procedureName}" is not a query`);
1232
1223
  const validatedInput = validateInput(input);
1233
1224
  if (parentCtx.db instanceof TxContext) {
1234
- const ctx = {
1235
- db: parentCtx.db,
1236
- serverCtx: parentCtx.serverCtx,
1225
+ const parentReader = parentCtx.db;
1226
+ return runCachedQueryInParent(callerDepsInstance, {
1237
1227
  log: parentCtx.log,
1238
- ...parentConn
1239
- };
1240
- return procedure.fn(ctx, validatedInput);
1228
+ span: parentCtx.span
1229
+ }, procedure, procedureName, validatedInput, parentCtx.serverCtx, parentReader);
1241
1230
  }
1242
- return runRoutedCallerQuery(parentCtx.db, parentCtx.serverCtx, (ctx) => procedure.fn(ctx, validatedInput), parentCtx.log);
1231
+ const parentDb = parentCtx.db;
1232
+ const lastSnapshotTs = await parentDb.impl.getLatestSnapshotTimestamp();
1233
+ const { data } = await runCachedQuery({
1234
+ db: parentDb,
1235
+ cache: cacheLayer,
1236
+ getMinRetainedTs: callerConfig.getMinRetainedTs,
1237
+ getUserId: callerConfig.getUserId
1238
+ }, {
1239
+ log: parentCtx.log,
1240
+ span: parentCtx.span
1241
+ }, procedure, procedureName, validatedInput, parentCtx.serverCtx, lastSnapshotTs);
1242
+ return data;
1243
1243
  },
1244
1244
  mutate: async (input, serverCtx) => {
1245
1245
  if (procedure.type !== "mutation") throw new Error(`Procedure "${procedureName}" is not a mutation`);
@@ -1262,6 +1262,7 @@ function createCaller(options) {
1262
1262
  db: dbCtx,
1263
1263
  serverCtx: parentCtx.serverCtx,
1264
1264
  log: parentCtx.log,
1265
+ span: parentCtx.span,
1265
1266
  ...primaryOnlyConn
1266
1267
  };
1267
1268
  if (parentCtx.db instanceof TxContext) ctx.db.appendInternalReadSet(parentCtx.db.internalGetReadSet());
@@ -1292,15 +1293,9 @@ function createCaller(options) {
1292
1293
  },
1293
1294
  liveQuery: procedure.type === "query" ? async (input, serverCtx) => {
1294
1295
  const validatedInput = validateInput(input);
1295
- return db.liveQuery(async (dbCtx) => {
1296
- const ctx = {
1297
- db: dbCtx,
1298
- serverCtx,
1299
- log: callerLog(),
1300
- ...primaryOnlyConn
1301
- };
1302
- return procedure.fn(ctx, validatedInput);
1303
- });
1296
+ const obs = { log: callerLog() };
1297
+ const lastSnapshotTs = await db.impl.getLatestSnapshotTimestamp();
1298
+ return runCachedQuery(callerDepsInstance, obs, procedure, procedureName, validatedInput, serverCtx, lastSnapshotTs);
1304
1299
  } : void 0
1305
1300
  };
1306
1301
  cache.set(procedureName, newProp);