1.2.0 • Published 2 years ago

postgrest-js-tools v1.2.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

postgrest-js-tools

Package License: ISC

Tiny tools library for @supabase/postgrest-js.

Features:

  1. intellisense while selecting fields (getShape)
  2. auto generated select string (getFields)
  3. 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; }>
1.2.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago