2.1.1 • Published 2 years ago

postgrester v2.1.1

Weekly downloads
411
License
Apache-2.0
Repository
github
Last release
2 years ago

postgrester

License NPM Package Build Status Code Coverage

Isomorphic PostgREST API Client for Javascript and Typescript.

Supported PostgREST versions:

  • v9.0.0
  • v8.0.0

Gettting Started

npm i postgrester

Example

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
})();

API

Options

When creating the instance via create([options]):

PropertyTypeDefaultDescription
axiosConfigAxiosRequestConfig{}Axios config called with axios.create().
axiosInstanceAxiosInstancenullAxios custom instance.
baseUristring""API URL. Deprecated

:warning: If you provide an axios instance via the axiosInstance property, it's useless to set axiosConfig since it would be overridden by your instance.

:warning: baseUri takes precedence over axiosConfig.baseURL. To avoid any confusion, baseUri will be deprecated in the next minor version release and should be removed in the next major one.

Vertical Filtering (columns) Methods

select()

NameTypeDefaultExamples
selectorstringrequired"*", "author", "category(id,label)"

You can also rename them by inserting a colon: original_column_name:new_column_name.

Hoizontal Filtering (rows) Methods

is()

NameTypeDefaultExamples
columnstringrequired"author", "category.label"
valueboolean \| nullrequired

eq()

NameTypeDefaultExamples
columnstringrequired"author", "category.label"
valueboolean \| number \| null \| stringrequired"Leo Tolstoy"
withQuotesbooleanfalse

Note: boolean and null values will be converted into an is().

neq()

NameTypeDefaultExamples
columnstringrequired"author", "category.label"
valueboolean \| number \| null \| stringrequired"Leo Tolstoy"
withQuotesbooleanfalse

Note: boolean and null values will be converted into a negated is().

gt()

NameTypeDefaultExamples
columnstringrequired"quantity", "category.updated_at"
valuenumber \| stringrequired
isInclusivebooleanfalse

gte()

NameTypeDefaultExamples
columnstringrequired"quantity", "category.updated_at"
valuenumber \| stringrequired

Note: This method is an alias for gt() with <isInclusive> set to true.

lt()

NameTypeDefaultExamples
columnstringrequired"quantity", "category.updated_at"
valuenumber \| stringrequired
isInclusivebooleanfalse

lte()

NameTypeDefaultExamples
columnstringrequired"quantity", "category.updated_at"
valuenumber \| stringrequired

Note: This method is an alias for lt() with <isInclusive> set to true.

in()

NameTypeDefaultExamples
columnstringrequired"author", "category.label"
valueArray<number \| string>required["Leo Tolstoy", "Fyodor Dostoevsky"]
withQuotesbooleanfalse

like()

NameTypeDefaultExamples
columnstringrequired"author", "category.label"
valuestringrequired"Tolstoy"

ilike()

NameTypeDefaultExamples
columnstringrequired"author", "category.label"
valuestringrequired"tolstoy"

not

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.

and

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.

or

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.

Ordering Methods

orderBy()

NameTypeDefaultExamples
columnstringrequired"author", "category.label"
isDescbooleanfalse

Pagination Methods

page()

NameTypeDefaultExamples
pageIndexnumberrequired0, 123
limitnumber10

Request Methods

All request methods are asynchronous promises.

get()

NameTypeDefaultExamples
pathstringrequired"/books"
withPagesLengthbooleanfalse

Return value:

Promise<{
  data: any;
  pagesLength: number;
  totalLength: number;
}>

:warning: Important Both pagesLength and totalLength will equal -1 if <withPagesLength> parameter is false or if the length couldn't be resolved.

post() / Insert

NameTypeDefaultExamples
pathstringrequired"/books"
dataobjectrequired
options{ return?: 'headers-only' \| 'representation' }

Return value:

Promise<void>

or (with { return: "representation" }):

Promise<{
  data: T
}>

post() / Upsert

:warning: Important
If data is an array, it will be considered as an upsert.
In this case, if you don't specify otherwise in options, merge-duplicates resolution will be used by default.

NameTypeDefaultExamples
pathstringrequired"/books"
dataobject[]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[]
}>

patch()

NameTypeDefaultExamples
pathstringrequired"/books"
dataobjectrequired

Return value:

Promise<void>

put()

NameTypeDefaultExamples
pathstringrequired"/books"
dataobjectrequired

Return value:

Promise<void>

delete()

NameTypeDefaultExamples
pathstringrequired"/books"
options{ return?: 'headers-only' \| 'representation' }

Return value:

Promise<void>

or (with { return: "representation" }):

Promise<{
  data: T[]
}>

Contribute

Please check our contributing documentation.

License

This package and its sources are distributed under Apache 2.0.


2.1.1

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

1.6.0

3 years ago

1.5.3

3 years ago

1.5.2

3 years ago

1.5.1

3 years ago

1.5.0

4 years ago

1.4.0

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.1

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

1.0.0-alpha.2

5 years ago

1.0.0-alpha.1

5 years ago

1.0.0-alpha.0

5 years ago