@prisma-next/sql-runtime 0.5.0-dev.6 → 0.5.0-dev.60

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 (46) hide show
  1. package/README.md +31 -22
  2. package/dist/exports-BcX9wp4z.mjs +1640 -0
  3. package/dist/exports-BcX9wp4z.mjs.map +1 -0
  4. package/dist/{index-yb51L_1h.d.mts → index-DkthtnOX.d.mts} +100 -25
  5. package/dist/index-DkthtnOX.d.mts.map +1 -0
  6. package/dist/index.d.mts +2 -2
  7. package/dist/index.mjs +2 -2
  8. package/dist/test/utils.d.mts +6 -5
  9. package/dist/test/utils.d.mts.map +1 -1
  10. package/dist/test/utils.mjs +13 -6
  11. package/dist/test/utils.mjs.map +1 -1
  12. package/package.json +13 -14
  13. package/src/codecs/decoding.ts +294 -173
  14. package/src/codecs/encoding.ts +162 -37
  15. package/src/codecs/validation.ts +22 -3
  16. package/src/content-hash.ts +44 -0
  17. package/src/exports/index.ts +12 -7
  18. package/src/fingerprint.ts +22 -0
  19. package/src/guardrails/raw.ts +165 -0
  20. package/src/lower-sql-plan.ts +3 -3
  21. package/src/marker.ts +75 -0
  22. package/src/middleware/before-compile-chain.ts +1 -0
  23. package/src/middleware/budgets.ts +26 -96
  24. package/src/middleware/lints.ts +3 -3
  25. package/src/middleware/sql-middleware.ts +6 -5
  26. package/src/runtime-spi.ts +44 -0
  27. package/src/sql-context.ts +438 -79
  28. package/src/sql-family-adapter.ts +3 -2
  29. package/src/sql-marker.ts +62 -47
  30. package/src/sql-runtime.ts +336 -113
  31. package/dist/exports-BQZSVXXt.mjs +0 -981
  32. package/dist/exports-BQZSVXXt.mjs.map +0 -1
  33. package/dist/index-yb51L_1h.d.mts.map +0 -1
  34. package/test/async-iterable-result.test.ts +0 -141
  35. package/test/before-compile-chain.test.ts +0 -223
  36. package/test/budgets.test.ts +0 -431
  37. package/test/context.types.test-d.ts +0 -68
  38. package/test/execution-stack.test.ts +0 -161
  39. package/test/json-schema-validation.test.ts +0 -571
  40. package/test/lints.test.ts +0 -160
  41. package/test/mutation-default-generators.test.ts +0 -254
  42. package/test/parameterized-types.test.ts +0 -529
  43. package/test/sql-context.test.ts +0 -384
  44. package/test/sql-family-adapter.test.ts +0 -103
  45. package/test/sql-runtime.test.ts +0 -792
  46. package/test/utils.ts +0 -297
package/src/sql-marker.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { MarkerStatement } from '@prisma-next/runtime-executor';
1
+ import type { MarkerStatement } from '@prisma-next/sql-relational-core/ast';
2
2
 
