@zeronsh/orbit 0.1.0 → 0.3.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.
@@ -268,11 +268,27 @@ function createSchema(def) {
268
268
  }
269
269
 
270
270
  // client/src/custom.ts
271
- function defineMutator(fn) {
272
- return fn;
271
+ function createOrbitApi(opts) {
272
+ return {
273
+ builder: createBuilder(opts.schema),
274
+ defineMutation: ((config) => config),
275
+ defineQuery: ((config) => config)
276
+ };
277
+ }
278
+ function defineMutation(config) {
279
+ return config;
273
280
  }
274
- function defineQuery(fn) {
275
- return fn;
281
+ function defineQuery(config) {
282
+ return config;
283
+ }
284
+ async function validateArgs(validator, args) {
285
+ if (!validator) return args;
286
+ let result = validator["~standard"].validate(args);
287
+ if (result instanceof Promise) result = await result;
288
+ if (result.issues) {
289
+ throw new Error(`invalid arguments: ${JSON.stringify(result.issues)}`);
290
+ }
291
+ return result.value;
276
292
  }
277
293
  function collectOps(schema, def, args, ctx) {
278
294
  const pk = {};
@@ -288,7 +304,7 @@ function collectOps(schema, def, args, ctx) {
288
304
  }
289
305
  );
290
306
  const tx = { location: "client", mutate };
291
- void def({ tx, args, ctx });
307
+ void def.handler({ tx, args, ctx });
292
308
  return ops;
293
309
  }
294
310
 
@@ -307,7 +323,11 @@ function clampTTL(ms) {
307
323
  return ms === Infinity ? Infinity : Math.min(ms, MAX_TTL_MS);
308
324
  }
309
325
  var defaultScheduler = {
310
- setTimeout: (fn, ms) => setTimeout(fn, ms),
326
+ setTimeout: (fn, ms) => {
327
+ const h = setTimeout(fn, ms);
328
+ h.unref?.();
329
+ return h;
330
+ },
311
331
  clearTimeout: (h) => clearTimeout(h)
312
332
  };
313
333
  var QueryManager = class {
@@ -621,8 +641,7 @@ function likeToRegExp(pattern, ci) {
621
641
  }
622
642
  function resolve(row, pos) {
623
643
  if (pos.type === "column") return row[pos.name];
624
- if (pos.type === "literal") return pos.value;
625
- return void 0;
644
+ return pos.value;
626
645
  }
627
646
  function evalSimple(row, op, left, right) {
628
647
  const l = resolve(row, left);
@@ -1611,6 +1630,9 @@ var Orbit = class {
1611
1630
  #pkByTable;
1612
1631
  #schema;
1613
1632
  #mutatorDefs;
1633
+ /** Context for optimistic mutators + local query derivation (a value or getter).
1634
+ * The server independently derives the authoritative ctx from the auth token. */
1635
+ #context;
1614
1636
  #nextMutationID = 1;
1615
1637
  #closed = false;
1616
1638
  #connecting = false;
@@ -1636,6 +1658,7 @@ var Orbit = class {
1636
1658
  constructor(opts) {
1637
1659
  this.#opts = opts;
1638
1660
  this.#schema = opts.schema;
1661
+ this.#context = opts.context;
1639
1662
  this.#maxReconnectMs = opts.maxReconnectMs ?? 3e4;
1640
1663
  this.#queryTTL = opts.queryTTL ?? "5m";
1641
1664
  this.#kv = opts.persist;
@@ -1660,7 +1683,7 @@ var Orbit = class {
1660
1683
  this.queries = queryDefs ? new Proxy({}, {
1661
1684
  get: (_t, name) => (args) => ({
1662
1685
  materialize: () => {
1663
- const ast = queryDefs[name]({ args, ctx: {} }).ast();
1686
+ const ast = queryDefs[name].handler({ args, ctx: this.#resolveContext() }).ast();
1664
1687
  const argList = args === void 0 ? [] : [args];
1665
1688
  return this.materializeNamed(name, argList, ast);
1666
1689
  }
@@ -1740,7 +1763,7 @@ var Orbit = class {
1740
1763
  let ops = [];
1741
1764
  if (def && this.#schema) {
1742
1765
  try {
1743
- ops = collectOps(this.#schema, def, args, {});
1766
+ ops = collectOps(this.#schema, def, args, this.#resolveContext());
1744
1767
  } catch {
1745
1768
  }
1746
1769
  }
@@ -1752,6 +1775,11 @@ var Orbit = class {
1752
1775
  this.#ws?.close();
1753
1776
  }
1754
1777
  // --- internals ----------------------------------------------------------
1778
+ /** Resolve the client context (a value or sync getter); `{}` when unset. */
1779
+ #resolveContext() {
1780
+ const c = this.#context;
1781
+ return typeof c === "function" ? c() : c ?? {};
1782
+ }
1755
1783
  /** Hydrate persisted state (if any), restore unconfirmed mutations, then connect. */
1756
1784
  async #init() {
1757
1785
  if (this.#kv) {
@@ -1946,6 +1974,6 @@ var IDBKV = class {
1946
1974
  }
1947
1975
  };
1948
1976
 
1949
- export { IDBKV, MaterializedView, MemoryKV, MemorySource, MemorySourceProvider, Orbit, PROTOCOL_VERSION, Query, QueryManager, SchemaQuery, SourceConnection, Store, StoreProvider, TypedQuery, View, boolean, buildPipeline, buildSchemaQueries, collectOps, compareValues, createBuilder, createSchema, defineMutator, defineQuery, evaluate, hashAST, hashString, json, nodeToRow, number, optional, parseTTL, relationships, string, table, tablesOf, unwrapSingular, valuesEqual };
1950
- //# sourceMappingURL=chunk-N2NAKHMU.js.map
1951
- //# sourceMappingURL=chunk-N2NAKHMU.js.map
1977
+ export { IDBKV, MaterializedView, MemoryKV, MemorySource, MemorySourceProvider, Orbit, PROTOCOL_VERSION, Query, QueryManager, SchemaQuery, SourceConnection, Store, StoreProvider, TypedQuery, View, boolean, buildPipeline, buildSchemaQueries, collectOps, compareValues, createBuilder, createOrbitApi, createSchema, defineMutation, defineQuery, evaluate, hashAST, hashString, json, nodeToRow, number, optional, parseTTL, relationships, string, table, tablesOf, unwrapSingular, validateArgs, valuesEqual };
1978
+ //# sourceMappingURL=chunk-EX4WSUC3.js.map
1979
+ //# sourceMappingURL=chunk-EX4WSUC3.js.map