定制 bluekachina/seedfromjson 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bluekachina/seedfromjson

Composer 安装命令:

composer require bluekachina/seedfromjson

包简介

Simplifies and standardizes seeding from JSON files named to match the table they will be populating

README 文档

README

Seed Your Database Using JSON files

This package makes it easy to have your DB seeded from JSON files. JSON files are expected to be named like the tables they contain the data for. This library was created mainly because of how easy it is to create JSON files from within the PHPStorm IDE

It supports PHP 7.3+

Installation

Require this package with composer using the following command:

composer require bluekachina/seedfromjson

Configuration

Publish the configuration for this package

php artisan vendor:publish --tag=config

|| ToDo: There's probably a more specific command you can use. I think you would need to name this package specifically.

Storage Setup

Create a disk in your config/filessystems.php file. Disks probably already exist for local, public, s3, etc... The name of the disk you create should match what is named within the config (default is seed_content)

    'seed_content' => [
        'driver' => 'local',
        'root' => database_path().'/seeders/seed_content',
    ],

Usage

  1. Create at least one JSON file within your seed_content folder
  2. Make use of this library from within one of your Laravel project's DatabaseSeeder.php file.
  3. Run php artisan migrate --seed

Examples

This library is registered as a singleton and so should be invoked using app(SeedFromJSON::class).

Step 1: Populate your seeding queue

Often times your tables will need to be populated in a specific sequence. Because of that, we make use of a queue so that you can still be in charge of that sequence. Simply add a new model to the seeding queue for each of the model/tables you want to populate

The method is defined with the following signature:

function addModelToSeedingQueue($model, $options = 0, $callback_pre = null, $callback_post = null);

An example usage would be:

app(SeedFromJSON::class)->addModelToSeedingQueue(User::class, OPT_TRUNCATE_TABLE | OPT_IMPORT_DATA );
  • When importing data, the system will look for a JSON file named the same way the model's table is named

Step 3: Begin seeding

Seeding takes place based on all the queued items that were just added

app(SeedFromJSON::class)->beginSeeding();

Quick Seeding

Quick seeding is enabled via config/env variable. See published config file By. By defaultg, quick seeding is enabled only in local environments

When enabled, seeding imports a limited number of records (First x records from JSON). The number of records (x) is also determined by config/env variable, and defaults to 100

Options

Many different options have been made available.
Each can be specified on a table-by-table basis, and since they're bitwise options, multiple can be used at a time (separated by |).

OPT_TRUNCATE_TABLE          => Determines whether or not to truncate the table connected to the model provided
OPT_IMPORT_DATA             => Determines if JSON file (filename matches tablename) is to be imported from
OPT_SKIP_PK                 => Determines if the PK field's value is to be preserved
OPT_DISABLE_FK_CONSTRAINTS  => Determines if foreign key constraints should be disabled prior to importing JSON data
OPT_NO_SEED_IN_PROD         => Determines if table is to be populated in a development environment only
OPT_NO_TRUNCATE_IN_PROD     => Determines if table is to be truncated in a development environment only
OPT_ALWAYS_FULL_SEED        => Determines if table is to be fully seeded -- even if running a quick seed

Advanced Use

The beginSeeding function takes an optional parameter that determines whether or not seeding is actually ready to commence

function beginSeeding($defer=false);

Example

ExampleModelSeeder.php

<?php

namespace Database\Seeders;

use App\Models\ExampleModel;
use bluekachina\seedfromjson\SeedFromJSON;
use Illuminate\Database\Seeder;

class ExampleModelSeeder extends Seeder
{
    // Run the database seeds.
    public function run($defer=false): void
    {
        app(SeedFromJSON::class)->addModelToSeedingQueue(ExampleModel::class, OPT_TRUNCATE_TABLE | OPT_IMPORT_DATA );
        app(SeedFromJSON::class)->beginSeeding($defer);
    }
}

Notice that we are including an optional parameter into the run function, and then handing that off to the beginSeeding function. This is what allows us to process the seeding both as an individual seeder AND as part of a batch.

In order to take advantage of this as part of a batch you can do something like the following: DatabaseSeeder.php

    public function run(): void
    {
        $batch_silent = true;
        $batch_payload = ['defer' => true];

        $this->call(Example1Seeder::class,      $batch_silent,   $batch_payload);
        $this->call(Example2Seeder::class,      $batch_silent,   $batch_payload);

        // Start seeding based on the queue of JSON files we just lined up
        app(SeedFromJSON::class)->beginSeeding();
    }

Here we're telling all of the individual calls to defer. Once they're all done, we invoke beginSeeding without deferring.

bluekachina/seedfromjson 适用场景与选型建议

bluekachina/seedfromjson 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.21k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 05 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「json」 「laravel」 「seed」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 bluekachina/seedfromjson 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bluekachina/seedfromjson 我们能提供哪些服务?
定制开发 / 二次开发

基于 bluekachina/seedfromjson 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.21k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-05-11