@niscorp/vex 0.3.1 → 0.4.1

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 (50) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/DESIGN.md +11 -7
  3. package/DOCS.md +52 -9
  4. package/dist/adapters/express/index.cjs +1 -1
  5. package/dist/adapters/express/index.cjs.map +1 -1
  6. package/dist/adapters/express/index.d.cts +3 -2
  7. package/dist/adapters/express/index.d.ts +3 -2
  8. package/dist/adapters/express/index.js +3 -2
  9. package/dist/adapters/express/index.js.map +1 -1
  10. package/dist/adapters/hono/index.cjs +1 -1
  11. package/dist/adapters/hono/index.cjs.map +1 -1
  12. package/dist/adapters/hono/index.d.cts +3 -2
  13. package/dist/adapters/hono/index.d.ts +3 -2
  14. package/dist/adapters/hono/index.js +3 -2
  15. package/dist/adapters/hono/index.js.map +1 -1
  16. package/dist/agent/index.cjs +144 -21
  17. package/dist/agent/index.cjs.map +1 -1
  18. package/dist/agent/index.d.cts +3 -2
  19. package/dist/agent/index.d.ts +3 -2
  20. package/dist/agent/index.js +146 -22
  21. package/dist/agent/index.js.map +1 -1
  22. package/dist/{chunk-3HORTMZ7.js → chunk-AKR6HU34.js} +9 -5
  23. package/dist/chunk-AKR6HU34.js.map +1 -0
  24. package/dist/chunk-F7RDSWYF.js +7 -0
  25. package/dist/chunk-F7RDSWYF.js.map +1 -0
  26. package/dist/{chunk-J7JYKCUA.js → chunk-UPQMKAAX.js} +3 -18
  27. package/dist/chunk-UPQMKAAX.js.map +1 -0
  28. package/dist/chunk-XTOK7YOR.js +18 -0
  29. package/dist/chunk-XTOK7YOR.js.map +1 -0
  30. package/dist/{handler-B4NAi4k_.d.cts → handler-CoJxInw-.d.ts} +2 -1
  31. package/dist/{handler-BRVhWuNk.d.ts → handler-CsFbanZA.d.cts} +2 -1
  32. package/dist/index.cjs +26 -13
  33. package/dist/index.cjs.map +1 -1
  34. package/dist/index.d.cts +6 -4
  35. package/dist/index.d.ts +6 -4
  36. package/dist/index.js +30 -20
  37. package/dist/index.js.map +1 -1
  38. package/dist/migrations/index.cjs +173 -0
  39. package/dist/migrations/index.cjs.map +1 -0
  40. package/dist/migrations/index.d.cts +20 -0
  41. package/dist/migrations/index.d.ts +20 -0
  42. package/dist/migrations/index.js +23 -0
  43. package/dist/migrations/index.js.map +1 -0
  44. package/dist/query.schema-B-LVNfyX.d.cts +461 -0
  45. package/dist/query.schema-B-LVNfyX.d.ts +461 -0
  46. package/dist/{types-DUJp5B3o.d.cts → types-B2HEmyNq.d.cts} +11 -463
  47. package/dist/{types-DUJp5B3o.d.ts → types-DC68ZUuq.d.ts} +11 -463
  48. package/package.json +12 -1
  49. package/dist/chunk-3HORTMZ7.js.map +0 -1
  50. package/dist/chunk-J7JYKCUA.js.map +0 -1
@@ -1,4 +1,5 @@
1
- import { al as ScopeValues, S as ScopePolicy, D as DatabaseSchema, f as Row, M as MutationDefinition, N as Mutation, b as Query, u as CoreMutation, a as QueryEngine, a0 as QueryResponse } from './types-DUJp5B3o.cjs';
1
+ import { aa as ScopeValues, S as ScopePolicy, D as DatabaseSchema, e as Row, M as MutationDefinition, u as Mutation, m as CoreMutation, a as QueryEngine, T as QueryResponse } from './types-DC68ZUuq.js';
2
+ import { Q as Query } from './query.schema-B-LVNfyX.js';
2
3
 
