Isomorphic Postgrest Client in Typescript which provides a fluent API.
npm i ts-postgrest-client
const client = new PostgrestClient({
host: 'http://localhost:3000'
})
const query = client.query('document')
.query('documentId').notEqual('test')
.select([
'documentId',
{
'schemaId': [
'name',
'title',
'settings'
]
}
])
client.execute(query).then(async res => {
console.log(await res.json())
})
Executing multiple queries in parallel:
const client = new PostgrestClient({
host: 'http://localhost:3000'
})
const documentQuery = client.query('document')
.query('documentId').notEqual('test')
const schemaQuery = client.query('schema')
.query('schemaId').notEqual('test')
Promise.all([client.execute(documentQuery), client.execute(schemaQuery)]).then(async res => {
console.log(await res.json())
})
Executing complex queries:
const client = new PostgrestClient({
host: 'http://localhost:3000'
})
const schemaQuery = client.query('schema')
.query('schemaId').notEqual(10)
const documentQuery = client.query('document')
.query('documentId').notEqual('test')
.addQuery(schemaQuery)
client.execute(documentQuery).then(async res => {
console.log(await res.json())
})
This client aims to closely mirror the Postgrest API, http://postgrest.org/en/v6.0/api.html.
Using an operator on a query can be easily achieved via the QueryBuilders expressive methods:
const documentQuery = client.query('document')
.query('documentId').notEqual(0).lessThan(10)
Additionally an escape hatch is provided via directly passing the Postgrest abbreviation in:
const documentQuery = client.query('document')
.query('documentId', 'neq', 0).query('documentId', 'lt', 10)
Method | in Postgrest | Name |
---|---|---|
.equals | eq | Equals |
.greaterThan | gt | Greater than |
.greaterThanOrEqual | gte | Greater than or equal |
.lessThan | lt | Less than |
.lessThanOrEqual | lte | Less than or equal |
.notEqual | neq | Not equal |
.like | like | Like |
.ilike | ilike | iLike |
.in | in | In |
.is | is | Is |