1.2.0 • Published 2 years ago
postgrest-js-tools v1.2.0
postgrest-js-tools
Tiny tools library for @supabase/postgrest-js
.
Features:
- intellisense while selecting fields (
getShape
) - auto generated select string (
getFields
) - return type based on the selected fields (
typeof shape
)
Quick start
Installation
npm i postgrest-js-tools
yarn add postgrest-js-tools
pnpm i postgrest-js-tools
Imports
Node
import { getShape, getFields } from "postgrest-js-tools";
Deno
import {
getShape,
getFields,
} from "https://cdn.skypack.dev/postgrest-js-tools?dts";
Usage
Click here to read about how to generate database types. These are the definitions
from the example below.
// import { definitions } from "path/to/types";
// or definitions["users"]
type User = {
id: string;
email: string;
unused: string; // field we don't want to select
};
// or definitions["posts"] & { user: definitions["users"] }
type Post = {
id: string;
title: string;
user_id: string;
user: User;
unused: string; // field we don't want to select
};
// with intellisense!
const shape = getShape<Post>()({
id: true,
title: false, // if value === false then we skip the key
user: { _: "user_id", id: true, email: true },
// user: { _: "user_id", "*": true }, // "*": true gets all the fields
});
// typeof shape => { id: string; user: { id: string; email: string; } }
// getFields(shape) => "id,user:user_id(id,email)"
const result = await supabase
.from<typeof shape>("posts")
.select(getFields(shape));
// typeof result => PostgrestResponse<{ id: string; user: { id: string; email: string; }>