Official WatermelonDB Docs
Read the official WatermelonDB docs for more info on what it is
Demo Project
Check out our demo project to see what’s possible
What is Laravel Watermelon?
This package provides a Watermelon DB backend sync implementation for Laravel. Watermelon DB is a robust local database synchronization tool to help develop offline-first application. One of the biggest hurdles is implementing the logic on your server to handle the synchronization process. That’s where this package comes in to provide a quick synchronization route you can get up and running in minutes.Usage
Once you’ve installed the package, you need to specify which models will be available through the synchronization endpoint. Open up theconfig/watermelon.php file and update the models array. The key needs to be the name of table
used locally in your application, and the value must be classname of the related model.
You can also change the route to be something other than /sync by editing the config file or setting the
WATERMELON_ROUTE environment variable. You will have to ensure your application makes synchronization requests to
whatever route you specify.
By default, only your global middleware will be applied to the synchronization endpoint. This means that unless you changed
the default global middleware in your Laravel project the synchronization endpoint will be unauthenticated. If you want to
have access to the currently authenticated user you will need to add the web middleware to the config file’s
middleware array. If you want to restrict access to the synchronization endpoint to authenticated users only, you can
add the auth middleware in addition to the web middleware. Of course, you can add any middleware you would like as
long as it’s registered in your project.
watermelon_id. The default IDs generated by Watermelon are alphanumeric strings, although you can change the type of
the column if you don’t use the default IDs autogenerated by Watermelon. A unique index on the column is recommended,
and I like placing it directly after the id column.
created_at, updated_at, and deleted_at timestamp columns, you will also need
to add those columns. Please refer to the Laravel documentation for implementing the
timestamps and
soft deleting functionality.
The only thing you must change in your model class is to use the Watermelon trait.
watermelon_id will be returned (although it will be passed through the endpoint as just id, this is intentional). To
include more attributes, you will need to add a watermelonAttributes property to your class. These attributes will be
included alongside the Watermelon ID and can be updated by change objects posted to the synchronization endpoint.
toWatermelonArray function.
Authorization
By default, all models will be accessible and able to be updated through the synchronization endpoint. This is obviously not ideal in most projects where you need control to authorize which models users can see and what they can update.Scoping entire records
You can implement a query scope on your models by overriding thescopeWatermelon function. This scope will be applied
before returning data to pull requests. This can be used, for example, to restrict records to only be retrievable by
users authorized to see them (to have access to the Auth::user() like in this example, don’t forget to add the web
and auth middlewares as shown in the config example at the start of the Usage section).