@geekmidas/cloud 10.0.0-alpha.99 → 10.0.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.
Files changed (74) hide show
  1. package/README.md +6 -14
  2. package/dist/index-ByEJy40r.d.cts +56 -0
  3. package/dist/index-ByEJy40r.d.cts.map +1 -0
  4. package/dist/index-DZ0QrJQr.d.mts +56 -0
  5. package/dist/index-DZ0QrJQr.d.mts.map +1 -0
  6. package/dist/index.cjs +3 -3
  7. package/dist/index.d.cts +1 -1
  8. package/dist/index.d.mts +1 -1
  9. package/dist/index.mjs +3 -2
  10. package/dist/utils/index.cjs +5 -106
  11. package/dist/utils/index.d.cts +2 -62
  12. package/dist/utils/index.d.mts +2 -62
  13. package/dist/utils/index.mjs +2 -78
  14. package/dist/utils-BdKG20_m.mjs +83 -0
  15. package/dist/utils-BdKG20_m.mjs.map +1 -0
  16. package/dist/utils-CtMjuIMR.cjs +129 -0
  17. package/dist/utils-CtMjuIMR.cjs.map +1 -0
  18. package/package.json +9 -47
  19. package/src/sst/{aws/Api.ts → Api.ts} +47 -19
  20. package/src/sst/{aws/Cron.ts → Cron.ts} +46 -6
  21. package/src/sst/{aws/Function.ts → Function.ts} +41 -12
  22. package/src/sst/Queue.ts +46 -0
  23. package/src/sst/{aws/Storage.ts → Storage.ts} +3 -3
  24. package/src/sst/Topic.ts +37 -0
  25. package/src/sst/__tests__/LinkedEnvironment.spec.ts +1 -4
  26. package/src/sst/__type-tests__/authorizers.type-test.ts +2 -2
  27. package/src/sst/__type-tests__/manifest.type-test.ts +77 -0
  28. package/src/sst/__type-tests__/messaging.type-test.ts +3 -3
  29. package/src/sst/__type-tests__/storage.type-test.ts +2 -2
  30. package/src/sst/index.ts +7 -67
  31. package/src/sst/naming.ts +16 -37
  32. package/src/sst/tsconfig.json +2 -2
  33. package/dist/utils/index.cjs.map +0 -1
  34. package/dist/utils/index.d.cts.map +0 -1
  35. package/dist/utils/index.d.mts.map +0 -1
  36. package/dist/utils/index.mjs.map +0 -1
  37. package/src/dokploy/Application.ts +0 -306
  38. package/src/dokploy/__tests__/Application.provider.spec.ts +0 -206
  39. package/src/dokploy/__tests__/Application.spec.ts +0 -69
  40. package/src/dokploy/index.ts +0 -18
  41. package/src/sst/__tests__/api.spec.ts +0 -185
  42. package/src/sst/__tests__/backends.spec.ts +0 -249
  43. package/src/sst/__tests__/bootstrap.spec.ts +0 -140
  44. package/src/sst/__tests__/database.spec.ts +0 -119
  45. package/src/sst/__tests__/encryption.spec.ts +0 -90
  46. package/src/sst/__tests__/fromManifest.integration.spec.ts +0 -427
  47. package/src/sst/__tests__/fromManifest.spec.ts +0 -567
  48. package/src/sst/__tests__/function.spec.ts +0 -121
  49. package/src/sst/__tests__/links.spec.ts +0 -209
  50. package/src/sst/__tests__/provides.spec.ts +0 -107
  51. package/src/sst/__tests__/ses.spec.ts +0 -93
  52. package/src/sst/__tests__/surfaces.spec.ts +0 -149
  53. package/src/sst/__type-tests__/overrides.type-test.ts +0 -152
  54. package/src/sst/aws/Cache.ts +0 -259
  55. package/src/sst/aws/Credential.ts +0 -61
  56. package/src/sst/aws/Database.ts +0 -204
  57. package/src/sst/aws/DatabaseBootstrap.ts +0 -312
  58. package/src/sst/aws/DerivedDatabase.ts +0 -117
  59. package/src/sst/aws/Email.ts +0 -181
  60. package/src/sst/aws/Encryption.ts +0 -91
  61. package/src/sst/aws/ExternalApi.ts +0 -58
  62. package/src/sst/aws/FileServer.ts +0 -82
  63. package/src/sst/aws/ObjectStorage.ts +0 -76
  64. package/src/sst/aws/Queue.ts +0 -149
  65. package/src/sst/aws/RestApiSurface.ts +0 -114
  66. package/src/sst/aws/Secret.ts +0 -76
  67. package/src/sst/aws/StaticSite.ts +0 -92
  68. package/src/sst/aws/Topic.ts +0 -68
  69. package/src/sst/aws/bootstrap/handler.ts +0 -119
  70. package/src/sst/aws/ses.ts +0 -132
  71. package/src/sst/errors.ts +0 -65
  72. package/src/sst/fromManifest.ts +0 -1116
  73. package/src/sst/overrides.ts +0 -133
  74. package/src/sst/upstash.d.ts +0 -38
