@supatype/cli 0.3.2 → 0.3.4

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 (89) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/.turbo/turbo-test.log +153 -150
  3. package/.turbo/turbo-typecheck.log +1 -1
  4. package/CHANGELOG.md +77 -0
  5. package/dist/augmentation-generator.d.ts.map +1 -1
  6. package/dist/augmentation-generator.js +70 -15
  7. package/dist/augmentation-generator.js.map +1 -1
  8. package/dist/cli-version-embedded.js +1 -1
  9. package/dist/commands/generate.d.ts.map +1 -1
  10. package/dist/commands/generate.js +3 -3
  11. package/dist/commands/generate.js.map +1 -1
  12. package/dist/commands/init.d.ts.map +1 -1
  13. package/dist/commands/init.js +29 -19
  14. package/dist/commands/init.js.map +1 -1
  15. package/dist/commands/seed.d.ts +27 -1
  16. package/dist/commands/seed.d.ts.map +1 -1
  17. package/dist/commands/seed.js +206 -33
  18. package/dist/commands/seed.js.map +1 -1
  19. package/dist/dev-compose.d.ts.map +1 -1
  20. package/dist/dev-compose.js +12 -10
  21. package/dist/dev-compose.js.map +1 -1
  22. package/dist/engine-client.d.ts +1 -0
  23. package/dist/engine-client.d.ts.map +1 -1
  24. package/dist/engine-client.js +47 -2
  25. package/dist/engine-client.js.map +1 -1
  26. package/dist/engine-floor.d.ts +27 -4
  27. package/dist/engine-floor.d.ts.map +1 -1
  28. package/dist/engine-floor.js +90 -9
  29. package/dist/engine-floor.js.map +1 -1
  30. package/dist/gitignore.d.ts.map +1 -1
  31. package/dist/gitignore.js +7 -0
  32. package/dist/gitignore.js.map +1 -1
  33. package/dist/hooks-generator.d.ts.map +1 -1
  34. package/dist/hooks-generator.js +18 -3
  35. package/dist/hooks-generator.js.map +1 -1
  36. package/dist/seed-connection.d.ts +60 -0
  37. package/dist/seed-connection.d.ts.map +1 -0
  38. package/dist/seed-connection.js +90 -0
  39. package/dist/seed-connection.js.map +1 -0
  40. package/dist/seed-ir.d.ts +66 -0
  41. package/dist/seed-ir.d.ts.map +1 -0
  42. package/dist/seed-ir.js +265 -0
  43. package/dist/seed-ir.js.map +1 -0
  44. package/dist/seed-runner.d.ts +44 -0
  45. package/dist/seed-runner.d.ts.map +1 -0
  46. package/dist/seed-runner.js +83 -0
  47. package/dist/seed-runner.js.map +1 -0
  48. package/dist/seed.d.ts +345 -0
  49. package/dist/seed.d.ts.map +1 -1
  50. package/dist/seed.js +138 -0
  51. package/dist/seed.js.map +1 -1
  52. package/dist/self-host-compose.d.ts +15 -0
  53. package/dist/self-host-compose.d.ts.map +1 -1
  54. package/dist/self-host-compose.js +31 -8
  55. package/dist/self-host-compose.js.map +1 -1
  56. package/dist/type-extractor.d.ts.map +1 -1
  57. package/dist/type-extractor.js +73 -0
  58. package/dist/type-extractor.js.map +1 -1
  59. package/dist/type-generation.d.ts +29 -0
  60. package/dist/type-generation.d.ts.map +1 -1
  61. package/dist/type-generation.js +52 -0
  62. package/dist/type-generation.js.map +1 -1
  63. package/package.json +2 -2
  64. package/src/augmentation-generator.ts +73 -14
  65. package/src/cli-version-embedded.ts +1 -1
  66. package/src/commands/generate.ts +7 -3
  67. package/src/commands/init.ts +31 -21
  68. package/src/commands/seed.ts +294 -44
  69. package/src/dev-compose.ts +12 -11
  70. package/src/engine-client.ts +52 -3
  71. package/src/engine-floor.ts +100 -9
  72. package/src/gitignore.ts +7 -0
  73. package/src/hooks-generator.ts +21 -6
  74. package/src/seed-connection.ts +146 -0
  75. package/src/seed-ir.ts +311 -0
  76. package/src/seed-runner.ts +109 -0
  77. package/src/seed.ts +408 -0
  78. package/src/self-host-compose.ts +32 -8
  79. package/src/type-extractor.ts +101 -0
  80. package/src/type-generation.ts +64 -0
  81. package/tests/access-composition.test.ts +123 -0
  82. package/tests/augmentation-generator.test.ts +149 -9
  83. package/tests/db-published-and-addressable.test.ts +69 -0
  84. package/tests/engine-floor.test.ts +93 -0
  85. package/tests/fixtures/seed_golden_ir.json +186 -0
  86. package/tests/runtime-contract.test.ts +31 -6
  87. package/tests/seed-connection.test.ts +158 -0
  88. package/tests/seed-ir.test.ts +436 -0
  89. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,186 @@
