Skip to content

Commit

Permalink
prepare to publish on npm
Browse files Browse the repository at this point in the history
  • Loading branch information
mzalevski committed Dec 3, 2021
1 parent dba88f7 commit 6bb0c26
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
.vscode
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# `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/npm/l/postgrest-js-tools)](#license)

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

Install

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

Usage

[click here to read how to generate database types -> `definitions`](https://supabase.com/docs/reference/javascript/generating-types#generate-database-types-from-openapi-specification)

```ts
// import { definitions } from "path/to/types";
import { getShape, getFields } from "postgrest-js-tools";

// or definitions["users"]
type User = {
id: string;
email: string;
unused: string; // field we don't want
};

// or definitions["posts"]
type Post = {
id: string;
user_id: string;
unused: string; // field we don't want
user: User;
};

// with intellisense!
const shape = getShape<Post>()({
id: true,
user: { _: "user_id", id: true, email: true },
});

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

### Methods

`getShape: <T>() => <U extends Partial<MakePropTypesBoolean<T, T>>>(fields: U) => ParseReturnType<T, U>`

`getFields: (shape: Record<string, any>) => string`
2 changes: 1 addition & 1 deletion index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getShape, getFields } from ".";
import { getShape, getFields } from "./src";

type CheckBase = { id: string; place_id: string; user_id: string };
type PlaceBase = { id: string; title: string };
Expand Down
88 changes: 67 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 24 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
{
"name": "postgrest-js-utils",
"version": "1.0.0",
"description": "",
"main": "index.js",
"name": "postgrest-js-tools",
"version": "0.1.0",
"description": "Tiny tools library for @supabase/postgrest-js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"test": "jest --watchAll=true",
"dev": "ts-node index.ts"
"build": "tsc"
},
"keywords": [],
"author": "",
"repository": {
"type": "git",
"url": "git+https://github.com/mzalevski/postgrest-js-tools.git"
},
"keywords": [
"supabase",
"postgrest",
"tools"
],
"author": "mzalevski",
"license": "ISC",
"devDependencies": {
"@types/jest": "^27.0.3",
"jest": "^27.3.1",
"ts-jest": "^27.0.7",
"ts-node": "^10.4.0",
"typescript": "^4.5.2"
}
},
"bugs": {
"url": "https://github.com/mzalevski/postgrest-js-tools/issues"
},
"homepage": "https://github.com/mzalevski/postgrest-js-tools#readme"
}
Loading

0 comments on commit 6bb0c26

Please sign in to comment.