@@ -1,152 +0,0 @@
1
- // Type-level tests for `fromManifest`'s overrides, inferred from the manifest.
2
- // Checked by `ts:check:sst`; each `@ts-expect-error` self-validates: if the
3
- // inference regresses, the now-unused directive becomes a TS error the gate
4
- // catches.
5
-
6
- import type { ConstructManifest } from '@geekmidas/manifest';
7
- import { App } from '../App';
8
- import { fromManifest } from '../fromManifest';
9
-
10
- const stack = new App({
11
- name: 'a',
12
- stage: 'dev',
13
- domain: 'example.com',
14
- hostedZoneId: 'Z',
15
- region: 'us-east-1',
16
- }).stack('shop');
17
-
18
- declare const vpc: sst.aws.Vpc;
19
-
20
- // The shape `gkm build` emits: `as const satisfies ConstructManifest`.
21
- const constructs = {
22
- Orders: {
23
- kind: 'database',
24
- id: 'Orders',
25
- engine: 'postgres',
26
- provides: ['ORDERS_URL'],
27
- },
28
- Billing: {
29
- kind: 'database-schema',
30
- id: 'Billing',
31
- of: 'Orders',
32
- schema: 'billing',
33
- provides: ['BILLING_URL'],
34
- },
35
- Sessions: { kind: 'cache', id: 'Sessions', provides: ['SESSIONS_URL'] },
36
- Rates: {
37
- kind: 'cache',
38
- id: 'Rates',
39
- of: 'Orders',
40
- provides: ['RATES_URL'],
41
- },
42
- Mail: { kind: 'email', id: 'Mail', provides: ['MAIL_URL'] },
43
- Uploads: { kind: 'objects', id: 'Uploads', provides: ['UPLOADS_URL'] },
44
- Api: {
45
- kind: 'rest-api',
46
- id: 'Api',
47
- path: 'apps/api',
48
- endpoints: [],
49
- provides: ['API_URL'],
50
- },
51
- } as const satisfies ConstructManifest;
52
-
53
- // Valid: the two the synth cannot guess, plus an optional bucket prop.
54
- export const ok = fromManifest(stack, constructs, {
55
- Orders: { vpc },
56
- Mail: { from: 'no-reply@example.com' },
57
- Uploads: { cors: false },
58
- });
59
-
60
- // A database without its VPC.
61
- // @ts-expect-error — `Orders` is a database: `vpc` is required.
62
- fromManifest(stack, constructs, { Mail: { from: 'a@example.com' } });
63
-
64
- // Mail without a sender.
65
- fromManifest(stack, constructs, {
66
- Orders: { vpc },
67
- // @ts-expect-error — every provider rejects an unverified sender.
68
- Mail: {},
69
- });
70
-
71
- // A misspelt id.
72
- fromManifest(stack, constructs, {
73
- Orders: { vpc },
74
- Mail: { from: 'a@example.com' },
75
- // @ts-expect-error — not a construct in this manifest.
76
- Ordrs: { vpc },
77
- });
78
-
79
- // A prop the kind's component never reads.
80
- fromManifest(stack, constructs, {
81
- Orders: { vpc },
82
- Mail: { from: 'a@example.com' },
83
- // @ts-expect-error — a surface takes API Gateway args, not bucket ones.
84
- Api: { versioning: true },
85
- });
86
-
87
- // What the declaration already decides is not overridable.
88
- fromManifest(stack, constructs, {
89
- // @ts-expect-error — the schema is the declaration's, not the deploy's.
90
- Orders: { vpc, schema: 'other' },
91
- Mail: { from: 'a@example.com' },
92
- });
93
-
94
- // A kind with nothing to override takes no key.
95
- fromManifest(stack, constructs, {
96
- Orders: { vpc },
97
- Mail: { from: 'a@example.com' },
98
- // @ts-expect-error — a schema derives from its database.
99
- Billing: {},
100
- });
101
-
102
- // Nor does a cache in a database: same address, one more table.
103
- fromManifest(stack, constructs, {
104
- Orders: { vpc },
105
- Mail: { from: 'a@example.com' },
106
- // @ts-expect-error — `Rates` lives in `Orders`.
107
- Rates: { region: 'eu-west-1' },
108
- });
109
-
110
- // The backend decides too: ElastiCache needs a VPC to live in…
111
- fromManifest(
112
- stack,
113
- constructs,
114
- // @ts-expect-error — `Sessions` on ElastiCache needs `vpc`.
115
- { Orders: { vpc }, Mail: { from: 'a@example.com' } },
116
- { cache: 'elasticache' },
117
- );
118
- export const elasticache = fromManifest(
119
- stack,
120
- constructs,
121
- {
122
- Orders: { vpc },
123
- Sessions: { vpc: { subnets: [], securityGroups: [] } },
124
- Mail: { from: 'a@example.com' },
125
- },
126
- { cache: 'elasticache' },
127
- );
128
-
129
- // …and mail that is not SES is an account somebody created, with its URL.
130
- fromManifest(
131
- stack,
132
- constructs,
133
- {
134
- Orders: { vpc },
135
- // @ts-expect-error — Resend's URL is not something a deploy can mint.
136
- Mail: { from: 'a@example.com' },
137
- },
138
- { email: 'resend' },
139
- );
140
- export const resend = fromManifest(
141
- stack,
142
- constructs,
143
- {
144
- Orders: { vpc },
145
- Mail: { from: 'a@example.com', url: 'smtp://resend' },
146
- },
147
- { email: 'resend' },
148
- );
149
-
150
- // A manifest whose ids are not known statically keeps the untyped record.
151
- declare const untyped: ConstructManifest;
152
- export const loose = fromManifest(stack, untyped, { Anything: { at: 'all' } });
@@ -1,259 +0,0 @@
1
- import { elasticache } from '@pulumi/aws';
2
- import { type GkmLinkable, ResourceType } from '../Linkable';
3
- import type { StackType } from '../Stack';
4
-
5
- /**
6
- * `Cache` — a key/value cache, and the infra half of the `cache` kind.
7
- *
8
- * The kind where the *backend* is the whole design, because unlike mail the
9
- * three do not speak the same protocol. What this composes is a URL whose
10
- * scheme names one of them, and the generated entry registers the matching
11
- * driver — so the two agree by construction rather than by being kept in step.
12
- *
13
- * Only one of the three provisions anything:
14
- *
15
- * - **`db`** provisions nothing at all. The cache is a table in the database
16
- * already declared, so this resolves that database's URL and the table is
17
- * created by the same thing that creates the schema. No second resource, no
18
- * second credential, and nothing to pay for while idle.
19
- * - **`upstash`** provisions a Redis database through Upstash's own Pulumi
20
- * provider, which SST installs on demand: `sst add upstash`. It is not one of
21
- * the two providers SST preloads, so the component checks for its global and
22
- * says which command is missing rather than failing on an undefined name. A
23
- * URL can still be supplied instead, for a database that already exists.
24
- * - **`elasticache`** is the one that creates something, and it comes with the
25
- * constraints of a thing in a VPC: it is reachable from functions in that VPC
26
- * and from nothing else.
27
- */
28
- export class Cache<
29
- TStage extends string = string,
30
- TDomain extends string = string,
31
- > implements GkmLinkable
32
- {
33
- readonly _id: string;
34
-
35
- private readonly url: $util.Input<string>;
36
-
37
- get _type() {
38
- return ResourceType.Cache;
39
- }
40
-
41
- constructor(
42
- _stack: StackType<TStage, TDomain>,
43
- name: string,
44
- props: CacheProps,
45
- ) {
46
- this._id = name;
47
- this.url = props.url ?? this.provision(name, props);
48
- }
49
-
50
- /**
51
- * Create the cache, for the two backends that can be created.
52
- *
53
- * `db` never reaches here — it resolves the declared database's URL, which
54
- * the caller passes in as `url`.
55
- */
56
- private provision(name: string, props: CacheProps): $util.Input<string> {
57
- return props.backend === 'upstash'
58
- ? this.provisionUpstash(name, props)
59
- : this.provisionElastiCache(name, props);
60
- }
61
-
62
- /**
63
- * An Upstash Redis database, over HTTP.
64
- *
65
- * The default backend, because HTTP with a token is reachable from a Lambda
66
- * with no VPC and no connection pool — the same argument that makes it worth
67
- * running a proxy in front of Redis locally so both speak one protocol.
68
- *
69
- * The token goes in the URL's userinfo rather than a second key, because an
70
- * address and the credential that opens it are one fact.
71
- */
72
- private provisionUpstash(
73
- name: string,
74
- props: CacheProps,
75
- ): $util.Input<string> {
76
- // Upstash is installed on demand rather than preloaded, so its global is
77
- // absent until `sst add upstash` has run. Checking for it turns an
78
- // undefined-name crash into a sentence naming the command.
79
- if (typeof upstash === 'undefined') throw new CacheNeedsProvider(name);
80
-
81
- const database = new upstash.RedisDatabase(`${name}Cache`, {
82
- databaseName: name,
83
- // `global` needs a primary region named alongside it, so a plain
84
- // region is the default that needs no second decision.
85
- region: props.region ?? 'eu-west-1',
86
- // In transit, always. The client speaks HTTPS either way, and a cache
87
- // reachable unencrypted over the public internet is not one.
88
- tls: true,
89
- });
90
-
91
- return $util
92
- .all([database.endpoint, database.restToken])
93
- .apply(
94
- ([endpoint, token]) =>
95
- `https://:${encodeURIComponent(token)}@${endpoint}`,
96
- );
97
- }
98
-
99
- /**
100
- * A serverless Valkey cache, in the VPC the caller named.
101
- *
102
- * Serverless rather than a node group because this design provisions per
103
- * stage, and a node running around the clock under every preview stage is a
104
- * bill nobody chose. The database went the other way — a plain RDS instance —
105
- * because a cluster buys complexity there rather than savings; the two are
106
- * separate calls, not one principle applied twice.
107
- *
108
- * TLS is not optional — ElastiCache Serverless only accepts encrypted
109
- * connections — which is why the URL is `rediss://` and why the driver
110
- * registers both schemes.
111
- */
112
- private provisionElastiCache(
113
- name: string,
114
- props: CacheProps,
115
- ): $util.Input<string> {
116
- if (!props.vpc) throw new CacheNeedsVpc(name);
117
-
118
- const cache = new elasticache.ServerlessCache(`${name}Cache`, {
119
- engine: 'valkey',
120
- name,
121
- subnetIds: props.vpc.subnets,
122
- securityGroupIds: props.vpc.securityGroups,
123
- });
124
-
125
- return $util.output(cache.endpoints).apply((endpoints) => {
126
- const endpoint = endpoints[0];
127
- if (!endpoint) throw new CacheNeedsVpc(name);
128
-
129
- return $util
130
- .all([endpoint.address, endpoint.port])
131
- .apply(([address, port]) => `rediss://${address}:${port}`);
132
- });
133
- }
134
-
135
- /** One key, the URL — and its scheme is the contract with the driver. */
136
- provides(): Record<string, $util.Input<string>> {
137
- return { url: this.url };
138
- }
139
-
140
- getSSTLink() {
141
- return { properties: { ...this.provides() } };
142
- }
143
- }
144
-
145
- export interface CacheProps {
146
- /** Which backend to create. `db` never creates anything and never gets here. */
147
- backend?: 'upstash' | 'elasticache' | 'db';
148
- /** The region to create an Upstash database in. */
149
- region?: string;
150
- /**
151
- * The cache's URL, whose scheme picks the driver.
152
- *
153
- * Supplied for the two backends that are not provisioned here — the declared
154
- * database's URL for `db`, a secret for `upstash`. Absent for `elasticache`,
155
- * which creates a cluster and composes its own.
156
- */
157
- url?: $util.Input<string>;
158
- /**
159
- * The VPC to put an ElastiCache cluster in.
160
- *
161
- * Required for that backend and meaningless for the others. A cache in a VPC
162
- * is reachable from functions in that VPC and from nothing else, which is
163
- * the trade that comes with choosing it.
164
- */
165
- vpc?: {
166
- subnets: $util.Input<$util.Input<string>[]>;
167
- securityGroups: $util.Input<$util.Input<string>[]>;
168
- };
169
- }
170
-
171
- /** An ElastiCache-backed cache was declared and no VPC was supplied for it. */
172
- export class CacheNeedsVpc extends Error {
173
- constructor(readonly id: string) {
174
- super(
175
- `'${id}' is an ElastiCache cache and needs a VPC to live in. Supply ` +
176
- `one through the deploy layer — ` +
177
- `fromManifest(stack, manifest, { ${id}: { vpc } }) — the same one ` +
178
- `the functions that read it run in, since a cache in a VPC is ` +
179
- `reachable from nowhere else.`,
180
- );
181
- this.name = 'CacheNeedsVpc';
182
- }
183
- }
184
-
185
- /**
186
- * A cache backed by the database was declared, and no database was.
187
- *
188
- * The same rule pg-boss has, and the same reason: provisioning a database to
189
- * hold only a cache is the resource this design refuses to invent on your
190
- * behalf.
191
- */
192
- /**
193
- * A cache's address: its database's URL, carrying the table it reads.
194
- *
195
- * The table travels in the URL rather than beside it because a database-backed
196
- * cache has no address of its own — two caches in one database resolve the same
197
- * connection string, so the parameter is the only thing that identifies which
198
- * one a client is holding. Mirrors what the local target composes, so the same
199
- * application code reads the same shape in both places.
200
- */
201
- export function withCacheTable(url: string, table: string): string {
202
- const parsed = new URL(url);
203
- parsed.searchParams.set('table', table);
204
-
205
- return parsed.toString();
206
- }
207
-
208
- /**
209
- * A `'db'` cache in an app that declares more than one database.
210
- *
211
- * Picking one would put the cache somewhere nobody chose, and the symptom —
212
- * entries that are never found — appears long after the deploy reported
213
- * success. The fix is a stronger statement in application code:
214
- * `orders.cache('Sessions')` names the database.
215
- */
216
- export class CacheIsAmbiguous extends Error {
217
- constructor(
218
- readonly id: string,
219
- readonly databases: readonly string[],
220
- ) {
221
- super(
222
- `'${id}' is a cache backed by the database, and this app declares ` +
223
- `${databases.length}: ${databases.join(', ')}. Declare the cache from ` +
224
- `its database — e.g. ${databases[0]}.cache('${id}') — rather than ` +
225
- `as a bare new Cache().`,
226
- );
227
- this.name = 'CacheIsAmbiguous';
228
- }
229
- }
230
-
231
- export class CacheNeedsDatabase extends Error {
232
- constructor(readonly id: string) {
233
- super(
234
- `'${id}' is a cache backed by the database, and this app declares no ` +
235
- `database for it to live in. Declare one, or declare the cache from ` +
236
- `the database it belongs to.`,
237
- );
238
- this.name = 'CacheNeedsDatabase';
239
- }
240
- }
241
-
242
- /**
243
- * An Upstash cache was declared and Upstash's provider is not installed.
244
- *
245
- * SST preloads two providers and installs the rest on demand, so the global this
246
- * component reaches for does not exist until somebody runs the command. Naming
247
- * the command beats an undefined-name crash halfway through a synth.
248
- */
249
- export class CacheNeedsProvider extends Error {
250
- constructor(readonly id: string) {
251
- super(
252
- `'${id}' is an Upstash cache and the Upstash provider is not ` +
253
- `installed. Run \`sst add upstash\` in the app, or supply a URL for ` +
254
- `a database that already exists — ` +
255
- `fromManifest(stack, manifest, { ${id}: { url } }).`,
256
- );
257
- this.name = 'CacheNeedsProvider';
258
- }
259
- }
@@ -1,61 +0,0 @@
1
- import { type GkmLinkable, ResourceType } from '../Linkable';
2
- import type { StackType } from '../Stack';
3
-
4
- /**
5
- * `Credential` — a third-party credential SST holds, and the infra half of the
6
- * `credential` kind.
7
- *
8
- * The *same storage* as a secret and a different kind, because what differs is
9
- * the lifecycle rather than the mechanism: a secret is generated and rotated by
10
- * the platform, while a credential is issued by someone else and has a shape
11
- * the construct validates on the way in. Both are values you set out of band
12
- * with `sst secret set`, under the construct's id.
13
- *
14
- * The role is `credentials` rather than `value`, and that is not cosmetic. The
15
- * role *is* the contract — `providedKeyFor` turns it into the key the app
16
- * declared — so a credential providing `value` would supply `STRIPE_VALUE`
17
- * against a declared `STRIPE_CREDENTIALS`, and `assertProvides` would reject
18
- * the stack at synth.
19
- *
20
- * It holds an `sst.Secret` rather than being one, and reports its own type,
21
- * for the same reason one level down: the type decides the keys a link
22
- * resolves to. As an SST secret it resolved to `STRIPE`, so a function that
23
- * declared `STRIPE_CREDENTIALS` was linked to nothing.
24
- */
25
- export class Credential<
26
- TStage extends string = string,
27
- TDomain extends string = string,
28
- > implements GkmLinkable
29
- {
30
- readonly _id: string;
31
-
32
- private readonly secret: sst.Secret;
33
-
34
- get _type() {
35
- return ResourceType.Credential;
36
- }
37
-
38
- constructor(
39
- _stack: StackType<TStage, TDomain>,
40
- name: string,
41
- props: CredentialProps = {},
42
- ) {
43
- this._id = name;
44
- // The id itself, as it always was: a value already set with
45
- // `sst secret set Stripe …` is still the one read.
46
- this.secret = new sst.Secret(name, props.placeholder);
47
- }
48
-
49
- provides(): Record<string, $util.Input<string>> {
50
- return { credentials: this.secret.value };
51
- }
52
-
53
- getSSTLink() {
54
- return { properties: { ...this.provides() } };
55
- }
56
- }
57
-
58
- export interface CredentialProps {
59
- /** A value until one is set, for a preview stage. */
60
- placeholder?: string;
61
- }
@@ -1,204 +0,0 @@
1
- import * as postgresUrl from '@geekmidas/db/pg/url';
2
- import { type GkmLinkable, ResourceType } from '../Linkable';
3
- import type { StackType } from '../Stack';
4
-
5
- /**
6
- * `Database` — an RDS Postgres instance, and the infra half of the `database`
7
- * kind.
8
- *
9
- * **A provisioned instance rather than Aurora Serverless v2.** Aurora is the
10
- * more interesting answer on paper — it scales to zero, so an idle preview
11
- * stage costs storage and nothing else, which suits a design that provisions
12
- * per stage. It is also a cluster: more moving parts, a different resource
13
- * type, and pricing that is harder to predict for the steady-state workload
14
- * most stages actually are. A plain instance is the ordinary thing, and the
15
- * ordinary thing is the better default.
16
- *
17
- * Aurora is not reachable from here: this class *is* the RDS component and its
18
- * props are that component's args, so a stage wanting a cluster needs a second
19
- * class rather than an override. Worth adding when something wants it; not
20
- * worth pretending it already exists.
21
- *
22
- * :::caution
23
- * Moving between the two replaces the database. They are different resources,
24
- * not different settings — the data does not come with you.
25
- * :::
26
- *
27
- * **Who provisions the read replica** — nobody does, and a reader resolves to
28
- * the writer's address. An Aurora cluster has a reader endpoint; an RDS
29
- * instance has no second address to hand out, so `DatabaseReader` returns this
30
- * one. That is the fallback the design already specified for `--target=server`
31
- * and it is safe for the same reason: read-only is enforced by the role's
32
- * grants, never by which endpoint you happened to reach. Adding `replicas`
33
- * creates instances but no endpoint that balances across them, so it changes
34
- * nothing here.
35
- */
36
- export class Database<
37
- TStage extends string = string,
38
- TDomain extends string = string,
39
- >
40
- extends sst.aws.Postgres
41
- implements GkmLinkable
42
- {
43
- readonly _id!: string;
44
-
45
- get _type() {
46
- return ResourceType.SSTPostgres;
47
- }
48
-
49
- /** The schema pinned on the connection's `search_path`, where one applies. */
50
- private readonly schema: string | undefined;
51
-
52
- /**
53
- * The VPC this cluster lives in.
54
- *
55
- * Kept so the bootstrap function can be put in the same one — a database
56
- * reachable from outside its VPC is the problem the requirement avoids, and
57
- * the DDL has to reach it from inside.
58
- */
59
- readonly vpc: sst.aws.Vpc;
60
-
61
- constructor(
62
- _stack: StackType<TStage, TDomain>,
63
- name: string,
64
- props: DatabaseProps,
65
- ) {
66
- const { schema, ...args } = props;
67
-
68
- super(name, args);
69
- this._id = name;
70
- this.schema = schema;
71
- this.vpc = args.vpc;
72
- }
73
-
74
- /**
75
- * One key, the URL a running handler connects with.
76
- *
77
- * The owner URL is deliberately not here. It exists — a migrator needs DDL
78
- * rights — but it is wired straight into the migrator by the adapter, so no
79
- * edge in any manifest can name it and nothing can be granted it by mistake.
80
- *
81
- * Composed through `@geekmidas/db`'s codec rather than by hand, which is what
82
- * puts `search_path` in as a libpq `options` parameter. A plain
83
- * `?search_path=` is accepted by every URL parser, ignored by the server, and
84
- * produces a database that looks empty.
85
- */
86
- provides(): Record<string, $util.Input<string>> {
87
- return { url: this.urlFor(this.role ? { as: this.role } : {}) };
88
- }
89
-
90
- /**
91
- * The runtime role this database's own handlers connect as.
92
- *
93
- * Set by the adapter once the bootstrap has registered the role — the
94
- * cluster exists before any role does, so it cannot be a constructor
95
- * argument. Until it is set (and under `roles: false`, where it never is),
96
- * the URL is the master's.
97
- */
98
- private role: { user: string; password: $util.Input<string> } | undefined;
99
-
100
- connectAs(role: { user: string; password: $util.Input<string> }): void {
101
- this.role = role;
102
- }
103
-
104
- /**
105
- * This cluster's connection URL, optionally through the reader endpoint or
106
- * pinned to a different schema.
107
- *
108
- * One composition serving three callers — the cluster itself, a reader, and
109
- * a schema tenant — so the three cannot come to disagree about how a URL is
110
- * put together. That matters more than it sounds: `search_path` is the part
111
- * that goes quietly missing when the join is done by hand.
112
- */
113
- urlFor(options: {
114
- /**
115
- * Ask for the read path.
116
- *
117
- * Kept in the signature and currently resolved to the same address: an
118
- * RDS instance has one endpoint. It states the caller's intent — a reader
119
- * wants the read path — so moving a stage to a cluster that *has* a second
120
- * endpoint changes one line here rather than every call site. What makes
121
- * the connection read-only is the role it authenticates as, not this.
122
- */
123
- reader?: boolean;
124
- schema?: string;
125
- /**
126
- * Connect as a role other than the master.
127
- *
128
- * What every tenant does. The master credential exists before any role
129
- * does, which is why the bootstrap uses it and why nothing else should: a
130
- * handler holding it could drop the database it was reading.
131
- */
132
- as?: { user: $util.Input<string>; password: $util.Input<string> };
133
- }): $util.Input<string> {
134
- // One address: an RDS instance has no reader endpoint. Reading through the
135
- // writer is safe because the reader *role* is what forbids writing.
136
- const host = this.host;
137
- // A role carries its own `search_path`, pinned by `ALTER ROLE`. It goes in
138
- // the URL only for the master, which has no role of its own to pin it on.
139
- const searchPath = options.as ? undefined : (options.schema ?? this.schema);
140
-
141
- return $util
142
- .all([
143
- host,
144
- this.port,
145
- this.database,
146
- options.as?.user ?? this.username,
147
- options.as?.password ?? this.password,
148
- ])
149
- .apply(([resolvedHost, port, database, username, password]) =>
150
- postgresUrl.build({
151
- host: resolvedHost,
152
- port,
153
- database,
154
- username,
155
- password,
156
- ...(searchPath ? { searchPath } : {}),
157
- }),
158
- );
159
- }
160
-
161
- override getSSTLink() {
162
- const link = super.getSSTLink();
163
- return {
164
- ...link,
165
- properties: { ...link.properties, ...this.provides() },
166
- };
167
- }
168
- }
169
-
170
- export interface DatabaseProps extends Omit<sst.aws.PostgresArgs, 'vpc'> {
171
- /** The schema to pin on the connection's `search_path`. */
172
- schema?: string;
173
- /**
174
- * The VPC the database lives in.
175
- *
176
- * Narrowed to the component from the wider argument the RDS component
177
- * accepts, because the bootstrap function has to run in this same VPC and a
178
- * function needs security groups as well as subnets — which the loose form
179
- * cannot carry. Requiring the component means one thing is passed and both
180
- * halves can use it.
181
- */
182
- vpc: sst.aws.Vpc;
183
- }
184
-
185
- /**
186
- * A database was declared and the deploy layer supplied no VPC.
187
- *
188
- * Not something the adapter can default. RDS lives in a VPC, and creating one
189
- * means creating a NAT gateway — a real monthly cost, in an account whose
190
- * networking may already be someone else's decision. So it is required, named,
191
- * and supplied where other provider-specific props are.
192
- */
193
- export class DatabaseNeedsVpc extends Error {
194
- constructor(readonly id: string) {
195
- super(
196
- `'${id}' is a database, and a database needs a VPC to live in. ` +
197
- `Supply one through the deploy layer — ` +
198
- `fromManifest(stack, manifest, { ${id}: { vpc } }) — because ` +
199
- `creating one means creating a NAT gateway, which costs money in an ` +
200
- `account whose networking may already be someone else's decision.`,
201
- );
202
- this.name = 'DatabaseNeedsVpc';
203
- }
204
- }