定制 phpnomad/datastore 二次开发

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

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

phpnomad/datastore

Composer 安装命令:

composer require phpnomad/datastore

包简介

README 文档

README

Latest Version Total Downloads PHP Version License

phpnomad/datastore is the storage-agnostic data access pattern at the heart of PHPNomad. It ships a small set of interfaces, traits, models, and events that let your business logic describe what data operations it needs without committing to where the data lives. It contains no concrete storage code of its own.

The pattern splits into two layers. Your Core layer holds the Datastore interface your application code depends on, the DatastoreHandler interface that storage implementations fulfill, and the domain models that flow between them. Your Service layer holds concrete handlers that talk to real storage, whether that's a SQL database, a REST API, a GraphQL endpoint, or an in-memory array for tests. phpnomad/db is one Service-layer implementation against SQL, and PHPNomad has been running this pattern in production for years, powering Siren, several MCP servers, and other client systems.

Installation

composer require phpnomad/datastore

Quick Start

The pattern centers on three files: a Datastore interface (your public API), a DatastoreHandler interface (the storage contract), and a Core implementation that wires them together with decorator traits.

Declare your public API by extending the base interfaces you need and adding any custom business methods:

<?php

namespace MyApp\Core\Datastores\Post\Interfaces;

use MyApp\Core\Models\Post;
use PHPNomad\Datastore\Interfaces\Datastore;
use PHPNomad\Datastore\Interfaces\DatastoreHasPrimaryKey;
use PHPNomad\Datastore\Interfaces\DatastoreHasWhere;

interface PostDatastore extends Datastore, DatastoreHasPrimaryKey, DatastoreHasWhere
{
    /**
     * @return Post[]
     */
    public function getPublishedPosts(): array;
}

Declare the storage contract as a sibling interface. It extends the same base interfaces but stays free of business-specific methods, so any backend can implement it:

<?php

namespace MyApp\Core\Datastores\Post\Interfaces;

use PHPNomad\Datastore\Interfaces\Datastore;
use PHPNomad\Datastore\Interfaces\DatastoreHasPrimaryKey;
use PHPNomad\Datastore\Interfaces\DatastoreHasWhere;

interface PostDatastoreHandler extends Datastore, DatastoreHasPrimaryKey, DatastoreHasWhere
{
}

Write the Core implementation once. Decorator traits delegate every standard method to the injected handler, so you only implement the custom business method:

<?php

namespace MyApp\Core\Datastores\Post;

use MyApp\Core\Datastores\Post\Interfaces\PostDatastore as PostDatastoreInterface;
use MyApp\Core\Datastores\Post\Interfaces\PostDatastoreHandler;
use PHPNomad\Datastore\Interfaces\Datastore;
use PHPNomad\Datastore\Traits\WithDatastoreDecorator;
use PHPNomad\Datastore\Traits\WithDatastorePrimaryKeyDecorator;
use PHPNomad\Datastore\Traits\WithDatastoreWhereDecorator;

class PostDatastore implements PostDatastoreInterface
{
    use WithDatastoreDecorator;
    use WithDatastorePrimaryKeyDecorator;
    use WithDatastoreWhereDecorator;

    protected Datastore $datastoreHandler;

    public function __construct(PostDatastoreHandler $datastoreHandler)
    {
        $this->datastoreHandler = $datastoreHandler;
    }

    public function getPublishedPosts(): array
    {
        return $this->datastoreHandler->where([
            [
                'type' => 'AND',
                'clauses' => [
                    ['column' => 'status', 'operator' => '=', 'value' => 'published'],
                ],
            ],
        ]);
    }
}

A Service-layer class like PostDatabaseDatastoreHandler implements PostDatastoreHandler against an actual backend. You bind the handler interface to that concrete class in your DI container at bootstrap time. Swapping storage later is a one-line change to the binding. Nothing in PostDatastore or its callers has to move.

Key Concepts

  • Datastore is your public API. DatastoreHandler is the storage contract. Both extend the same base interfaces, but only the Datastore adds custom business methods.
  • Base interfaces are composable. Mix Datastore, DatastoreHasPrimaryKey, DatastoreHasWhere, and DatastoreHasCounts to describe exactly the operations your entity supports, no more.
  • Decorator traits (WithDatastoreDecorator, WithDatastorePrimaryKeyDecorator, WithDatastoreWhereDecorator, WithDatastoreCountDecorator) delegate the standard methods to your handler so implementations stay lean.
  • DataModel and ModelAdapter keep domain entities independent of storage format. Adapters translate between raw arrays and model objects on read and write.
  • RecordCreated, RecordUpdated, and RecordDeleted events let handler implementations broadcast state changes through phpnomad/event without coupling consumers to any specific storage backend.

Documentation

Full documentation lives at phpnomad.com, including the datastore conceptual overview, the Core datastore layer reference, model and adapter guidance, and the phpnomad/db Service-layer implementation for SQL-backed storage.

License

MIT, see LICENSE.txt for the full text.

phpnomad/datastore 适用场景与选型建议

phpnomad/datastore 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.08k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 12 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-18