thedava/dod-lite

一个基于文件的伪数据库

v0.1.2 2024-08-11 20:34 UTC

This package is auto-updated.

Last update: 2024-09-11 20:59:37 UTC


README

.github/workflows/tests.yml

这是一个简单的基于文件的文档导向伪数据库。

这个库仍在alpha阶段!请自行承担风险使用!

主要目标是创建一个结合SQLite和MongoDB文档数据库功能的库:在文件中存储数据,无需独立运行的数据库。

安装

通过Composer

composer require thedava/dod-lite

使用方法

DodLite的核心组件是DocumentManager。它用于管理集合并提供一些实用功能,如在不同集合间移动文档。DocumentManager的完整文档和DodLite基本概念的解释可以在这里找到。

为了快速入门,您可以使用DocumentManagerFactory创建一个新的DocumentManager实例(无需深入了解适配器)。工厂提供了多种方法来创建针对不同用例的DocumentManager实例。有关更多信息,请参阅适配器文档。

# \DodLite\DocumentManagerFactory methods
public static function createLocalFile(string $folderPath): DocumentManager
public static function createCachedLocalFile(string $folderPath): DocumentManager
public static function createIndexedLocalFile(string $folderPath): DocumentManager
public static function createIndexCachedLocalFile(string $folderPath): DocumentManager
// Create a new DocumentManager
$documentManager = \DodLite\DocumentManagerFactory::createLocalFile('/tmp');

// Get/Create collection "docs"
$collection = $documentManager->getCollection('docs');

写入数据

// Create a new document
$document = $collection->createDocument('README', [
    'file' => 'README.md',
    'headline' => 'Document-oriented Database Lite',
    'description' => 'A simple file based document-oriented pseudo database.',
]);

// Persist document
$collection->writeDocument($document);

// Create another document and persist it immediately
$document = $collection->createDocument('Exceptions', [
    'file' => '05-Exceptions.md',
    'headline' => 'Exceptions',
    'description' => 'DodLite employs a consistent error handling system',
], write: true);

// Write data directly without a document
$collection->writeData('Adapters', [
    'file' => '04-Adapters.md',
    'headline' => 'Adapters',
    'description' => 'DodLite uses adapters to store data',
]);

// Create a document manually and persist it
$document = new \DodLite\Documents\Document('Concepts', [
    'file' => '03-Concepts.md',
    'headline' => 'Concepts',
    'description' => 'Basic principles',
]);
$collection->writeDocument($document);

更新数据

// Retrieve document
$document = $collection->getDocument('Concepts');

// Update content manually
$content = $document->getContent();
$content['description'] = 'DodLite uses collections and documents to store data';
$document->setContent($content);

// Update content via helper method (array_replace_recursive)
$document->updateContent([
    'headline' => 'Concepts and Basic principles',
]);

// Persist document
$collection->writeDocument($document);

读取数据

// Get document
$document = $collection->getDocument('Adapters');
var_dump($document->getContent()); // { 'file' => '04-Adapters.md', ... }

// Get the first document that matches a filter
$document = $collection->getDocumentByFilter(
    new \DodLite\Filter\CallbackFilter(fn(Document $document) => $document->getContent()['file'] === '05-Exceptions.md')
);

// Get all documents
$documents = $collection->getAllDocuments();
foreach ($documents as $id => $document) {
    var_dump($document->getContent());
}

// Get all documents filtered
$documents = $collection->getAllDocumentsByFilter(
    new \DodLite\Filter\CallbackFilter(fn(Document $document) => str_ends_with($document->getContent()['file'], '.md'))
);
foreach ($documents as $id => $document) {
    var_dump($document->getContent());
}

检查数据是否存在

// Check if README exists
var_dump($collection->hasDocumentById('README')); // true

删除数据

// Delete document directly by id
$collection->deleteDocumentById('Adapters');

// Delete a document object
$document = $collection->getDocument('Exceptions');
$collection->deleteDocument($document);

适配器

DodLite使用适配器来存储数据。适配器负责读取和写入数据。它们还提供了基于其他适配器的附加功能。有关适配器的完整列表,请参阅适配器文档。

错误处理

DodLite中抛出的每个错误要么是\DodLite\DodException或其派生类型。有关所有异常的完整解释,请参阅异常文档。

扩展

文档