https://github.com/b2pweb/bdf-prime-mongodb
Bdf prime MongoDB component
https://github.com/b2pweb/bdf-prime-mongodb
mongodb odm
Last synced: 2 months ago
JSON representation
Bdf prime MongoDB component
- Host: GitHub
- URL: https://github.com/b2pweb/bdf-prime-mongodb
- Owner: b2pweb
- License: mit
- Created: 2021-06-18T10:05:50.000Z (about 5 years ago)
- Default Branch: 2.1
- Last Pushed: 2023-10-19T09:10:48.000Z (over 2 years ago)
- Last Synced: 2023-12-17T10:49:21.242Z (over 2 years ago)
- Topics: mongodb, odm
- Language: PHP
- Homepage:
- Size: 236 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Prime MongoDB driver
[](https://github.com/b2pweb/bdf-prime-mongodb/actions/workflows/php.yml)
[](https://codecov.io/github/b2pweb/bdf-prime-mongodb)
[](https://packagist.org/packages/b2pweb/bdf-prime-mongodb)
[](https://packagist.org/packages/b2pweb/bdf-prime-mongodb)
[](https://shepherd.dev/github/b2pweb/bdf-prime-mongodb)
MongoDB driver for [Prime](https://github.com/b2pweb/bdf-prime)
## Installation
Install with composer :
```bash
composer require b2pweb/bdf-prime-mongodb
```
Create connection :
```php
declareConnection('mongo', 'mongodb://127.0.0.1/my_collection?noAuth=true');
// With credentials
$connections->declareConnection('mongo', 'mongodb://user:password@127.0.0.1/my_database');
// Get the connection locator
$locator = new MongoCollectionLocator($connections);
Mongo::configure($locator); // Configure active record system
```
## Usage
### Declare a document
Declare the base document class by extending `Bdf\Prime\MongoDB\Document\MongoDocument`.
The `_id` field is declared by this class.
You can use typed property for generate an automatic type mapping.
Untyped fields will not be converted when retrieving from mongo.
> Note: it's advisable to declare all fields as nullable in case of missing field
```php
where('name', 'John') // Simple where works as expected
->where('value.attr', ':like', 'P%') // "like" operator is converted to a regex
->where('value.foo', '$type', 'javascript') // Use mongodb operator
;
// Get all documents which match with filters
$query->all();
// First returns the first matching document or null
$query->first();
```
### Testing
Use `Bdf\Prime\MongoDB\Test\MongoTester` for create testing data.
```php
tester = new MongoTester();
$this->tester
// Declare given collections. Collections are automatically declared when `push()` on new collection
->declare(FooDocument::class, BarDocument::class)
// Push to mongo given documents with a key for retrieve the value on test
->push([
'doc1' => new MyDocument(),
'doc2' => new MyDocument(),
])
;
}
protected function tearDown() : void
{
$this->tester->destroy(); // Drop all declared collections
}
public function my_test()
{
$doc1 = $this->tester->get('doc1'); // get document declared on setUp method
$this->tester->push($newDoc = new FooDocument()); // Push a single document without a key (cannot be retrieved with `get()`)
// ...
$this->assertNotEquals($doc1, $this->tester->refresh($doc1)); // Retrieve the DB version of the given document
$this->assertNull($newDoc, $this->tester->refresh($newDoc)); // refresh can be used to check if the document exists on DB
}
public function with_array_access_test()
{
// Array access syntax can also be used instead of "classic" method calls
$doc1 = $this->tester['doc1']; // get document declared on setUp method
$this->tester[] = $newDoc = new FooDocument(); // Push a single document without a key (cannot be retrieved with `get()`)
$this->tester['doc3'] = new MyDocument(); // Push a single document with a key
// ...
$this->assertNotEquals($doc1, $this->tester[$doc1]); // Retrieve the DB version of the given document
$this->assertTrue(isset($this->tester[$newDoc])); // Check if the document exists on DB
unset($this->tester['doc3']); // Deleted a declared document
unset($this->tester[$newDoc]); // Can also be used to delete a document without key
$this->assertFalse(isset($this->tester[$newDoc])); // Document is now deleted
}
}
```
### Case-insensitive search and index
To enable case-insensitive search by default, you can add default collation on table options.
See [Case Insensitive Indexes](https://docs.mongodb.com/manual/core/index-case-insensitive/#case-insensitive-indexes-on-collections-with-a-default-collation)
```php
collation(['locale' => 'en', 'strength' => 2]);
}
}
```
### Multiple document classes
Mongo is schemaless, so a collection can store documents with different formats.
You can select a document class corresponding to DB fields by using a custom `Bdf\Prime\MongoDB\Document\Selector\DocumentSelectorInterface`,
declared using `DocumentMapper::createDocumentSelector()` :
```php
FooDocument::class,
'bar' => BarDocument::class,
]);
// If you can't introduce a field for perform discrimination, you can check fields existence :
return new DiscriminatorFieldDocumentSelector($documentBaseClass, [
FooDocument::class => ['foo'],
BarDocument::class => ['bar'],
]);
}
}
// Get the base collection : it handles all document types
$collection = BaseDocument::collection();
$collection->add(new BaseDocument(...));
$collection->add(new FooDocument(...));
$collection->add(new BarDocument(...));
$collection->all(); // Return all documents from all types
// Handle only "FooDocument" document class
$fooCollection = FooDocument::collection();
$fooCollection->all(); // Return only document of type "foo"
```