Skip to content

Commit 3278d8f

Browse files
feat: super db (#372)
* meta schema * update get-tenants stack * meta -> super
1 parent fd83e3c commit 3278d8f

File tree

22 files changed

+222
-50
lines changed

22 files changed

+222
-50
lines changed

apps/stack/.sst/types/index.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ declare module "sst/node/config" {
1212
}
1313
}
1414
}import "sst/node/config";
15+
declare module "sst/node/config" {
16+
export interface SecretResources {
17+
"SUPER_DATABASE_URL": {
18+
value: string;
19+
}
20+
}
21+
}import "sst/node/config";
22+
declare module "sst/node/config" {
23+
export interface SecretResources {
24+
"SUPER_DATABASE_AUTH_TOKEN": {
25+
value: string;
26+
}
27+
}
28+
}import "sst/node/config";
1529
declare module "sst/node/config" {
1630
export interface SecretResources {
1731
"CLERK_SECRET_KEY": {
@@ -46,13 +60,6 @@ declare module "sst/node/config" {
4660
value: string;
4761
}
4862
}
49-
}import "sst/node/config";
50-
declare module "sst/node/config" {
51-
export interface SecretResources {
52-
"TENANTS": {
53-
value: string;
54-
}
55-
}
5663
}import "sst/node/event-bus";
5764
declare module "sst/node/event-bus" {
5865
export interface EventBusResources {

apps/stack/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@acme/extract-functions": "*",
2323
"@acme/extract-schema": "*",
2424
"@acme/source-control": "*",
25+
"@acme/super-schema": "*",
2526
"@acme/transform-functions": "*",
2627
"@acme/transform-schema": "*",
2728
"@aws-sdk/client-sqs": "^3.470.0",

apps/stack/src/config/crawl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { events } from "@acme/crawl-schema";
33
import type { Context, InsertEventEntities } from "@acme/crawl-functions";
44
import type { EventNamespaceType } from "@acme/crawl-schema/src/events";
55
import { getTenantDb, type OmitDb } from "./get-tenant-db";
6-
import type { Tenant } from "./tenants";
6+
import type { Tenant } from "@acme/super-schema";
77

88
const context: OmitDb<Context<InsertEventEntities>> = {
99
entities: {

apps/stack/src/config/create-event.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { EventBus } from "sst/node/event-bus";
77
import { z } from "zod";
88
import { crawlComplete, crawlFailed } from "./crawl";
99
import type { EventNamespaceType } from "@acme/crawl-schema";
10-
import type { Tenant } from "./tenants";
10+
import type { Tenant } from "@acme/super-schema";
1111

1212
const client = new EventBridgeClient({});
1313
type InferShapeOutput<Shape extends z.ZodRawShape> = z.infer<

apps/stack/src/config/create-message.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { SQSEvent } from "aws-lambda";
77
import { Queue } from 'sst/node/queue'
88
import type { EventNamespaceType } from "@acme/crawl-schema";
99
import { crawlComplete, crawlFailed } from "./crawl";
10-
import type { Tenant } from "./tenants";
10+
import type { Tenant } from "@acme/super-schema";
1111

1212
const sqs = new SQSClient();
1313

apps/stack/src/config/get-tenant-db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createClient } from "@libsql/client";
22
import { drizzle } from "drizzle-orm/libsql";
33
import { Config } from "sst/node/config";
44
import { getTenants } from "./tenants";
5-
import type { Tenant } from "./tenants";
5+
import type { Tenant } from "@acme/super-schema";
66

77
export type OmitDb<T> = Omit<T, 'db'>;
88

apps/stack/src/config/tenants.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
1+
import { createClient } from "@libsql/client";
2+
import { drizzle } from "drizzle-orm/libsql";
13
import { Config } from "sst/node/config";
2-
import { z } from "zod";
4+
import { getTenants as getTenantsFromDb } from "@acme/super-schema";
35

4-
const TenantSchema = z.object({
5-
id: z.number(),
6-
tenant: z.string(),
7-
dbUrl: z.string(),
8-
});
6+
const superDb = drizzle(createClient({ url: Config.SUPER_DATABASE_URL, authToken: Config.SUPER_DATABASE_AUTH_TOKEN }))
97

10-
const TenantArraySchema = z.array(TenantSchema);
8+
const TENANTS = await getTenantsFromDb(superDb);
119

12-
export type Tenant = z.infer<typeof TenantSchema>;
13-
14-
export const getTenants = () => {
15-
const validTenants = TenantArraySchema.safeParse(JSON.parse(Config.TENANTS));
16-
if (!validTenants.success) {
17-
console.error("Invalid Config 'TENANTS' value:", ...validTenants.error.issues);
18-
throw new Error("Invalid Config 'TENANTS' value");
19-
}
20-
21-
return validTenants.data;
22-
}
10+
export const getTenants = () => TENANTS;

apps/stack/src/info/tenants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const handler = ApiHandler(async (_evt) => {
66
return {
77
statusCode: 200,
88
body: JSON.stringify({
9-
tenants: getTenants().map(({tenant}) => tenant),
9+
tenants: getTenants().map(({name}) => ({tenant:name})),
1010
}),
1111
};
1212
});

apps/stack/stacks/ExtractStack.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ export function ExtractStack({ stack }: StackContext) {
1818
const ENV = ENVSchema.parse(process.env);
1919

2020
const TENANT_DATABASE_AUTH_TOKEN = new Config.Secret(stack, "TENANT_DATABASE_AUTH_TOKEN");
21+
const SUPER_DATABASE_URL = new Config.Secret(stack, "SUPER_DATABASE_URL");
22+
const SUPER_DATABASE_AUTH_TOKEN = new Config.Secret(stack, "SUPER_DATABASE_AUTH_TOKEN");
2123
const CLERK_SECRET_KEY = new Config.Secret(stack, "CLERK_SECRET_KEY");
2224
const REDIS_URL = new Config.Secret(stack, "REDIS_URL");
2325
const REDIS_TOKEN = new Config.Secret(stack, "REDIS_TOKEN");
2426
const REDIS_USER_TOKEN_TTL = new Config.Parameter(stack, "REDIS_USER_TOKEN_TTL", { value: (20 * 60).toString() });
2527
const PER_PAGE = new Config.Parameter(stack, "PER_PAGE", { value: (30).toString() });
26-
const TENANTS = new Config.Secret(stack, "TENANTS");
2728

2829
const bus = new EventBus(stack, "ExtractBus", {
2930
rules: {
@@ -61,8 +62,9 @@ export function ExtractStack({ stack }: StackContext) {
6162
retries: 10,
6263
function: {
6364
bind: [
64-
TENANTS,
6565
TENANT_DATABASE_AUTH_TOKEN,
66+
SUPER_DATABASE_AUTH_TOKEN,
67+
SUPER_DATABASE_URL,
6668
CLERK_SECRET_KEY,
6769
REDIS_URL,
6870
REDIS_TOKEN,
@@ -86,8 +88,9 @@ export function ExtractStack({ stack }: StackContext) {
8688
bind: [
8789
bus,
8890
extractQueue,
89-
TENANTS,
9091
TENANT_DATABASE_AUTH_TOKEN,
92+
SUPER_DATABASE_AUTH_TOKEN,
93+
SUPER_DATABASE_URL,
9194
CLERK_SECRET_KEY,
9295
REDIS_URL,
9396
REDIS_TOKEN,
@@ -164,8 +167,9 @@ export function ExtractStack({ stack }: StackContext) {
164167
function: {
165168
bind: [
166169
bus,
167-
TENANTS,
168170
TENANT_DATABASE_AUTH_TOKEN,
171+
SUPER_DATABASE_AUTH_TOKEN,
172+
SUPER_DATABASE_URL,
169173
CLERK_SECRET_KEY,
170174
REDIS_URL,
171175
REDIS_TOKEN,
@@ -200,7 +204,8 @@ export function ExtractStack({ stack }: StackContext) {
200204
},
201205
bind: [
202206
extractQueue,
203-
TENANTS,
207+
SUPER_DATABASE_AUTH_TOKEN,
208+
SUPER_DATABASE_URL,
204209
CLERK_SECRET_KEY,
205210
REDIS_URL,
206211
REDIS_TOKEN,
@@ -218,7 +223,8 @@ export function ExtractStack({ stack }: StackContext) {
218223

219224
return {
220225
ExtractBus: bus,
221-
TENANTS,
222226
TENANT_DATABASE_AUTH_TOKEN,
227+
SUPER_DATABASE_AUTH_TOKEN,
228+
SUPER_DATABASE_URL,
223229
};
224230
}

apps/stack/stacks/InfoStack.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ import { ExtractStack } from "./ExtractStack";
88
export function InfoStack({ stack }: StackContext) {
99

1010
const {
11-
TENANTS,
11+
SUPER_DATABASE_AUTH_TOKEN,
12+
SUPER_DATABASE_URL,
1213
} = use(ExtractStack);
1314

1415

1516
const api = new Api(stack, "InfoApi", {
1617
defaults: {
1718
function: {
18-
bind: [TENANTS]
19+
bind: [
20+
SUPER_DATABASE_AUTH_TOKEN,
21+
SUPER_DATABASE_URL,
22+
]
1923
}
2024
},
2125
routes: {

0 commit comments

Comments
 (0)