teguh02/filament-db-sync

This package allow sync two different laravel filament app db

v1.2 2024-10-25 08:13 UTC

This package is auto-updated.

Last update: 2024-10-25 10:01:40 UTC


README

Background

Latest Version on Packagist Total Downloads

Installation

You can install the package via composer:

composer require teguh02/filament-db-sync

Panel Configuration

Install the library in your panel service provider

<?php
use Teguh02\FilamentDbSync\FilamentDbSync;

public function panel(Panel $panel): Panel
{
    return $panel
    // ... another filament configuration
        ->plugins([
            FilamentDbSync::make(),
        ]);
}

Configuration File and Migrations

Publish the configuration and migrations

php artisan vendor:publish --provider="Teguh02\FilamentDbSync\FilamentDbSyncServiceProvider"
php artisan migrate

Usage

Model configuration

In your model class, add the fillable and the casts attribute. For example if we have a model with name is Items, the model configuration should will be below

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Items extends Model
{
    use HasFactory;

    // Define the fillable property to 
    // allow mass assignment on the model
    // and the database sync process
    protected $fillable = [
        'name',
        'description',
        'price',
        'stock',
        'expired_at',
    ];

    // Define the casts property to
    // automatically cast the data type
    // of the model attributes
    protected $casts = [
        'stock' => 'integer',
        'price' => 'integer',
        'expired_at' => 'date',
    ];
}

db_sync.php config

For the plugins config, you can adjust manually as you needs.

<?php

// config for Teguh02/FilamentDbSync
return [

    /**
     * The host where the data will be synced
     */
    'sync_host' => env('SYNC_HOST', 'http://localhost'),

    /**
     * The token to be used for authentication
     */
    'auth_token' => env('SYNC_TOKEN', 'default_token'),

    /**
     * Models configuration
     */
    'models' => [
        /**
         * If set to true, the package will
         * automatically scan all models in the app/Models directory
         */
        'auto_scan' => env('AUTO_SCAN_MODELS', true),

        /**
         * If auto_scan is set to true,
         * this configuration will be used to exclude models
         * which will not be synced
         */
        'excluded' => [
            // App\Models\User::class,
        ],

        /**
         * When auto_scan is set to false,
         * this configuration will be used to include models
         */
        'included' => [
            // App\Models\User::class,
        ],

        /**
         * The column to be used as the key
         * when syncing data
         */
        'column_as_key' => [
            // class => column
            App\Models\User::class => 'email',

            // or you can use the table name
            // table_name => column
            // 'users' => 'email',
        ],
    ],

    /**
     * Sync configuration
     */
    'sync' => [
        /**
         * The action to be taken when there is duplicate data
         *
         * Available options:
         * - update : update the existing data
         * - duplicate : create a new data with the same data
         */
        'duplicate_data_action' => env('DUPLICATE_DATA_ACTION', 'update'),
    ],
];

Sync Env Configuration

Please set your env configuration following below, for example we have 2 different server below. Server 1 app domain is server1.com and then server 2 domain is server2.com

On the Server 1 :

SYNC_TOKEN=FUswndOCKEm5rAKzgqFDsXZ5euWhA535tOzgE00n9tuP4IsofFslPM5VgtrT
SYNC_HOST=http://server2.com

On the Server 2 :

SYNC_TOKEN=FUswndOCKEm5rAKzgqFDsXZ5euWhA535tOzgE00n9tuP4IsofFslPM5VgtrT
SYNC_HOST=http://server1.com

Queue configuration

Because this plugin use a jobs function to execute huge data, please set your queue driver according your needs

#QUEUE_CONNECTION=sync
QUEUE_CONNECTION=database

Screenshoot

Screenshot

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.