Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Jenssegers/Mongodb/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,18 @@ public function expire($columns, $seconds)
}

/**
* @inheritdoc
* Indicate that the collection needs to be created.
* @param array $options
* @return void
*/
public function create()
public function create($options = [])
{
$collection = $this->collection->getCollectionName();

$db = $this->connection->getMongoDB();

// Ensure the collection is created.
$db->createCollection($collection);
$db->createCollection($collection, $options);
}

/**
Expand Down
34 changes: 26 additions & 8 deletions src/Jenssegers/Mongodb/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ public function hasColumns($table, array $columns)

/**
* Determine if the given collection exists.
* @param string $collection
* @param string $name
* @return bool
*/
public function hasCollection($collection)
public function hasCollection($name)
{
$db = $this->connection->getMongoDB();

foreach ($db->listCollections() as $collectionFromMongo) {
if ($collectionFromMongo->getName() == $collection) {
return true;
}
}
$collections = iterator_to_array($db->listCollections([
'filter' => [
'name' => $name,
],
]), false);

return false;
return count($collections) ? true : false;
}

/**
Expand Down Expand Up @@ -134,6 +134,24 @@ protected function createBlueprint($collection, Closure $callback = null)
return new Blueprint($this->connection, $collection);
}

/**
* Get collection.
* @param string $name
* @return bool|\MongoDB\Model\CollectionInfo
*/
public function getCollection($name)
{
$db = $this->connection->getMongoDB();

$collections = iterator_to_array($db->listCollections([
'filter' => [
'name' => $name,
],
]), false);

return count($collections) ? current($collections) : false;
}

/**
* Get all of the collections names for the database.
* @return array
Expand Down
4 changes: 4 additions & 0 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public function testCreateWithOptions(): void
Schema::create('newcollection_two', null, ['capped' => true, 'size' => 1024]);
$this->assertTrue(Schema::hasCollection('newcollection_two'));
$this->assertTrue(Schema::hasTable('newcollection_two'));

$collection = Schema::getCollection('newcollection_two');
$this->assertTrue($collection['options']['capped']);
$this->assertEquals(1024, $collection['options']['size']);
}

public function testDrop(): void
Expand Down