1
+ {
2
+ "irVersion": 1,
3
+ "schemaFingerprint": "sha256:0000000000000000000000000000000000000000000000000000000000000000",
4
+ "operations": [
5
+ {
6
+ "op": "upsert",
7
+ "model": "Profile",
8
+ "id": "alex",
9
+ "where": {
10
+ "id": "a0000000-0000-4000-8000-000000000001"
11
+ },
12
+ "data": {
13
+ "display_name": "Alex Rivera",
14
+ "full_name": "Alex Rivera"
15
+ }
16
+ },
17
+ {
18
+ "op": "upsert",
19
+ "model": "Organiser",
20
+ "id": "northern",
21
+ "where": {
22
+ "slug": "northern-lights-events"
23
+ },
24
+ "data": {
25
+ "name": "Northern Lights Events",
26
+ "contact_email": "hello@northernlights.test",
27
+ "owner": {
28
+ "connect": {
29
+ "id": {
30
+ "$ref": "alex.id"
31
+ }
32
+ }
33
+ },
34
+ "events": {
35
+ "create": [
36
+ {
37
+ "title": "Southwest Dev Conference",
38
+ "status": "published",
39
+ "starts_at": {
40
+ "kind": "after",
41
+ "base": {
42
+ "kind": "startOf",
43
+ "unit": "day"
44
+ },
45
+ "interval": {
46
+ "days": 30,
47
+ "hours": 9
48
+ }
49
+ },
50
+ "ends_at": {
51
+ "kind": "after",
52
+ "base": {
53
+ "kind": "startOf",
54
+ "unit": "day"
55
+ },
56
+ "interval": {
57
+ "days": 30,
58
+ "hours": 18
59
+ }
60
+ },
61
+ "created_at": {
62
+ "kind": "now"
63
+ },
64
+ "ticket_types": {
65
+ "createMany": [
66
+ {
67
+ "name": "Early Bird",
68
+ "price": {
69
+ "$numeric": "75.0000"
70
+ },
71
+ "quantity": 100
72
+ },
73
+ {
74
+ "name": "Standard",
75
+ "price": {
76
+ "$numeric": "125.0000"
77
+ },
78
+ "quantity": 250
79
+ }
80
+ ]
81
+ }
82
+ }
83
+ ]
84
+ }
85
+ }
86
+ },
87
+ {
88
+ "op": "createMany",
89
+ "model": "Venue",
90
+ "rows": [
91
+ {
92
+ "name": "The Exchange",
93
+ "city": "Bristol",
94
+ "capacity": 450,
95
+ "country": {
96
+ "$default": true
97
+ },
98
+ "organiser": {
99
+ "connect": {
100
+ "slug": "northern-lights-events"
101
+ }
102
+ }
103
+ },
104
+ {
105
+ "name": "Archway Studios",
106
+ "city": "London",
107
+ "capacity": 180,
108
+ "country": {
109
+ "$default": true
110
+ },
111
+ "organiser": {
112
+ "connect": {
113
+ "slug": "northern-lights-events"
114
+ }
115
+ }
116
+ }
117
+ ]
118
+ },
119
+ {
120
+ "op": "group",
121
+ "if": {
122
+ "kind": "notExists",
123
+ "model": "SiteSettings"
124
+ },
125
+ "operations": [
126
+ {
127
+ "op": "create",
128
+ "model": "SiteSettings",
129
+ "data": {
130
+ "site_name": "Launch Test Tickets",
131
+ "default_currency": "GBP"
132
+ }
133
+ }
134
+ ]
135
+ },
136
+ {
137
+ "op": "group",
138
+ "if": {
139
+ "kind": "exists",
140
+ "model": "Event",
141
+ "where": {
142
+ "type": "compare",
143
+ "op": "eq",
144
+ "left": {
145
+ "kind": "column",
146
+ "name": "status"
147
+ },
148
+ "right": {
149
+ "kind": "literal",
150
+ "value": "published"
151
+ }
152
+ }
153
+ },
154
+ "operations": [
155
+ {
156
+ "op": "create",
157
+ "model": "PromoCode",
158
+ "data": {
159
+ "code": "EARLY20",
160
+ "discount_kind": "percentage",
161
+ "discount_value": 20,
162
+ "valid_until": {
163
+ "kind": "after",
164
+ "base": {
165
+ "kind": "now"
166
+ },
167
+ "interval": {
168
+ "days": 25
169
+ }
170
+ },
171
+ "event": {
172
+ "connect": {
173
+ "slug": "southwest-dev-conference"
174
+ }
175
+ }
176
+ }
177
+ }
178
+ ]
179
+ },
180
+ {
181
+ "op": "sql",
182
+ "text": "ANALYZE event",
183
+ "params": []
184
+ }
185
+ ]
186
+ }
@@ -276,22 +276,47 @@ describe("runtime contract", () => {
276
276
  expect(compose).not.toContain("- ./kong.yml:/etc/kong/kong.yml:ro")
277
277
  })
