# postgrest-js-tools

> Tiny tools library for @supabase/postgrest-js

Latest version **1.2.0** (published 2022-08-27) · ISC license · 0 weekly downloads

## Install

```sh
npm install postgrest-js-tools
pnpm add postgrest-js-tools
yarn add postgrest-js-tools
bun add postgrest-js-tools
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2022-08-27 |
| First published | 2021-12-03 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 26 |
| Author | mzalevski |
| Maintainers | mzalevski |
| Keywords | supabase, postgrest, tools |

## Links

- npm: https://www.npmjs.com/package/postgrest-js-tools
- Repository: https://github.com/mzalevski/postgrest-js-tools
- Homepage: https://github.com/mzalevski/postgrest-js-tools#readme
- Issues: https://github.com/mzalevski/postgrest-js-tools/issues
- npm.io page: https://npm.io/package/postgrest-js-tools

## Recent versions

- 1.2.0 (latest) — 2022-08-27
- 1.1.1 — 2022-07-11
- 1.1.0 — 2022-04-16
- 1.0.2 — 2021-12-18
- 1.0.1 — 2021-12-03
- 1.0.0 — 2021-12-03

## README

# `postgrest-js-tools`

[![Package](https://img.shields.io/npm/v/postgrest-js-tools)](https://www.npmjs.com/package/postgrest-js-tools)
[![License: ISC](https://img.shields.io/badge/License-ISC-blue.svg)](https://opensource.org/licenses/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

```bash
npm i postgrest-js-tools
```

```bash
yarn add postgrest-js-tools
```

```bash
pnpm i postgrest-js-tools
```

Imports

Node

```ts
import { getShape, getFields } from "postgrest-js-tools";
```

Deno

```ts
import {
  getShape,
  getFields,
} from "https://cdn.skypack.dev/postgrest-js-tools?dts";
```

Usage

Click [here](https://supabase.com/docs/reference/javascript/generating-types#generate-database-types-from-openapi-specification) to read about how to generate database types. These are the `definitions` from the example below.

```ts
// 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; }>
```

---
_Source: https://npm.io/package/postgrest-js-tools · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