3
3
  export interface SqlStatement {
4
4
  readonly sql: string;
@@ -12,6 +12,15 @@ export interface WriteMarkerInput {
12
12
  readonly canonicalVersion?: number;
13
13
  readonly appTag?: string;
14
14
  readonly meta?: Record<string, unknown>;
15
+ /**
16
+ * Applied-invariants set on the marker.
17
+ *
18
+ * - `undefined` → existing column left untouched. Sign and
19
+ * verify-database paths use this; they don't accumulate invariants.
20
+ * - explicit value (including `[]`) → column overwritten with
21
+ * exactly that value.
22
+ */
23
+ readonly invariants?: readonly string[];
15
24
  }
16
25
 
17
26
  export const ensureSchemaStatement: SqlStatement = {
@@ -28,7 +37,8 @@ export const ensureTableStatement: SqlStatement = {
28
37
  canonical_version int,
29
38
  updated_at timestamptz not null default now(),
30
39
  app_tag text,
31
- meta jsonb not null default '{}'
40
+ meta jsonb not null default '{}',
41
+ invariants text[] not null default '{}'
32
42
  )`,
33
43
  params: [],
34
44
  };
@@ -42,7 +52,8 @@ export function readContractMarker(): MarkerStatement {
42
52
  canonical_version,
43
53
  updated_at,
44
54
  app_tag,
45
- meta
55
+ meta,
56
+ invariants
46
57
  from prisma_contract.marker
47
58
  where id = $1`,
48
59
  params: [1],
@@ -54,52 +65,56 @@ export interface WriteContractMarkerStatements {
54
65
  readonly update: SqlStatement;
55
66
  }
56
67
 
57
- export function writeContractMarker(input: WriteMarkerInput): WriteContractMarkerStatements {
58
- const baseParams: readonly unknown[] = [
59
- 1,
60
- input.storageHash,
61
- input.profileHash,
62
- input.contractJson ?? null,
63
- input.canonicalVersion ?? null,
64
- input.appTag ?? null,
65
- JSON.stringify(input.meta ?? {}),
68
+ /**
69
+ * Variable columns that participate in INSERT/UPDATE alongside the
70
+ * always-on `id = $1` and `updated_at = now()`. Each column declares
71
+ * its name, optional cast type, and parameter value; the placeholder
72
+ * (`$N`) is computed positionally below — adding or reordering a
73
+ * column doesn't desync indices. `invariants` only appears when the
74
+ * caller supplies it — see `WriteMarkerInput.invariants`.
75
+ */
76
+ function markerColumns(
77
+ input: WriteMarkerInput,
78
+ ): ReadonlyArray<{ readonly name: string; readonly type?: string; readonly param: unknown }> {
79
+ return [
80
+ { name: 'core_hash', param: input.storageHash },
81
+ { name: 'profile_hash', param: input.profileHash },
82
+ { name: 'contract_json', type: 'jsonb', param: input.contractJson ?? null },
83
+ { name: 'canonical_version', param: input.canonicalVersion ?? null },
84
+ { name: 'app_tag', param: input.appTag ?? null },
85
+ { name: 'meta', type: 'jsonb', param: JSON.stringify(input.meta ?? {}) },
86
+ ...(input.invariants !== undefined
87
+ ? [{ name: 'invariants' as const, type: 'text[]' as const, param: input.invariants }]
88
+ : []),
66
89
  ];
90
+ }
67
91
 
68
- const insert: SqlStatement = {
69
- sql: `insert into prisma_contract.marker (
70
- id,
71
- core_hash,
72
- profile_hash,
73
- contract_json,
74
- canonical_version,
75
- updated_at,
76
- app_tag,
77
- meta
78
- ) values (
79
- $1,
80
- $2,
81
- $3,
82
- $4::jsonb,
83
- $5,
84
- now(),
85
- $6,
86
- $7::jsonb
87
- )`,
88
- params: baseParams,
89
- };
92
+ export function writeContractMarker(input: WriteMarkerInput): WriteContractMarkerStatements {
93
+ const cols = markerColumns(input);
94
+ // $1 is reserved for `id`; subsequent positions follow the order of cols.
95
+ const placed = cols.map((c, i) => ({
96
+ name: c.name,
97
+ expr: c.type ? `$${i + 2}::${c.type}` : `$${i + 2}`,
98
+ param: c.param,
99
+ }));
100
+ const params: readonly unknown[] = [1, ...placed.map((c) => c.param)];
90
101
 
91
- const update: SqlStatement = {
92
- sql: `update prisma_contract.marker set
93
- core_hash = $2,
94
- profile_hash = $3,
95
- contract_json = $4::jsonb,
96
- canonical_version = $5,
97
- updated_at = now(),
98
- app_tag = $6,
99
- meta = $7::jsonb
100
- where id = $1`,
101
- params: baseParams,
102
- };
102
+ // `updated_at = now()` is a SQL literal with no parameter slot, so it
103
+ // sits outside `placed` and is appended directly to each statement.
104
+ const insertColumns = ['id', ...placed.map((c) => c.name), 'updated_at'].join(', ');
105
+ const insertValues = ['$1', ...placed.map((c) => c.expr), 'now()'].join(', ');
106
+ const setClauses = [...placed.map((c) => `${c.name} = ${c.expr}`), 'updated_at = now()'].join(
107
+ ', ',
108
+ );
103
109
 
104
- return { insert, update };
110
+ return {
111
+ insert: {
112
+ sql: `insert into prisma_contract.marker (${insertColumns}) values (${insertValues})`,
113
+ params,
114
+ },
115
+ update: {
116
+ sql: `update prisma_contract.marker set ${setClauses} where id = $1`,
117
+ params,
118
+ },
119
+ };
105
120
  }