278
278
 
279
- it("devLocal compose omits host-published db and server ports", () => {
279
+ it("devLocal compose omits the production ports", () => {
280
280
  const compose = renderSelfHostCompose(baseConfig, process.cwd(), { devLocal: true })
281
281
  expect(compose).not.toContain('"5432:5432"')
282
282
  expect(compose).not.toContain('"9999:9999"')
283
283
  expect(compose).toContain("${SUPATYPE_KONG_PORT:-18473}:8000")
284
- expect(compose).not.toContain("SUPATYPE_DEV_DB_PORT")
285
284
  })
286
285
 
287
- it("devLocal compose publishes db to host when overrides.engine is set", () => {
288
- const compose = renderSelfHostCompose(
286
+ /**
287
+ * The database is reachable from this machine, always.
288
+ *
289
+ * It used to be published in dev only when something was known to need it, which meant a
290
+ * fresh project had a database nothing on the host could reach: `supatype seed` resolves a
291
+ * connection string and the engine connects over TCP, so the first seed got ECONNREFUSED
292
+ * and the remedy was an environment variable nobody had reason to know about.
293
+ */
294
+ it("devLocal compose publishes the database on loopback", () => {
295
+ const compose = renderSelfHostCompose(baseConfig, process.cwd(), { devLocal: true })
296
+ expect(compose).toContain("127.0.0.1:${SUPATYPE_DEV_DB_PORT:-54329}:5432")
297
+ })
298
+
299
+ /** Loopback, so it is reachable from here and from nowhere else. */
300
+ it("devLocal never publishes the database beyond this machine", () => {
301
+ const compose = renderSelfHostCompose(baseConfig, process.cwd(), { devLocal: true })
302
+ const dbPorts = compose
303
+ .split("\n")
304
+ .filter((line) => line.includes(":5432"))
305
+ .filter((line) => line.trim().startsWith("-"))
306
+ expect(dbPorts.length).toBeGreaterThan(0)
307
+ for (const line of dbPorts) expect(line).toContain("127.0.0.1:")
308
+ })
309
+
310
+ it("an engine override no longer decides whether the database is published", () => {
311
+ const withOverride = renderSelfHostCompose(
289
312
  { ...baseConfig, overrides: { engine: "/tmp/supatype-engine" } },
290
313
  process.cwd(),
291
314
  { devLocal: true },
292
315
  )
293
- expect(compose).toContain("127.0.0.1:${SUPATYPE_DEV_DB_PORT:-54329}:5432")
294
- expect(compose).not.toContain('"5432:5432"')
316
+ const without = renderSelfHostCompose(baseConfig, process.cwd(), { devLocal: true })
317
+ expect(withOverride).toContain("127.0.0.1:${SUPATYPE_DEV_DB_PORT:-54329}:5432")
318
+ expect(without).toContain("127.0.0.1:${SUPATYPE_DEV_DB_PORT:-54329}:5432")
319
+ expect(withOverride).not.toContain('"5432:5432"')
295
320
  })
296
321
 
297
322
  it("devLocal proxy upstream rewrites localhost to host.docker.internal", () => {
@@ -0,0 +1,158 @@
1
+ /**
2
+ * Which database a seed run targets, and what it says when it cannot tell.
3
+ *
4
+ * The error is the reason this is a file of its own. The old seed template carried a
5
+ * hardcoded fallback DSN, so a project with no `.env` did not fail: it tried
6
+ * `localhost:5432` with a guessed password and reported a connection error naming none of
7
+ * the things it had looked at. Someone reading that cannot tell "I have not set
8
+ * DATABASE_URL" from "the stack is not running", which is the difference between one
9
+ * command and half an hour.
10
+ */
11
+
12
+ import { afterEach, beforeEach, describe, expect, it } from "vitest"
13
+ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"
14
+ import { tmpdir } from "node:os"
15
+ import { join } from "node:path"
16
+ import { DsnNotFound, redact, resolveSeedDsn } from "../src/seed-connection.js"
17
+ import type { SupatypeProjectConfig } from "../src/project-config.js"
18
+
19
+ const SECRET = "postgres://user:s3cret@db.example.test:5432/app"
20
+
21
+ function config(overrides: Partial<SupatypeProjectConfig> = {}): SupatypeProjectConfig {
22
+ return {
23
+ project: { name: "launch-test" },
24
+ database: { provider: "docker" },
25
+ ...overrides,
26
+ } as SupatypeProjectConfig
27
+ }
28
+
29
+ let cwd: string
30
+ let previousDatabaseUrl: string | undefined
31
+
32
+ beforeEach(() => {
33
+ cwd = mkdtempSync(join(tmpdir(), "supatype-dsn-"))
34
+ mkdirSync(cwd, { recursive: true })
35
+ previousDatabaseUrl = process.env["DATABASE_URL"]
36
+ delete process.env["DATABASE_URL"]
37
+ })
38
+
39
+ afterEach(() => {
40
+ if (previousDatabaseUrl === undefined) delete process.env["DATABASE_URL"]
41
+ else process.env["DATABASE_URL"] = previousDatabaseUrl
42
+ rmSync(cwd, { recursive: true, force: true })
43
+ })
44
+
45
+ describe("where the connection string comes from", () => {
46
+ it("takes --connection first, because it was typed just now", () => {
47
+ process.env["DATABASE_URL"] = "postgres://env/one"
48
+ const resolved = resolveSeedDsn(cwd, config(), { connection: SECRET })
49
+ expect(resolved.dsn).toBe(SECRET)
50
+ expect(resolved.source).toBe("--connection")
51
+ })
52
+
53
+ /**
54
+ * A stated external database is the whole stack's database. Seeding somewhere else while
55
+ * the services read from here would look exactly like data loss, so it outranks the rest.
56
+ */
57
+ it("prefers a declared external database over DATABASE_URL", () => {
58
+ process.env["DATABASE_URL"] = "postgres://env/one"
59
+ const resolved = resolveSeedDsn(
60
+ cwd,
61
+ // `provider` is omitted when `external` is set: there is no backend to choose.
62
+ config({ database: { external: { url: SECRET } } } as Partial<SupatypeProjectConfig>),
63
+ )
64
+ expect(resolved.dsn).toBe(SECRET)
65
+ })
66
+
67
+ it("takes `connection` from the config before the environment", () => {
68
+ process.env["DATABASE_URL"] = "postgres://env/one"
69
+ const resolved = resolveSeedDsn(cwd, config({ connection: SECRET } as Partial<SupatypeProjectConfig>))
70
+ expect(resolved.dsn).toBe(SECRET)
71
+ })
72
+
73
+ it("takes DATABASE_URL from the environment", () => {
74
+ process.env["DATABASE_URL"] = SECRET
75
+ const resolved = resolveSeedDsn(cwd, config())
76
+ expect(resolved.dsn).toBe(SECRET)
77
+ expect(resolved.source).toBe("DATABASE_URL in the environment")
78
+ })
79
+
80
+ it("falls back to the project's .env", () => {
81
+ writeFileSync(join(cwd, ".env"), `DATABASE_URL=${SECRET}\n`, "utf8")
82
+ const resolved = resolveSeedDsn(cwd, config())
83
+ expect(resolved.dsn).toBe(SECRET)
84
+ expect(resolved.source).toContain(".env")
85
+ })
86
+
87
+ /** The usual rule, and the one Compose follows for the same names. */
88
+ it("lets a real environment variable beat the file", () => {
89
+ writeFileSync(join(cwd, ".env"), "DATABASE_URL=postgres://file/one\n", "utf8")
90
+ process.env["DATABASE_URL"] = "postgres://env/two"
91
+ expect(resolveSeedDsn(cwd, config()).dsn).toBe("postgres://env/two")
92
+ })
93
+
94
+ it("derives one for a project whose database it manages", () => {
95
+ const resolved = resolveSeedDsn(cwd, config(), { allowDerived: true })
96
+ expect(resolved.dsn).toContain("launch-test")
97
+ expect(resolved.source).toBe("the project's own local database")
98
+ })
99
+ })
100
+
101
+ describe("when nothing names a database", () => {
102
+ it("refuses rather than guessing", () => {
103
+ expect(() => resolveSeedDsn(cwd, config())).toThrow(DsnNotFound)
104
+ })
105
+
106
+ it("names every source it tried, and what was at each", () => {
107
+ let message = ""
108
+ try {
109
+ resolveSeedDsn(cwd, config())
110
+ } catch (e) {
111
+ message = e instanceof Error ? e.message : String(e)
112
+ }
113
+
114
+ for (const source of [
115
+ "--connection",
116
+ "database.external.url in supatype.config.ts",
117
+ "connection in supatype.config.ts",
118
+ "DATABASE_URL in the environment",
119
+ ".env",
120
+ ]) {
121
+ expect(message).toContain(source)
122
+ }
123
+ expect(message).toContain("no .env file")
124
+ })
125
+
126
+ it("distinguishes a missing .env from one without the key", () => {
127
+ writeFileSync(join(cwd, ".env"), "SOMETHING_ELSE=1\n", "utf8")
128
+ let message = ""
129
+ try {
130
+ resolveSeedDsn(cwd, config())
131
+ } catch (e) {
132
+ message = e instanceof Error ? e.message : String(e)
133
+ }
134
+ expect(message).toContain("the file has no DATABASE_URL")
135
+ })
136
+ })
137
+
138
+ describe("what reaches the terminal", () => {
139
+ /**
140
+ * Every one of these ends up in a terminal and often in a CI log. Printing a DSN to
141
+ * explain where it came from should not be how a password ends up somewhere it is kept.
142
+ */
143
+ it("removes the password", () => {
144
+ expect(redact(SECRET)).toBe("postgres://user:***@db.example.test:5432/app")
145
+ expect(redact(SECRET)).not.toContain("s3cret")
146
+ })
147
+
148
+ it("leaves a connection string with no password alone", () => {
149
+ expect(redact("postgres://db.example.test:5432/app")).toBe("postgres://db.example.test:5432/app")
150
+ })
151
+
152
+ it("does not print the password while explaining where it looked", () => {
153
+ process.env["DATABASE_URL"] = SECRET
154
+ const resolved = resolveSeedDsn(cwd, config())
155
+ const printed = resolved.tried.map((source) => source.detail).join("\n")
156
+ expect(printed).not.toContain("s3cret")
157
+ })
158
+ })