What is @supabase/postgrest-js?
@supabase/postgrest-js is a JavaScript client library for interacting with PostgREST APIs. It allows you to perform CRUD operations, filter data, and manage real-time subscriptions with ease. This package is particularly useful for developers working with Supabase, a backend-as-a-service platform that provides a PostgREST API for interacting with PostgreSQL databases.
What are @supabase/postgrest-js's main functionalities?
CRUD Operations
This feature allows you to perform basic CRUD (Create, Read, Update, Delete) operations on your database tables. The code sample demonstrates how to insert a new record, read records, update a record, and delete a record from a 'users' table.
const { createClient } = require('@supabase/postgrest-js');
const client = createClient('https://your-project.supabase.co', 'public-anon-key');
// Create a new record
client.from('users').insert({ username: 'johndoe', age: 30 }).then(response => console.log(response));
// Read records
client.from('users').select('*').then(response => console.log(response));
// Update a record
client.from('users').update({ age: 31 }).eq('username', 'johndoe').then(response => console.log(response));
// Delete a record
client.from('users').delete().eq('username', 'johndoe').then(response => console.log(response));
Filtering Data
This feature allows you to filter data based on specific conditions. The code sample demonstrates how to filter records where the age is 30 and how to use advanced filtering with multiple conditions.
const { createClient } = require('@supabase/postgrest-js');
const client = createClient('https://your-project.supabase.co', 'public-anon-key');
// Filter records
client.from('users').select('*').eq('age', 30).then(response => console.log(response));
// Advanced filtering
client.from('users').select('*').or('age.eq.30,username.eq.johndoe').then(response => console.log(response));
Real-time Subscriptions
This feature allows you to subscribe to real-time changes in your database tables. The code sample demonstrates how to set up a subscription to listen for any changes in the 'users' table and log the changes to the console.
const { createClient } = require('@supabase/postgrest-js');
const client = createClient('https://your-project.supabase.co', 'public-anon-key');
// Subscribe to changes in the 'users' table
const subscription = client.from('users').on('*', payload => {
console.log('Change received!', payload);
}).subscribe();
Other packages similar to @supabase/postgrest-js
pg
The 'pg' package is a PostgreSQL client for Node.js. It allows you to interact with PostgreSQL databases using SQL queries. Unlike @supabase/postgrest-js, which provides a higher-level abstraction for CRUD operations and real-time subscriptions, 'pg' requires you to write raw SQL queries and manage connections manually.
knex
Knex.js is a SQL query builder for Node.js, supporting multiple database types including PostgreSQL. It provides a more flexible and powerful way to build SQL queries compared to @supabase/postgrest-js. However, it does not offer built-in real-time subscriptions and requires more setup for basic CRUD operations.
sequelize
Sequelize is a promise-based Node.js ORM for various SQL databases, including PostgreSQL. It provides a higher-level abstraction for database operations, similar to @supabase/postgrest-js, but with more features like model definitions, associations, and migrations. However, it does not natively support real-time subscriptions.
Postgrest JS
Isomorphic JavaScript client for PostgREST. The goal of this library is to make an "ORM-like" restful interface.
Contents
Usage
Install
npm install --save @supabase/postgrest-js
Initialize
import { PostgrestClient } from ' @supabase/postgrest-js'
let client = new PostgrestClient('https://your-postgrest.com')
GET
let { body: countries } = await client
.from('countries')
.select('*')
let { body: countries } = await client
.from('countries')
.select('name')
let { body: countries } = await client
.from('countries')
.select(`
name,
cities (
name
)
`)
let { body: countries } = await client
.from('countries')
.select(`
name,
cities (
name
)
`)
.filter('name', 'eq', 'New Zealand')
.filter('cities.name', 'eq', 'Auckland')
let { body: countries } = await client
.from('countries')
.select('name')
.order('name')
let { body: countries } = await client
.from('countries')
.select('name')
.range(0, 10)
let { body: countries } = await client
.from('countries')
.match({ 'continent': 'Asia' })
.select('*')
let { body: countries } = await client
.from('countries')
.eq('name', 'New Zealand')
.select('*')
let { body: countries } = await client
.from('countries')
.gt('id', 20)
.select('*')
let { body: countries } = await client
.from('countries')
.lt('id', 20)
.select('*')
let { body: countries } = await client
.from('countries')
.gte('id', 20)
.select('*')
let { body: countries } = await client
.from('countries')
.lte('id', 20)
.select('*')
let { body: countries } = await client
.from('countries')
.like('name', '%Zeal%')
.select('*')
let { body: countries } = await client
.from('countries')
.ilike('name', '%Zeal%')
.select('*')
let { body: countries } = await client
.from('countries')
.is('name', null)
.select('*')
let { body: countries } = await client
.from('countries')
.in('name', ['China', 'France'])
.select('*')
let { body: countries } = await client
.from('countries')
.not('name', 'China')
.select('*')
POST
You can insert records using insert()
. Insert accepts an array of objects so that you can batch insert.
let { status } = await client
.from('messages')
.insert([{ message: 'Hello World 👋'])
PATCH
You can update records using update()
.
let { status } = await client
.from('messages')
.eq('message', 'Hello World 👋')
.update({ message: 'Goodbye World 👋' })
DELETE
You can delete records using delete()
.
let { status } = await client
.from('messages')
.eq('message', 'Goodbye World 👋')
.delete()
Usage online
To see details examples of usage, see the Supabase docs:
Contributing
- Fork the repo on GitHub
- Clone the project to your own machine
- Commit changes to your own branch
- Push your work back up to your fork
- Submit a Pull request so that we can review your changes and merge
License
This repo is liscenced under MIT.
Credits