@prisma/ppg
Version:
Lightweight client for Prisma Postgres
146 lines (140 loc) • 3.79 kB
TypeScript
/**
* Result of the database query.
*/
interface QueryResponse {
/**
* Column definitions.
*/
columns: Column[];
/**
* Array of rows, where each row is an array of values with their indices
* corresponding to the indices of the column definitions.
*/
rows: unknown[][];
}
/**
* Column definition.
*/
interface Column {
/**
* Name of the column.
*/
name: string;
/**
* Object identifier of the column type.
*
* If you need to know the column type name, you can use the `oid` to query
* the `pg_type` catalog:
*
* ```ts
* await client.query(
* `SELECT typname FROM pg_type WHERE oid = $1`,
* [column.oid]
* );
* ```
*/
oid: number;
}
interface Queryable {
query(query: string, parameters: unknown[]): Promise<QueryResponse>;
}
/**
* Template literal tag function that executes the query.
*
* ```ts
* const [user] = await sql<User>`SELECT * FROM users WHERE id = ${id}`;
* ```
*/
interface Sql {
<Record = unknown>(strings: TemplateStringsArray, ...values: unknown[]): Promise<Record[]>;
/**
* Executes a raw query defined as a string with placeholders and the list
* of parameters.
*
* ```ts
* const [user] = await sql.query<User>("SELECT * FROM users WHERE id = $1", [id]);
* ```
*/
query<Record>(query: string, params: unknown[]): Promise<Record[]>;
}
type Deserialize = (value: unknown, oid: unknown) => unknown;
/**
* Client configuration options.
*/
interface ClientOptions {
/**
* Prisma Postgres URL.
*/
connectionString: string;
}
/**
* Generic error that occurred when sending the request or executing a query.
*/
declare class RequestError extends Error {
constructor(message: string, httpCode?: number);
}
interface PostgresError {
error: string;
severity_local: string;
severity: string;
code: string;
position?: string;
file: string;
line: string;
routine: string;
}
/**
* Error in the database query.
*/
declare class SqlError extends RequestError {
error: string;
severityLocal: string;
severity: string;
code: string;
position?: string;
file: string;
line: string;
routine: string;
constructor(pgError: PostgresError);
}
/**
* Low level HTTP client interface.
*
* ```ts
* const client = new Client({
* connectionString: "prisma+postgres://accelerate.prisma-data.net/?api_key=..."
* });
*
* const { columns, rows } = await client.query(`SELECT * FROM "users" WHERE id = $1`, [1]);
* ```
*/
declare class Client implements Queryable {
#private;
constructor(options: ClientOptions);
/**
* Executes a query against the Prisma Postgres database.
*/
query(query: string, parameters: unknown[]): Promise<QueryResponse>;
}
declare class ConnectionStringError extends Error {
constructor(message: string);
}
/**
* Connects to the specified Prisma Postgres database and returns a high-level
* SQL client provided as a template literal tag function.
*
* ```ts
* const sql = ppg("prisma+postgres://accelerate.prisma-data.net/?api_key=...");
* const posts: Post[] = await sql`SELECT * FROM posts WHERE user_id = ${userId}`;
* ```
*
* The interpolated values are automatically converted to SQL parameters to
* prevent SQL injection attacks.
*
* You can also pass a custom deserializer function to convert the values based
* on the column type.
*
* See also {@link Client} for the low-level client API.
*/
declare function ppg(url: string, deserialize?: Deserialize): Sql;
export { Client, type ClientOptions, type Column, ConnectionStringError, type Deserialize, type QueryResponse, RequestError, type Sql, SqlError, ppg as default, ppg };