Isomorphic PostgREST API Client for Javascript and Typescript.
Supported PostgREST versions:
v9.0.0
v8.0.0
npm i postgrester
import { create } from "postgrester";
const postgrestClient = create({
axiosConfig: { baseURL: "https://api.example.com" }
});
(async () => {
const { data, pagesLength } = await postgrestClient
.select("*")
.select("author(first_name,last_name)")
.is("is_published", true)
.not.is("isbn", null)
.eq("publication_year", 1970)
.in("language_code", ["en-UK", "en-US"])
.ilike("title", "island")
.like("author.last_name", "Hemingway")
.orderBy("publication_year", true) // `true` = DESC
.orderBy("title")
.page(3, 25) // 4th page with 25 items per page
.get("/books", true); // `true` = require the total `pagesLength` value to be calculated
})();
When creating the instance via create([options])
:
Property | Type | Default | Description |
---|---|---|---|
axiosConfig |
AxiosRequestConfig |
{} |
Axios config called with axios.create() . |
axiosInstance |
AxiosInstance |
null |
Axios custom instance. |
baseUri |
string |
"" |
API URL. Deprecated |
⚠️ If you provide an axios instance via theaxiosInstance
property, it's useless to setaxiosConfig
since it would be overridden by your instance.
⚠️ baseUri
takes precedence overaxiosConfig.baseURL
. To avoid any confusion,baseUri
will be deprecated in the next minor version release and should be removed in the next major one.
Name | Type | Default | Examples |
---|---|---|---|
selector |
string |
required | "*" , "author" , "category(id,label)" |
You can also rename them by inserting a colon: original_column_name:new_column_name
.
Name | Type | Default | Examples |
---|---|---|---|
column |
string |
required | "author" , "category.label" |
value |
boolean | null |
required |
Name | Type | Default | Examples |
---|---|---|---|
column |
string |
required | "author" , "category.label" |
value |
boolean | number | null | string |
required | "Leo Tolstoy" |
withQuotes |
boolean |
false |
Note:
boolean
andnull
values will be converted into anis()
.
Name | Type | Default | Examples |
---|---|---|---|
column |
string |
required | "author" , "category.label" |
value |
boolean | number | null | string |
required | "Leo Tolstoy" |
withQuotes |
boolean |
false |
Note:
boolean
andnull
values will be converted into a negatedis()
.
Name | Type | Default | Examples |
---|---|---|---|
column |
string |
required | "quantity" , "category.updated_at" |
value |
number | string |
required | |
isInclusive |
boolean |
false |
Name | Type | Default | Examples |
---|---|---|---|
column |
string |
required | "quantity" , "category.updated_at" |
value |
number | string |
required |
Note: This method is an alias for
gt()
with<isInclusive>
set totrue
.
Name | Type | Default | Examples |
---|---|---|---|
column |
string |
required | "quantity" , "category.updated_at" |
value |
number | string |
required | |
isInclusive |
boolean |
false |
Name | Type | Default | Examples |
---|---|---|---|
column |
string |
required | "quantity" , "category.updated_at" |
value |
number | string |
required |
Note: This method is an alias for
lt()
with<isInclusive>
set totrue
.
Name | Type | Default | Examples |
---|---|---|---|
column |
string |
required | "author" , "category.label" |
value |
Array<number | string> |
required | ["Leo Tolstoy", "Fyodor Dostoevsky"] |
withQuotes |
boolean |
false |
Name | Type | Default | Examples |
---|---|---|---|
column |
string |
required | "author" , "category.label" |
value |
string |
required | "Tolstoy" |
Name | Type | Default | Examples |
---|---|---|---|
column |
string |
required | "author" , "category.label" |
value |
string |
required | "tolstoy" |
This getter ONLY negates the FIRST following filtering method.
For example, postgrestClient.not.is("category_id", null).ilike("author", "dostoevsky")
will
negate the is()
filter but not the ilike()
one.
This getter condition ALL the following filtering methods to be conjuncted as "ands".
For example, postgrestClient.and.is("category_id", null).ilike("author", "dostoevsky")
will
intersect both is()
and ilike()
filters.
This getter condition ALL the following filtering methods to be conjuncted as "ors".
For example, postgrestClient.and.is("category_id", null).ilike("author", "dostoevsky")
will
unite both is()
and ilike()
filters.
Name | Type | Default | Examples |
---|---|---|---|
column |
string |
required | "author" , "category.label" |
isDesc |
boolean |
false |
Name | Type | Default | Examples |
---|---|---|---|
pageIndex |
number |
required | 0 , 123 |
limit |
number |
10 |
All request methods are asynchronous promises.
Name | Type | Default | Examples |
---|---|---|---|
path |
string |
required | "/books" |
withPagesLength |
boolean |
false |
Return value:
Promise<{
data: any;
pagesLength: number;
totalLength: number;
}>
⚠️ Important
BothpagesLength
andtotalLength
will equal-1
if<withPagesLength>
parameter isfalse
or if the length couldn't be resolved.
Name | Type | Default | Examples |
---|---|---|---|
path |
string |
required | "/books" |
data |
object |
required | |
options |
{ return?: 'headers-only' | 'representation' } |
Return value:
Promise<void>
or (with { return: "representation" }
):
Promise<{
data: T
}>
⚠️ Important
Ifdata
is an array, it will be considered as an upsert.
In this case, if you don't specify otherwise inoptions
,merge-duplicates
resolution will be used by default.
Name | Type | Default | Examples |
---|---|---|---|
path |
string |
required | "/books" |
data |
object[] |
required | |
options |
{ onConflict?: string, resolution?: "ignore-duplicates" | "merge-duplicates", return?: 'headers-only' | 'representation' } |
{ resolution: "merge-duplicates" } |
Return value:
Promise<void>
or (with { return: "representation" }
):
Promise<{
data: T[]
}>
Name | Type | Default | Examples |
---|---|---|---|
path |
string |
required | "/books" |
data |
object |
required |
Return value:
Promise<void>
Name | Type | Default | Examples |
---|---|---|---|
path |
string |
required | "/books" |
data |
object |
required |
Return value:
Promise<void>
Name | Type | Default | Examples |
---|---|---|---|
path | string |
required | "/books" |
options |
{ return?: 'headers-only' | 'representation' } |
Return value:
Promise<void>
or (with { return: "representation" }
):
Promise<{
data: T[]
}>
Please check our contributing documentation.
This package and its sources are distributed under Apache 2.0.