UNPKG

@settlemint/sdk-hasura

Version:

Hasura and PostgreSQL integration module for SettleMint SDK, enabling database operations and GraphQL queries

234 lines (231 loc) • 7.29 kB
/* SettleMint Hasura SDK - GraphQL API */ import { extractBaseUrlBeforeSegment } from "@settlemint/sdk-utils"; import { appendHeaders } from "@settlemint/sdk-utils/http"; import { requestLogger } from "@settlemint/sdk-utils/logging"; import { ensureServer } from "@settlemint/sdk-utils/runtime"; import { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from "@settlemint/sdk-utils/validation"; import { initGraphQLTada, readFragment } from "gql.tada"; import { GraphQLClient } from "graphql-request"; import { z } from "zod"; //#region src/utils/track-all-tables.ts /** * Track all tables in a database * * @param databaseName - The name of the database to track tables for * @param client - The client options to use for the Hasura client * @param tableOptions - The options to use for the table tracking * @param tableOptions.includeSchemas - The schemas to include in the tracking * @param tableOptions.excludeSchemas - The schemas to exclude from the tracking * @returns A promise that resolves to an object with a result property indicating success or failure * @example * import { trackAllTables } from "@settlemint/sdk-hasura/utils/track-all-tables"; * * const client = createHasuraMetadataClient({ * instance: "http://localhost:8080", * accessToken: "test", * adminSecret: "test", * }); * * const result = await trackAllTables("default", client, { * excludeSchemas: ["drizzle"], * }); * if (result.result === "success") { * console.log("Tables tracked successfully"); * } else { * console.error("Failed to track tables"); * } */ async function trackAllTables(databaseName, client, tableOptions = { includeSchemas: undefined, excludeSchemas: undefined }) { const messages = []; const { includeSchemas, excludeSchemas } = tableOptions; const getTablesResult = await client({ type: "pg_get_source_tables", args: { source: databaseName } }); if (!getTablesResult.ok) { throw new Error(`Failed to get tables: ${JSON.stringify(getTablesResult.data)}`); } const tables = getTablesResult.data; if (tables.length === 0) { return { result: "no-tables", messages }; } messages.push(`Found ${tables.length} tables in database "${databaseName}"`); await client({ type: "pg_untrack_tables", args: { tables: tables.map((table) => ({ table })), allow_warnings: true } }); const tablesToTrack = tables.filter((table) => { if (Array.isArray(includeSchemas)) { return includeSchemas.includes(table.schema); } if (Array.isArray(excludeSchemas)) { return !excludeSchemas.includes(table.schema); } return true; }); const trackResult = await client({ type: "pg_track_tables", args: { tables: tablesToTrack.map((table) => ({ table })), allow_warnings: true } }); if (!trackResult.ok) { throw new Error(`Failed to track tables: ${JSON.stringify(trackResult.data)}`); } messages.push(`Successfully tracked ${tablesToTrack.length} tables`); return { result: "success", messages }; } //#endregion //#region src/hasura.ts /** * Schema for validating client options for the Hasura client. */ const ClientOptionsSchema = z.object({ instance: UrlOrPathSchema, accessToken: ApplicationAccessTokenSchema.optional(), adminSecret: z.string(), cache: z.enum([ "default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload" ]).optional() }); /** * Creates a Hasura GraphQL client with proper type safety using gql.tada * * @param options - Configuration options for the client * @param clientOptions - Optional GraphQL client configuration options * @param logger - Optional logger to use for logging the requests * @returns An object containing: * - client: The configured GraphQL client instance * - graphql: The initialized gql.tada function for type-safe queries * @throws Will throw an error if the options fail validation against ClientOptionsSchema * @example * import { createHasuraClient } from '@settlemint/sdk-hasura'; * import type { introspection } from "@schemas/hasura-env"; * import { createLogger, requestLogger } from "@settlemint/sdk-utils/logging"; * * const logger = createLogger(); * * const { client, graphql } = createHasuraClient<{ * introspection: introspection; * disableMasking: true; * scalars: { * timestamp: string; * timestampz: string; * uuid: string; * date: string; * time: string; * jsonb: string; * numeric: string; * interval: string; * geometry: string; * geography: string; * }; * }>({ * instance: process.env.SETTLEMINT_HASURA_ENDPOINT, * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN, * adminSecret: process.env.SETTLEMINT_HASURA_ADMIN_SECRET, * }, { * fetch: requestLogger(logger, "hasura", fetch) as typeof fetch, * }); * * // Making GraphQL queries * const query = graphql(` * query GetUsers { * users { * id * name * email * } * } * `); * * const result = await client.request(query); */ function createHasuraClient(options, clientOptions, logger) { ensureServer(); const validatedOptions = validate(ClientOptionsSchema, options); const graphql = initGraphQLTada(); const fullUrl = new URL(validatedOptions.instance).toString(); return { client: new GraphQLClient(fullUrl, { ...clientOptions, headers: appendHeaders(clientOptions?.headers, { "x-auth-token": validatedOptions.accessToken, "x-hasura-admin-secret": validatedOptions.adminSecret }), fetch: logger ? requestLogger(logger, "hasura", fetch) : fetch }), graphql }; } /** * Creates a Hasura Metadata client * * @param options - Configuration options for the client * @param logger - Optional logger to use for logging the requests * @returns A function that can be used to make requests to the Hasura Metadata API * @throws Will throw an error if the options fail validation against ClientOptionsSchema * @example * import { createHasuraMetadataClient } from '@settlemint/sdk-hasura'; * * const client = createHasuraMetadataClient({ * instance: process.env.SETTLEMINT_HASURA_ENDPOINT, * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN, * adminSecret: process.env.SETTLEMINT_HASURA_ADMIN_SECRET, * }); * * const result = await client({ * type: "pg_get_source_tables", * args: { * source: "default", * }, * }); */ function createHasuraMetadataClient(options, logger) { ensureServer(); const validatedOptions = validate(ClientOptionsSchema, options); const baseUrl = extractBaseUrlBeforeSegment(options.instance, "/v1/graphql"); const queryEndpoint = new URL(`${baseUrl}/v1/metadata`).toString(); const fetchInstance = logger ? requestLogger(logger, "hasura", fetch) : fetch; return async (query) => { const response = await fetchInstance(queryEndpoint, { method: "POST", headers: appendHeaders({ "Content-Type": "application/json" }, { "x-auth-token": validatedOptions.accessToken, "x-hasura-admin-secret": validatedOptions.adminSecret }), body: JSON.stringify(query) }); if (!response.ok) { return { ok: false, data: await response.json() }; } return { ok: true, data: await response.json() }; }; } //#endregion export { ClientOptionsSchema, createHasuraClient, createHasuraMetadataClient, readFragment, trackAllTables }; //# sourceMappingURL=hasura.js.map