dkikki/phpnosql

一个功能强大的PHP NoSQL数据库实现,设计得与SQLite一样功能丰富。

dev-main 2024-09-30 15:06 UTC

This package is auto-updated.

Last update: 2024-09-30 15:06:51 UTC


README

一个基于PHP的NoSQL数据库,用于在PHP数组中存储数据。

安装

composer require dikki/phpnosql

使用

查看PhpNoSql.php类以观察方法。

查看这个简单的博客文章类以观察如何使用PhpNoSql类。

<?php

declare(strict_types=1);

use Dikki\PhpNoSql\PhpNoSql;

class BlogPost
{
    private PhpNoSql $db;

    public function __construct(PhpNoSql $db)
    {
        $this->db = $db;
    }

    public function createPost(string $title, string $content, string $author): string
    {
        $post = [
            'title' => $title,
            'content' => $content,
            'author' => $author,
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
        ];

        return $this->db->create($post);
    }

    public function getPost(string $id): ?array
    {
        $posts = $this->db->read(['id' => $id]);
        return $posts[0] ?? null;
    }

    public function getAllPosts(): array
    {
        return $this->db->read([], ['order' => ['created_at' => 'DESC']]);
    }

    public function updatePost(string $id, string $title, string $content): bool
    {
        $updated = $this->db->update(['id' => $id], [
            'title' => $title,
            'content' => $content,
            'updated_at' => date('Y-m-d H:i:s'),
        ]);

        return $updated > 0;
    }

    public function deletePost(string $id): bool
    {
        return $this->db->delete(['id' => $id]) > 0;
    }
}

文档将很快更新。