Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions apps/api/src/app/api/electric/[...path]/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { db } from "@superset/db/client";
import {
agentCommands,
apikeys,
devicePresence,
integrationConnections,
invitations,
Expand Down Expand Up @@ -109,8 +108,10 @@ export async function buildWhereClause(
case "agent_commands":
return build(agentCommands, agentCommands.organizationId, organizationId);

case "auth.apikeys":
return build(apikeys, apikeys.userId, userId);
case "auth.apikeys": {
const fragment = `"metadata"::jsonb->>'organizationId' = $1`;
return { fragment, params: [organizationId] };
}

case "integration_connections":
return build(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ import { z } from "zod";
const columnMapper = snakeCamelMapper();
const electricUrl = `${env.NEXT_PUBLIC_API_URL}/api/electric/v1/shape`;

const apiKeyDisplaySchema = z.object({
id: z.string(),
name: z.string().nullable(),
start: z.string().nullable(),
createdAt: z.coerce.date(),
lastRequest: z.coerce.date().nullable(),
});

type ApiKeyDisplay = z.infer<typeof apiKeyDisplaySchema>;

interface OrgCollections {
tasks: Collection<SelectTask>;
taskStatuses: Collection<SelectTaskStatus>;
Expand All @@ -36,6 +46,7 @@ interface OrgCollections {
devicePresence: Collection<SelectDevicePresence>;
integrationConnections: Collection<SelectIntegrationConnection>;
subscriptions: Collection<SelectSubscription>;
apiKeys: Collection<ApiKeyDisplay>;
}

// Per-org collections cache
Expand Down Expand Up @@ -73,34 +84,6 @@ const organizationsCollection = createCollection(
}),
);

const apiKeyDisplaySchema = z.object({
id: z.string(),
name: z.string().nullable(),
start: z.string().nullable(),
createdAt: z.coerce.date(),
lastRequest: z.coerce.date().nullable(),
});

type ApiKeyDisplay = z.infer<typeof apiKeyDisplaySchema>;

const apiKeysCollection = createCollection(
electricCollectionOptions<ApiKeyDisplay>({
id: "apikeys",
shapeOptions: {
url: electricUrl,
params: { table: "auth.apikeys" },
headers: {
Authorization: () => {
const token = getAuthToken();
return token ? `Bearer ${token}` : "";
},
},
columnMapper,
},
getKey: (item) => item.id,
}),
);

function createOrgCollections(organizationId: string): OrgCollections {
const headers = {
Authorization: () => {
Expand Down Expand Up @@ -313,6 +296,22 @@ function createOrgCollections(organizationId: string): OrgCollections {
}),
);

const apiKeys = createCollection(
electricCollectionOptions<ApiKeyDisplay>({
id: `apikeys-${organizationId}`,
shapeOptions: {
url: electricUrl,
params: {
table: "auth.apikeys",
organizationId,
},
headers,
columnMapper,
},
getKey: (item) => item.id,
}),
);

return {
tasks,
taskStatuses,
Expand All @@ -324,6 +323,7 @@ function createOrgCollections(organizationId: string): OrgCollections {
devicePresence,
integrationConnections,
subscriptions,
apiKeys,
};
}

Expand All @@ -335,8 +335,7 @@ function createOrgCollections(organizationId: string): OrgCollections {
export async function preloadCollections(
organizationId: string,
): Promise<void> {
const { organizations, apiKeys, ...orgCollections } =
getCollections(organizationId);
const { organizations, ...orgCollections } = getCollections(organizationId);
await Promise.allSettled(
Object.values(orgCollections).map((c) =>
(c as Collection<object>).preload(),
Expand All @@ -363,6 +362,5 @@ export function getCollections(organizationId: string) {
return {
...orgCollections,
organizations: organizationsCollection,
apiKeys: apiKeysCollection,
};
}
Loading