定制 pruvo/laravel-firestore-connection 二次开发

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

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

pruvo/laravel-firestore-connection

最新稳定版本:v1.8.0

Composer 安装命令:

composer require pruvo/laravel-firestore-connection

包简介

Google Firebase database connection to Laravel

README 文档

README

Latest Version on Packagist Total Downloads GitHub Actions

This package adds functionalities to the Eloquent model and Query builder for Google Firestore, using the original Laravel API.

Installation

You can install the package via composer:

composer require pruvo/laravel-firestore-connection

Configuration

You can use Firestore either as the main database, either as a side database. To do so, add a new firebase connection to config/database.php:

'firestore' => [
    'driver' => 'firestore',
    'database' => \Google\Cloud\Firestore\FirestoreClient::DEFAULT_DATABASE,
    'prefix' => '',

    // The project ID from the Google Developer's Console.
    'projectId' => env('GOOGLE_CLOUD_PROJECT'),

    // The full path to your service account credentials .json file 
    // retrieved from the Google Developers Console.
    'keyFilePath' => env('GOOGLE_APPLICATION_CREDENTIALS'),

    // A hostname and port to emulator service.
    // 'emulatorHost'=> env('FIRESTORE_EMULATOR_HOST', 'localhost:8900'),
],

Eloquent

Extending the base model

This package includes a Firestore enabled Eloquent class that you can use to define models for corresponding collections.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Pruvo\LaravelFirestoreConnection\Firestoreable;

class Book extends Model
{
    use HasFactory;
    use Firestoreable;

    public $connection = 'firestore';
    public $table = 'books';
    public $primaryKey = 'id';
    public $keyType = 'string';
    public $perPage = 10;
}

Limitations

Laravel is a framework originally designed to use SQL databases for its ORM. However, Firestore is a NoSQL database, and therefore, it does not support the same features as SQL databases.

From point of view that you are used to SQL databases, the following features are not supported:

SQL syntaxe

There are not any way to query firestore database using string (like SQL syntaxe). All queries must be done via Firestore SDK.

Only supports AND operator

  • SQL database supports select * from users where status = 'disabled' or age > 18; but it is not supported on Firestore.

Do not support equals to null

  • But there is a workaround: orderBy('field', 'ASC')->startAt(null).

Model does not support relationship because it is impossible cross collection data, so

  • you can not use belongsTo, hasOne, hasMany, morphOne, morphMany, belongsToMany or morphToMany;
  • In contrast, Firestore has subcollections, so you can use hasSubcollection to define relationships.

Model ID behavior

  • Firestore does not support numeric auto increment. To keep the records orderable it uses Str::orderedUuid();.
  • Firestore is a document database, so the primary key is the document name.
  • If auto increment is enabled, and you want to set a custom UUID, you must force fill the __name__ attribute before saving the model.
$user = User::newModelInstance([
    'name' => 'Jhon Joe',
    'email' => 'jhon.joe@example.com',
    'password'=> bcrypt('123456'),
]);
$user->forceFill(['__name__' => '00000000-0000-0000-0000-000000000000'])->save();

Firestore database types

  • Avoid use reference type becouse it can not be serialized and does not have advantages. Instead use document reference path (string representation of DocumentReference).
  • Avoid use map and array types unless it is needed. The map is equivalent to associative array, and array is equivalent to sequencial array.
  • Date attributes are stored as string by default. To store as timestamp (native Firestore type) you must to:
    • set a instance of DateTimeInterface as value; Carbon extends DateTimeInterface and can safely be used;
    • use the cast Pruvo\LaravelFirestoreConnection\Casts\AsCarbon on the model;
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Pruvo\LaravelFirestoreConnection\Casts\AsCarbon;
use Pruvo\LaravelFirestoreConnection\Firestoreable;

class Book extends Model
{
    use HasFactory;
    use Firestoreable;

    public $connection = 'firestore';
    public $table = 'books';
    public $primaryKey = 'id';
    public $keyType = 'string';
    public $perPage = 10;

    protected $casts = [
        'created_at' => AsCarbon::class,
        'updated_at' => AsCarbon::class,
    ];
}

Complexy queries

  • It is strogly recommended use Laravel Scout with pruvo/laravel-firestore-connection.
  • Laravel Scout retrive models by results ids using whereIn. Firestore support up to 10 ids by request. So paginate by 10 per page.
  • Firebase have an extension that export and sync all data with Google Big Query. BigQuery is SQL like so there you can cross data and build B.I. panels.

Firestore specific operators

Firestore does not support all SQL-like operators and have some specific operators. Check the full list.

Query builder

DB::table('posts')->where('tags', 'array-contains-any', ['cat', 'dog'])->get();
// or
DB::table('posts')->whereArrayContainsAny('tags', ['cat', 'dog'])->get();

Eloquent builder

Post::where('tags', 'array-contains-any', ['cat', 'dog'])->get();
// or
Post::whereArrayContainsAny('tags', ['cat', 'dog'])->get();

Firestore specific operations

Firestore has specific operations endAt, endBefore, limitToLast, startAfter and startAt. Check the full list.

DB::table('user')->orderBy('age', 'ASC')->startAfter([17])->get();
// or
User::orderBy('age', 'ASC')->startAfter([17])->get();

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email ennio.sousa@pruvo.app instead of using the issue tracker.

Credits

License

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

pruvo/laravel-firestore-connection 适用场景与选型建议

pruvo/laravel-firestore-connection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.02k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 07 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 pruvo/laravel-firestore-connection 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

与 pruvo/laravel-firestore-connection 相关的其它包

同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:

统计信息

  • 总下载量: 1.02k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-07-16