PHPackages                             jamielsharief/document-store - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. jamielsharief/document-store

ActiveLibrary[Database &amp; ORM](/categories/database)

jamielsharief/document-store
============================

Document Store

1.0.1(5y ago)4300[1 issues](https://github.com/jamielsharief/document-store/issues)1MITPHPPHP &gt;=7.3.0

Since Sep 28Pushed 5y ago2 watchersCompare

[ Source](https://github.com/jamielsharief/document-store)[ Packagist](https://packagist.org/packages/jamielsharief/document-store)[ RSS](/packages/jamielsharief-document-store/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (9)Used By (1)

DocumentStore
=============

[](#documentstore)

[![license](https://camo.githubusercontent.com/6fdb99389fe9d9e8a5c197002a191ace7c8b12a2020c0fa5756cf17aa08a4966/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874477265656e2e737667)](https://camo.githubusercontent.com/6fdb99389fe9d9e8a5c197002a191ace7c8b12a2020c0fa5756cf17aa08a4966/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874477265656e2e737667)[![build](https://github.com/jamielsharief/document-store/workflows/CI/badge.svg)](https://github.com/jamielsharief/document-store/actions)[![coverage](https://camo.githubusercontent.com/b24a7edc5160fd973f0eaf13042c03dc09e4b5f59c0852cb287ffda421226f0e/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6a616d69656c736861726965662f646f63756d656e742d73746f72652f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/jamielsharief/document-store?branch=master)

DocumentStore is a Key-Value Store (KVS) that stores data in JSON documents, giving you a productive way to work with simple or nested data and allows for felxible and dynamic schema. Data can easily be synced across multiple servers. This provides a consistent interface for working with JSON data as a no SQL database.

Creating a Document
-------------------

[](#creating-a-document)

Create a `Document` and add any data you like, it can be nested include arrays

```
use DocumentStore\Document;

$document = new Document();
$document->title = 'Patterns of Enterprise Application Architecture';
$document->author = 'Martin Fowler';
$document->type = 'hardcover';
$document->isbn = [
    '978-0321127426',
    '0321127420'
];
```

You can also create a `Document` from an array

```
use DocumentStore\Document;

$document = new Document([
    'title' => 'Patterns of Enterprise Application Architecture',
    'author' => 'Martin Fowler',
    'type' => 'hardcover',
    'isbn' => [
        '978-0321127426',
        '0321127420'
    ]
]);
```

The `Document` object can also be accessed as an array

```
echo $document['title'];
$document['description'] = null;
unset($document['something']);
```

Set
---

[](#set)

To add a `Document` to the `DocumentStore`

```
use DocumentStore\DocumentStore;
use DocumentStore\Document;

$store = new DocumentStore(storage_path('books'));

$document = new Document();
$document->title = 'Patterns of Enterprise Application Architecture';
$document->author = 'Martin Fowler';
$document->type = 'hardcover';
$document->isbn = [
    '978-0321127426',
    '0321127420'
];

$store->set('0321127420', $document);
```

You can also group `Documents` using a prefixes (as many levels) which organizes the `Documents` into folders.

```
$store->set('programming/0321127420', $document);
```

Get
---

[](#get)

To get a `Document` from the `DocumentStore`

```
use DocumentStore\DocumentStore;
$store = new DocumentStore(storage_path('books'));

$document = $store->get('0321127420');

echo $document->title;
```

To get a `Document` with a prefix

```
$document = $store->get('programming/0321127420');
```

Has
---

[](#has)

To check a `Document` exists

```
use DocumentStore\DocumentStore;
$store = new DocumentStore(storage_path('books'));

$result = $store->has('0321127420');
```

To check a `Document` with a prefix

```
$store->has('programming/0321127420');
```

Delete
------

[](#delete)

To delete a `Document` from the `DocumentStore`

```
use DocumentStore\DocumentStore;
$store = new DocumentStore(storage_path('books'));

$store->delete('0321127420');
```

To delete a `Document` with a prefix

```
$store->delete('programming/0321127420');
```

List
----

[](#list)

To list `Documents` in the `DocumentStore`

```
use DocumentStore\DocumentStore;
$store = new DocumentStore(storage_path('books'));

$list = $store->list(); // ['programming/0321127420']
$list = $store->list('programming'); // ['programming/0321127420']
```

Document
--------

[](#document)

### Key

[](#key)

If you are working a `Document` that was stored in the `DocumentStore`, you can use the `key` method to get the key that was used. This is handy for when working with search results from find first or find all.

```
$key = $document->key();
```

### To Array

[](#to-array)

To convert the `Document` into an array

```
$document->toArray();
```

#### To Json

[](#to-json)

To convert the `Document` into a JSON string

```
$document->toJson();
$document->toJson(['pretty' => true]);
```

Searching
---------

[](#searching)

You can also search `Documents` in the `DocumentStore`, this is a file based searched . So if you have 100,000s of Documents in a prefix or regularly perform large amounts of searches and speed is an issue, maybe an indexing software like `Elasticsearch` or `Solr` might be worth looking at.

### Keys

[](#keys)

To get a list a keys that match a set of conditions (see below). There are also finders, which are detailed after conditions.

```
use DocumentStore\DocumentStore;
$store = new DocumentStore(storage_path('books'));

$list = $store->search([
    'conditions' => ['author' => 'Dean Koontz'],
    'prefix' => 'fiction/horror', // optional
    'limit' => 10,  // limit the number of results
]);
```

The following options can be passed

- prefix: to search a given prefix e.g. computers/development
- conditions: an array of conditions
- limit: the maximum number of results to find
- offset: the offset from which matching record to find. This is handy when you want to paginate results using limit

### Fields

[](#fields)

You can search different fields using the dot notation, and you can searched nested data as well.

Lets say you have this data

```
{
    "name": "Tony Stark",
    "emails": [
        "tony@stark.com"
    ],
    "addreses": [
        {
            "street": "1000 malibu drive",
            "state": "california",
            "country": "US"
        },
        {
            "street": "25 corp road",
            "state": "california",
            "country": "US"
        }
    ],
    "status": "new"
}
```

Here are some examples how to search the the different levels of fields;

```
$conditions = [
    'name' => 'Tony Stark' // searches string
    'emails' => 'tony@stark.com', // searches data array
    'addresses.street' => '1000 malibu drive' // searches the street fields in addresses etc
];
```

### Conditions

[](#conditions)

Here are some examples on the different search conditions

#### Equals

[](#equals)

You can check single or multiple values (IN)

```
$conditions = ['author' => 'Dean Koontz']
$conditions = ['author' => ['Steven King','Dean Koontz']
```

#### Not Equals

[](#not-equals)

You can check single or multiple values (NOT IN)

```
$conditions = ['author !=' => 'Dean Koontz']
$conditions = ['author !=' => ['Steven King','Dean Koontz']]
```

#### Arithmetic

[](#arithmetic)

To check field values, such as greater, less than etc.

```
$conditions = ['age >' 21];
$conditions = ['age >=' 21];
$conditions = ['age
