@settlemint/sdk-hasura
Version:
Hasura and PostgreSQL integration module for SettleMint SDK, enabling database operations and GraphQL queries
155 lines (153 loc) • 5.44 kB
TypeScript
/* SettleMint Hasura SDK - GraphQL API */
import { Logger } from "@settlemint/sdk-utils/logging";
import { AbstractSetupSchema, FragmentOf, ResultOf, VariablesOf, initGraphQLTada, readFragment } from "gql.tada";
import { GraphQLClient } from "graphql-request";
import { z } from "zod";
//#region src/utils/track-all-tables.d.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");
* }
*/
declare function trackAllTables(databaseName: string, client: ReturnType<typeof createHasuraMetadataClient>, tableOptions?: {
includeSchemas?: string[];
excludeSchemas?: string[];
}): Promise<{
result: "success" | "no-tables";
messages: string[];
}>;
//#endregion
//#region src/hasura.d.ts
/**
* Type definition for GraphQL client configuration options
*/
type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];
/**
* Schema for validating client options for the Hasura client.
*/
declare const ClientOptionsSchema: z.ZodObject<{
instance: z.ZodUnion<readonly [z.ZodString, z.ZodString]>;
accessToken: z.ZodOptional<z.ZodString>;
adminSecret: z.ZodString;
cache: z.ZodOptional<z.ZodEnum<{
default: "default";
"force-cache": "force-cache";
"no-cache": "no-cache";
"no-store": "no-store";
"only-if-cached": "only-if-cached";
reload: "reload";
}>>;
}, z.core.$strip>;
/**
* Type definition for client options derived from the ClientOptionsSchema.
*/
type ClientOptions = z.infer<typeof ClientOptionsSchema>;
/**
* 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);
*/
declare function createHasuraClient<const Setup extends AbstractSetupSchema>(options: ClientOptions, clientOptions?: RequestConfig, logger?: Logger): {
client: GraphQLClient;
graphql: initGraphQLTada<Setup>;
};
/**
* 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",
* },
* });
*/
declare function createHasuraMetadataClient(options: ClientOptions, logger?: Logger): <T>(query: object) => Promise<{
ok: boolean;
data: T;
}>;
//#endregion
export { ClientOptions, ClientOptionsSchema, type FragmentOf, RequestConfig, type ResultOf, type VariablesOf, createHasuraClient, createHasuraMetadataClient, readFragment, trackAllTables };
//# sourceMappingURL=hasura.d.ts.map