@codelitdev/platform-cli 0.1.0-alpha.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.
- package/LICENSE +21 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +43 -0
- package/dist/codemod-readme-title.d.ts +5 -0
- package/dist/codemod-readme-title.js +21 -0
- package/dist/create.d.ts +6 -0
- package/dist/create.js +124 -0
- package/dist/doctor.d.ts +12 -0
- package/dist/doctor.js +111 -0
- package/dist/fs.d.ts +5 -0
- package/dist/fs.js +42 -0
- package/dist/git.d.ts +1 -0
- package/dist/git.js +19 -0
- package/dist/hash.d.ts +2 -0
- package/dist/hash.js +8 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/manifest.d.ts +12 -0
- package/dist/manifest.js +31 -0
- package/dist/template.d.ts +2 -0
- package/dist/template.js +23 -0
- package/dist/upgrade.d.ts +13 -0
- package/dist/upgrade.js +97 -0
- package/package.json +41 -0
- package/template/.github/workflows/platform-conformance.yml +28 -0
- package/template/README.md +5 -0
- package/template/apps/api/.env.example +18 -0
- package/template/apps/api/README.md +3 -0
- package/template/apps/api/billing.config.ts +25 -0
- package/template/apps/api/drizzle.config.ts +7 -0
- package/template/apps/api/package.json +53 -0
- package/template/apps/api/scripts/generate-auth-schema.mjs +173 -0
- package/template/apps/api/scripts/migrate.mjs +15 -0
- package/template/apps/api/src/app.test.ts +479 -0
- package/template/apps/api/src/auth/api-keys.ts +131 -0
- package/template/apps/api/src/auth/authenticate.ts +166 -0
- package/template/apps/api/src/auth/better-auth.ts +38 -0
- package/template/apps/api/src/auth/oauth.ts +9 -0
- package/template/apps/api/src/auth/options.ts +69 -0
- package/template/apps/api/src/billing.ts +58 -0
- package/template/apps/api/src/conformance-adapter.ts +335 -0
- package/template/apps/api/src/db/migrate.ts +26 -0
- package/template/apps/api/src/db/migrations/0000_application.sql +72 -0
- package/template/apps/api/src/db/migrations/0000_auth.sql +178 -0
- package/template/apps/api/src/db/migrations/0001_billing.sql +234 -0
- package/template/apps/api/src/db/schema/auth.generated.ts +185 -0
- package/template/apps/api/src/db/schema/billing.generated.ts +447 -0
- package/template/apps/api/src/db/schema/index.ts +3 -0
- package/template/apps/api/src/db/schema/notes.ts +31 -0
- package/template/apps/api/src/db/schema/tenants.ts +83 -0
- package/template/apps/api/src/deps.ts +12 -0
- package/template/apps/api/src/dispatch.ts +476 -0
- package/template/apps/api/src/express-app.ts +161 -0
- package/template/apps/api/src/invitations.ts +217 -0
- package/template/apps/api/src/logger.ts +26 -0
- package/template/apps/api/src/main.ts +98 -0
- package/template/apps/api/src/mcp.ts +127 -0
- package/template/apps/api/src/notes.ts +251 -0
- package/template/apps/api/src/openapi.ts +12 -0
- package/template/apps/api/src/permissions.ts +35 -0
- package/template/apps/api/src/runtime.ts +167 -0
- package/template/apps/api/src/seed.ts +340 -0
- package/template/apps/api/src/tenancy.ts +116 -0
- package/template/apps/api/src/types.ts +20 -0
- package/template/apps/api/src/worker.ts +57 -0
- package/template/apps/api/tsconfig.json +14 -0
- package/template/apps/web/.env.example +7 -0
- package/template/apps/web/app/api/[...path]/route.ts +51 -0
- package/template/apps/web/app/globals.css +156 -0
- package/template/apps/web/app/layout.tsx +34 -0
- package/template/apps/web/app/login/page.tsx +127 -0
- package/template/apps/web/app/notes/page.tsx +93 -0
- package/template/apps/web/app/page.tsx +80 -0
- package/template/apps/web/components/auth-gate.tsx +34 -0
- package/template/apps/web/components/browser-observability.tsx +53 -0
- package/template/apps/web/components/tenant-switcher.tsx +51 -0
- package/template/apps/web/lib/observability-config.ts +15 -0
- package/template/apps/web/next-env.d.ts +2 -0
- package/template/apps/web/next.config.ts +7 -0
- package/template/apps/web/package.json +26 -0
- package/template/apps/web/tsconfig.json +21 -0
- package/template/biome.template.json +50 -0
- package/template/docker-compose.yml +14 -0
- package/template/package.json +40 -0
- package/template/packages/api-contract/package.json +23 -0
- package/template/packages/api-contract/src/index.ts +256 -0
- package/template/packages/api-contract/src/mcp-parity.ts +158 -0
- package/template/packages/api-contract/tsconfig.json +11 -0
- package/template/patches/@anatine__zod-openapi@1.14.2.patch +14 -0
- package/template/tooling/platform/config.ts +2 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Clock,
|
|
3
|
+
createPlatformError,
|
|
4
|
+
createPublicId,
|
|
5
|
+
type PlatformError,
|
|
6
|
+
type PlatformRequestContext,
|
|
7
|
+
serializeDate,
|
|
8
|
+
uuidv7,
|
|
9
|
+
} from "@codelitdev/platform";
|
|
10
|
+
import { and, eq, sql } from "drizzle-orm";
|
|
11
|
+
import * as schema from "./db/schema/index.js";
|
|
12
|
+
import type { ReferencePermission } from "./permissions.js";
|
|
13
|
+
import type { AppDb } from "./types.js";
|
|
14
|
+
|
|
15
|
+
export type NoteDto = {
|
|
16
|
+
id: string;
|
|
17
|
+
tenantId: string;
|
|
18
|
+
title: string;
|
|
19
|
+
body: string;
|
|
20
|
+
createdAt: string;
|
|
21
|
+
updatedAt: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type Ctx = PlatformRequestContext<string, string, ReferencePermission>;
|
|
25
|
+
|
|
26
|
+
function toDto(row: typeof schema.notes.$inferSelect, publicTenantId: string): NoteDto {
|
|
27
|
+
return {
|
|
28
|
+
id: row.publicId,
|
|
29
|
+
tenantId: publicTenantId,
|
|
30
|
+
title: row.title,
|
|
31
|
+
body: row.body,
|
|
32
|
+
createdAt: serializeDate(row.createdAt),
|
|
33
|
+
updatedAt: serializeDate(row.updatedAt),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function listNotes(
|
|
38
|
+
db: AppDb,
|
|
39
|
+
ctx: Ctx,
|
|
40
|
+
publicTenantId: string,
|
|
41
|
+
): Promise<{ ok: true; value: NoteDto[] } | { ok: false; error: PlatformError }> {
|
|
42
|
+
if (!ctx.permissions.has("notes:read")) {
|
|
43
|
+
return { ok: false, error: createPlatformError("forbidden") };
|
|
44
|
+
}
|
|
45
|
+
const rows = await db
|
|
46
|
+
.select()
|
|
47
|
+
.from(schema.notes)
|
|
48
|
+
.where(eq(schema.notes.tenantId, ctx.tenantId!));
|
|
49
|
+
return { ok: true, value: rows.map((row) => toDto(row, publicTenantId)) };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function createNote(
|
|
53
|
+
db: AppDb,
|
|
54
|
+
ctx: Ctx,
|
|
55
|
+
publicTenantId: string,
|
|
56
|
+
input: { title: string; body: string },
|
|
57
|
+
clock: Clock,
|
|
58
|
+
): Promise<{ ok: true; value: NoteDto } | { ok: false; error: PlatformError }> {
|
|
59
|
+
if (!ctx.permissions.has("notes:write")) {
|
|
60
|
+
return { ok: false, error: createPlatformError("forbidden") };
|
|
61
|
+
}
|
|
62
|
+
const now = clock.now();
|
|
63
|
+
const row = {
|
|
64
|
+
id: uuidv7(clock),
|
|
65
|
+
publicId: createPublicId("note", clock),
|
|
66
|
+
tenantId: ctx.tenantId!,
|
|
67
|
+
title: input.title,
|
|
68
|
+
body: input.body,
|
|
69
|
+
createdBy: ctx.principalId,
|
|
70
|
+
createdAt: now,
|
|
71
|
+
updatedAt: now,
|
|
72
|
+
};
|
|
73
|
+
await db.transaction(async (tx) => {
|
|
74
|
+
await tx.insert(schema.notes).values(row);
|
|
75
|
+
await tx.insert(schema.auditEvents).values({
|
|
76
|
+
id: uuidv7(clock),
|
|
77
|
+
tenantId: ctx.tenantId,
|
|
78
|
+
actorId: ctx.principalId,
|
|
79
|
+
action: "note.created",
|
|
80
|
+
resourceType: "note",
|
|
81
|
+
resourceId: row.publicId,
|
|
82
|
+
requestId: ctx.requestId,
|
|
83
|
+
createdAt: now,
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
return { ok: true, value: toDto(row, publicTenantId) };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export async function updateNote(
|
|
90
|
+
db: AppDb,
|
|
91
|
+
ctx: Ctx,
|
|
92
|
+
publicTenantId: string,
|
|
93
|
+
notePublicId: string,
|
|
94
|
+
input: { title?: string; body?: string },
|
|
95
|
+
clock: Clock,
|
|
96
|
+
): Promise<{ ok: true; value: NoteDto } | { ok: false; error: PlatformError }> {
|
|
97
|
+
if (!ctx.permissions.has("notes:write")) {
|
|
98
|
+
return { ok: false, error: createPlatformError("forbidden") };
|
|
99
|
+
}
|
|
100
|
+
return db.transaction(async (tx) => {
|
|
101
|
+
const existing = await tx
|
|
102
|
+
.select()
|
|
103
|
+
.from(schema.notes)
|
|
104
|
+
.where(
|
|
105
|
+
and(
|
|
106
|
+
eq(schema.notes.publicId, notePublicId),
|
|
107
|
+
eq(schema.notes.tenantId, ctx.tenantId!),
|
|
108
|
+
),
|
|
109
|
+
)
|
|
110
|
+
.limit(1);
|
|
111
|
+
const row = existing[0];
|
|
112
|
+
if (!row) {
|
|
113
|
+
return { ok: false as const, error: createPlatformError("not_found") };
|
|
114
|
+
}
|
|
115
|
+
const now = clock.now();
|
|
116
|
+
const next = {
|
|
117
|
+
title: input.title ?? row.title,
|
|
118
|
+
body: input.body ?? row.body,
|
|
119
|
+
updatedAt: now,
|
|
120
|
+
};
|
|
121
|
+
await tx.update(schema.notes).set(next).where(eq(schema.notes.id, row.id));
|
|
122
|
+
await tx.insert(schema.auditEvents).values({
|
|
123
|
+
id: uuidv7(clock),
|
|
124
|
+
tenantId: ctx.tenantId,
|
|
125
|
+
actorId: ctx.principalId,
|
|
126
|
+
action: "note.updated",
|
|
127
|
+
resourceType: "note",
|
|
128
|
+
resourceId: row.publicId,
|
|
129
|
+
requestId: ctx.requestId,
|
|
130
|
+
createdAt: now,
|
|
131
|
+
});
|
|
132
|
+
return {
|
|
133
|
+
ok: true as const,
|
|
134
|
+
value: toDto({ ...row, ...next }, publicTenantId),
|
|
135
|
+
};
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export async function deleteNote(
|
|
140
|
+
db: AppDb,
|
|
141
|
+
ctx: Ctx,
|
|
142
|
+
notePublicId: string,
|
|
143
|
+
clock: Clock,
|
|
144
|
+
): Promise<{ ok: true; value: { id: string } } | { ok: false; error: PlatformError }> {
|
|
145
|
+
if (!ctx.permissions.has("notes:delete")) {
|
|
146
|
+
return { ok: false, error: createPlatformError("forbidden") };
|
|
147
|
+
}
|
|
148
|
+
return db.transaction(async (tx) => {
|
|
149
|
+
const existing = await tx
|
|
150
|
+
.select()
|
|
151
|
+
.from(schema.notes)
|
|
152
|
+
.where(
|
|
153
|
+
and(
|
|
154
|
+
eq(schema.notes.publicId, notePublicId),
|
|
155
|
+
eq(schema.notes.tenantId, ctx.tenantId!),
|
|
156
|
+
),
|
|
157
|
+
)
|
|
158
|
+
.limit(1);
|
|
159
|
+
const row = existing[0];
|
|
160
|
+
if (!row) {
|
|
161
|
+
return { ok: false as const, error: createPlatformError("not_found") };
|
|
162
|
+
}
|
|
163
|
+
await tx.delete(schema.notes).where(eq(schema.notes.id, row.id));
|
|
164
|
+
await tx.insert(schema.auditEvents).values({
|
|
165
|
+
id: uuidv7(clock),
|
|
166
|
+
tenantId: ctx.tenantId,
|
|
167
|
+
actorId: ctx.principalId,
|
|
168
|
+
action: "note.deleted",
|
|
169
|
+
resourceType: "note",
|
|
170
|
+
resourceId: row.publicId,
|
|
171
|
+
requestId: ctx.requestId,
|
|
172
|
+
createdAt: clock.now(),
|
|
173
|
+
});
|
|
174
|
+
return { ok: true as const, value: { id: row.publicId } };
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export async function countOwners(db: AppDb, tenantId: string): Promise<number> {
|
|
179
|
+
const rows = await db
|
|
180
|
+
.select()
|
|
181
|
+
.from(schema.memberships)
|
|
182
|
+
.where(
|
|
183
|
+
and(
|
|
184
|
+
eq(schema.memberships.tenantId, tenantId),
|
|
185
|
+
eq(schema.memberships.isOwner, true),
|
|
186
|
+
),
|
|
187
|
+
);
|
|
188
|
+
return rows.length;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export async function removeMember(
|
|
192
|
+
db: AppDb,
|
|
193
|
+
ctx: Ctx,
|
|
194
|
+
memberUserId: string,
|
|
195
|
+
clock: Clock,
|
|
196
|
+
): Promise<{ ok: true } | { ok: false; error: PlatformError }> {
|
|
197
|
+
if (!ctx.permissions.has("tenant:admin") || !ctx.tenantId) {
|
|
198
|
+
return { ok: false, error: createPlatformError("forbidden") };
|
|
199
|
+
}
|
|
200
|
+
return db.transaction(async (tx) => {
|
|
201
|
+
// Every membership removal for a tenant locks the tenant row first.
|
|
202
|
+
// PostgreSQL serializes these transactions, so two concurrent owner
|
|
203
|
+
// removals cannot both observe two owners and delete the last pair.
|
|
204
|
+
await tx.execute(sql`SELECT id FROM tenants WHERE id = ${ctx.tenantId} FOR UPDATE`);
|
|
205
|
+
const rows = await tx
|
|
206
|
+
.select()
|
|
207
|
+
.from(schema.memberships)
|
|
208
|
+
.where(
|
|
209
|
+
and(
|
|
210
|
+
eq(schema.memberships.tenantId, ctx.tenantId!),
|
|
211
|
+
eq(schema.memberships.userId, memberUserId),
|
|
212
|
+
),
|
|
213
|
+
)
|
|
214
|
+
.limit(1);
|
|
215
|
+
const member = rows[0];
|
|
216
|
+
if (!member) {
|
|
217
|
+
return { ok: false as const, error: createPlatformError("not_found") };
|
|
218
|
+
}
|
|
219
|
+
if (member.isOwner) {
|
|
220
|
+
const owners = await tx
|
|
221
|
+
.select()
|
|
222
|
+
.from(schema.memberships)
|
|
223
|
+
.where(
|
|
224
|
+
and(
|
|
225
|
+
eq(schema.memberships.tenantId, ctx.tenantId!),
|
|
226
|
+
eq(schema.memberships.isOwner, true),
|
|
227
|
+
),
|
|
228
|
+
);
|
|
229
|
+
if (owners.length <= 1) {
|
|
230
|
+
return {
|
|
231
|
+
ok: false as const,
|
|
232
|
+
error: createPlatformError("conflict", {
|
|
233
|
+
safeDetails: { reason: "last_owner" },
|
|
234
|
+
}),
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
await tx.delete(schema.memberships).where(eq(schema.memberships.id, member.id));
|
|
239
|
+
await tx.insert(schema.auditEvents).values({
|
|
240
|
+
id: uuidv7(clock),
|
|
241
|
+
tenantId: ctx.tenantId,
|
|
242
|
+
actorId: ctx.principalId,
|
|
243
|
+
action: "membership.removed",
|
|
244
|
+
resourceType: "membership",
|
|
245
|
+
resourceId: memberUserId,
|
|
246
|
+
requestId: ctx.requestId,
|
|
247
|
+
createdAt: clock.now(),
|
|
248
|
+
});
|
|
249
|
+
return { ok: true as const };
|
|
250
|
+
});
|
|
251
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { contract } from "@__PRODUCT_SLUG__/api-contract";
|
|
2
|
+
import { generateOpenApi } from "@ts-rest/open-api";
|
|
3
|
+
|
|
4
|
+
export function createOpenApiDocument(publicApiUrl: string) {
|
|
5
|
+
return generateOpenApi(contract, {
|
|
6
|
+
info: {
|
|
7
|
+
title: "__PRODUCT_NAME__ API",
|
|
8
|
+
version: "1.0.0",
|
|
9
|
+
},
|
|
10
|
+
servers: [{ url: publicApiUrl }],
|
|
11
|
+
});
|
|
12
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export const REFERENCE_PERMISSIONS = [
|
|
2
|
+
"notes:read",
|
|
3
|
+
"notes:write",
|
|
4
|
+
"notes:delete",
|
|
5
|
+
"tenant:admin",
|
|
6
|
+
"billing:read",
|
|
7
|
+
] as const;
|
|
8
|
+
|
|
9
|
+
export type ReferencePermission = (typeof REFERENCE_PERMISSIONS)[number];
|
|
10
|
+
|
|
11
|
+
export const OWNER_PERMISSIONS: readonly ReferencePermission[] = [
|
|
12
|
+
...REFERENCE_PERMISSIONS,
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
export const MEMBER_PERMISSIONS: readonly ReferencePermission[] = [
|
|
16
|
+
"notes:read",
|
|
17
|
+
"notes:write",
|
|
18
|
+
"billing:read",
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
export function parsePermissions(value: string): Set<ReferencePermission> {
|
|
22
|
+
const allowed = new Set<string>(REFERENCE_PERMISSIONS);
|
|
23
|
+
const parsed = new Set<ReferencePermission>();
|
|
24
|
+
for (const item of value.split(",")) {
|
|
25
|
+
const trimmed = item.trim();
|
|
26
|
+
if (allowed.has(trimmed)) parsed.add(trimmed as ReferencePermission);
|
|
27
|
+
}
|
|
28
|
+
return parsed;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function serializePermissions(
|
|
32
|
+
permissions: Iterable<ReferencePermission>,
|
|
33
|
+
): string {
|
|
34
|
+
return [...permissions].join(",");
|
|
35
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import http from "node:http";
|
|
2
|
+
import { createObservability, type Observability } from "@codelitdev/observability";
|
|
3
|
+
import { type Clock, frozenClock, systemClock } from "@codelitdev/platform";
|
|
4
|
+
import { PGlite } from "@electric-sql/pglite";
|
|
5
|
+
import { toNodeHandler } from "better-auth/node";
|
|
6
|
+
import { drizzle as drizzlePostgres } from "drizzle-orm/node-postgres";
|
|
7
|
+
import { drizzle } from "drizzle-orm/pglite";
|
|
8
|
+
import { Pool } from "pg";
|
|
9
|
+
import type { Logger } from "pino";
|
|
10
|
+
import { createReferenceAuth } from "./auth/better-auth.js";
|
|
11
|
+
import { type BillingBundle, composeBilling } from "./billing.js";
|
|
12
|
+
import { applyMigrations } from "./db/migrate.js";
|
|
13
|
+
import * as billingSchema from "./db/schema/billing.generated.js";
|
|
14
|
+
import * as schema from "./db/schema/index.js";
|
|
15
|
+
import type { DispatchDeps } from "./deps.js";
|
|
16
|
+
import type { AppDb } from "./types.js";
|
|
17
|
+
|
|
18
|
+
export type Runtime = DispatchDeps & {
|
|
19
|
+
client: PGlite | Pool;
|
|
20
|
+
logger: Logger;
|
|
21
|
+
authServer: http.Server | null;
|
|
22
|
+
close(): Promise<void>;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export async function createPgliteRuntime(options: {
|
|
26
|
+
clock?: Clock;
|
|
27
|
+
apiKeyPepper?: string;
|
|
28
|
+
serviceName?: string;
|
|
29
|
+
publicApiUrl?: string;
|
|
30
|
+
webOrigin?: string;
|
|
31
|
+
logger?: Logger;
|
|
32
|
+
observability?: Observability;
|
|
33
|
+
authSecret?: string;
|
|
34
|
+
}): Promise<Runtime> {
|
|
35
|
+
const client = new PGlite();
|
|
36
|
+
await applyMigrations((sql) => client.exec(sql));
|
|
37
|
+
const db = drizzle(client, {
|
|
38
|
+
schema: { ...schema, ...billingSchema },
|
|
39
|
+
}) as AppDb;
|
|
40
|
+
const clock = options.clock ?? systemClock;
|
|
41
|
+
const billing = composeBilling(db, clock);
|
|
42
|
+
let handler: ReturnType<typeof toNodeHandler> | undefined;
|
|
43
|
+
let authServer: http.Server | null = null;
|
|
44
|
+
let publicApiUrl = options.publicApiUrl;
|
|
45
|
+
if (!publicApiUrl) {
|
|
46
|
+
authServer = http.createServer((req, res) => {
|
|
47
|
+
if (!handler) {
|
|
48
|
+
res.statusCode = 503;
|
|
49
|
+
res.end();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
void handler(req, res);
|
|
53
|
+
});
|
|
54
|
+
await new Promise<void>((resolve) => {
|
|
55
|
+
authServer!.listen(0, "127.0.0.1", () => resolve());
|
|
56
|
+
});
|
|
57
|
+
const address = authServer.address();
|
|
58
|
+
if (!address || typeof address === "string") {
|
|
59
|
+
throw new Error("auth_listen_failed");
|
|
60
|
+
}
|
|
61
|
+
publicApiUrl = `http://127.0.0.1:${address.port}`;
|
|
62
|
+
}
|
|
63
|
+
const auth = createReferenceAuth({
|
|
64
|
+
db,
|
|
65
|
+
publicApiUrl,
|
|
66
|
+
webOrigin: options.webOrigin,
|
|
67
|
+
secret: options.authSecret ?? "test-secret-that-is-at-least-thirty-two-characters",
|
|
68
|
+
});
|
|
69
|
+
handler = toNodeHandler(auth.auth);
|
|
70
|
+
const importTables = await client.query(
|
|
71
|
+
"select tablename from pg_tables where schemaname = 'public'",
|
|
72
|
+
);
|
|
73
|
+
if (importTables.rows.length === 0) {
|
|
74
|
+
throw new Error("migrations_did_not_create_tables");
|
|
75
|
+
}
|
|
76
|
+
const observability =
|
|
77
|
+
options.observability ??
|
|
78
|
+
createObservability({
|
|
79
|
+
serviceName: options.serviceName ?? "__PRODUCT_SLUG__-api",
|
|
80
|
+
environment: "test",
|
|
81
|
+
logs: { level: "silent" },
|
|
82
|
+
contextPolicy: {
|
|
83
|
+
propertyAllowlist: new Set(["path", "method", "job_id"]),
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
return {
|
|
87
|
+
client,
|
|
88
|
+
db,
|
|
89
|
+
clock,
|
|
90
|
+
apiKeyPepper: options.apiKeyPepper ?? "test-pepper-please-rotate",
|
|
91
|
+
billing,
|
|
92
|
+
serviceName: options.serviceName ?? "__PRODUCT_SLUG__-api",
|
|
93
|
+
databaseReady: true,
|
|
94
|
+
logger: options.logger ?? observability.logger,
|
|
95
|
+
observability,
|
|
96
|
+
auth,
|
|
97
|
+
authServer,
|
|
98
|
+
async close() {
|
|
99
|
+
await new Promise<void>((resolve) => {
|
|
100
|
+
if (!authServer) {
|
|
101
|
+
resolve();
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
authServer.close(() => resolve());
|
|
105
|
+
});
|
|
106
|
+
await client.close();
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Production runtime; migrations are applied by the application deployment. */
|
|
112
|
+
export async function createPostgresRuntime(options: {
|
|
113
|
+
databaseUrl: string;
|
|
114
|
+
clock?: Clock;
|
|
115
|
+
apiKeyPepper: string;
|
|
116
|
+
serviceName?: string;
|
|
117
|
+
publicApiUrl: string;
|
|
118
|
+
webOrigin?: string;
|
|
119
|
+
logger?: Logger;
|
|
120
|
+
observability?: Observability;
|
|
121
|
+
authSecret: string;
|
|
122
|
+
}): Promise<Runtime> {
|
|
123
|
+
const client = new Pool({ connectionString: options.databaseUrl });
|
|
124
|
+
await client.query("select 1");
|
|
125
|
+
const db = drizzlePostgres(client, {
|
|
126
|
+
schema: { ...schema, ...billingSchema },
|
|
127
|
+
}) as unknown as AppDb;
|
|
128
|
+
const clock = options.clock ?? systemClock;
|
|
129
|
+
const billing = composeBilling(db, clock);
|
|
130
|
+
const auth = createReferenceAuth({
|
|
131
|
+
db,
|
|
132
|
+
publicApiUrl: options.publicApiUrl,
|
|
133
|
+
webOrigin: options.webOrigin,
|
|
134
|
+
secret: options.authSecret,
|
|
135
|
+
});
|
|
136
|
+
const observability =
|
|
137
|
+
options.observability ??
|
|
138
|
+
createObservability({
|
|
139
|
+
serviceName: options.serviceName ?? "reference-api",
|
|
140
|
+
environment: "production",
|
|
141
|
+
contextPolicy: {
|
|
142
|
+
propertyAllowlist: new Set(["path", "method", "job_id"]),
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
return {
|
|
146
|
+
client,
|
|
147
|
+
db,
|
|
148
|
+
clock,
|
|
149
|
+
apiKeyPepper: options.apiKeyPepper,
|
|
150
|
+
billing,
|
|
151
|
+
serviceName: options.serviceName ?? "reference-api",
|
|
152
|
+
databaseReady: true,
|
|
153
|
+
logger: options.logger ?? observability.logger,
|
|
154
|
+
observability,
|
|
155
|
+
auth,
|
|
156
|
+
authServer: null,
|
|
157
|
+
async close() {
|
|
158
|
+
await client.end();
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function freezeRuntimeClock(at: Date): Clock {
|
|
164
|
+
return frozenClock(at);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export type { BillingBundle };
|