Skip to content

Commit

Permalink
⚡ quicker khulan() helper
Browse files Browse the repository at this point in the history
🔧 added uri and driver options
  • Loading branch information
bnomei committed Jul 13, 2024
1 parent 45974dd commit da52fae
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
8 changes: 6 additions & 2 deletions classes/Mongodb.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ public function __construct(array $options = [])
'database' => option('bnomei.mongodb.database'),
'username' => option('bnomei.mongodb.username'),
'password' => option('bnomei.mongodb.password'),
'uriOptions' => option('bnomei.mongodb.uriOptions'),
'driverOptions' => option('bnomei.mongodb.driverOptions'),
'collection-cache' => option('bnomei.mongodb.collections.cache'),
'collection-content' => option('bnomei.mongodb.collections.content'),
], $options);

foreach ($this->options as $key => $call) {
if (! is_string($call) && is_callable($call) && in_array($key, [
'host', 'port', 'database', 'username', 'password',
'host', 'port', 'database', 'username', 'password', 'uriOptions', 'driverOptions',
])) {
$this->options[$key] = $call();
}
Expand Down Expand Up @@ -196,7 +198,9 @@ public function client(): Client
}

$this->_client = new Client(
'mongodb://'.$auth.$this->options['host'].':'.$this->options['port']
'mongodb://'.$auth.$this->options['host'].':'.$this->options['port'],
$this->options['uriOptions'] ?? [],
$this->options['driverOptions'] ?? [],
);
$this->_client->selectDatabase($this->options['database']);

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bnomei/kirby-mongodb",
"type": "kirby-plugin",
"version": "1.4.1",
"version": "1.4.2",
"description": "Khulan is a cache driver and content cache with NoSQL interface for Kirby using MongoDB",
"license": "MIT",
"authors": [
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,21 @@ function mongo(?string $collection = null): \Bnomei\Mongodb|Collection

if (! function_exists('khulan')) {

function khulan(string|array|null $search = null): mixed
function khulan(string|array|null $search = null, ?array $options = null): mixed
{
$collection = \Bnomei\Mongodb::singleton()->contentCollection();

// only get these fields as it is faster and enough for kirby
$options ??= [
'projection' => [
'id' => 1,
'uuid' => 1,
'modelType' => 1,
],
];

if (is_array($search)) {
return Khulan::documentsToModels($collection->find($search));
return Khulan::documentsToModels($collection->find($search, $options));

} elseif (is_string($search)) {
return Khulan::documentToModel($collection->findOne([
Expand All @@ -43,7 +52,7 @@ function khulan(string|array|null $search = null): mixed
['uuid' => $search],
['email' => $search], // user
],
]));
], $options));
}

return $collection;
Expand All @@ -61,6 +70,8 @@ function khulan(string|array|null $search = null): mixed
'database' => 'kirby',
'username' => null,
'password' => null,
'uriOptions' => [],
'driverOptions' => [],

// collections
'collections' => [
Expand Down
16 changes: 16 additions & 0 deletions tests/MongodbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,22 @@
expect($count)->toBe(1);
});

it('can use options on the khulan-find to limit selected fields', function () {
Khulan::index();

// these are the default options for khulan
// and optimized for speed
$page = khulan('betterharder', [
'projection' => [
'id' => 1,
'uuid' => 1,
'modelType' => 1,
],
]);

expect($page)->not()->toBeNull();
});

it('can run the benchmark', function () {
mongo()->benchmark();
})->skip();
8 changes: 4 additions & 4 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php return array(
'root' => array(
'name' => 'bnomei/kirby-mongodb',
'pretty_version' => '1.4.1',
'version' => '1.4.1.0',
'pretty_version' => '1.4.2',
'version' => '1.4.2.0',
'reference' => null,
'type' => 'kirby-plugin',
'install_path' => __DIR__ . '/../../',
Expand All @@ -11,8 +11,8 @@
),
'versions' => array(
'bnomei/kirby-mongodb' => array(
'pretty_version' => '1.4.1',
'version' => '1.4.1.0',
'pretty_version' => '1.4.2',
'version' => '1.4.2.0',
'reference' => null,
'type' => 'kirby-plugin',
'install_path' => __DIR__ . '/../../',
Expand Down

0 comments on commit da52fae

Please sign in to comment.