@di-framework/pglite-component 0.0.0-stage → 6.0.11

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/wit/world.wit ADDED
@@ -0,0 +1,186 @@
1
+ package di-framework:pglite@0.2.0;
2
+
3
+ /// Types shared by the `database` interface.
4
+ ///
5
+ /// Mirrors `di-framework:sqlite/types` on purpose so consumers can switch
6
+ /// between the SQLite and PGlite providers with minimal code changes:
7
+ /// same `value`/`row`/`error` shapes, same connection lifecycle
8
+ /// (`open`/`query`/`run`/`exec`/`first`/transactions/`close`).
9
+ /// Differences from SQLite are documented on the affected items.
10
+ interface types {
11
+ /// A structured error reported by Postgres itself.
12
+ record pg-error {
13
+ /// SQLSTATE code (e.g. `23505` = unique_violation).
14
+ /// SQLite reports numeric result codes instead.
15
+ code: string,
16
+ /// Human-readable message from the server.
17
+ message: string,
18
+ }
19
+
20
+ /// Errors raised by functions in this package. Intentionally non-exhaustive:
21
+ /// consumers must keep a catch-all arm.
22
+ variant error {
23
+ /// The data directory could not be opened, initialization failed, or
24
+ /// another connection already owns this component instance.
25
+ open-failed(string),
26
+ /// The connection has been closed with `close` and cannot be used again.
27
+ closed,
28
+ /// The SQL was rejected before execution. Causes: a syntax error (42601),
29
+ /// an undeterminable parameter type (42P18), a bad savepoint name, a NUL
30
+ /// byte in the SQL, or more than one statement passed to `run`/`query`/
31
+ /// `first` (the server reports that as a syntax error; unlike `exec`,
32
+ /// which allows `;`-separated batches like the SQLite provider).
33
+ invalid-sql(string),
34
+ /// Wrong number of parameters or a value that could not be bound.
35
+ /// Parameters bind positionally to `$1`, `$2`, ... (not `?`/`?NNN`).
36
+ invalid-params(string),
37
+ /// Postgres reported an error while executing the statement (constraint
38
+ /// violation, undefined table, ...). Branch on `pg-error.code` (SQLSTATE)
39
+ /// for machine-readable handling.
40
+ execution-failed(pg-error),
41
+ /// A column value could not be converted into a `value`.
42
+ value-conversion-failed(string),
43
+ /// The embedded engine is unusable. Restart the component to recover.
44
+ connection-lost(string),
45
+ /// `begin`, `commit`, `rollback` (or a savepoint call) was issued in a
46
+ /// state that does not permit it (e.g. `commit` with no open transaction).
47
+ invalid-transaction-state(string),
48
+ /// Any other implementation-specific failure.
49
+ other(string),
50
+ }
51
+
52
+ /// A dynamically-typed value, used for parameters and results.
53
+ /// Identical to the SQLite provider plus `boolean`: Postgres has a native
54
+ /// boolean type and the text encoding (`t`/`f`) is lossy without it.
55
+ /// `json` and `timestamp` are new in 0.2.0 for PostgreSQL-specific types.
56
+ ///
57
+ /// Results (session is `DateStyle=ISO`, `TimeZone=UTC`):
58
+ /// - `bool` -> `boolean`; `int2`/`int4`/`int8` -> `integer` (`int8` too);
59
+ /// `float4`/`float8` -> `real`; `bytea` -> `blob`.
60
+ /// - `json` and `jsonb` -> `json` (JSON text).
61
+ /// - `timestamptz` -> `timestamp`, RFC 3339 in UTC (`2024-01-15T10:30:00Z`,
62
+ /// fractional seconds kept). `infinity` and `-infinity` are returned as
63
+ /// those literals.
64
+ /// - `timestamp` (no zone) and `date` -> `text` (`2024-01-15 10:30:00`,
65
+ /// `2024-01-15`).
66
+ /// - `uuid` and `numeric` -> `text`; arrays -> `text` as PostgreSQL array
67
+ /// literals (`{a,b}`); all other types -> `text`.
68
+ ///
69
+ /// Parameters: `text`, `json` and `timestamp` are sent with unknown type
70
+ /// (OID 0) so PostgreSQL infers the type from context; a parameter with no
71
+ /// context (`SELECT $1 IS NULL`) needs a cast.
72
+ variant value {
73
+ null,
74
+ integer(s64),
75
+ real(f64),
76
+ text(string),
77
+ blob(list<u8>),
78
+ boolean(bool),
79
+ json(string),
80
+ timestamp(string),
81
+ }
82
+
83
+ /// A result row: `(column-name, value)` pairs in column order.
84
+ type row = list<tuple<string, value>>;
85
+
86
+ /// Options accepted by `open`. Every field is optional; `none` means the
87
+ /// documented default.
88
+ record open-options {
89
+ /// Database name for the session. Only `template1` is currently supported.
90
+ database: option<string>,
91
+ /// Reserved: WASI lacks the signal timer used for query cancellation.
92
+ /// Only none or zero is supported; nonzero values fail open.
93
+ statement-timeout-ms: option<u32>,
94
+ }
95
+ }
96
+
97
+ /// Embedded PostgreSQL access. The engine is bundled into the provider and
98
+ /// queries pass through WebAssembly memory; no sockets or sidecar are used.
99
+ /// Data and runtime files live underneath a directory in a writable WASI
100
+ /// filesystem preopen. The host must grant exclusive ownership of that
101
+ /// directory to one component instance at a time.
102
+ interface database {
103
+ use types.{error, value, row, open-options};
104
+
105
+ /// Open a persistent database root (e.g. `/data/orders`); an empty path
106
+ /// selects `/data/pglite`. No ephemeral fallback. The root contains `data/`
107
+ /// (PGDATA), `runtime/` (bundled support files), and bootstrap scratch files.
108
+ /// Only one open connection and one database root per component instance
109
+ /// are supported. Close/drop releases the connection for reuse at that root.
110
+ open: func(path: string, options: option<open-options>) -> result<connection, error>;
111
+
112
+ /// The bundled engine version string, `17.5`; does not open a database.
113
+ server-version: func() -> result<string, error>;
114
+
115
+ /// An exclusive handle to the embedded session. Closing or dropping it
116
+ /// rolls back uncommitted work and resets session state.
117
+ resource connection {
118
+ /// Execute one or more `;`-separated statements and discard any results.
119
+ /// Intended for DDL / migrations. Untrusted input must never be
120
+ /// interpolated into `sql`.
121
+ exec: func(sql: string) -> result<_, error>;
122
+
123
+ /// Execute a single parameterized statement and return the number of rows
124
+ /// affected (the `CommandComplete` tag count). Parameters bind positionally
125
+ /// to `$1`, `$2`, ...
126
+ run: func(sql: string, params: list<value>) -> result<u64, error>;
127
+
128
+ /// Execute a single parameterized statement and return all rows.
129
+ query: func(sql: string, params: list<value>) -> result<list<row>, error>;
130
+
131
+ /// Execute a single parameterized statement and return only the first row,
132
+ /// or `none` when the statement produced no rows.
133
+ first: func(sql: string, params: list<value>) -> result<option<row>, error>;
134
+
135
+ /// `BEGIN`. Fails with `invalid-transaction-state` if a transaction is
136
+ /// already open on this connection; nest with `savepoint` instead.
137
+ begin: func() -> result<_, error>;
138
+
139
+ /// `COMMIT` the open transaction.
140
+ commit: func() -> result<_, error>;
141
+
142
+ /// `ROLLBACK` the open transaction.
143
+ rollback: func() -> result<_, error>;
144
+
145
+ /// `SAVEPOINT <name>`; usable for nested transactions. `name` must match
146
+ /// `[A-Za-z_][A-Za-z0-9_]*`.
147
+ savepoint: func(name: string) -> result<_, error>;
148
+
149
+ /// `RELEASE SAVEPOINT <name>`.
150
+ release-savepoint: func(name: string) -> result<_, error>;
151
+
152
+ /// `ROLLBACK TO SAVEPOINT <name>` followed by `RELEASE`, so the savepoint
153
+ /// is fully unwound.
154
+ rollback-to-savepoint: func(name: string) -> result<_, error>;
155
+
156
+ /// True while a transaction (or savepoint) is open on this connection.
157
+ /// Reported by the engine, including transactions issued through `exec`.
158
+ in-transaction: func() -> bool;
159
+
160
+ /// Rows affected by the most recent `run`/`exec` on this connection.
161
+ rows-affected: func() -> u64;
162
+
163
+ /// Explicitly close the connection. Subsequent calls fail with `closed`.
164
+ close: func() -> result<_, error>;
165
+ }
166
+ }
167
+
168
+ /// The world implemented by the provider component.
169
+ /// Kept symmetric with the sqlite-component: `types` is exported explicitly
170
+ /// so the component is self-contained for `wac plug`.
171
+ ///
172
+ /// The private engine import is resolved during the provider build. The
173
+ /// shipped artifact has only WASI imports, including filesystem and randomness.
174
+ world pglite-provider {
175
+ import di-framework:pglite-engine/engine@0.1.0;
176
+ export types;
177
+ export database;
178
+ }
179
+
180
+ /// The world a consumer targets when it wants a Postgres database.
181
+ /// `wac plug` satisfies these imports with the `pglite-provider`
182
+ /// component's exports.
183
+ world imports {
184
+ import types;
185
+ import database;
186
+ }