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.