byjg / anydataset-nosql
Anydataset NoSQL standardizes the access to non-relational databases/repositories and provides a consistent interface for both NoSQL document databases and Key/Value stores.
Requires
- php: >=8.3 <8.6
- ext-curl: *
- ext-json: *
- aws/aws-sdk-php: 3.*
- byjg/anydataset: ^6.0
- byjg/cache-engine: ^6.0
- byjg/serializer: ^6.0
- byjg/webrequest: ^6.0
Requires (Dev)
- phpunit/phpunit: ^10.5|^11.5
- vimeo/psalm: ^5.9|^6.13
Suggests
- ext-mongodb: *
- byjg/cache-engine: ^6.0
Provides
Conflicts
None
Replaces
None
README
| sidebar_key | anydataset-nosql | |||
|---|---|---|---|---|
| sidebar_category | data-access | |||
| tags |
|
NoSQL Database Abstraction
One interface for document databases and one for key/value stores, whatever the service behind them. The implementation supports:
- MongoDB (document-based)
- AWS DynamoDB (key/value)
- S3-Like Storage (key/value)
- Cloudflare KV (key/value)
Anydataset is an agnostic data source abstraction layer in PHP. See more about Anydataset here.
Why
MongoDB, DynamoDB, S3 and Cloudflare KV each come with their own SDK and their own way to read, write and list. Storing a file in S3 today and in Cloudflare KV tomorrow means rewriting every call.
Anydataset NoSQL defines two contracts, NoSqlInterface for documents and
KeyValueInterface for key/value stores, and picks the service from the connection URI:
$store = Factory::getInstance('s3://key:secret@us-east-1/my-bucket'); // AWS S3 $store = Factory::getInstance('kv://user:token@account/namespace'); // Cloudflare KV $store = Factory::getInstance('dynamodb://key:secret@host/table'); // DynamoDB $store->put('invoice-42', $pdf); $pdf = $store->get('invoice-42');
- Change the service with the URI, not the code that uses it.
- Same iterator as the rest of AnyDataset.
getIterator()returns the same rows as a database or a file, and document queries use the sameIteratorFilter.
Features
- Access both document-based and key/value repositories with consistent interfaces
- NoSQL document-based databases accessed through
NoSqlInterface - Key/Value stores accessed through
KeyValueInterface - Unified connection string format based on URIs
- Type-safe DynamoDB attribute definitions using enums
Connection Based on URI
The connection string for databases is based on URL.
See below the current implemented drivers:
| Datasource | Connection String |
|---|---|
| MongoDB | mongodb://username:password@hostname:port/database |
| S3 | s3://accesskey:secretkey@region/bucket?params |
| Cloudflare KV | kv://username:password@accountid/namespaceid |
| AWS DynamoDB | dynamodb://accesskey:secretkey@hostname/tablename?params |
Documentation
NoSQL Drivers
- MongoDB - Document-based NoSQL database
Key/Value Drivers
- AWS DynamoDB - Key/Value store with type-safe attribute definitions
- AWS S3 - Object storage as Key/Value store
- Cloudflare KV - Cloudflare Workers KV storage
Additional Features
- Running Tests - How to run the test suite
Installation
Just type:
composer require "byjg/anydataset-nosql"
Running Tests
docker compose up -d vendor/bin/phpunit
More details are in Running Tests.