Combining Supabase with Vue.js allows you to build robust, scalable apps with real-time support. During the project's early development phase, migration files automate the process of resetting and rebuilding database tables with just a few commands, saving developers a substantial amount of time.
In this guide, we'll look at how to create migration files for a remote Supabase database so that the database can be updated directly from our Vue.js project.
Supabase is an open-source alternative to Firebase, providing all the backend services you need to build a web or mobile application. With Supabase, you can set up a PostgreSQL database, manage authentication, and create real-time APIs quickly and efficiently.
If you prefer learning by watching video tutorials, checkout the Vue.js Master Class 2024 Edition course on Vue School. In this course, we use Supabase extensively as a backend solution to our project management app that’s we’re building throughout the course.
Integrating Supabase with Vue.js allows you to leverage these powerful features in your frontend applications. Let’s have a look at some of those features:
Supabase offers a JavaScript library that easily integrate with Vue.js, enabling you to manage your backend directly from your frontend code. This smooth integration simplifies the development process, reducing the amount of boilerplate code and allowing you to focus on building features.
With Supabase's real-time capabilities, you can build dynamic and responsive Vue.js applications that reflect live changes without needing additional infrastructure. This is particularly useful for applications that require instant updates, such as chat applications, live dashboards, and collaborative tools.
Supabase provides comprehensive authentication solutions, making it easy to manage user sessions and permissions in your Vue.js applications. With built-in support for social logins and JWT-based authentication, you can implement secure and scalable user management with minimal effort.
Built on PostgreSQL, Supabase ensures that your database can scale with your application, handling large amounts of data and high traffic.
Both Supabase and Vue.js are designed to be developer-friendly, with extensive documentation, community support, and straightforward APIs. This reduces the learning curve and speeds up development, allowing you to deliver high-quality applications faster.
Supabase offers a generous free tier, making it a cost-effective solution for startups and small projects. As your application grows, you can scale your usage and only pay for what you need, ensuring cost efficiency without compromising on performance.
By leveraging the powerful combination of Supabase and Vue.js, you can build modern web applications that are not only feature-rich but also easy to maintain and scale.
Speaking of features, one of the best features in Supabase is the ability to create migration files, which leads us to the next section.
Database migrations are scripts that help you manage and version-control changes to your database schema. They are crucial for keeping your database structure aligned with your application's needs and for collaborating with your development team.
Database schema migration files are crucial for keeping your database structure updated and in sync with your application's needs. Acting like version control, they make it easy to track, undo, and collaborate on changes. Some of the benefits of creating migration files are:
Creating migration files involves writing SQL scripts that define changes to your database schema, such as adding or modifying tables, columns, and indexes. Applying migrations involves running these scripts to update the database schema. Tools like the Supabase CLI make it easy to manage migrations within your development workflow.
In this guide, we will focus on using migration files to resetting and rebuilding the remote Supabase database during the initial development phase.
Before we dive into creating Supabase database migration files in Vue.js, ensure you have the following set up:
npm create vue@latest
To get started, we need to install the Supabase CLI in our project as a dev dependency. Follow these steps:
npm install supabase --save-dev
Next, we need to set up Supabase in our project. This involves initializing Supabase configurations.
package.json
file and add the following script:{
"scripts": {
"supabase:init": "supabase init"
}
}
npm run supabase:init
This command generates a new supabase
directory containing the necessary configuration files.
Before proceeding, we need to log into Supabase.
package.json
:{
"scripts": {
"supabase:login": "supabase login"
}
}
npm run supabase:login
To link our local Supabase configuration with the remote Supabase project, follow these steps:
package.json
:{
"scripts": {
"supabase:link": "supabase link --project-ref YOUR_PROJECT_REF"
}
}
YOUR_PROJECT_REF
with the project reference ID from your Supabase dashboard.npm run supabase:link
Linking your local configuration with the remote project ensures that any database changes you make locally are mirrored on your live database.
With the setup complete, we can now create a new migration file.
package.json
:{
"scripts": {
"db:migrate:new": "supabase migration new"
}
}
npm run db:migrate:new projects-schema
This command creates a new directory called migrations
inside the supabase
directory. It also generates a timestamped file named projects-schema
, where we will define our database schema.
Let's define the schema for the projects
table in the generated migration file. Here’s the SQL code:
DROP table if exists projects;
create table
projects (
id bigint primary key generated always as identity not null,
created_at timestamptz default now() not null,
name text not null,
slug text unique not null,
collaborators text array default array[]::varchar[] not null
);
This SQL script will check to see if the database has a projects table already and drop it if exists. Then, it will create a projects
table with columns such as id
, created_at
, name
, slug
, and collaborators
.
Warning: Dropping tables in production is dangerous. You need to be careful not to run any migration file that drops important tables in production.
To apply our migrations and reset the database, we need to add and run a reset script.
package.json
:{
"scripts": {
"db:reset": "supabase db reset --linked"
}
}
npm run db:reset
This command resets the linked project's database according to the schema defined in the migration file. It drops existing tables and types, then recreates them based on the migration scripts.
After resetting the database, it's often useful to populate it with initial data. This process is called seeding, and it helps to set up a consistent initial state for your application.
Seeder scripts are used to insert predefined data into your database. This can include default users, configuration settings, or sample data for development and testing. By automating this process, you ensure that every environment (development, staging, production) starts with the same baseline data.
You can write the SQL scripts for seeding the Supabase database in the supabase/seed.js
file that was created when we ran the Supabase’s initialization script. Here’s an example of how you might write a seeder script for your projects
table:
INSERT INTO projects (name, slug, collaborators)
VALUES
('Project Alpha', 'project-alpha', '[]'),
('Project Beta', 'project-beta', '["1", "2"]');
This script inserts two sample projects into the projects
table. You can create similar scripts for other tables in your database.
In our Vue.js Master Class 2024 Edition course, while developing our project management app, we opted for a unique approach. We combined faker.js and the Supabase client to populate our remote database. This approach may not be conventional, but it resonates well with frontend developers. Explore the Seeding the Supabase Remote Database with Faker.js lesson for a firsthand look at this implementation.
Each time you run supabase start
or supabase db reset
, the supabase/seed.sql
file is executed. Seeding occurs once all database migrations have completed. As a general rule, you should only include data in your seed file rather than schema declarations.
This ensures that your database is always populated with the necessary data for your application to function correctly.
While creating Supabase database migration files, there are few things to keep in mind to ensure a smooth workflow:
modify-tasks-table
or projects-schema
.In this guide, we've shown you how to create Supabase database migration files in your Vue.js projects. Using these migration files helps keep your database schema up-to-date and in sync with your application, saving you time and effort. They also allow you to easily reset the remote Supabase database during development.
For more in-depth learning about Supabase migrations, seeds, authentication, and more, check out the Vue.js 3 Master Class at Vue School. This course provides detailed tutorials and insights to help you master Supabase and Vue.js.
By following this guide and exploring additional resources, you'll be well-prepared to take full advantage of Supabase and Vue.js, creating applications that are both efficient and effective.
Our goal is to be the number one source of Vue.js knowledge for all skill levels. We offer the knowledge of our industry leaders through awesome video courses for a ridiculously low price.
More than 200.000 users have already joined us. You are welcome too!
© All rights reserved. Made with ❤️ by BitterBrains, Inc.