Skip to content

Commit 80b0111

Browse files
committed
Adding fluent schema builder support
1 parent 4c8d908 commit 80b0111

File tree

5 files changed

+152
-28
lines changed

5 files changed

+152
-28
lines changed

phpunit.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@
77
convertNoticesToExceptions="true"
88
convertWarningsToExceptions="true"
99
processIsolation="false"
10-
stopOnFailure="false"
10+
stopOnFailure="true"
1111
syntaxCheck="false"
1212
>
1313
<testsuites>
14-
<testsuite name="Laravel MongoDB Test Suite">
14+
<testsuite name="all">
1515
<directory>tests/</directory>
1616
</testsuite>
17+
<testsuite name="schema">
18+
<directory>tests/SchemaTest.php</directory>
19+
</testsuite>
20+
<testsuite name="seeder">
21+
<directory>tests/SeederTest.php</directory>
22+
</testsuite>
23+
<testsuite name="cache">
24+
<directory>tests/CacheTest.php</directory>
25+
</testsuite>
1726
</testsuites>
1827
</phpunit>

src/Jenssegers/Mongodb/Schema/Blueprint.php

Lines changed: 114 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
use Closure;
44
use Illuminate\Database\Connection;
55

6-
class Blueprint {
6+
class Blueprint extends \Illuminate\Database\Schema\Blueprint {
7+
8+
/**
9+
* The MongoConnection object for this blueprint.
10+
*
11+
* @var MongoConnection
12+
*/
13+
protected $connection;
714

815
/**
916
* The MongoCollection object for this blueprint.
@@ -12,6 +19,13 @@ class Blueprint {
1219
*/
1320
protected $collection;
1421

22+
/**
23+
* Fluent columns
24+
*
25+
* @var array
26+
*/
27+
protected $columns = array();
28+
1529
/**
1630
* Create a new schema blueprint.
1731
*
@@ -21,6 +35,7 @@ class Blueprint {
2135
*/
2236
public function __construct(Connection $connection, $collection)
2337
{
38+
$this->connection = $connection;
2439
$this->collection = $connection->getCollection($collection);
2540
}
2641

@@ -31,11 +46,26 @@ public function __construct(Connection $connection, $collection)
3146
* @param array $options
3247
* @return bool
3348
*/
34-
public function index($columns, $options = array())
49+
public function index($columns = null, $options = array())
3550
{
36-
$result = $this->collection->ensureIndex($columns, $options);
51+
$columns = $this->fluent($columns);
52+
53+
// Columns are passed as a default array
54+
if (is_array($columns) && is_int(key($columns)))
55+
{
56+
// Transform the columns to the required array format
57+
$transform = array();
58+
foreach ($columns as $column)
59+
{
60+
$transform[$column] = 1;
61+
}
62+
63+
$columns = $transform;
64+
}
3765

38-
return (1 == (int) $result['ok']);
66+
$this->collection->ensureIndex($columns, $options);
67+
68+
return $this;
3969
}
4070

4171
/**
@@ -44,11 +74,16 @@ public function index($columns, $options = array())
4474
* @param string|array $columns
4575
* @return bool
4676
*/
47-
public function dropIndex($columns)
77+
public function dropIndex($columns = null)
4878
{
49-
$result = $this->collection->deleteIndex($columns);
79+
$columns = $this->fluent($columns);
80+
81+
foreach ($columns as $column)
82+
{
83+
$this->collection->deleteIndex($column);
84+
}
5085

51-
return (1 == (int) $result['ok']);
86+
return $this;
5287
}
5388

5489
/**
@@ -57,44 +92,70 @@ public function dropIndex($columns)
5792
* @param string|array $columns
5893
* @return bool
5994
*/
60-
public function unique($columns)
95+
public function unique($columns = null)
6196
{
62-
return $this->index($columns, array('unique' => true));
97+
$columns = $this->fluent($columns);
98+
$this->index($columns, array('unique' => true));
99+
100+
return $this;
63101
}
64102

65103
/**
66104
* Specify a non blocking index for the collection.
67-
*
105+
*
68106
* @param string|array $columns
69107
* @return bool
70108
*/
71-
public function background($columns)
109+
public function background($columns = null)
72110
{
73-
return $this->index($columns, array('background' => true));
111+
$columns = $this->fluent($columns);
112+
$this->index($columns, array('background' => true));
113+
114+
return $this;
74115
}
75116

76117
/**
77118
* Specify a sparse index for the collection.
78-
*
119+
*
79120
* @param string|array $columns
80121
* @return bool
81122
*/
82-
public function sparse($columns)
123+
public function sparse($columns = null)
83124
{
84-
return $this->index($columns, array('sparse' => true));
125+
$columns = $this->fluent($columns);
126+
$this->index($columns, array('sparse' => true));
127+
128+
return $this;
85129
}
86130

87131
/**
88132
* Specify the number of seconds after wich a document should be considered expired based,
89133
* on the given single-field index containing a date.
90-
*
134+
*
91135
* @param string|array $columns
92136
* @param int $seconds
93137
* @return bool
94138
*/
95139
public function expire($columns, $seconds)
96140
{
97-
return $this->index($columns, array('expireAfterSeconds' => $seconds));
141+
$columns = $this->fluent($columns);
142+
$this->index($columns, array('expireAfterSeconds' => $seconds));
143+
144+
return $this;
145+
}
146+
147+
/**
148+
* Indicate that the table needs to be created.
149+
*
150+
* @return bool
151+
*/
152+
public function create()
153+
{
154+
$collection = $this->collection->getName();
155+
156+
// Ensure the collection is created
157+
$db = $this->connection->getMongoDB();
158+
$db->createCollection($collection);
98159
}
99160

100161
/**
@@ -104,9 +165,43 @@ public function expire($columns, $seconds)
104165
*/
105166
public function drop()
106167
{
107-
$result = $this->collection->drop();
168+
$this->collection->drop();
169+
}
108170

109-
return (1 == (int) $result['ok']);
171+
/**
172+
* Add a new column to the blueprint.
173+
*
174+
* @param string $type
175+
* @param string $name
176+
* @param array $parameters
177+
* @return Blueprint
178+
*/
179+
protected function addColumn($type, $name, array $parameters = array())
180+
{
181+
$this->fluent($name);
182+
return $this;
183+
}
184+
185+
/**
186+
* Allow fluent columns
187+
*
188+
* @param string|array $columns
189+
* @return string|array
190+
*/
191+
protected function fluent($columns = null)
192+
{
193+
if (is_null($columns))
194+
{
195+
return $this->columns;
196+
}
197+
else if (is_string($columns))
198+
{
199+
return $this->columns = array($columns);
200+
}
201+
else
202+
{
203+
return $this->columns = $columns;
204+
}
110205
}
111206

112207
}

src/Jenssegers/Mongodb/Schema/Builder.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ public function collection($collection, Closure $callback)
5151
{
5252
$blueprint = $this->createBlueprint($collection);
5353

54-
return $callback($blueprint);
54+
if ($callback)
55+
{
56+
$callback($blueprint);
57+
}
5558
}
5659

5760
/**
@@ -75,12 +78,13 @@ public function table($collection, Closure $callback)
7578
*/
7679
public function create($collection, Closure $callback = null)
7780
{
78-
$db = $this->connection->getMongoDB();
79-
$db->createCollection($collection);
81+
$blueprint = $this->createBlueprint($collection);
82+
83+
$blueprint->create();
8084

8185
if ($callback)
8286
{
83-
return $this->collection($collection, $callback);
87+
$callback($blueprint);
8488
}
8589
}
8690

@@ -93,7 +97,7 @@ public function create($collection, Closure $callback = null)
9397
public function drop($collection)
9498
{
9599
$blueprint = $this->createBlueprint($collection);
96-
100+
97101
return $blueprint->drop();
98102
}
99103

@@ -108,4 +112,4 @@ protected function createBlueprint($collection, Closure $callback = null)
108112
return new Blueprint($this->connection, $collection);
109113
}
110114

111-
}
115+
}

tests/ConnectionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testMultipleConnections()
5252

5353
# Add fake host
5454
$db = $app['config']['database.connections']['mongodb'];
55-
$db['host'] = array($db['host'], '1.2.3.4');
55+
$db['host'] = array($db['host'], '0.0.0.0');
5656

5757
$connection = new Connection($db);
5858
$mongoclient = $connection->getMongoClient();

tests/SchemaTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ public function testExpire()
9292
$this->assertEquals(60, $index['expireAfterSeconds']);
9393
}
9494

95+
public function testFluent()
96+
{
97+
Schema::collection('newcollection', function($collection)
98+
{
99+
$collection->string('email')->index();
100+
$collection->string('token')->index();
101+
$collection->timestamp('created_at');
102+
});
103+
104+
$index = $this->getIndex('newcollection', 'email');
105+
$this->assertEquals(1, $index['key']['email']);
106+
107+
$index = $this->getIndex('newcollection', 'token');
108+
$this->assertEquals(1, $index['key']['token']);
109+
}
110+
95111
protected function getIndex($collection, $name)
96112
{
97113
$collection = DB::getCollection($collection);

0 commit comments

Comments
 (0)