@pgxsinkit/contracts 0.1.32 → 0.1.40
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/README.md +13 -0
- package/dist/apply-strategy.d.ts +35 -0
- package/dist/apply-strategy.d.ts.map +1 -0
- package/dist/config.d.ts +210 -32
- package/dist/config.d.ts.map +1 -1
- package/dist/convergence-model.d.ts +106 -0
- package/dist/convergence-model.d.ts.map +1 -0
- package/dist/fingerprint.d.ts +136 -0
- package/dist/fingerprint.d.ts.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5222 -3494
- package/dist/index.js.map +37 -30
- package/dist/mutation.d.ts +78 -0
- package/dist/mutation.d.ts.map +1 -1
- package/dist/projection.d.ts +88 -0
- package/dist/projection.d.ts.map +1 -0
- package/dist/registry-diff.d.ts +50 -0
- package/dist/registry-diff.d.ts.map +1 -0
- package/dist/registry.d.ts +253 -2
- package/dist/registry.d.ts.map +1 -1
- package/dist/runtime.d.ts +21 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/sql-defaults.d.ts +27 -0
- package/dist/sql-defaults.d.ts.map +1 -0
- package/dist/sql-identifier.d.ts +26 -0
- package/dist/sql-identifier.d.ts.map +1 -0
- package/dist/supabase-rls.d.ts +99 -3
- package/dist/supabase-rls.d.ts.map +1 -1
- package/package.json +8 -3
- package/skills/registry-authoring/SKILL.md +376 -0
- package/src/apply-strategy.ts +124 -0
- package/src/config.ts +254 -106
- package/src/convergence-model.ts +190 -0
- package/src/fingerprint.ts +265 -0
- package/src/index.ts +7 -0
- package/src/mutation.ts +94 -2
- package/src/projection.ts +150 -0
- package/src/registry-diff.ts +215 -0
- package/src/registry.ts +751 -23
- package/src/runtime.ts +21 -1
- package/src/sql-defaults.ts +29 -0
- package/src/sql-identifier.ts +130 -0
- package/src/supabase-rls.ts +515 -37
package/src/runtime.ts
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
|
-
export type SyncRuntimePhase = "booting" | "syncing" | "ready" | "degraded";
|
|
1
|
+
export type SyncRuntimePhase = "booting" | "syncing" | "ready" | "degraded" | "auth-needed";
|
|
2
2
|
|
|
3
3
|
export interface SyncRuntimeStatus {
|
|
4
|
+
/**
|
|
5
|
+
* `auth-needed` (ADR-0013): the read path is hitting auth errors (401/403) and is retrying
|
|
6
|
+
* forever with backoff — distinct from `degraded` (a sync commit exhausted its retries). The app
|
|
7
|
+
* should prompt re-login; sync auto-resumes (phase returns to `ready`/`syncing`) the instant
|
|
8
|
+
* re-authentication makes the token valid again. It never silently wedges or permanently stops.
|
|
9
|
+
*/
|
|
4
10
|
phase: SyncRuntimePhase;
|
|
5
11
|
isRunning: boolean;
|
|
6
12
|
lastError?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Per-consistency-group readiness (ADR-0032 decision 6): `groupKey → isReady`, updated as each group
|
|
15
|
+
* catches up. Exposed on both the in-process and worker-attached clients so an app can drive
|
|
16
|
+
* progressive per-group paint without waiting on the all-eager-groups `ready` gate. Absent until the
|
|
17
|
+
* sync runtime has started (e.g. while `syncEnabled` is false there are no groups to report).
|
|
18
|
+
*/
|
|
19
|
+
groups?: Record<string, boolean>;
|
|
7
20
|
}
|
|
8
21
|
|
|
9
22
|
export interface SyncServerAddress {
|
|
@@ -16,6 +29,13 @@ export interface MutationDiagnostics {
|
|
|
16
29
|
sendingCount: number;
|
|
17
30
|
ackedCount: number;
|
|
18
31
|
failedCount: number;
|
|
32
|
+
/** Mutations the server permanently rejected (terminal); surfaced, never retried (ADR-0006). */
|
|
33
|
+
quarantinedCount: number;
|
|
34
|
+
/**
|
|
35
|
+
* Stale writes the server declined under the `reject-if-stale` Conflict policy (terminal,
|
|
36
|
+
* ADR-0015). The optimistic Overlay is kept; the user resolves each as a new write or discards it.
|
|
37
|
+
*/
|
|
38
|
+
conflictedCount: number;
|
|
19
39
|
lastFlushAtUs?: string;
|
|
20
40
|
lastAckAtUs?: string;
|
|
21
41
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { sql, type SQL } from "drizzle-orm";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The BODY of the canonical microsecond clock — `clock_timestamp()` epoch microseconds as BIGINT.
|
|
5
|
+
* This text is the SINGLE source for the `public.pgxsinkit_clock_us()` function body rendered by the
|
|
6
|
+
* server's utilities migration ({@link renderPgxsinkitUtilitiesMigration}) and NOTHING else. No column
|
|
7
|
+
* DEFAULT and no generated apply-function DDL may embed this expression — every surface CALLS the
|
|
8
|
+
* function ({@link CLOCK_US_CALL_SQL_TEXT} / {@link clockMicrosecondsSql}), so the semantic choices
|
|
9
|
+
* (`clock_timestamp()` over `now()`; `FLOOR` + `BIGINT`) live in exactly one reviewable body.
|
|
10
|
+
*/
|
|
11
|
+
export const NOW_MICROSECONDS_SQL_TEXT = "CAST(FLOOR(EXTRACT(EPOCH FROM clock_timestamp()) * 1000000) AS BIGINT)";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The CALL form of the canonical microsecond clock, for string-rendering contexts (the apply-function
|
|
15
|
+
* generator's PL/pgSQL literals). Deliberately `clock_timestamp()` (via the function body), never
|
|
16
|
+
* `now()`/`transaction_timestamp()`: `now()` freezes at transaction start, but LWW/audit ordering of a
|
|
17
|
+
* multi-row apply within one transaction needs an ADVANCING clock. The DB function is the canonical
|
|
18
|
+
* home — never inline the expression at a call site.
|
|
19
|
+
*/
|
|
20
|
+
export const CLOCK_US_CALL_SQL_TEXT = "public.pgxsinkit_clock_us()";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The canonical microsecond clock as a Drizzle fragment, for `.default(...)` column positions — the
|
|
24
|
+
* CALL form `public.pgxsinkit_clock_us()`, never the inline expression. Deliberately `clock_timestamp()`
|
|
25
|
+
* (inside the function), never `now()`/`transaction_timestamp()`: `now()` freezes at transaction start,
|
|
26
|
+
* whereas LWW/audit ordering of a multi-row apply within one transaction needs an advancing clock. The
|
|
27
|
+
* DB function (installed by the utilities migration) is the canonical home; never inline the expression.
|
|
28
|
+
*/
|
|
29
|
+
export const clockMicrosecondsSql: SQL = sql.raw(CLOCK_US_CALL_SQL_TEXT);
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The single SQL identifier and literal resolver (ADR-0004).
|
|
3
|
+
*
|
|
4
|
+
* Every package routes identifier quoting and literal escaping through this module
|
|
5
|
+
* so there is exactly one definition of "what is a bare-safe identifier", the
|
|
6
|
+
* reserved-word set, quoting, and string-literal escaping. Previously five-plus
|
|
7
|
+
* copies disagreed — one of them (the client mutation path) left reserved-word
|
|
8
|
+
* table names unquoted, the same class of bug the local-schema generator had to fix.
|
|
9
|
+
*
|
|
10
|
+
* Note: this is the bare-quoting concern. Validation guards that merely assert a
|
|
11
|
+
* value is a syntactically valid identifier (e.g. allowing uppercase, which is legal
|
|
12
|
+
* but must be quoted) are a distinct concern and live with their callers.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
// PostgreSQL fully-reserved keywords: cannot be used as a bare identifier.
|
|
16
|
+
const RESERVED_SQL_KEYWORDS = new Set([
|
|
17
|
+
"all",
|
|
18
|
+
"analyse",
|
|
19
|
+
"analyze",
|
|
20
|
+
"and",
|
|
21
|
+
"any",
|
|
22
|
+
"array",
|
|
23
|
+
"as",
|
|
24
|
+
"asc",
|
|
25
|
+
"asymmetric",
|
|
26
|
+
"both",
|
|
27
|
+
"case",
|
|
28
|
+
"cast",
|
|
29
|
+
"check",
|
|
30
|
+
"collate",
|
|
31
|
+
"column",
|
|
32
|
+
"constraint",
|
|
33
|
+
"create",
|
|
34
|
+
"current_catalog",
|
|
35
|
+
"current_date",
|
|
36
|
+
"current_role",
|
|
37
|
+
"current_time",
|
|
38
|
+
"current_timestamp",
|
|
39
|
+
"current_user",
|
|
40
|
+
"default",
|
|
41
|
+
"deferrable",
|
|
42
|
+
"desc",
|
|
43
|
+
"distinct",
|
|
44
|
+
"do",
|
|
45
|
+
"else",
|
|
46
|
+
"end",
|
|
47
|
+
"except",
|
|
48
|
+
"false",
|
|
49
|
+
"fetch",
|
|
50
|
+
"for",
|
|
51
|
+
"foreign",
|
|
52
|
+
"from",
|
|
53
|
+
"grant",
|
|
54
|
+
"group",
|
|
55
|
+
"having",
|
|
56
|
+
"in",
|
|
57
|
+
"initially",
|
|
58
|
+
"intersect",
|
|
59
|
+
"into",
|
|
60
|
+
"lateral",
|
|
61
|
+
"leading",
|
|
62
|
+
"limit",
|
|
63
|
+
"localtime",
|
|
64
|
+
"localtimestamp",
|
|
65
|
+
"not",
|
|
66
|
+
"null",
|
|
67
|
+
"offset",
|
|
68
|
+
"on",
|
|
69
|
+
"only",
|
|
70
|
+
"or",
|
|
71
|
+
"order",
|
|
72
|
+
"placing",
|
|
73
|
+
"primary",
|
|
74
|
+
"references",
|
|
75
|
+
"returning",
|
|
76
|
+
"select",
|
|
77
|
+
"session_user",
|
|
78
|
+
"some",
|
|
79
|
+
"symmetric",
|
|
80
|
+
"table",
|
|
81
|
+
"then",
|
|
82
|
+
"to",
|
|
83
|
+
"trailing",
|
|
84
|
+
"true",
|
|
85
|
+
"union",
|
|
86
|
+
"unique",
|
|
87
|
+
"user",
|
|
88
|
+
"using",
|
|
89
|
+
"variadic",
|
|
90
|
+
"when",
|
|
91
|
+
"where",
|
|
92
|
+
"window",
|
|
93
|
+
"with",
|
|
94
|
+
]);
|
|
95
|
+
|
|
96
|
+
/** Always wrap an identifier in double quotes, escaping any embedded quote. */
|
|
97
|
+
export function quoteIdentifier(value: string): string {
|
|
98
|
+
return `"${value.replace(/"/g, '""')}"`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Escape a SQL string-literal body (`'` -> `''`); does not add the surrounding quotes. */
|
|
102
|
+
export function escapeSqlLiteral(value: string): string {
|
|
103
|
+
return value.replace(/'/g, "''");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** A SQL string literal, surrounding quotes included. */
|
|
107
|
+
export function quoteSqlLiteral(value: string): string {
|
|
108
|
+
return `'${escapeSqlLiteral(value)}'`;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* A bare-safe identifier: a simple lowercase identifier Postgres will neither fold
|
|
113
|
+
* nor misparse. Uppercase is excluded because Postgres folds an unquoted identifier
|
|
114
|
+
* to lowercase, which would change its meaning. Internal to {@link maybeQuoteIdentifier}.
|
|
115
|
+
*/
|
|
116
|
+
function isSimpleIdentifier(value: string): boolean {
|
|
117
|
+
return /^[a-z_][a-z0-9_]*$/.test(value);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Quote only when required: bare for a simple, non-reserved identifier; quoted
|
|
122
|
+
* otherwise. Keeps generated SQL stable for the common case while never emitting a
|
|
123
|
+
* reserved word or mixed-case name unquoted.
|
|
124
|
+
*/
|
|
125
|
+
export function maybeQuoteIdentifier(value: string): string {
|
|
126
|
+
if (isSimpleIdentifier(value) && !RESERVED_SQL_KEYWORDS.has(value)) {
|
|
127
|
+
return value;
|
|
128
|
+
}
|
|
129
|
+
return quoteIdentifier(value);
|
|
130
|
+
}
|