3
4
  type MutationTx = {
4
5
  query: (sql: string, params?: unknown[]) => Promise<{
@@ -1,4 +1,5 @@
1
- import { al as ScopeValues, S as ScopePolicy, D as DatabaseSchema, f as Row, M as MutationDefinition, N as Mutation, b as Query, u as CoreMutation, a as QueryEngine, a0 as QueryResponse } from './types-DUJp5B3o.js';
1
+ import { aa as ScopeValues, S as ScopePolicy, D as DatabaseSchema, e as Row, M as MutationDefinition, u as Mutation, m as CoreMutation, a as QueryEngine, T as QueryResponse } from './types-B2HEmyNq.cjs';
2
+ import { Q as Query } from './query.schema-B-LVNfyX.cjs';
2
3
 
3
4
  type MutationTx = {
4
5
  query: (sql: string, params?: unknown[]) => Promise<{
package/dist/index.cjs CHANGED
@@ -1446,6 +1446,7 @@ var canonicalHash = (value) => hashText(canonicalText(value));
1446
1446
 
1447
1447
  // src/cache/hash.ts
1448
1448
  var isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
1449
+ var sortedDeep = (value) => Array.isArray(value) ? value.map(sortedDeep) : isRecord(value) ? Object.fromEntries(Object.keys(value).sort().map((key) => [key, sortedDeep(value[key])])) : value;
1449
1450
  var normalizeShape = (shape) => {
1450
1451
  if (shape === null) return "null";
1451
1452
  if (shape === void 0) return "unknown";
@@ -1454,7 +1455,9 @@ var normalizeShape = (shape) => {
1454
1455
  return first !== void 0 ? [normalizeShape(first)] : [];
1455
1456
  }
1456
1457
  if (isRecord(shape)) {
1457
- const sortedKeys = Object.keys(shape).sort();
1458
+ const keys = Object.keys(shape);
1459
+ if (keys.length === 1 && keys[0]?.startsWith("$") === true) return sortedDeep(shape);
1460
+ const sortedKeys = keys.sort();
1458
1461
  const normalized = {};
1459
1462
  for (const key of sortedKeys) {
1460
1463
  normalized[key] = normalizeShape(shape[key]);
@@ -1880,12 +1883,12 @@ var createQueryEngine = (engineConfig) => {
1880
1883
  const now = Date.now();
1881
1884
  if (isTouchDue(entry, now)) fireAndForget(cache.set(key, { ...entry, lastUsedAt: now }));
1882
1885
  };
1883
- const resolveFingerprint = async (validRequest, hasRequest, requestHash) => {
1886
+ const resolveFingerprint = async (validRequest, hasRequest, requestHash, scope) => {
1884
1887
  const fp = validRequest.fingerprint;
1885
1888
  if (fp === void 0) {
1886
1889
  return { fingerprint: mintFingerprint(), hit: false, replaced: false };
1887
1890
  }
1888
- const entry = await cache.get(fp);
1891
+ const entry = await cache.get(fp, scope);
1889
1892
  if (entry?.kind === "mutation") {
1890
1893
  throw new VexError(
1891
1894
  "invalid_request",
@@ -1989,6 +1992,7 @@ var createQueryEngine = (engineConfig) => {
1989
1992
  let result = rows;
1990
1993
  let mappingMs;
1991
1994
  let mappedIr = cachedIr;
1995
+ let mappedConfig;
1992
1996
  if (validRequest.shape !== void 0) {
1993
1997
  if (cachedIr !== void 0) {
1994
1998
  const mapStart = Date.now();
@@ -1996,14 +2000,15 @@ var createQueryEngine = (engineConfig) => {
1996
2000
  mappingMs = Date.now() - mapStart;
1997
2001
  } else if (mapToShape !== void 0) {
1998
2002
  const mapStart = Date.now();
1999
- const mapped = await mapToShape(rows, validRequest.shape);
2003
+ const mapped = await mapToShape(rows, validRequest.shape, validRequest.intent);
2000
2004
  mappingMs = Date.now() - mapStart;
2001
2005
  result = mapped.transformed;
2002
2006
  mappedIr = mapped.ir;
2007
+ mappedConfig = mapped.config;
2003
2008
  }
2004
2009
  if (mappingMs !== void 0) emit({ type: "query.mapped", mappingMs });
2005
2010
  }
2006
- return { result, rowCount: rows.length, executionMs, mappingMs, mappedIr };
2011
+ return { result, rowCount: rows.length, executionMs, mappingMs, mappedIr, ...mappedConfig !== void 0 ? { mappedConfig } : {} };
2007
2012
  };
2008
2013
  const execute = async (request, options) => {
2009
2014
  if (strata.exceedsDepth(request)) throw new VexError("invalid_request", `Invalid request: ${strata.depthRefusal()}`);
@@ -2029,7 +2034,7 @@ var createQueryEngine = (engineConfig) => {
2029
2034
  fingerprint: validRequest.fingerprint,
2030
2035
  entities: options?.entities
2031
2036
  });
2032
- const cached = await resolveFingerprint(validRequest, hasRequest, requestHash);
2037
+ const cached = await resolveFingerprint(validRequest, hasRequest, requestHash, scope);
2033
2038
  emit({ type: "query.cache", hit: cached.hit, fingerprint: cached.fingerprint, replaced: cached.replaced });
2034
2039
  let dsl = cached.dsl;
2035
2040
  let agentMs;
@@ -2116,7 +2121,7 @@ var createQueryEngine = (engineConfig) => {
2116
2121
  emit({ type: "query.done", totalMs: Date.now() - t0 });
2117
2122
  return { result: result2, meta: { ...metaOf(rows.length), timing: { executionMs: executionMs2 } } };
2118
2123
  }
2119
- const { result, rowCount, executionMs, mappingMs, mappedIr } = await executeAndMap(
2124
+ const { result, rowCount, executionMs, mappingMs, mappedIr, mappedConfig } = await executeAndMap(
2120
2125
  compiled,
2121
2126
  effectiveRequest,
2122
2127
  cached.cachedIr,
@@ -2131,6 +2136,7 @@ var createQueryEngine = (engineConfig) => {
2131
2136
  kind: "ok",
2132
2137
  dsl,
2133
2138
  ...mappedIr !== void 0 ? { prismIr: mappedIr } : {},
2139
+ ...mappedConfig !== void 0 ? { mapping: mappedConfig } : {},
2134
2140
  createdAt: now,
2135
2141
  lastUsedAt: now,
2136
2142
  ...requestHash !== void 0 ? { requestHash } : {},
@@ -3017,7 +3023,7 @@ var quoteIdent2 = (name, label) => {
3017
3023
  }
3018
3024
  return `"${name}"`;
3019
3025
  };
3020
- var SELECT_COLS = "key, kind, intent, shape, dsl, prism_ir, reach, reason, created_at, expires_at, schema_fingerprint, protected, last_used_at, request_hash, refresh";
3026
+ var SELECT_COLS = "key, kind, intent, shape, dsl, prism_ir, mapping, reach, reason, created_at, expires_at, schema_fingerprint, protected, last_used_at, request_hash, refresh";
3021
3027
  var rowToEntry = (row) => {
3022
3028
  const createdAt = row["created_at"].getTime();
3023
3029
  const expiresAtRaw = row["expires_at"];
@@ -3048,6 +3054,7 @@ var rowToEntry = (row) => {
3048
3054
  kind: "ok",
3049
3055
  dsl: row["dsl"],
3050
3056
  ...row["prism_ir"] != null ? { prismIr: row["prism_ir"] } : {},
3057
+ ...row["mapping"] != null ? { mapping: row["mapping"] } : {},
3051
3058
  ...row["reach"] != null ? { reach: row["reach"] } : {},
3052
3059
  ...isRefresh(row["refresh"]) ? { refresh: row["refresh"] } : {},
3053
3060
  ...meta
@@ -3093,6 +3100,10 @@ var createPostgresCache = (config) => {
3093
3100
  { kind: "sql", sql: `ALTER TABLE ${qualified} ADD COLUMN IF NOT EXISTS reach text` },
3094
3101
  { kind: "sql", sql: `ALTER TABLE ${qualified} ADD COLUMN IF NOT EXISTS refresh text` }
3095
3102
  ]
3103
+ },
3104
+ {
3105
+ description: "The prism config a mapping was compiled from",
3106
+ steps: [{ kind: "sql", sql: `ALTER TABLE ${qualified} ADD COLUMN IF NOT EXISTS mapping jsonb` }]
3096
3107
  }
3097
3108
  ]
3098
3109
  };
@@ -3136,6 +3147,7 @@ var createPostgresCache = (config) => {
3136
3147
  const isOk = entry.kind === "ok";
3137
3148
  const dsl = isOk ? JSON.stringify(entry.dsl) : entry.kind === "mutation" ? JSON.stringify(entry.mutation) : null;
3138
3149
  const prismIr = isOk && entry.prismIr !== void 0 ? JSON.stringify(entry.prismIr) : null;
3150
+ const mapping = isOk && entry.mapping !== void 0 ? JSON.stringify(entry.mapping) : null;
3139
3151
  const reason = entry.kind === "unsatisfiable" ? entry.reason : null;
3140
3152
  const intent = entry.intent ?? null;
3141
3153
  const shape = entry.shape !== void 0 ? JSON.stringify(entry.shape) : null;
@@ -3148,8 +3160,8 @@ var createPostgresCache = (config) => {
3148
3160
  const reach = entry.kind === "unsatisfiable" ? null : entry.reach ?? null;
3149
3161
  const refresh = entry.kind === "ok" ? entry.refresh ?? null : null;
3150
3162
  await pool.query(
3151
- `INSERT INTO ${qualified} (key, kind, intent, shape, dsl, prism_ir, reach, reason, created_at, expires_at, schema_fingerprint, protected, last_used_at, request_hash, refresh)
3152
- VALUES ($1, $2, $3, $4::jsonb, $5::jsonb, $6::jsonb, $7, $8, $9, $10, $11, $12, $13, $14, $15)
3163
+ `INSERT INTO ${qualified} (key, kind, intent, shape, dsl, prism_ir, reach, reason, created_at, expires_at, schema_fingerprint, protected, last_used_at, request_hash, refresh, mapping)
3164
+ VALUES ($1, $2, $3, $4::jsonb, $5::jsonb, $6::jsonb, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16::jsonb)
3153
3165
  ON CONFLICT (key) DO UPDATE SET
3154
3166
  kind = EXCLUDED.kind,
3155
3167
  intent = EXCLUDED.intent,
@@ -3164,8 +3176,9 @@ var createPostgresCache = (config) => {
3164
3176
  protected = EXCLUDED.protected,
3165
3177
  last_used_at = EXCLUDED.last_used_at,
3166
3178
  request_hash = EXCLUDED.request_hash,
3167
- refresh = EXCLUDED.refresh`,
3168
- [key, entry.kind, intent, shape, dsl, prismIr, reach, reason, createdAt, expiresAt, fingerprint, isProtected, lastUsedAt, requestHash, refresh]
3179
+ refresh = EXCLUDED.refresh,
3180
+ mapping = EXCLUDED.mapping`,
3181
+ [key, entry.kind, intent, shape, dsl, prismIr, reach, reason, createdAt, expiresAt, fingerprint, isProtected, lastUsedAt, requestHash, refresh, mapping]
3169
3182
  );
3170
3183
  };
3171
3184
  const del = async (key) => {
@@ -4076,7 +4089,7 @@ var runQuery = async (config, body, scope, probe, live) => {
4076
4089
  try {
4077
4090
  let entryReach;
4078
4091
  if (parsed.data.fingerprint !== void 0) {
4079
- const entry = await engine.cache.get(parsed.data.fingerprint);
4092
+ const entry = await engine.cache.get(parsed.data.fingerprint, scope);
4080
4093
  if (entry !== void 0 && entry.kind === "ok") entryReach = entry.reach;
4081
4094
  if (probe !== void 0 && entryReach !== void 0) probe.reach = entryReach;
4082
4095
  if (entry !== void 0 && entry.kind === "mutation") {