nathanheffley/laravel-watermelon
Composer 安装命令:
composer require nathanheffley/laravel-watermelon
包简介
Easily set up a sync endpoint to support Watermelon DB in your Laravel projects.
README 文档
README
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.
This project is still in active development and does not support schema versions or migrations yet. Both of these are major parts of the Watermelon DB spec. Please expect large changes at least until those features are implemented.
Installation
Before getting started you'll need to install the package and publish the config file.
composer require nathanheffley/laravel-watermelon
php artisan vendor:publish --tag="watermelon-config"
Usage
Once you've installed the package, you need to specify which models will be available through the synchronization
endpoint. Open up the config/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.
<?php use App\Models\Project; use App\Models\Task; return [ 'route' => env('WATERMELON_ROUTE', '/watermelon'), 'middleware' => [ 'web', 'auth', ], 'models' => [ 'projects' => Project::class, 'tasks' => Task::class, ], ];
Once you've specified which models should be available through the synchronization endpoint, you'll need to implement some functionality in the models to support being served as Watermelon change objects.
You will need to add a database column to all of your models to keep track of what the Watermelon ID is, called
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.
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class AddWatermelonIdToTasksTable extends Migration { public function up() { Schema::table('tasks', function (Blueprint $table) { $table->string('watermelon_id')->after('id')->unique(); }); } public function down() { Schema::table('tasks', function (Blueprint $table) { $table->dropColumn('watermelon_id'); }); } }
If you did not originally include the 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.
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use NathanHeffley\LaravelWatermelon\Traits\Watermelon; class Task extends Model { use SoftDeletes, Watermelon; }
The attributes returned through the synchronization endpoint are whitelisted by default. Out of the box, only the
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.
class Task extends Model { use SoftDeletes, Watermelon; protected array $watermelonAttributes = [ 'content', 'is_completed', ]; }
If you need more control over the attributes returned you can override the entire 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 the scopeWatermelon 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).
use Illuminate\Support\Facades\Auth; class Task extends Model { // ... public function scopeWatermelon($query) { return $query->where('user_id', Auth::user()->id); } }
Package Development
If you have PHP and Composer installed on your local machine you should be able to easily run the PHPUnit test suite.
./vendor/bin/phpunit
If you prefer to run the tests within a Docker container, this project includes Laravel Sail.
To install the dependencies:
docker run --rm \
-u "$(id -u):$(id -g)" \
-v $(pwd):/opt \
-w /opt \
laravelsail/php80-composer:latest \
composer install --ignore-platform-reqs
docker-compose up -d
sail exec laravel.test ./vendor/bin/phpunit
nathanheffley/laravel-watermelon 适用场景与选型建议
nathanheffley/laravel-watermelon 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 34.86k 次下载、GitHub Stars 达 66, 最近一次更新时间为 2021 年 08 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「package」 「laravel」 「offline」 「watermelon」 「watermelondb」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nathanheffley/laravel-watermelon 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nathanheffley/laravel-watermelon 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nathanheffley/laravel-watermelon 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
Simple ASCII output of array data
统计信息
- 总下载量: 34.86k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 66
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-08-08